From 8cb5b8a5940531c71a74f39148a1579ca5521c68 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Wed, 15 Nov 2023 15:33:52 +0900 Subject: [PATCH] Add waitForActionAttempt option --- examples/unlock.ts | 28 +++-- generate-routes.ts | 51 ++++++++ src/lib/seam/connect/index.ts | 8 ++ .../seam/connect/resolve-action-attempt.ts | 25 ++-- .../connect/routes/access-codes-unmanaged.ts | 12 ++ src/lib/seam/connect/routes/access-codes.ts | 22 ++++ .../seam/connect/routes/acs-access-groups.ts | 13 ++ .../seam/connect/routes/acs-credentials.ts | 15 +++ src/lib/seam/connect/routes/acs-systems.ts | 6 + src/lib/seam/connect/routes/acs-users.ts | 21 ++++ .../seam/connect/routes/action-attempts.ts | 25 ++++ .../seam/connect/routes/client-sessions.ts | 19 +++ .../seam/connect/routes/connect-webviews.ts | 13 ++ .../seam/connect/routes/connected-accounts.ts | 8 ++ .../seam/connect/routes/devices-unmanaged.ts | 8 ++ src/lib/seam/connect/routes/devices.ts | 13 ++ src/lib/seam/connect/routes/events.ts | 6 + src/lib/seam/connect/routes/locks.ts | 47 ++++++++ .../routes/noise-sensors-noise-thresholds.ts | 12 ++ .../thermostats-climate-setting-schedules.ts | 13 ++ src/lib/seam/connect/routes/thermostats.ts | 18 +++ .../seam/connect/routes/user-identities.ts | 20 ++++ src/lib/seam/connect/routes/webhooks.ts | 11 ++ src/lib/seam/connect/routes/workspaces.ts | 8 ++ ...est.ts => wait-for-action-attempt.test.ts} | 112 +++++++++++------- 25 files changed, 469 insertions(+), 65 deletions(-) rename test/seam/connect/{resolve-action-attempt.test.ts => wait-for-action-attempt.test.ts} (58%) diff --git a/examples/unlock.ts b/examples/unlock.ts index 6debf4e9..b168a286 100644 --- a/examples/unlock.ts +++ b/examples/unlock.ts @@ -3,7 +3,7 @@ import type { Builder, Command, Describe } from 'landlubber' import { isSeamActionAttemptFailedError, isSeamActionAttemptTimeoutError, - resolveActionAttempt, + type LocksUnlockDoorResponse, } from '@seamapi/http/connect' import type { Handler } from './index.js' @@ -24,23 +24,29 @@ export const builder: Builder = { } export const handler: Handler = async ({ deviceId, seam, logger }) => { - const actionAttempt = await seam.locks.unlockDoor({ - device_id: deviceId, - }) - try { - const sucessfulActionAttempt = await resolveActionAttempt( - actionAttempt, - seam, + const actionAttempt = await seam.locks.unlockDoor( + { + device_id: deviceId, + }, + { waitForActionAttempt: true }, ) - logger.info({ actionAttempt: sucessfulActionAttempt }, 'unlocked') + logger.info({ actionAttempt }, 'unlocked') } catch (err: unknown) { - if (isSeamActionAttemptFailedError(err)) { + if ( + isSeamActionAttemptFailedError( + err, + ) + ) { logger.info({ err }, 'Could not unlock the door') return } - if (isSeamActionAttemptTimeoutError(err)) { + if ( + isSeamActionAttemptTimeoutError< + LocksUnlockDoorResponse['action_attempt'] + >(err) + ) { logger.info({ err }, 'Door took too long to unlock') return } diff --git a/generate-routes.ts b/generate-routes.ts index a3eb50ba..5afe006a 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -256,12 +256,21 @@ import { type SeamHttpOptionsWithPersonalAccessToken, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + resolveActionAttempt, + type ResolveActionAttemptOptions, +} from 'lib/seam/connect/resolve-action-attempt.js' ${ namespace === 'client_sessions' ? '' : "import { SeamHttpClientSessions } from './client-sessions.js'" } +${ + namespace === 'action_attempts' + ? '' + : "import { SeamHttpActionAttempts } from './action-attempts.js'" +} ${subresources .map((subresource) => renderSubresourceImport(subresource, namespace)) .join('\n')} @@ -314,6 +323,7 @@ const renderClassMethod = ({ name, namespace, })}, + ${renderClassMethodOptions({ resource })} ): Promise<${ resource === null ? 'void' @@ -330,10 +340,45 @@ const renderClassMethod = ({ requestFormat === 'params' ? 'params,' : '' } ${requestFormat === 'body' ? 'data: body,' : ''} }) + ${ + resource === 'action_attempt' + ? `if (waitForActionAttempt) { return resolveActionAttempt(data.${resource}, SeamHttpActionAttempts.fromClient(this.client), { timeout, pollingInterval }) }` + : '' + } ${resource === null ? '' : `return data.${resource}`} } ` +const renderClassMethodOptions = ({ + resource, +}: Pick): string => { + if (resource === 'action_attempt') { + return `{ waitForActionAttempt = false, timeout = 5000, pollingInterval = 500 }: ${renderClassMethodOptionsTypeDef( + { resource }, + )} = {},` + } + return '' +} + +const renderClassMethodOptionsType = ({ + name, + namespace, +}: Pick): string => + [pascalCase(namespace), pascalCase(name), 'Options'].join('') + +const renderClassMethodOptionsTypeDef = ({ + resource, +}: Pick): string => { + if (resource === 'action_attempt') { + return ` + Partial & { + waitForActionAttempt?: boolean + } + ` + } + return 'never' +} + const renderSubresourceMethod = ( subresource: string, namespace: string, @@ -354,6 +399,7 @@ const renderEndpointExports = ({ name, path, namespace, + resource, requestFormat, }: Endpoint): string => ` export type ${renderRequestType({ @@ -364,6 +410,11 @@ export type ${renderRequestType({ export type ${renderResponseType({ name, namespace })}= SetNonNullable< Required> > + +export type ${renderClassMethodOptionsType({ + name, + namespace, +})} = ${renderClassMethodOptionsTypeDef({ resource })} ` const renderRequestType = ({ diff --git a/src/lib/seam/connect/index.ts b/src/lib/seam/connect/index.ts index 0deaa60c..7c858374 100644 --- a/src/lib/seam/connect/index.ts +++ b/src/lib/seam/connect/index.ts @@ -1,6 +1,14 @@ export { SeamHttpInvalidTokenError } from './auth.js' export * from './error-interceptor.js' export * from './options.js' +export { + isSeamActionAttemptError, + isSeamActionAttemptFailedError, + isSeamActionAttemptTimeoutError, + SeamActionAttemptError, + SeamActionAttemptFailedError, + SeamActionAttemptTimeoutError, +} from './resolve-action-attempt.js' export * from './routes/index.js' export * from './seam-http.js' export * from './seam-http-error.js' diff --git a/src/lib/seam/connect/resolve-action-attempt.ts b/src/lib/seam/connect/resolve-action-attempt.ts index 22b4f5d0..c88ec1f0 100644 --- a/src/lib/seam/connect/resolve-action-attempt.ts +++ b/src/lib/seam/connect/resolve-action-attempt.ts @@ -3,19 +3,18 @@ import type { FailedActionAttempt, SuccessfulActionAttempt, } from './action-attempt-types.js' -import type { SeamHttp } from './seam-http.js' +import type { SeamHttpActionAttempts } from './routes/index.js' -interface Options { - timeout?: number - pollingInterval?: number +export interface ResolveActionAttemptOptions { + timeout: number + pollingInterval: number } export const resolveActionAttempt = async ( - actionAttemptOrPromise: T | Promise, - seam: SeamHttp, - { timeout = 5000, pollingInterval = 500 }: Options = {}, + actionAttempt: T, + actionAttempts: SeamHttpActionAttempts, + { timeout, pollingInterval }: ResolveActionAttemptOptions, ): Promise> => { - const actionAttempt = await actionAttemptOrPromise let timeoutRef const timeoutPromise = new Promise>( (_resolve, reject) => { @@ -27,7 +26,7 @@ export const resolveActionAttempt = async ( try { return await Promise.race([ - pollActionAttempt(actionAttempt, seam, { pollingInterval }), + pollActionAttempt(actionAttempt, actionAttempts, { pollingInterval }), timeoutPromise, ]) } finally { @@ -37,8 +36,8 @@ export const resolveActionAttempt = async ( const pollActionAttempt = async ( actionAttempt: T, - seam: SeamHttp, - options: Pick, + actionAttempts: SeamHttpActionAttempts, + options: Pick, ): Promise> => { if (isSuccessfulActionAttempt(actionAttempt)) { return actionAttempt @@ -50,13 +49,13 @@ const pollActionAttempt = async ( await new Promise((resolve) => setTimeout(resolve, options.pollingInterval)) - const nextActionAttempt = await seam.actionAttempts.get({ + const nextActionAttempt = await actionAttempts.get({ action_attempt_id: actionAttempt.action_attempt_id, }) return await pollActionAttempt( nextActionAttempt as unknown as T, - seam, + actionAttempts, options, ) } diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 924f3d9d..29cf2717 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -148,6 +148,7 @@ export class SeamHttpAccessCodesUnmanaged { data: body, }, ) + return data.access_code } @@ -160,6 +161,7 @@ export class SeamHttpAccessCodesUnmanaged { method: 'post', data: body, }) + return data.access_codes } @@ -179,6 +181,8 @@ export type AccessCodesUnmanagedConvertToManagedResponse = SetNonNullable< Required> > +export type AccessCodesUnmanagedConvertToManagedOptions = never + export type AccessCodesUnmanagedDeleteBody = RouteRequestBody<'/access_codes/unmanaged/delete'> @@ -186,6 +190,8 @@ export type AccessCodesUnmanagedDeleteResponse = SetNonNullable< Required> > +export type AccessCodesUnmanagedDeleteOptions = never + export type AccessCodesUnmanagedGetParams = RouteRequestBody<'/access_codes/unmanaged/get'> @@ -193,6 +199,8 @@ export type AccessCodesUnmanagedGetResponse = SetNonNullable< Required> > +export type AccessCodesUnmanagedGetOptions = never + export type AccessCodesUnmanagedListParams = RouteRequestBody<'/access_codes/unmanaged/list'> @@ -200,9 +208,13 @@ export type AccessCodesUnmanagedListResponse = SetNonNullable< Required> > +export type AccessCodesUnmanagedListOptions = never + export type AccessCodesUnmanagedUpdateBody = RouteRequestBody<'/access_codes/unmanaged/update'> export type AccessCodesUnmanagedUpdateResponse = SetNonNullable< Required> > + +export type AccessCodesUnmanagedUpdateOptions = never diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index 0ccf655b..c9600c29 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -133,6 +133,7 @@ export class SeamHttpAccessCodes { method: 'post', data: body, }) + return data.access_code } @@ -145,6 +146,7 @@ export class SeamHttpAccessCodes { method: 'post', data: body, }) + return data.access_codes } @@ -166,6 +168,7 @@ export class SeamHttpAccessCodes { data: body, }, ) + return data.generated_code } @@ -177,6 +180,7 @@ export class SeamHttpAccessCodes { method: 'post', data: body, }) + return data.access_code } @@ -188,6 +192,7 @@ export class SeamHttpAccessCodes { method: 'post', data: body, }) + return data.access_codes } @@ -200,6 +205,7 @@ export class SeamHttpAccessCodes { method: 'post', data: body, }) + return data.backup_access_code } @@ -218,6 +224,8 @@ export type AccessCodesCreateResponse = SetNonNullable< Required> > +export type AccessCodesCreateOptions = never + export type AccessCodesCreateMultipleBody = RouteRequestBody<'/access_codes/create_multiple'> @@ -225,12 +233,16 @@ export type AccessCodesCreateMultipleResponse = SetNonNullable< Required> > +export type AccessCodesCreateMultipleOptions = never + export type AccessCodesDeleteBody = RouteRequestBody<'/access_codes/delete'> export type AccessCodesDeleteResponse = SetNonNullable< Required> > +export type AccessCodesDeleteOptions = never + export type AccessCodesGenerateCodeBody = RouteRequestBody<'/access_codes/generate_code'> @@ -238,18 +250,24 @@ export type AccessCodesGenerateCodeResponse = SetNonNullable< Required> > +export type AccessCodesGenerateCodeOptions = never + export type AccessCodesGetParams = RouteRequestBody<'/access_codes/get'> export type AccessCodesGetResponse = SetNonNullable< Required> > +export type AccessCodesGetOptions = never + export type AccessCodesListParams = RouteRequestBody<'/access_codes/list'> export type AccessCodesListResponse = SetNonNullable< Required> > +export type AccessCodesListOptions = never + export type AccessCodesPullBackupAccessCodeBody = RouteRequestBody<'/access_codes/pull_backup_access_code'> @@ -257,8 +275,12 @@ export type AccessCodesPullBackupAccessCodeResponse = SetNonNullable< Required> > +export type AccessCodesPullBackupAccessCodeOptions = never + export type AccessCodesUpdateBody = RouteRequestBody<'/access_codes/update'> export type AccessCodesUpdateResponse = SetNonNullable< Required> > + +export type AccessCodesUpdateOptions = never diff --git a/src/lib/seam/connect/routes/acs-access-groups.ts b/src/lib/seam/connect/routes/acs-access-groups.ts index ccc339d3..c45f8985 100644 --- a/src/lib/seam/connect/routes/acs-access-groups.ts +++ b/src/lib/seam/connect/routes/acs-access-groups.ts @@ -136,6 +136,7 @@ export class SeamHttpAcsAccessGroups { method: 'post', data: body, }) + return data.acs_access_group } @@ -147,6 +148,7 @@ export class SeamHttpAcsAccessGroups { method: 'post', data: body, }) + return data.acs_access_groups } @@ -159,6 +161,7 @@ export class SeamHttpAcsAccessGroups { method: 'post', data: body, }) + return data.acs_users } @@ -178,6 +181,8 @@ export type AcsAccessGroupsAddUserResponse = SetNonNullable< Required> > +export type AcsAccessGroupsAddUserOptions = never + export type AcsAccessGroupsGetParams = RouteRequestBody<'/acs/access_groups/get'> @@ -185,6 +190,8 @@ export type AcsAccessGroupsGetResponse = SetNonNullable< Required> > +export type AcsAccessGroupsGetOptions = never + export type AcsAccessGroupsListParams = RouteRequestBody<'/acs/access_groups/list'> @@ -192,6 +199,8 @@ export type AcsAccessGroupsListResponse = SetNonNullable< Required> > +export type AcsAccessGroupsListOptions = never + export type AcsAccessGroupsListUsersParams = RouteRequestBody<'/acs/access_groups/list_users'> @@ -199,9 +208,13 @@ export type AcsAccessGroupsListUsersResponse = SetNonNullable< Required> > +export type AcsAccessGroupsListUsersOptions = never + export type AcsAccessGroupsRemoveUserBody = RouteRequestBody<'/acs/access_groups/remove_user'> export type AcsAccessGroupsRemoveUserResponse = SetNonNullable< Required> > + +export type AcsAccessGroupsRemoveUserOptions = never diff --git a/src/lib/seam/connect/routes/acs-credentials.ts b/src/lib/seam/connect/routes/acs-credentials.ts index ca6cc1e6..27f952e7 100644 --- a/src/lib/seam/connect/routes/acs-credentials.ts +++ b/src/lib/seam/connect/routes/acs-credentials.ts @@ -136,6 +136,7 @@ export class SeamHttpAcsCredentials { method: 'post', data: body, }) + return data.acs_credential } @@ -155,6 +156,7 @@ export class SeamHttpAcsCredentials { method: 'post', data: body, }) + return data.acs_credential } @@ -166,6 +168,7 @@ export class SeamHttpAcsCredentials { method: 'post', data: body, }) + return data.acs_credentials } @@ -185,6 +188,8 @@ export type AcsCredentialsAssignResponse = SetNonNullable< Required> > +export type AcsCredentialsAssignOptions = never + export type AcsCredentialsCreateBody = RouteRequestBody<'/acs/credentials/create'> @@ -192,6 +197,8 @@ export type AcsCredentialsCreateResponse = SetNonNullable< Required> > +export type AcsCredentialsCreateOptions = never + export type AcsCredentialsDeleteBody = RouteRequestBody<'/acs/credentials/delete'> @@ -199,21 +206,29 @@ export type AcsCredentialsDeleteResponse = SetNonNullable< Required> > +export type AcsCredentialsDeleteOptions = never + export type AcsCredentialsGetParams = RouteRequestBody<'/acs/credentials/get'> export type AcsCredentialsGetResponse = SetNonNullable< Required> > +export type AcsCredentialsGetOptions = never + export type AcsCredentialsListParams = RouteRequestBody<'/acs/credentials/list'> export type AcsCredentialsListResponse = SetNonNullable< Required> > +export type AcsCredentialsListOptions = never + export type AcsCredentialsUnassignBody = RouteRequestBody<'/acs/credentials/unassign'> export type AcsCredentialsUnassignResponse = SetNonNullable< Required> > + +export type AcsCredentialsUnassignOptions = never diff --git a/src/lib/seam/connect/routes/acs-systems.ts b/src/lib/seam/connect/routes/acs-systems.ts index afbdf2f4..be5e714d 100644 --- a/src/lib/seam/connect/routes/acs-systems.ts +++ b/src/lib/seam/connect/routes/acs-systems.ts @@ -128,6 +128,7 @@ export class SeamHttpAcsSystems { method: 'post', data: body, }) + return data.acs_system } @@ -139,6 +140,7 @@ export class SeamHttpAcsSystems { method: 'post', data: body, }) + return data.acs_systems } } @@ -149,8 +151,12 @@ export type AcsSystemsGetResponse = SetNonNullable< Required> > +export type AcsSystemsGetOptions = never + export type AcsSystemsListParams = RouteRequestBody<'/acs/systems/list'> export type AcsSystemsListResponse = SetNonNullable< Required> > + +export type AcsSystemsListOptions = never diff --git a/src/lib/seam/connect/routes/acs-users.ts b/src/lib/seam/connect/routes/acs-users.ts index b18cc9f0..3f7626d6 100644 --- a/src/lib/seam/connect/routes/acs-users.ts +++ b/src/lib/seam/connect/routes/acs-users.ts @@ -136,6 +136,7 @@ export class SeamHttpAcsUsers { method: 'post', data: body, }) + return data.acs_user } @@ -155,6 +156,7 @@ export class SeamHttpAcsUsers { method: 'post', data: body, }) + return data.acs_user } @@ -166,6 +168,7 @@ export class SeamHttpAcsUsers { method: 'post', data: body, }) + return data.acs_users } @@ -211,30 +214,40 @@ export type AcsUsersAddToAccessGroupResponse = SetNonNullable< Required> > +export type AcsUsersAddToAccessGroupOptions = never + export type AcsUsersCreateBody = RouteRequestBody<'/acs/users/create'> export type AcsUsersCreateResponse = SetNonNullable< Required> > +export type AcsUsersCreateOptions = never + export type AcsUsersDeleteBody = RouteRequestBody<'/acs/users/delete'> export type AcsUsersDeleteResponse = SetNonNullable< Required> > +export type AcsUsersDeleteOptions = never + export type AcsUsersGetParams = RouteRequestBody<'/acs/users/get'> export type AcsUsersGetResponse = SetNonNullable< Required> > +export type AcsUsersGetOptions = never + export type AcsUsersListParams = RouteRequestBody<'/acs/users/list'> export type AcsUsersListResponse = SetNonNullable< Required> > +export type AcsUsersListOptions = never + export type AcsUsersRemoveFromAccessGroupBody = RouteRequestBody<'/acs/users/remove_from_access_group'> @@ -242,20 +255,28 @@ export type AcsUsersRemoveFromAccessGroupResponse = SetNonNullable< Required> > +export type AcsUsersRemoveFromAccessGroupOptions = never + export type AcsUsersSuspendBody = RouteRequestBody<'/acs/users/suspend'> export type AcsUsersSuspendResponse = SetNonNullable< Required> > +export type AcsUsersSuspendOptions = never + export type AcsUsersUnsuspendBody = RouteRequestBody<'/acs/users/unsuspend'> export type AcsUsersUnsuspendResponse = SetNonNullable< Required> > +export type AcsUsersUnsuspendOptions = never + export type AcsUsersUpdateBody = RouteRequestBody<'/acs/users/update'> export type AcsUsersUpdateResponse = SetNonNullable< Required> > + +export type AcsUsersUpdateOptions = never diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index 46176cd4..65ad4325 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -24,6 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + resolveActionAttempt, + type ResolveActionAttemptOptions, +} from 'lib/seam/connect/resolve-action-attempt.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -122,12 +126,26 @@ export class SeamHttpActionAttempts { async get( body?: ActionAttemptsGetParams, + { + waitForActionAttempt = false, + timeout = 5000, + pollingInterval = 500, + }: Partial & { + waitForActionAttempt?: boolean + } = {}, ): Promise { const { data } = await this.client.request({ url: '/action_attempts/get', method: 'post', data: body, }) + if (waitForActionAttempt) { + return await resolveActionAttempt( + data.action_attempt, + SeamHttpActionAttempts.fromClient(this.client), + { timeout, pollingInterval }, + ) + } return data.action_attempt } @@ -139,6 +157,7 @@ export class SeamHttpActionAttempts { method: 'post', data: body, }) + return data.action_attempts } } @@ -149,8 +168,14 @@ export type ActionAttemptsGetResponse = SetNonNullable< Required> > +export type ActionAttemptsGetOptions = Partial & { + waitForActionAttempt?: boolean +} + export type ActionAttemptsListParams = RouteRequestBody<'/action_attempts/list'> export type ActionAttemptsListResponse = SetNonNullable< Required> > + +export type ActionAttemptsListOptions = never diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index 05c88473..1d0f0117 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -126,6 +126,7 @@ export class SeamHttpClientSessions { method: 'post', data: body, }) + return data.client_session } @@ -145,6 +146,7 @@ export class SeamHttpClientSessions { method: 'post', data: body, }) + return data.client_session } @@ -157,6 +159,7 @@ export class SeamHttpClientSessions { method: 'post', data: body, }) + return data.client_session } @@ -169,6 +172,7 @@ export class SeamHttpClientSessions { method: 'post', data: body, }) + return data.client_session } @@ -180,6 +184,7 @@ export class SeamHttpClientSessions { method: 'post', data: body, }) + return data.client_sessions } @@ -199,6 +204,8 @@ export type ClientSessionsCreateResponse = SetNonNullable< Required> > +export type ClientSessionsCreateOptions = never + export type ClientSessionsDeleteBody = RouteRequestBody<'/client_sessions/delete'> @@ -206,12 +213,16 @@ export type ClientSessionsDeleteResponse = SetNonNullable< Required> > +export type ClientSessionsDeleteOptions = never + export type ClientSessionsGetParams = RouteRequestBody<'/client_sessions/get'> export type ClientSessionsGetResponse = SetNonNullable< Required> > +export type ClientSessionsGetOptions = never + export type ClientSessionsGetOrCreateBody = RouteRequestBody<'/client_sessions/get_or_create'> @@ -219,6 +230,8 @@ export type ClientSessionsGetOrCreateResponse = SetNonNullable< Required> > +export type ClientSessionsGetOrCreateOptions = never + export type ClientSessionsGrantAccessBody = RouteRequestBody<'/client_sessions/grant_access'> @@ -226,15 +239,21 @@ export type ClientSessionsGrantAccessResponse = SetNonNullable< Required> > +export type ClientSessionsGrantAccessOptions = never + export type ClientSessionsListParams = RouteRequestBody<'/client_sessions/list'> export type ClientSessionsListResponse = SetNonNullable< Required> > +export type ClientSessionsListOptions = never + export type ClientSessionsRevokeBody = RouteRequestBody<'/client_sessions/revoke'> export type ClientSessionsRevokeResponse = SetNonNullable< Required> > + +export type ClientSessionsRevokeOptions = never diff --git a/src/lib/seam/connect/routes/connect-webviews.ts b/src/lib/seam/connect/routes/connect-webviews.ts index 643bdb03..52a62506 100644 --- a/src/lib/seam/connect/routes/connect-webviews.ts +++ b/src/lib/seam/connect/routes/connect-webviews.ts @@ -132,6 +132,7 @@ export class SeamHttpConnectWebviews { method: 'post', data: body, }) + return data.connect_webview } @@ -151,6 +152,7 @@ export class SeamHttpConnectWebviews { method: 'post', data: body, }) + return data.connect_webview } @@ -162,6 +164,7 @@ export class SeamHttpConnectWebviews { method: 'post', data: body, }) + return data.connect_webviews } @@ -181,6 +184,8 @@ export type ConnectWebviewsCreateResponse = SetNonNullable< Required> > +export type ConnectWebviewsCreateOptions = never + export type ConnectWebviewsDeleteBody = RouteRequestBody<'/connect_webviews/delete'> @@ -188,12 +193,16 @@ export type ConnectWebviewsDeleteResponse = SetNonNullable< Required> > +export type ConnectWebviewsDeleteOptions = never + export type ConnectWebviewsGetParams = RouteRequestBody<'/connect_webviews/get'> export type ConnectWebviewsGetResponse = SetNonNullable< Required> > +export type ConnectWebviewsGetOptions = never + export type ConnectWebviewsListParams = RouteRequestBody<'/connect_webviews/list'> @@ -201,9 +210,13 @@ export type ConnectWebviewsListResponse = SetNonNullable< Required> > +export type ConnectWebviewsListOptions = never + export type ConnectWebviewsViewParams = RouteRequestParams<'/connect_webviews/view'> export type ConnectWebviewsViewResponse = SetNonNullable< Required> > + +export type ConnectWebviewsViewOptions = never diff --git a/src/lib/seam/connect/routes/connected-accounts.ts b/src/lib/seam/connect/routes/connected-accounts.ts index ab80d337..980ca370 100644 --- a/src/lib/seam/connect/routes/connected-accounts.ts +++ b/src/lib/seam/connect/routes/connected-accounts.ts @@ -140,6 +140,7 @@ export class SeamHttpConnectedAccounts { method: 'post', data: body, }) + return data.connected_account } @@ -151,6 +152,7 @@ export class SeamHttpConnectedAccounts { method: 'get', params, }) + return data.connected_accounts } } @@ -162,6 +164,8 @@ export type ConnectedAccountsDeleteResponse = SetNonNullable< Required> > +export type ConnectedAccountsDeleteOptions = never + export type ConnectedAccountsGetParams = RouteRequestBody<'/connected_accounts/get'> @@ -169,9 +173,13 @@ export type ConnectedAccountsGetResponse = SetNonNullable< Required> > +export type ConnectedAccountsGetOptions = never + export type ConnectedAccountsListParams = RouteRequestParams<'/connected_accounts/list'> export type ConnectedAccountsListResponse = SetNonNullable< Required> > + +export type ConnectedAccountsListOptions = never diff --git a/src/lib/seam/connect/routes/devices-unmanaged.ts b/src/lib/seam/connect/routes/devices-unmanaged.ts index d46f943c..df5d06c9 100644 --- a/src/lib/seam/connect/routes/devices-unmanaged.ts +++ b/src/lib/seam/connect/routes/devices-unmanaged.ts @@ -128,6 +128,7 @@ export class SeamHttpDevicesUnmanaged { method: 'post', data: body, }) + return data.device } @@ -139,6 +140,7 @@ export class SeamHttpDevicesUnmanaged { method: 'post', data: body, }) + return data.devices } @@ -158,6 +160,8 @@ export type DevicesUnmanagedGetResponse = SetNonNullable< Required> > +export type DevicesUnmanagedGetOptions = never + export type DevicesUnmanagedListParams = RouteRequestBody<'/devices/unmanaged/list'> @@ -165,9 +169,13 @@ export type DevicesUnmanagedListResponse = SetNonNullable< Required> > +export type DevicesUnmanagedListOptions = never + export type DevicesUnmanagedUpdateBody = RouteRequestBody<'/devices/unmanaged/update'> export type DevicesUnmanagedUpdateResponse = SetNonNullable< Required> > + +export type DevicesUnmanagedUpdateOptions = never diff --git a/src/lib/seam/connect/routes/devices.ts b/src/lib/seam/connect/routes/devices.ts index c28bdaca..6e5fd6fa 100644 --- a/src/lib/seam/connect/routes/devices.ts +++ b/src/lib/seam/connect/routes/devices.ts @@ -139,6 +139,7 @@ export class SeamHttpDevices { method: 'post', data: body, }) + return data.device } @@ -150,6 +151,7 @@ export class SeamHttpDevices { method: 'post', data: body, }) + return data.devices } @@ -162,6 +164,7 @@ export class SeamHttpDevices { method: 'post', data: body, }) + return data.device_providers } @@ -180,18 +183,24 @@ export type DevicesDeleteResponse = SetNonNullable< Required> > +export type DevicesDeleteOptions = never + export type DevicesGetParams = RouteRequestBody<'/devices/get'> export type DevicesGetResponse = SetNonNullable< Required> > +export type DevicesGetOptions = never + export type DevicesListParams = RouteRequestBody<'/devices/list'> export type DevicesListResponse = SetNonNullable< Required> > +export type DevicesListOptions = never + export type DevicesListDeviceProvidersParams = RouteRequestBody<'/devices/list_device_providers'> @@ -199,8 +208,12 @@ export type DevicesListDeviceProvidersResponse = SetNonNullable< Required> > +export type DevicesListDeviceProvidersOptions = never + export type DevicesUpdateBody = RouteRequestBody<'/devices/update'> export type DevicesUpdateResponse = SetNonNullable< Required> > + +export type DevicesUpdateOptions = never diff --git a/src/lib/seam/connect/routes/events.ts b/src/lib/seam/connect/routes/events.ts index ea62bb57..7a3192f2 100644 --- a/src/lib/seam/connect/routes/events.ts +++ b/src/lib/seam/connect/routes/events.ts @@ -126,6 +126,7 @@ export class SeamHttpEvents { method: 'post', data: body, }) + return data.event } @@ -135,6 +136,7 @@ export class SeamHttpEvents { method: 'post', data: body, }) + return data.events } } @@ -145,8 +147,12 @@ export type EventsGetResponse = SetNonNullable< Required> > +export type EventsGetOptions = never + export type EventsListParams = RouteRequestBody<'/events/list'> export type EventsListResponse = SetNonNullable< Required> > + +export type EventsListOptions = never diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index bf742e3b..f4aa59db 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -24,7 +24,12 @@ import { type SeamHttpOptionsWithPersonalAccessToken, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + resolveActionAttempt, + type ResolveActionAttemptOptions, +} from 'lib/seam/connect/resolve-action-attempt.js' +import { SeamHttpActionAttempts } from './action-attempts.js' import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpLocks { @@ -126,6 +131,7 @@ export class SeamHttpLocks { method: 'post', data: body, }) + return data.device } @@ -135,28 +141,57 @@ export class SeamHttpLocks { method: 'post', data: body, }) + return data.devices } async lockDoor( body?: LocksLockDoorBody, + { + waitForActionAttempt = false, + timeout = 5000, + pollingInterval = 500, + }: Partial & { + waitForActionAttempt?: boolean + } = {}, ): Promise { const { data } = await this.client.request({ url: '/locks/lock_door', method: 'post', data: body, }) + if (waitForActionAttempt) { + return await resolveActionAttempt( + data.action_attempt, + SeamHttpActionAttempts.fromClient(this.client), + { timeout, pollingInterval }, + ) + } return data.action_attempt } async unlockDoor( body?: LocksUnlockDoorBody, + { + waitForActionAttempt = false, + timeout = 5000, + pollingInterval = 500, + }: Partial & { + waitForActionAttempt?: boolean + } = {}, ): Promise { const { data } = await this.client.request({ url: '/locks/unlock_door', method: 'post', data: body, }) + if (waitForActionAttempt) { + return await resolveActionAttempt( + data.action_attempt, + SeamHttpActionAttempts.fromClient(this.client), + { timeout, pollingInterval }, + ) + } return data.action_attempt } } @@ -167,20 +202,32 @@ export type LocksGetResponse = SetNonNullable< Required> > +export type LocksGetOptions = never + export type LocksListParams = RouteRequestBody<'/locks/list'> export type LocksListResponse = SetNonNullable< Required> > +export type LocksListOptions = never + export type LocksLockDoorBody = RouteRequestBody<'/locks/lock_door'> export type LocksLockDoorResponse = SetNonNullable< Required> > +export type LocksLockDoorOptions = Partial & { + waitForActionAttempt?: boolean +} + export type LocksUnlockDoorBody = RouteRequestBody<'/locks/unlock_door'> export type LocksUnlockDoorResponse = SetNonNullable< Required> > + +export type LocksUnlockDoorOptions = Partial & { + waitForActionAttempt?: boolean +} 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 f452adc6..27b6956b 100644 --- a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +++ b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts @@ -148,6 +148,7 @@ export class SeamHttpNoiseSensorsNoiseThresholds { method: 'post', data: body, }) + return data.noise_threshold } @@ -160,6 +161,7 @@ export class SeamHttpNoiseSensorsNoiseThresholds { method: 'post', data: body, }) + return data.noise_thresholds } @@ -179,6 +181,8 @@ export type NoiseSensorsNoiseThresholdsCreateResponse = SetNonNullable< Required> > +export type NoiseSensorsNoiseThresholdsCreateOptions = never + export type NoiseSensorsNoiseThresholdsDeleteBody = RouteRequestBody<'/noise_sensors/noise_thresholds/delete'> @@ -186,6 +190,8 @@ export type NoiseSensorsNoiseThresholdsDeleteResponse = SetNonNullable< Required> > +export type NoiseSensorsNoiseThresholdsDeleteOptions = never + export type NoiseSensorsNoiseThresholdsGetParams = RouteRequestBody<'/noise_sensors/noise_thresholds/get'> @@ -193,6 +199,8 @@ export type NoiseSensorsNoiseThresholdsGetResponse = SetNonNullable< Required> > +export type NoiseSensorsNoiseThresholdsGetOptions = never + export type NoiseSensorsNoiseThresholdsListParams = RouteRequestBody<'/noise_sensors/noise_thresholds/list'> @@ -200,9 +208,13 @@ export type NoiseSensorsNoiseThresholdsListResponse = SetNonNullable< Required> > +export type NoiseSensorsNoiseThresholdsListOptions = never + export type NoiseSensorsNoiseThresholdsUpdateBody = RouteRequestBody<'/noise_sensors/noise_thresholds/update'> export type NoiseSensorsNoiseThresholdsUpdateResponse = SetNonNullable< Required> > + +export type NoiseSensorsNoiseThresholdsUpdateOptions = never 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 f1b47e22..05f5fd85 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -136,6 +136,7 @@ export class SeamHttpThermostatsClimateSettingSchedules { data: body, }, ) + return data.climate_setting_schedule } @@ -162,6 +163,7 @@ export class SeamHttpThermostatsClimateSettingSchedules { method: 'post', data: body, }) + return data.climate_setting_schedule } @@ -178,6 +180,7 @@ export class SeamHttpThermostatsClimateSettingSchedules { data: body, }, ) + return data.climate_setting_schedules } @@ -201,6 +204,8 @@ export type ThermostatsClimateSettingSchedulesCreateResponse = SetNonNullable< Required> > +export type ThermostatsClimateSettingSchedulesCreateOptions = never + export type ThermostatsClimateSettingSchedulesDeleteBody = RouteRequestBody<'/thermostats/climate_setting_schedules/delete'> @@ -208,6 +213,8 @@ export type ThermostatsClimateSettingSchedulesDeleteResponse = SetNonNullable< Required> > +export type ThermostatsClimateSettingSchedulesDeleteOptions = never + export type ThermostatsClimateSettingSchedulesGetParams = RouteRequestBody<'/thermostats/climate_setting_schedules/get'> @@ -215,6 +222,8 @@ export type ThermostatsClimateSettingSchedulesGetResponse = SetNonNullable< Required> > +export type ThermostatsClimateSettingSchedulesGetOptions = never + export type ThermostatsClimateSettingSchedulesListParams = RouteRequestBody<'/thermostats/climate_setting_schedules/list'> @@ -222,9 +231,13 @@ export type ThermostatsClimateSettingSchedulesListResponse = SetNonNullable< Required> > +export type ThermostatsClimateSettingSchedulesListOptions = never + export type ThermostatsClimateSettingSchedulesUpdateBody = RouteRequestBody<'/thermostats/climate_setting_schedules/update'> export type ThermostatsClimateSettingSchedulesUpdateResponse = SetNonNullable< Required> > + +export type ThermostatsClimateSettingSchedulesUpdateOptions = never diff --git a/src/lib/seam/connect/routes/thermostats.ts b/src/lib/seam/connect/routes/thermostats.ts index a27898ba..2eab9da0 100644 --- a/src/lib/seam/connect/routes/thermostats.ts +++ b/src/lib/seam/connect/routes/thermostats.ts @@ -141,6 +141,7 @@ export class SeamHttpThermostats { method: 'post', data: body, }) + return data.thermostat } @@ -168,6 +169,7 @@ export class SeamHttpThermostats { method: 'post', data: body, }) + return data.thermostats } @@ -202,36 +204,48 @@ export type ThermostatsCoolResponse = SetNonNullable< Required> > +export type ThermostatsCoolOptions = never + export type ThermostatsGetParams = RouteRequestBody<'/thermostats/get'> export type ThermostatsGetResponse = SetNonNullable< Required> > +export type ThermostatsGetOptions = never + export type ThermostatsHeatBody = RouteRequestBody<'/thermostats/heat'> export type ThermostatsHeatResponse = SetNonNullable< Required> > +export type ThermostatsHeatOptions = never + export type ThermostatsHeatCoolBody = RouteRequestBody<'/thermostats/heat_cool'> export type ThermostatsHeatCoolResponse = SetNonNullable< Required> > +export type ThermostatsHeatCoolOptions = never + export type ThermostatsListParams = RouteRequestBody<'/thermostats/list'> export type ThermostatsListResponse = SetNonNullable< Required> > +export type ThermostatsListOptions = never + export type ThermostatsOffBody = RouteRequestBody<'/thermostats/off'> export type ThermostatsOffResponse = SetNonNullable< Required> > +export type ThermostatsOffOptions = never + export type ThermostatsSetFanModeBody = RouteRequestBody<'/thermostats/set_fan_mode'> @@ -239,8 +253,12 @@ export type ThermostatsSetFanModeResponse = SetNonNullable< Required> > +export type ThermostatsSetFanModeOptions = never + export type ThermostatsUpdateBody = RouteRequestBody<'/thermostats/update'> export type ThermostatsUpdateResponse = SetNonNullable< Required> > + +export type ThermostatsUpdateOptions = never diff --git a/src/lib/seam/connect/routes/user-identities.ts b/src/lib/seam/connect/routes/user-identities.ts index 1d5adb19..96e55dec 100644 --- a/src/lib/seam/connect/routes/user-identities.ts +++ b/src/lib/seam/connect/routes/user-identities.ts @@ -136,6 +136,7 @@ export class SeamHttpUserIdentities { method: 'post', data: body, }) + return data.user_identity } @@ -147,6 +148,7 @@ export class SeamHttpUserIdentities { method: 'post', data: body, }) + return data.user_identity } @@ -171,6 +173,7 @@ export class SeamHttpUserIdentities { method: 'post', data: body, }) + return data.accessible_devices } @@ -183,6 +186,7 @@ export class SeamHttpUserIdentities { method: 'post', data: body, }) + return data.acs_users } @@ -212,6 +216,8 @@ export type UserIdentitiesAddAcsUserResponse = SetNonNullable< Required> > +export type UserIdentitiesAddAcsUserOptions = never + export type UserIdentitiesCreateBody = RouteRequestBody<'/user_identities/create'> @@ -219,12 +225,16 @@ export type UserIdentitiesCreateResponse = SetNonNullable< Required> > +export type UserIdentitiesCreateOptions = never + export type UserIdentitiesGetParams = RouteRequestBody<'/user_identities/get'> export type UserIdentitiesGetResponse = SetNonNullable< Required> > +export type UserIdentitiesGetOptions = never + export type UserIdentitiesGrantAccessToDeviceBody = RouteRequestBody<'/user_identities/grant_access_to_device'> @@ -232,6 +242,8 @@ export type UserIdentitiesGrantAccessToDeviceResponse = SetNonNullable< Required> > +export type UserIdentitiesGrantAccessToDeviceOptions = never + export type UserIdentitiesListAccessibleDevicesParams = RouteRequestBody<'/user_identities/list_accessible_devices'> @@ -239,6 +251,8 @@ export type UserIdentitiesListAccessibleDevicesResponse = SetNonNullable< Required> > +export type UserIdentitiesListAccessibleDevicesOptions = never + export type UserIdentitiesListAcsUsersParams = RouteRequestBody<'/user_identities/list_acs_users'> @@ -246,6 +260,8 @@ export type UserIdentitiesListAcsUsersResponse = SetNonNullable< Required> > +export type UserIdentitiesListAcsUsersOptions = never + export type UserIdentitiesRemoveAcsUserBody = RouteRequestBody<'/user_identities/remove_acs_user'> @@ -253,9 +269,13 @@ export type UserIdentitiesRemoveAcsUserResponse = SetNonNullable< Required> > +export type UserIdentitiesRemoveAcsUserOptions = never + export type UserIdentitiesRevokeAccessToDeviceBody = RouteRequestBody<'/user_identities/revoke_access_to_device'> export type UserIdentitiesRevokeAccessToDeviceResponse = SetNonNullable< Required> > + +export type UserIdentitiesRevokeAccessToDeviceOptions = never diff --git a/src/lib/seam/connect/routes/webhooks.ts b/src/lib/seam/connect/routes/webhooks.ts index 2050bddb..81a1304b 100644 --- a/src/lib/seam/connect/routes/webhooks.ts +++ b/src/lib/seam/connect/routes/webhooks.ts @@ -132,6 +132,7 @@ export class SeamHttpWebhooks { method: 'post', data: body, }) + return data.webhook } @@ -149,6 +150,7 @@ export class SeamHttpWebhooks { method: 'post', data: body, }) + return data.webhook } @@ -160,6 +162,7 @@ export class SeamHttpWebhooks { method: 'get', params, }) + return data.webhooks } } @@ -170,20 +173,28 @@ export type WebhooksCreateResponse = SetNonNullable< Required> > +export type WebhooksCreateOptions = never + export type WebhooksDeleteBody = RouteRequestBody<'/webhooks/delete'> export type WebhooksDeleteResponse = SetNonNullable< Required> > +export type WebhooksDeleteOptions = never + export type WebhooksGetParams = RouteRequestBody<'/webhooks/get'> export type WebhooksGetResponse = SetNonNullable< Required> > +export type WebhooksGetOptions = never + export type WebhooksListParams = RouteRequestParams<'/webhooks/list'> export type WebhooksListResponse = SetNonNullable< Required> > + +export type WebhooksListOptions = never diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index d5b75cb2..abbf5ee8 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -132,6 +132,7 @@ export class SeamHttpWorkspaces { method: 'get', params, }) + return data.workspace } @@ -143,6 +144,7 @@ export class SeamHttpWorkspaces { method: 'get', params, }) + return data.workspaces } @@ -161,15 +163,21 @@ export type WorkspacesGetResponse = SetNonNullable< Required> > +export type WorkspacesGetOptions = never + export type WorkspacesListParams = RouteRequestParams<'/workspaces/list'> export type WorkspacesListResponse = SetNonNullable< Required> > +export type WorkspacesListOptions = never + export type WorkspacesResetSandboxBody = RouteRequestBody<'/workspaces/reset_sandbox'> export type WorkspacesResetSandboxResponse = SetNonNullable< Required> > + +export type WorkspacesResetSandboxOptions = never diff --git a/test/seam/connect/resolve-action-attempt.test.ts b/test/seam/connect/wait-for-action-attempt.test.ts similarity index 58% rename from test/seam/connect/resolve-action-attempt.test.ts rename to test/seam/connect/wait-for-action-attempt.test.ts index 13b67345..bedf2914 100644 --- a/test/seam/connect/resolve-action-attempt.test.ts +++ b/test/seam/connect/wait-for-action-attempt.test.ts @@ -2,13 +2,12 @@ import test from 'ava' import { getTestServer } from 'fixtures/seam/connect/api.js' import { - resolveActionAttempt, SeamActionAttemptFailedError, SeamActionAttemptTimeoutError, SeamHttp, } from '@seamapi/http/connect' -test('resolveActionAttempt: waits for pending action attempt', async (t) => { +test('waitForActionAttempt: waits for pending action attempt', async (t) => { const { seed, endpoint, db } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { @@ -21,6 +20,11 @@ test('resolveActionAttempt: waits for pending action attempt', async (t) => { t.is(actionAttempt.status, 'pending') + db.updateActionAttempt({ + action_attempt_id: actionAttempt.action_attempt_id, + status: 'pending', + }) + setTimeout(() => { db.updateActionAttempt({ action_attempt_id: actionAttempt.action_attempt_id, @@ -28,11 +32,18 @@ test('resolveActionAttempt: waits for pending action attempt', async (t) => { }) }, 1000) - const { status } = await resolveActionAttempt(actionAttempt, seam) + const { status } = await seam.actionAttempts.get( + { + action_attempt_id: actionAttempt.action_attempt_id, + }, + { + waitForActionAttempt: true, + }, + ) t.is(status, 'success') }) -test('resolveActionAttempt: returns successful action attempt', async (t) => { +test('waitForActionAttempt: returns successful action attempt', async (t) => { const { seed, endpoint, db } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { @@ -59,16 +70,20 @@ test('resolveActionAttempt: returns successful action attempt', async (t) => { return } - const resolvedActionAttempt = await resolveActionAttempt( - successfulActionAttempt, - seam, + const resolvedActionAttempt = await seam.actionAttempts.get( + { + action_attempt_id: actionAttempt.action_attempt_id, + }, + { + waitForActionAttempt: true, + }, ) - t.is(resolvedActionAttempt, successfulActionAttempt) + t.deepEqual(resolvedActionAttempt, successfulActionAttempt) }) -test('resolveActionAttempt: times out while waiting for action attempt', async (t) => { - const { seed, endpoint } = await getTestServer(t) +test('waitForActionAttempt: times out while waiting for action attempt', async (t) => { + const { seed, endpoint, db } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint, @@ -80,18 +95,29 @@ test('resolveActionAttempt: times out while waiting for action attempt', async ( t.is(actionAttempt.status, 'pending') + db.updateActionAttempt({ + action_attempt_id: actionAttempt.action_attempt_id, + status: 'pending', + }) + const err = await t.throwsAsync( async () => - await resolveActionAttempt(actionAttempt, seam, { - timeout: 100, - }), + await seam.actionAttempts.get( + { + action_attempt_id: actionAttempt.action_attempt_id, + }, + { + waitForActionAttempt: true, + timeout: 100, + }, + ), { instanceOf: SeamActionAttemptTimeoutError }, ) - t.is(err?.actionAttempt, actionAttempt) + t.deepEqual(err?.actionAttempt, actionAttempt) }) -test('resolveActionAttempt: rejects when action attempt fails', async (t) => { +test('waitForActionAttempt: rejects when action attempt fails', async (t) => { const { seed, endpoint, db } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { @@ -102,7 +128,7 @@ test('resolveActionAttempt: rejects when action attempt fails', async (t) => { device_id: seed.august_device_1, }) - t.is(actionAttempt.status, 'pending') + t.deepEqual(actionAttempt.status, 'pending') db.updateActionAttempt({ action_attempt_id: actionAttempt.action_attempt_id, @@ -114,7 +140,15 @@ test('resolveActionAttempt: rejects when action attempt fails', async (t) => { }) const err = await t.throwsAsync( - async () => await resolveActionAttempt(actionAttempt, seam), + async () => + await seam.actionAttempts.get( + { + action_attempt_id: actionAttempt.action_attempt_id, + }, + { + waitForActionAttempt: true, + }, + ), { instanceOf: SeamActionAttemptFailedError, message: 'Failed' }, ) @@ -123,8 +157,8 @@ test('resolveActionAttempt: rejects when action attempt fails', async (t) => { t.is(err?.code, 'foo') }) -test('resolveActionAttempt: times out if waiting for polling interval', async (t) => { - const { seed, endpoint } = await getTestServer(t) +test('waitForActionAttempt: times out if waiting for polling interval', async (t) => { + const { seed, endpoint, db } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint, @@ -134,31 +168,27 @@ test('resolveActionAttempt: times out if waiting for polling interval', async (t device_id: seed.august_device_1, }) - const err = await t.throwsAsync( - async () => - await resolveActionAttempt(actionAttempt, seam, { - timeout: 500, - pollingInterval: 10_000, - }), - { instanceOf: SeamActionAttemptTimeoutError }, - ) - - t.is(err?.actionAttempt, actionAttempt) -}) - -test('resolveActionAttempt: wraps methods that return as action attempt', async (t) => { - const { seed, endpoint } = await getTestServer(t) + t.is(actionAttempt.status, 'pending') - const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { - endpoint, + db.updateActionAttempt({ + action_attempt_id: actionAttempt.action_attempt_id, + status: 'pending', }) - const { status } = await resolveActionAttempt( - seam.locks.unlockDoor({ - device_id: seed.august_device_1, - }), - seam, + const err = await t.throwsAsync( + async () => + await seam.actionAttempts.get( + { + action_attempt_id: actionAttempt.action_attempt_id, + }, + { + waitForActionAttempt: true, + timeout: 500, + pollingInterval: 10_000, + }, + ), + { instanceOf: SeamActionAttemptTimeoutError }, ) - t.is(status, 'success') + t.deepEqual(err?.actionAttempt, actionAttempt) })