diff --git a/README.md b/README.md index ac28e1a6..114bdb11 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,21 @@ const devices = await seam.client.get('/devices/list') An Axios compatible client may be provided to create a `SeamHttp` instance. This API is used internally and is not directly supported. +#### Inspecting the Request + +All client methods return an instance of `SeamHttpRequest`. +Inspect the request before it is sent to the server by intentionally not awaiting the `SeamHttpRequest`: + +```ts +const seam = new SeamHttp('your-api-key') + +const request = seam.devices.list() + +console.log(`${request.method} ${request.url}`, JSON.stringify(request.body)) + +const devices = await request.execute() +``` + ## Development and Testing ### Quickstart diff --git a/generate-routes.ts b/generate-routes.ts index 1e0078cc..6d4c091f 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -295,6 +295,7 @@ import { import { resolveActionAttempt, } from 'lib/seam/connect/resolve-action-attempt.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' ${ namespace === 'client_sessions' @@ -354,41 +355,25 @@ const renderClassMethod = ({ path, isRequestParamOptional, }: Endpoint): string => ` - async ${camelCase(name)}( + ${camelCase(name)}( ${requestFormat}${isRequestParamOptional ? '?' : ''}: ${renderRequestType({ name, namespace, })}, ${renderClassMethodOptions({ resource })} - ): Promise<${ + ): SeamHttpRequest<${ resource === null - ? 'void' - : `${renderResponseType({ name, namespace })}['${resource}']` + ? 'void, undefined' + : `${renderResponseType({ name, namespace })}, '${resource}'` }> { - ${ - resource === null ? '' : 'const { data } = ' - }await this.client.request<${renderResponseType({ - name, - namespace, - })}>({ - url: '${path}', + return new SeamHttpRequest(this, { + path: '${path}', method: '${snakeCase(method)}', ${ requestFormat === 'params' ? 'params,' : '' - } ${requestFormat === 'body' ? 'data: body,' : ''} + } ${requestFormat === 'body' ? 'body,' : ''} + responseKey: ${resource === null ? 'undefined' : `'${resource}'`}, + ${resource === 'action_attempt' ? 'options' : ''} }) - ${ - resource === 'action_attempt' - ? `const waitForActionAttempt = options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return resolveActionAttempt( - data.${resource}, - SeamHttpActionAttempts.fromClient(this.client, { ...this.defaults, waitForActionAttempt: false }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - }` - : '' - } - ${resource === null ? '' : `return data.${resource}`} } ` diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 7935541b..813eff99 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,56 +154,58 @@ export class SeamHttpAccessCodesUnmanaged { await clientSessions.get() } - async convertToManaged( + convertToManaged( body?: AccessCodesUnmanagedConvertToManagedBody, - ): Promise { - await this.client.request({ - url: '/access_codes/unmanaged/convert_to_managed', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/unmanaged/convert_to_managed', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async delete(body?: AccessCodesUnmanagedDeleteBody): Promise { - await this.client.request({ - url: '/access_codes/unmanaged/delete', + delete( + body?: AccessCodesUnmanagedDeleteBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/unmanaged/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: AccessCodesUnmanagedGetParams, - ): Promise { - const { data } = await this.client.request( - { - url: '/access_codes/unmanaged/get', - method: 'post', - data: body, - }, - ) - - return data.access_code + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/unmanaged/get', + method: 'post', + body, + responseKey: 'access_code', + }) } - async list( + list( body?: AccessCodesUnmanagedListParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/access_codes/unmanaged/list', - method: 'post', - data: body, - }) - - return data.access_codes + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/unmanaged/list', + method: 'post', + body, + responseKey: 'access_codes', + }) } - async update(body?: AccessCodesUnmanagedUpdateBody): Promise { - await this.client.request({ - url: '/access_codes/unmanaged/update', + update( + body?: AccessCodesUnmanagedUpdateBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/unmanaged/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index fd9a8ec1..968bcab4 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpAccessCodesUnmanaged } from './access-codes-unmanaged.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -158,95 +159,90 @@ export class SeamHttpAccessCodes { return SeamHttpAccessCodesUnmanaged.fromClient(this.client, this.defaults) } - async create( + create( body?: AccessCodesCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/access_codes/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/create', method: 'post', - data: body, + body, + responseKey: 'access_code', }) - - return data.access_code } - async createMultiple( + createMultiple( body?: AccessCodesCreateMultipleBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/access_codes/create_multiple', - method: 'post', - data: body, - }) - - return data.access_codes + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/create_multiple', + method: 'post', + body, + responseKey: 'access_codes', + }) } - async delete(body?: AccessCodesDeleteBody): Promise { - await this.client.request({ - url: '/access_codes/delete', + delete(body?: AccessCodesDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async generateCode( + generateCode( body?: AccessCodesGenerateCodeBody, - ): Promise { - const { data } = await this.client.request( - { - url: '/access_codes/generate_code', - method: 'post', - data: body, - }, - ) - - return data.generated_code + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/generate_code', + method: 'post', + body, + responseKey: 'generated_code', + }) } - async get( + get( body?: AccessCodesGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/access_codes/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/get', method: 'post', - data: body, + body, + responseKey: 'access_code', }) - - return data.access_code } - async list( + list( body?: AccessCodesListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/access_codes/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/list', method: 'post', - data: body, + body, + responseKey: 'access_codes', }) - - return data.access_codes } - async pullBackupAccessCode( + pullBackupAccessCode( body?: AccessCodesPullBackupAccessCodeBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/access_codes/pull_backup_access_code', - method: 'post', - data: body, - }) - - return data.backup_access_code + ): SeamHttpRequest< + AccessCodesPullBackupAccessCodeResponse, + 'backup_access_code' + > { + return new SeamHttpRequest(this, { + path: '/access_codes/pull_backup_access_code', + method: 'post', + body, + responseKey: 'backup_access_code', + }) } - async update(body?: AccessCodesUpdateBody): Promise { - await this.client.request({ - url: '/access_codes/update', + update(body?: AccessCodesUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/access_codes/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/acs-access-groups.ts b/src/lib/seam/connect/routes/acs-access-groups.ts index 7aa024f1..71f93cb0 100644 --- a/src/lib/seam/connect/routes/acs-access-groups.ts +++ b/src/lib/seam/connect/routes/acs-access-groups.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,56 +154,56 @@ export class SeamHttpAcsAccessGroups { await clientSessions.get() } - async addUser(body?: AcsAccessGroupsAddUserBody): Promise { - await this.client.request({ - url: '/acs/access_groups/add_user', + addUser(body?: AcsAccessGroupsAddUserBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/access_groups/add_user', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: AcsAccessGroupsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/access_groups/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/access_groups/get', method: 'post', - data: body, + body, + responseKey: 'acs_access_group', }) - - return data.acs_access_group } - async list( + list( body?: AcsAccessGroupsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/access_groups/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/access_groups/list', method: 'post', - data: body, + body, + responseKey: 'acs_access_groups', }) - - return data.acs_access_groups } - async listUsers( + listUsers( body?: AcsAccessGroupsListUsersParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/acs/access_groups/list_users', - method: 'post', - data: body, - }) - - return data.acs_users + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/access_groups/list_users', + method: 'post', + body, + responseKey: 'acs_users', + }) } - async removeUser(body?: AcsAccessGroupsRemoveUserBody): Promise { - await this.client.request({ - url: '/acs/access_groups/remove_user', + removeUser( + body?: AcsAccessGroupsRemoveUserBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/access_groups/remove_user', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/acs-credential-pools.ts b/src/lib/seam/connect/routes/acs-credential-pools.ts index 0e6783c9..d812efa6 100644 --- a/src/lib/seam/connect/routes/acs-credential-pools.ts +++ b/src/lib/seam/connect/routes/acs-credential-pools.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,16 +154,15 @@ export class SeamHttpAcsCredentialPools { await clientSessions.get() } - async list( + list( body?: AcsCredentialPoolsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credential_pools/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credential_pools/list', method: 'post', - data: body, + body, + responseKey: 'acs_credential_pools', }) - - return data.acs_credential_pools } } diff --git a/src/lib/seam/connect/routes/acs-credential-provisioning-automations.ts b/src/lib/seam/connect/routes/acs-credential-provisioning-automations.ts index 188e1f2e..3c8736ea 100644 --- a/src/lib/seam/connect/routes/acs-credential-provisioning-automations.ts +++ b/src/lib/seam/connect/routes/acs-credential-provisioning-automations.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -156,21 +157,18 @@ export class SeamHttpAcsCredentialProvisioningAutomations { await clientSessions.get() } - async launch( + launch( body?: AcsCredentialProvisioningAutomationsLaunchBody, - ): Promise< - AcsCredentialProvisioningAutomationsLaunchResponse['acs_credential_provisioning_automation'] + ): SeamHttpRequest< + AcsCredentialProvisioningAutomationsLaunchResponse, + 'acs_credential_provisioning_automation' > { - const { data } = - await this.client.request( - { - url: '/acs/credential_provisioning_automations/launch', - method: 'post', - data: body, - }, - ) - - return data.acs_credential_provisioning_automation + return new SeamHttpRequest(this, { + path: '/acs/credential_provisioning_automations/launch', + method: 'post', + body, + responseKey: 'acs_credential_provisioning_automation', + }) } } diff --git a/src/lib/seam/connect/routes/acs-credentials.ts b/src/lib/seam/connect/routes/acs-credentials.ts index f55f5722..f8c03175 100644 --- a/src/lib/seam/connect/routes/acs-credentials.ts +++ b/src/lib/seam/connect/routes/acs-credentials.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,84 +154,79 @@ export class SeamHttpAcsCredentials { await clientSessions.get() } - async assign( + assign( body?: AcsCredentialsAssignBody, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/assign', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/assign', method: 'post', - data: body, + body, + responseKey: 'acs_credential', }) - - return data.acs_credential } - async create( + create( body?: AcsCredentialsCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/create', method: 'post', - data: body, + body, + responseKey: 'acs_credential', }) - - return data.acs_credential } - async delete(body?: AcsCredentialsDeleteBody): Promise { - await this.client.request({ - url: '/acs/credentials/delete', + delete(body?: AcsCredentialsDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: AcsCredentialsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/get', method: 'post', - data: body, + body, + responseKey: 'acs_credential', }) - - return data.acs_credential } - async list( + list( body?: AcsCredentialsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/list', method: 'post', - data: body, + body, + responseKey: 'acs_credentials', }) - - return data.acs_credentials } - async unassign( + unassign( body?: AcsCredentialsUnassignBody, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/unassign', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/unassign', method: 'post', - data: body, + body, + responseKey: 'acs_credential', }) - - return data.acs_credential } - async update( + update( body?: AcsCredentialsUpdateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/credentials/update', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/credentials/update', method: 'post', - data: body, + body, + responseKey: 'acs_credential', }) - - return data.acs_credential } } diff --git a/src/lib/seam/connect/routes/acs-entrances.ts b/src/lib/seam/connect/routes/acs-entrances.ts index a0c6c68d..bdb3cb3e 100644 --- a/src/lib/seam/connect/routes/acs-entrances.ts +++ b/src/lib/seam/connect/routes/acs-entrances.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,49 +154,51 @@ export class SeamHttpAcsEntrances { await clientSessions.get() } - async get( + get( body?: AcsEntrancesGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/entrances/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/entrances/get', method: 'post', - data: body, + body, + responseKey: 'acs_entrance', }) - - return data.acs_entrance } - async grantAccess(body?: AcsEntrancesGrantAccessBody): Promise { - await this.client.request({ - url: '/acs/entrances/grant_access', + grantAccess( + body?: AcsEntrancesGrantAccessBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/entrances/grant_access', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async list( + list( body?: AcsEntrancesListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/entrances/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/entrances/list', method: 'post', - data: body, + body, + responseKey: 'acs_entrances', }) - - return data.acs_entrances } - async listCredentialsWithAccess( + listCredentialsWithAccess( body?: AcsEntrancesListCredentialsWithAccessParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/acs/entrances/list_credentials_with_access', - method: 'post', - data: body, - }) - - return data.acs_credentials + ): SeamHttpRequest< + AcsEntrancesListCredentialsWithAccessResponse, + 'acs_credentials' + > { + return new SeamHttpRequest(this, { + path: '/acs/entrances/list_credentials_with_access', + method: 'post', + body, + responseKey: 'acs_credentials', + }) } } diff --git a/src/lib/seam/connect/routes/acs-systems.ts b/src/lib/seam/connect/routes/acs-systems.ts index 5403df63..bd79b213 100644 --- a/src/lib/seam/connect/routes/acs-systems.ts +++ b/src/lib/seam/connect/routes/acs-systems.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,28 +154,26 @@ export class SeamHttpAcsSystems { await clientSessions.get() } - async get( + get( body?: AcsSystemsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/systems/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/systems/get', method: 'post', - data: body, + body, + responseKey: 'acs_system', }) - - return data.acs_system } - async list( + list( body?: AcsSystemsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/systems/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/systems/list', method: 'post', - data: body, + body, + responseKey: 'acs_systems', }) - - return data.acs_systems } } diff --git a/src/lib/seam/connect/routes/acs-users.ts b/src/lib/seam/connect/routes/acs-users.ts index 51ffd54d..67418c56 100644 --- a/src/lib/seam/connect/routes/acs-users.ts +++ b/src/lib/seam/connect/routes/acs-users.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,112 +154,116 @@ export class SeamHttpAcsUsers { await clientSessions.get() } - async addToAccessGroup(body?: AcsUsersAddToAccessGroupBody): Promise { - await this.client.request({ - url: '/acs/users/add_to_access_group', + addToAccessGroup( + body?: AcsUsersAddToAccessGroupBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/add_to_access_group', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async create( + create( body?: AcsUsersCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/users/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/create', method: 'post', - data: body, + body, + responseKey: 'acs_user', }) - - return data.acs_user } - async delete(body?: AcsUsersDeleteBody): Promise { - await this.client.request({ - url: '/acs/users/delete', + delete(body?: AcsUsersDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: AcsUsersGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/users/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/get', method: 'post', - data: body, + body, + responseKey: 'acs_user', }) - - return data.acs_user } - async list( + list( body?: AcsUsersListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/acs/users/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/list', method: 'post', - data: body, + body, + responseKey: 'acs_users', }) - - return data.acs_users } - async listAccessibleEntrances( + listAccessibleEntrances( body?: AcsUsersListAccessibleEntrancesParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/acs/users/list_accessible_entrances', - method: 'post', - data: body, - }) - - return data.acs_entrances + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/list_accessible_entrances', + method: 'post', + body, + responseKey: 'acs_entrances', + }) } - async removeFromAccessGroup( + removeFromAccessGroup( body?: AcsUsersRemoveFromAccessGroupBody, - ): Promise { - await this.client.request({ - url: '/acs/users/remove_from_access_group', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/remove_from_access_group', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async revokeAccessToAllEntrances( + revokeAccessToAllEntrances( body?: AcsUsersRevokeAccessToAllEntrancesBody, - ): Promise { - await this.client.request({ - url: '/acs/users/revoke_access_to_all_entrances', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/revoke_access_to_all_entrances', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async suspend(body?: AcsUsersSuspendBody): Promise { - await this.client.request({ - url: '/acs/users/suspend', + suspend(body?: AcsUsersSuspendBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/suspend', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async unsuspend(body?: AcsUsersUnsuspendBody): Promise { - await this.client.request({ - url: '/acs/users/unsuspend', + unsuspend(body?: AcsUsersUnsuspendBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/unsuspend', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async update(body?: AcsUsersUpdateBody): Promise { - await this.client.request({ - url: '/acs/users/update', + update(body?: AcsUsersUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/acs/users/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index ca70e78e..e81c2bef 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -31,7 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' -import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -154,40 +154,28 @@ export class SeamHttpActionAttempts { await clientSessions.get() } - async get( + get( body?: ActionAttemptsGetParams, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/action_attempts/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/action_attempts/get', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async list( + list( body?: ActionAttemptsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/action_attempts/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/action_attempts/list', method: 'post', - data: body, + body, + responseKey: 'action_attempts', }) - - return data.action_attempts } } diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index 495fb802..13912df8 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' export class SeamHttpClientSessions { client: Client @@ -151,81 +152,76 @@ export class SeamHttpClientSessions { await clientSessions.get() } - async create( + create( body?: ClientSessionsCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/client_sessions/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/create', method: 'post', - data: body, + body, + responseKey: 'client_session', }) - - return data.client_session } - async delete(body?: ClientSessionsDeleteBody): Promise { - await this.client.request({ - url: '/client_sessions/delete', + delete(body?: ClientSessionsDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: ClientSessionsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/client_sessions/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/get', method: 'post', - data: body, + body, + responseKey: 'client_session', }) - - return data.client_session } - async getOrCreate( + getOrCreate( body?: ClientSessionsGetOrCreateBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/client_sessions/get_or_create', - method: 'post', - data: body, - }) - - return data.client_session + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/get_or_create', + method: 'post', + body, + responseKey: 'client_session', + }) } - async grantAccess( + grantAccess( body?: ClientSessionsGrantAccessBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/client_sessions/grant_access', - method: 'post', - data: body, - }) - - return data.client_session + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/grant_access', + method: 'post', + body, + responseKey: 'client_session', + }) } - async list( + list( body?: ClientSessionsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/client_sessions/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/list', method: 'post', - data: body, + body, + responseKey: 'client_sessions', }) - - return data.client_sessions } - async revoke(body?: ClientSessionsRevokeBody): Promise { - await this.client.request({ - url: '/client_sessions/revoke', + revoke(body?: ClientSessionsRevokeBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/client_sessions/revoke', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/connect-webviews.ts b/src/lib/seam/connect/routes/connect-webviews.ts index 389f3384..99be2c74 100644 --- a/src/lib/seam/connect/routes/connect-webviews.ts +++ b/src/lib/seam/connect/routes/connect-webviews.ts @@ -35,6 +35,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -157,55 +158,54 @@ export class SeamHttpConnectWebviews { await clientSessions.get() } - async create( + create( body?: ConnectWebviewsCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/connect_webviews/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connect_webviews/create', method: 'post', - data: body, + body, + responseKey: 'connect_webview', }) - - return data.connect_webview } - async delete(body?: ConnectWebviewsDeleteBody): Promise { - await this.client.request({ - url: '/connect_webviews/delete', + delete(body?: ConnectWebviewsDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connect_webviews/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: ConnectWebviewsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/connect_webviews/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connect_webviews/get', method: 'post', - data: body, + body, + responseKey: 'connect_webview', }) - - return data.connect_webview } - async list( + list( body?: ConnectWebviewsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/connect_webviews/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connect_webviews/list', method: 'post', - data: body, + body, + responseKey: 'connect_webviews', }) - - return data.connect_webviews } - async view(params?: ConnectWebviewsViewParams): Promise { - await this.client.request({ - url: '/connect_webviews/view', + view(params?: ConnectWebviewsViewParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connect_webviews/view', method: 'get', params, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/connected-accounts.ts b/src/lib/seam/connect/routes/connected-accounts.ts index a88a29cf..2d82aca7 100644 --- a/src/lib/seam/connect/routes/connected-accounts.ts +++ b/src/lib/seam/connect/routes/connected-accounts.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,50 +154,46 @@ export class SeamHttpConnectedAccounts { await clientSessions.get() } - async delete(body?: ConnectedAccountsDeleteBody): Promise { - await this.client.request({ - url: '/connected_accounts/delete', + delete(body?: ConnectedAccountsDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connected_accounts/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: ConnectedAccountsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/connected_accounts/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connected_accounts/get', method: 'post', - data: body, + body, + responseKey: 'connected_account', }) - - return data.connected_account } - async list( + list( body?: ConnectedAccountsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/connected_accounts/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connected_accounts/list', method: 'post', - data: body, + body, + responseKey: 'connected_accounts', }) - - return data.connected_accounts } - async update( + update( body?: ConnectedAccountsUpdateBody, - ): Promise { - const { data } = await this.client.request( - { - url: '/connected_accounts/update', - method: 'post', - data: body, - }, - ) - - return data.connected_account + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/connected_accounts/update', + method: 'post', + body, + responseKey: 'connected_account', + }) } } diff --git a/src/lib/seam/connect/routes/devices-simulate.ts b/src/lib/seam/connect/routes/devices-simulate.ts index c0b934fe..dd840c44 100644 --- a/src/lib/seam/connect/routes/devices-simulate.ts +++ b/src/lib/seam/connect/routes/devices-simulate.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,11 +154,12 @@ export class SeamHttpDevicesSimulate { await clientSessions.get() } - async remove(body?: DevicesSimulateRemoveBody): Promise { - await this.client.request({ - url: '/devices/simulate/remove', + remove(body?: DevicesSimulateRemoveBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/simulate/remove', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/devices-unmanaged.ts b/src/lib/seam/connect/routes/devices-unmanaged.ts index f255cb25..84c205bc 100644 --- a/src/lib/seam/connect/routes/devices-unmanaged.ts +++ b/src/lib/seam/connect/routes/devices-unmanaged.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,35 +154,34 @@ export class SeamHttpDevicesUnmanaged { await clientSessions.get() } - async get( + get( body?: DevicesUnmanagedGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/devices/unmanaged/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/unmanaged/get', method: 'post', - data: body, + body, + responseKey: 'device', }) - - return data.device } - async list( + list( body?: DevicesUnmanagedListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/devices/unmanaged/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/unmanaged/list', method: 'post', - data: body, + body, + responseKey: 'devices', }) - - return data.devices } - async update(body?: DevicesUnmanagedUpdateBody): Promise { - await this.client.request({ - url: '/devices/unmanaged/update', + update(body?: DevicesUnmanagedUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/unmanaged/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/devices.ts b/src/lib/seam/connect/routes/devices.ts index 98ac0f91..e09f6be0 100644 --- a/src/lib/seam/connect/routes/devices.ts +++ b/src/lib/seam/connect/routes/devices.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpDevicesSimulate } from './devices-simulate.js' @@ -163,54 +164,52 @@ export class SeamHttpDevices { return SeamHttpDevicesSimulate.fromClient(this.client, this.defaults) } - async delete(body?: DevicesDeleteBody): Promise { - await this.client.request({ - url: '/devices/delete', + delete(body?: DevicesDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get(body?: DevicesGetParams): Promise { - const { data } = await this.client.request({ - url: '/devices/get', + get(body?: DevicesGetParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/get', method: 'post', - data: body, + body, + responseKey: 'device', }) - - return data.device } - async list( + list( body?: DevicesListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/devices/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/list', method: 'post', - data: body, + body, + responseKey: 'devices', }) - - return data.devices } - async listDeviceProviders( + listDeviceProviders( body?: DevicesListDeviceProvidersParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/devices/list_device_providers', - method: 'post', - data: body, - }) - - return data.device_providers + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/list_device_providers', + method: 'post', + body, + responseKey: 'device_providers', + }) } - async update(body?: DevicesUpdateBody): Promise { - await this.client.request({ - url: '/devices/update', + update(body?: DevicesUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/devices/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/events.ts b/src/lib/seam/connect/routes/events.ts index b24f07f3..d7996a59 100644 --- a/src/lib/seam/connect/routes/events.ts +++ b/src/lib/seam/connect/routes/events.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,24 +154,22 @@ export class SeamHttpEvents { await clientSessions.get() } - async get(body?: EventsGetParams): Promise { - const { data } = await this.client.request({ - url: '/events/get', + get(body?: EventsGetParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/events/get', method: 'post', - data: body, + body, + responseKey: 'event', }) - - return data.event } - async list(body?: EventsListParams): Promise { - const { data } = await this.client.request({ - url: '/events/list', + list(body?: EventsListParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/events/list', method: 'post', - data: body, + body, + responseKey: 'events', }) - - return data.events } } diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index 2ede8942..73eb37d2 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -31,9 +31,8 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' -import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' -import { SeamHttpActionAttempts } from './action-attempts.js' import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpLocks { @@ -155,72 +154,48 @@ export class SeamHttpLocks { await clientSessions.get() } - async get(body?: LocksGetParams): Promise { - const { data } = await this.client.request({ - url: '/locks/get', + get(body?: LocksGetParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/locks/get', method: 'post', - data: body, + body, + responseKey: 'device', }) - - return data.device } - async list(body?: LocksListParams): Promise { - const { data } = await this.client.request({ - url: '/locks/list', + list(body?: LocksListParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/locks/list', method: 'post', - data: body, + body, + responseKey: 'devices', }) - - return data.devices } - async lockDoor( + lockDoor( body?: LocksLockDoorBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/locks/lock_door', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/locks/lock_door', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async unlockDoor( + unlockDoor( body?: LocksUnlockDoorBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/locks/unlock_door', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/locks/unlock_door', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } } diff --git a/src/lib/seam/connect/routes/networks.ts b/src/lib/seam/connect/routes/networks.ts index 66958d23..d127b731 100644 --- a/src/lib/seam/connect/routes/networks.ts +++ b/src/lib/seam/connect/routes/networks.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,26 +154,26 @@ export class SeamHttpNetworks { await clientSessions.get() } - async get(body?: NetworksGetParams): Promise { - const { data } = await this.client.request({ - url: '/networks/get', + get( + body?: NetworksGetParams, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/networks/get', method: 'post', - data: body, + body, + responseKey: 'network', }) - - return data.network } - async list( + list( body?: NetworksListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/networks/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/networks/list', method: 'post', - data: body, + body, + responseKey: 'networks', }) - - return data.networks } } diff --git a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts index 7a8a3f27..7bffab43 100644 --- a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +++ b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -156,58 +157,67 @@ export class SeamHttpNoiseSensorsNoiseThresholds { await clientSessions.get() } - async create( + create( body?: NoiseSensorsNoiseThresholdsCreateBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/noise_sensors/noise_thresholds/create', - method: 'post', - data: body, - }) - - return data.noise_threshold + ): SeamHttpRequest< + NoiseSensorsNoiseThresholdsCreateResponse, + 'noise_threshold' + > { + return new SeamHttpRequest(this, { + path: '/noise_sensors/noise_thresholds/create', + method: 'post', + body, + responseKey: 'noise_threshold', + }) } - async delete(body?: NoiseSensorsNoiseThresholdsDeleteBody): Promise { - await this.client.request({ - url: '/noise_sensors/noise_thresholds/delete', + delete( + body?: NoiseSensorsNoiseThresholdsDeleteBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/noise_sensors/noise_thresholds/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: NoiseSensorsNoiseThresholdsGetParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/noise_sensors/noise_thresholds/get', - method: 'post', - data: body, - }) - - return data.noise_threshold + ): SeamHttpRequest< + NoiseSensorsNoiseThresholdsGetResponse, + 'noise_threshold' + > { + return new SeamHttpRequest(this, { + path: '/noise_sensors/noise_thresholds/get', + method: 'post', + body, + responseKey: 'noise_threshold', + }) } - async list( + list( body?: NoiseSensorsNoiseThresholdsListParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/noise_sensors/noise_thresholds/list', - method: 'post', - data: body, - }) - - return data.noise_thresholds + ): SeamHttpRequest< + NoiseSensorsNoiseThresholdsListResponse, + 'noise_thresholds' + > { + return new SeamHttpRequest(this, { + path: '/noise_sensors/noise_thresholds/list', + method: 'post', + body, + responseKey: 'noise_thresholds', + }) } - async update(body?: NoiseSensorsNoiseThresholdsUpdateBody): Promise { - await this.client.request({ - url: '/noise_sensors/noise_thresholds/update', + update( + body?: NoiseSensorsNoiseThresholdsUpdateBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/noise_sensors/noise_thresholds/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/phones-simulate.ts b/src/lib/seam/connect/routes/phones-simulate.ts index c75450ad..45ef4752 100644 --- a/src/lib/seam/connect/routes/phones-simulate.ts +++ b/src/lib/seam/connect/routes/phones-simulate.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,17 +154,15 @@ export class SeamHttpPhonesSimulate { await clientSessions.get() } - async createSandboxPhone( + createSandboxPhone( body?: PhonesSimulateCreateSandboxPhoneBody, - ): Promise { - const { data } = - await this.client.request({ - url: '/phones/simulate/create_sandbox_phone', - method: 'post', - data: body, - }) - - return data.phone + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/phones/simulate/create_sandbox_phone', + method: 'post', + body, + responseKey: 'phone', + }) } } diff --git a/src/lib/seam/connect/routes/phones.ts b/src/lib/seam/connect/routes/phones.ts index 526ec7ce..e4b68223 100644 --- a/src/lib/seam/connect/routes/phones.ts +++ b/src/lib/seam/connect/routes/phones.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpPhonesSimulate } from './phones-simulate.js' @@ -158,22 +159,22 @@ export class SeamHttpPhones { return SeamHttpPhonesSimulate.fromClient(this.client, this.defaults) } - async deactivate(body?: PhonesDeactivateBody): Promise { - await this.client.request({ - url: '/phones/deactivate', + deactivate(body?: PhonesDeactivateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/phones/deactivate', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async list(body?: PhonesListParams): Promise { - const { data } = await this.client.request({ - url: '/phones/list', + list(body?: PhonesListParams): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/phones/list', method: 'post', - data: body, + body, + responseKey: 'phones', }) - - return data.phones } } diff --git a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts index d6039b7e..252b3ca4 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -156,77 +157,68 @@ export class SeamHttpThermostatsClimateSettingSchedules { await clientSessions.get() } - async create( + create( body?: ThermostatsClimateSettingSchedulesCreateBody, - ): Promise< - ThermostatsClimateSettingSchedulesCreateResponse['climate_setting_schedule'] + ): SeamHttpRequest< + ThermostatsClimateSettingSchedulesCreateResponse, + 'climate_setting_schedule' > { - const { data } = - await this.client.request( - { - url: '/thermostats/climate_setting_schedules/create', - method: 'post', - data: body, - }, - ) - - return data.climate_setting_schedule + return new SeamHttpRequest(this, { + path: '/thermostats/climate_setting_schedules/create', + method: 'post', + body, + responseKey: 'climate_setting_schedule', + }) } - async delete( + delete( body?: ThermostatsClimateSettingSchedulesDeleteBody, - ): Promise { - await this.client.request( - { - url: '/thermostats/climate_setting_schedules/delete', - method: 'post', - data: body, - }, - ) + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/climate_setting_schedules/delete', + method: 'post', + body, + responseKey: undefined, + }) } - async get( + get( body?: ThermostatsClimateSettingSchedulesGetParams, - ): Promise< - ThermostatsClimateSettingSchedulesGetResponse['climate_setting_schedule'] + ): SeamHttpRequest< + ThermostatsClimateSettingSchedulesGetResponse, + 'climate_setting_schedule' > { - const { data } = - await this.client.request({ - url: '/thermostats/climate_setting_schedules/get', - method: 'post', - data: body, - }) - - return data.climate_setting_schedule + return new SeamHttpRequest(this, { + path: '/thermostats/climate_setting_schedules/get', + method: 'post', + body, + responseKey: 'climate_setting_schedule', + }) } - async list( + list( body?: ThermostatsClimateSettingSchedulesListParams, - ): Promise< - ThermostatsClimateSettingSchedulesListResponse['climate_setting_schedules'] + ): SeamHttpRequest< + ThermostatsClimateSettingSchedulesListResponse, + 'climate_setting_schedules' > { - const { data } = - await this.client.request( - { - url: '/thermostats/climate_setting_schedules/list', - method: 'post', - data: body, - }, - ) - - return data.climate_setting_schedules + return new SeamHttpRequest(this, { + path: '/thermostats/climate_setting_schedules/list', + method: 'post', + body, + responseKey: 'climate_setting_schedules', + }) } - async update( + update( body?: ThermostatsClimateSettingSchedulesUpdateBody, - ): Promise { - await this.client.request( - { - url: '/thermostats/climate_setting_schedules/update', - method: 'post', - data: body, - }, - ) + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/climate_setting_schedules/update', + method: 'post', + body, + responseKey: undefined, + }) } } diff --git a/src/lib/seam/connect/routes/thermostats.ts b/src/lib/seam/connect/routes/thermostats.ts index 25f81c6f..f766eb0e 100644 --- a/src/lib/seam/connect/routes/thermostats.ts +++ b/src/lib/seam/connect/routes/thermostats.ts @@ -31,9 +31,8 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' -import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' -import { SeamHttpActionAttempts } from './action-attempts.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpThermostatsClimateSettingSchedules } from './thermostats-climate-setting-schedules.js' @@ -163,155 +162,99 @@ export class SeamHttpThermostats { ) } - async cool( + cool( body?: ThermostatsCoolBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/cool', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/cool', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async get( + get( body?: ThermostatsGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/get', method: 'post', - data: body, + body, + responseKey: 'thermostat', }) - - return data.thermostat } - async heat( + heat( body?: ThermostatsHeatBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/heat', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/heat', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async heatCool( + heatCool( body?: ThermostatsHeatCoolBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/heat_cool', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/heat_cool', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async list( + list( body?: ThermostatsListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/list', method: 'post', - data: body, + body, + responseKey: 'thermostats', }) - - return data.thermostats } - async off( + off( body?: ThermostatsOffBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/off', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/off', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async setFanMode( + setFanMode( body?: ThermostatsSetFanModeBody, options: Pick = {}, - ): Promise { - const { data } = await this.client.request({ - url: '/thermostats/set_fan_mode', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/set_fan_mode', method: 'post', - data: body, + body, + responseKey: 'action_attempt', + options, }) - const waitForActionAttempt = - options.waitForActionAttempt ?? this.defaults.waitForActionAttempt - if (waitForActionAttempt !== false) { - return await resolveActionAttempt( - data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client, { - ...this.defaults, - waitForActionAttempt: false, - }), - typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, - ) - } - return data.action_attempt } - async update(body?: ThermostatsUpdateBody): Promise { - await this.client.request({ - url: '/thermostats/update', + update(body?: ThermostatsUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/thermostats/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/user-identities-enrollment-automations.ts b/src/lib/seam/connect/routes/user-identities-enrollment-automations.ts index 3bc85d7f..30792eb2 100644 --- a/src/lib/seam/connect/routes/user-identities-enrollment-automations.ts +++ b/src/lib/seam/connect/routes/user-identities-enrollment-automations.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -156,67 +157,57 @@ export class SeamHttpUserIdentitiesEnrollmentAutomations { await clientSessions.get() } - async delete( + delete( body?: UserIdentitiesEnrollmentAutomationsDeleteBody, - ): Promise { - await this.client.request( - { - url: '/user_identities/enrollment_automations/delete', - method: 'post', - data: body, - }, - ) + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/enrollment_automations/delete', + method: 'post', + body, + responseKey: undefined, + }) } - async get( + get( body?: UserIdentitiesEnrollmentAutomationsGetParams, - ): Promise< - UserIdentitiesEnrollmentAutomationsGetResponse['enrollment_automation'] + ): SeamHttpRequest< + UserIdentitiesEnrollmentAutomationsGetResponse, + 'enrollment_automation' > { - const { data } = - await this.client.request( - { - url: '/user_identities/enrollment_automations/get', - method: 'post', - data: body, - }, - ) - - return data.enrollment_automation + return new SeamHttpRequest(this, { + path: '/user_identities/enrollment_automations/get', + method: 'post', + body, + responseKey: 'enrollment_automation', + }) } - async launch( + launch( body?: UserIdentitiesEnrollmentAutomationsLaunchBody, - ): Promise< - UserIdentitiesEnrollmentAutomationsLaunchResponse['enrollment_automation'] + ): SeamHttpRequest< + UserIdentitiesEnrollmentAutomationsLaunchResponse, + 'enrollment_automation' > { - const { data } = - await this.client.request( - { - url: '/user_identities/enrollment_automations/launch', - method: 'post', - data: body, - }, - ) - - return data.enrollment_automation + return new SeamHttpRequest(this, { + path: '/user_identities/enrollment_automations/launch', + method: 'post', + body, + responseKey: 'enrollment_automation', + }) } - async list( + list( body?: UserIdentitiesEnrollmentAutomationsListParams, - ): Promise< - UserIdentitiesEnrollmentAutomationsListResponse['enrollment_automations'] + ): SeamHttpRequest< + UserIdentitiesEnrollmentAutomationsListResponse, + 'enrollment_automations' > { - const { data } = - await this.client.request( - { - url: '/user_identities/enrollment_automations/list', - method: 'post', - data: body, - }, - ) - - return data.enrollment_automations + return new SeamHttpRequest(this, { + path: '/user_identities/enrollment_automations/list', + method: 'post', + body, + responseKey: 'enrollment_automations', + }) } } diff --git a/src/lib/seam/connect/routes/user-identities.ts b/src/lib/seam/connect/routes/user-identities.ts index 51d5525b..bc0e8f04 100644 --- a/src/lib/seam/connect/routes/user-identities.ts +++ b/src/lib/seam/connect/routes/user-identities.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpUserIdentitiesEnrollmentAutomations } from './user-identities-enrollment-automations.js' @@ -161,132 +162,134 @@ export class SeamHttpUserIdentities { ) } - async addAcsUser(body?: UserIdentitiesAddAcsUserBody): Promise { - await this.client.request({ - url: '/user_identities/add_acs_user', + addAcsUser( + body?: UserIdentitiesAddAcsUserBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/add_acs_user', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async create( + create( body?: UserIdentitiesCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/user_identities/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/create', method: 'post', - data: body, + body, + responseKey: 'user_identity', }) - - return data.user_identity } - async delete(body?: UserIdentitiesDeleteBody): Promise { - await this.client.request({ - url: '/user_identities/delete', + delete(body?: UserIdentitiesDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get( + get( body?: UserIdentitiesGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/user_identities/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/get', method: 'post', - data: body, + body, + responseKey: 'user_identity', }) - - return data.user_identity } - async grantAccessToDevice( + grantAccessToDevice( body?: UserIdentitiesGrantAccessToDeviceBody, - ): Promise { - await this.client.request({ - url: '/user_identities/grant_access_to_device', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/grant_access_to_device', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async list( + list( body?: UserIdentitiesListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/user_identities/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/list', method: 'post', - data: body, + body, + responseKey: 'user_identities', }) - - return data.user_identities } - async listAccessibleDevices( + listAccessibleDevices( body?: UserIdentitiesListAccessibleDevicesParams, - ): Promise< - UserIdentitiesListAccessibleDevicesResponse['accessible_devices'] + ): SeamHttpRequest< + UserIdentitiesListAccessibleDevicesResponse, + 'accessible_devices' > { - const { data } = - await this.client.request({ - url: '/user_identities/list_accessible_devices', - method: 'post', - data: body, - }) - - return data.accessible_devices + return new SeamHttpRequest(this, { + path: '/user_identities/list_accessible_devices', + method: 'post', + body, + responseKey: 'accessible_devices', + }) } - async listAcsSystems( + listAcsSystems( body?: UserIdentitiesListAcsSystemsParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/user_identities/list_acs_systems', - method: 'post', - data: body, - }) - - return data.acs_systems + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/list_acs_systems', + method: 'post', + body, + responseKey: 'acs_systems', + }) } - async listAcsUsers( + listAcsUsers( body?: UserIdentitiesListAcsUsersParams, - ): Promise { - const { data } = - await this.client.request({ - url: '/user_identities/list_acs_users', - method: 'post', - data: body, - }) - - return data.acs_users + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/list_acs_users', + method: 'post', + body, + responseKey: 'acs_users', + }) } - async removeAcsUser(body?: UserIdentitiesRemoveAcsUserBody): Promise { - await this.client.request({ - url: '/user_identities/remove_acs_user', + removeAcsUser( + body?: UserIdentitiesRemoveAcsUserBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/remove_acs_user', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async revokeAccessToDevice( + revokeAccessToDevice( body?: UserIdentitiesRevokeAccessToDeviceBody, - ): Promise { - await this.client.request({ - url: '/user_identities/revoke_access_to_device', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/revoke_access_to_device', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async update(body?: UserIdentitiesUpdateBody): Promise { - await this.client.request({ - url: '/user_identities/update', + update(body?: UserIdentitiesUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/user_identities/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/webhooks.ts b/src/lib/seam/connect/routes/webhooks.ts index 59d14565..39b4d85c 100644 --- a/src/lib/seam/connect/routes/webhooks.ts +++ b/src/lib/seam/connect/routes/webhooks.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,53 +154,54 @@ export class SeamHttpWebhooks { await clientSessions.get() } - async create( + create( body?: WebhooksCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/webhooks/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/webhooks/create', method: 'post', - data: body, + body, + responseKey: 'webhook', }) - - return data.webhook } - async delete(body?: WebhooksDeleteBody): Promise { - await this.client.request({ - url: '/webhooks/delete', + delete(body?: WebhooksDeleteBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/webhooks/delete', method: 'post', - data: body, + body, + responseKey: undefined, }) } - async get(body?: WebhooksGetParams): Promise { - const { data } = await this.client.request({ - url: '/webhooks/get', + get( + body?: WebhooksGetParams, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/webhooks/get', method: 'post', - data: body, + body, + responseKey: 'webhook', }) - - return data.webhook } - async list( + list( body?: WebhooksListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/webhooks/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/webhooks/list', method: 'post', - data: body, + body, + responseKey: 'webhooks', }) - - return data.webhooks } - async update(body?: WebhooksUpdateBody): Promise { - await this.client.request({ - url: '/webhooks/update', + update(body?: WebhooksUpdateBody): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/webhooks/update', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index 1773a116..c90cc1a3 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -31,6 +31,7 @@ import { limitToSeamHttpRequestOptions, parseOptions, } from 'lib/seam/connect/parse-options.js' +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -153,47 +154,47 @@ export class SeamHttpWorkspaces { await clientSessions.get() } - async create( + create( body?: WorkspacesCreateBody, - ): Promise { - const { data } = await this.client.request({ - url: '/workspaces/create', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/workspaces/create', method: 'post', - data: body, + body, + responseKey: 'workspace', }) - - return data.workspace } - async get( + get( body?: WorkspacesGetParams, - ): Promise { - const { data } = await this.client.request({ - url: '/workspaces/get', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/workspaces/get', method: 'post', - data: body, + body, + responseKey: 'workspace', }) - - return data.workspace } - async list( + list( body?: WorkspacesListParams, - ): Promise { - const { data } = await this.client.request({ - url: '/workspaces/list', + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/workspaces/list', method: 'post', - data: body, + body, + responseKey: 'workspaces', }) - - return data.workspaces } - async resetSandbox(body?: WorkspacesResetSandboxBody): Promise { - await this.client.request({ - url: '/workspaces/reset_sandbox', + resetSandbox( + body?: WorkspacesResetSandboxBody, + ): SeamHttpRequest { + return new SeamHttpRequest(this, { + path: '/workspaces/reset_sandbox', method: 'post', - data: body, + body, + responseKey: undefined, }) } } diff --git a/src/lib/seam/connect/seam-http-request.ts b/src/lib/seam/connect/seam-http-request.ts new file mode 100644 index 00000000..b21cf00a --- /dev/null +++ b/src/lib/seam/connect/seam-http-request.ts @@ -0,0 +1,144 @@ +import { serializeUrlSearchParams } from '@seamapi/url-search-params-serializer' +import type { Method } from 'axios' + +import type { Client } from './client.js' +import type { SeamHttpRequestOptions } from './options.js' +import { resolveActionAttempt } from './resolve-action-attempt.js' +import { SeamHttpActionAttempts } from './routes/index.js' + +interface SeamHttpRequestParent { + readonly client: Client + readonly defaults: Required +} + +interface SeamHttpRequestConfig { + readonly path: string + readonly method: Method + readonly body?: unknown + readonly params?: undefined | Record + readonly responseKey: TResponseKey + readonly options?: Pick +} + +export class SeamHttpRequest< + const TResponse, + const TResponseKey extends keyof TResponse | undefined, +> implements + PromiseLike< + TResponseKey extends keyof TResponse ? TResponse[TResponseKey] : undefined + > +{ + readonly #parent: SeamHttpRequestParent + readonly #config: SeamHttpRequestConfig + + constructor( + parent: SeamHttpRequestParent, + config: SeamHttpRequestConfig, + ) { + this.#parent = parent + this.#config = config + } + + public get responseKey(): TResponseKey { + return this.#config.responseKey + } + + public get url(): URL { + const { client } = this.#parent + const { params } = this.#config + + const serializer = + typeof client.defaults.paramsSerializer === 'function' + ? client.defaults.paramsSerializer + : serializeUrlSearchParams + + const origin = getUrlPrefix(client.defaults.baseURL ?? '') + + const pathname = this.#config.path.startsWith('/') + ? this.#config.path + : `/${this.#config.path}` + + const path = params == null ? pathname : `${pathname}?${serializer(params)}` + + return new URL(`${origin}${path}`) + } + + public get method(): Method { + return this.#config.method + } + + public get body(): unknown { + return this.#config.body + } + + async execute(): Promise< + TResponseKey extends keyof TResponse ? TResponse[TResponseKey] : undefined + > { + const { client } = this.#parent + const response = await client.request({ + url: this.#config.path, + method: this.#config.method, + data: this.#config.body, + params: this.#config.params, + }) + if (this.responseKey === undefined) { + return undefined as TResponseKey extends keyof TResponse + ? TResponse[TResponseKey] + : undefined + } + const data = response.data[this.responseKey] + if (this.responseKey === 'action_attempt') { + const waitForActionAttempt = + this.#config.options?.waitForActionAttempt ?? + this.#parent.defaults.waitForActionAttempt + if (waitForActionAttempt !== false) { + return await resolveActionAttempt( + data, + SeamHttpActionAttempts.fromClient(client, { + ...this.#parent.defaults, + waitForActionAttempt: false, + }), + typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, + ) + } + } + return data + } + + then< + TResult1 = TResponseKey extends keyof TResponse + ? TResponse[TResponseKey] + : undefined, + TResult2 = never, + >( + onfulfilled?: + | (( + value: TResponseKey extends keyof TResponse + ? TResponse[TResponseKey] + : undefined, + ) => TResult1 | PromiseLike) + | null + | undefined, + onrejected?: + | ((reason: any) => TResult2 | PromiseLike) + | null + | undefined, + ): PromiseLike { + return this.execute().then(onfulfilled, onrejected) + } +} + +const getUrlPrefix = (input: string): string => { + if (URL.canParse(input)) { + const url = new URL(input).toString() + if (url.endsWith('/')) return url.slice(0, -1) + return url + } + if (globalThis.location != null) { + const pathname = input.startsWith('/') ? input : `/${input}` + return new URL(`${globalThis.location.origin}${pathname}`).toString() + } + throw new Error( + `Cannot resolve origin from ${input} in a non-browser environment`, + ) +} diff --git a/test/seam/connect/seam-http-request.test.ts b/test/seam/connect/seam-http-request.test.ts new file mode 100644 index 00000000..a575bb9c --- /dev/null +++ b/test/seam/connect/seam-http-request.test.ts @@ -0,0 +1,170 @@ +import test from 'ava' +import { getTestServer } from 'fixtures/seam/connect/api.js' + +import { SeamHttp } from '@seamapi/http/connect' + +import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js' + +test('SeamHttp: returns a SeamHttpRequest', async (t) => { + const { seed, endpoint } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }) + + const request = seam.devices.get({ device_id: seed.august_device_1 }) + + t.true(request instanceof SeamHttpRequest) + t.truthy(request.url) + t.is(request.responseKey, 'device') + t.deepEqual(request.body, { + device_id: seed.august_device_1, + }) + + const device = await request + t.is(device.workspace_id, seed.seed_workspace_1) + t.is(device.device_id, seed.august_device_1) + + // Ensure that the type of the response is correct. + type Expected = ResponseFromSeamHttpRequest + const validDeviceType: Expected['device_type'] = 'august_lock' + t.truthy(validDeviceType) + + // @ts-expect-error invalid device type. + const invalidDeviceType: Expected['device_type'] = 'invalid_device_type' + t.truthy(invalidDeviceType) +}) + +test('SeamHttpRequest: url is a URL for post requests', async (t) => { + const { seed, endpoint } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject(new URL(`${endpoint}/devices/get`)), + ) +}) + +test('SeamHttpRequest: url is a URL for get requests', async (t) => { + const { seed, endpoint } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }) + + const { url } = seam.connectWebviews.view({ + connect_webview_id: 'connect_webview_1', + auth_token: 'auth_token_1', + }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject( + new URL( + `${endpoint}/connect_webviews/view?auth_token=auth_token_1&connect_webview_id=connect_webview_1`, + ), + ), + ) +}) + +test('SeamHttpRequest: url is a URL when endpoint is a url without a path', async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: 'https://example.com', + }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject(new URL('https://example.com/devices/get')), + ) +}) + +test('SeamHttpRequest: url is a URL when endpoint is a url with a path', async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: 'https://example.com/some/sub/path', + }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject(new URL('https://example.com/some/sub/path/devices/get')), + ) +}) + +test.failing( + 'SeamHttpRequest: url is a URL when endpoint is path', + async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: '/some/sub/path', + }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject( + new URL('https://example.com/some/sub/path/devices/get'), + ), + ) + }, +) + +test.failing( + 'SeamHttpRequest: url is a URL when endpoint is empty', + async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: '', + }) + + // TODO: Set globalThis.location.origin = 'https://example.com' + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.true(url instanceof URL) + t.deepEqual( + toPlainUrlObject(url), + toPlainUrlObject(new URL('https://example.com/devices/get')), + ) + }, +) + +test('SeamHttpRequest: url throws if unable to resolve origin', async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: '', + }) + + const request = seam.devices.get({ device_id: 'abc123' }) + + t.throws(() => request.url, { message: /Cannot resolve origin/ }) +}) + +const toPlainUrlObject = (url: URL): Omit => { + return { + pathname: url.pathname, + hash: url.hash, + hostname: url.hostname, + protocol: url.protocol, + username: url.username, + port: url.port, + password: url.password, + host: url.host, + href: url.href, + origin: url.origin, + search: url.search, + } +} + +type ResponseFromSeamHttpRequest = + T extends SeamHttpRequest + ? TResponseKey extends keyof TResponse + ? TResponse[TResponseKey] + : undefined + : never