diff --git a/README.md b/README.md index 1d6445d8..154a985c 100644 --- a/README.md +++ b/README.md @@ -185,11 +185,15 @@ const [lock] = await seam.locks.list() if (lock == null) throw new Error('No locks in this workspace') try { - await seam.locks.unlockDoor({ device_id: lock.device_id, }, { - waitForActionAttempt: true - pollingInterval: 1000, - timeout: 5000, - }) + await seam.locks.unlockDoor( + { device_id: lock.device_id }, + { + waitForActionAttempt: { + pollingInterval: 1000, + timeout: 5000, + }, + }, + ) console.log('Door unlocked') } catch (err: unknown) { if (isSeamActionAttemptFailedError(err)) { diff --git a/generate-routes.ts b/generate-routes.ts index 5afe006a..ca0806bd 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -342,7 +342,13 @@ const renderClassMethod = ({ }) ${ resource === 'action_attempt' - ? `if (waitForActionAttempt) { return resolveActionAttempt(data.${resource}, SeamHttpActionAttempts.fromClient(this.client), { timeout, pollingInterval }) }` + ? `if (waitForActionAttempt != null && waitForActionAttempt !== false) { + return resolveActionAttempt( + data.${resource}, + SeamHttpActionAttempts.fromClient(this.client), + typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, + ) + }` : '' } ${resource === null ? '' : `return data.${resource}`} @@ -353,7 +359,7 @@ const renderClassMethodOptions = ({ resource, }: Pick): string => { if (resource === 'action_attempt') { - return `{ waitForActionAttempt = false, timeout = 5000, pollingInterval = 500 }: ${renderClassMethodOptionsTypeDef( + return `{ waitForActionAttempt = false }: ${renderClassMethodOptionsTypeDef( { resource }, )} = {},` } @@ -371,8 +377,8 @@ const renderClassMethodOptionsTypeDef = ({ }: Pick): string => { if (resource === 'action_attempt') { return ` - Partial & { - waitForActionAttempt?: boolean + { + waitForActionAttempt?: boolean | Partial } ` } diff --git a/src/lib/seam/connect/resolve-action-attempt.ts b/src/lib/seam/connect/resolve-action-attempt.ts index c88ec1f0..6f148af9 100644 --- a/src/lib/seam/connect/resolve-action-attempt.ts +++ b/src/lib/seam/connect/resolve-action-attempt.ts @@ -6,14 +6,14 @@ import type { import type { SeamHttpActionAttempts } from './routes/index.js' export interface ResolveActionAttemptOptions { - timeout: number - pollingInterval: number + timeout?: number + pollingInterval?: number } export const resolveActionAttempt = async ( actionAttempt: T, actionAttempts: SeamHttpActionAttempts, - { timeout, pollingInterval }: ResolveActionAttemptOptions, + { timeout = 5000, pollingInterval = 500 }: ResolveActionAttemptOptions, ): Promise> => { let timeoutRef const timeoutPromise = new Promise>( diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index 65ad4325..fe20dffa 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -128,10 +128,8 @@ export class SeamHttpActionAttempts { body?: ActionAttemptsGetParams, { waitForActionAttempt = false, - timeout = 5000, - pollingInterval = 500, - }: Partial & { - waitForActionAttempt?: boolean + }: { + waitForActionAttempt?: boolean | Partial } = {}, ): Promise { const { data } = await this.client.request({ @@ -139,11 +137,11 @@ export class SeamHttpActionAttempts { method: 'post', data: body, }) - if (waitForActionAttempt) { + if (waitForActionAttempt != null && waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, SeamHttpActionAttempts.fromClient(this.client), - { timeout, pollingInterval }, + typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } return data.action_attempt @@ -168,8 +166,8 @@ export type ActionAttemptsGetResponse = SetNonNullable< Required> > -export type ActionAttemptsGetOptions = Partial & { - waitForActionAttempt?: boolean +export interface ActionAttemptsGetOptions { + waitForActionAttempt?: boolean | Partial } export type ActionAttemptsListParams = RouteRequestBody<'/action_attempts/list'> diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index f4aa59db..05345480 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -149,10 +149,8 @@ export class SeamHttpLocks { body?: LocksLockDoorBody, { waitForActionAttempt = false, - timeout = 5000, - pollingInterval = 500, - }: Partial & { - waitForActionAttempt?: boolean + }: { + waitForActionAttempt?: boolean | Partial } = {}, ): Promise { const { data } = await this.client.request({ @@ -160,11 +158,11 @@ export class SeamHttpLocks { method: 'post', data: body, }) - if (waitForActionAttempt) { + if (waitForActionAttempt != null && waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, SeamHttpActionAttempts.fromClient(this.client), - { timeout, pollingInterval }, + typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } return data.action_attempt @@ -174,10 +172,8 @@ export class SeamHttpLocks { body?: LocksUnlockDoorBody, { waitForActionAttempt = false, - timeout = 5000, - pollingInterval = 500, - }: Partial & { - waitForActionAttempt?: boolean + }: { + waitForActionAttempt?: boolean | Partial } = {}, ): Promise { const { data } = await this.client.request({ @@ -185,11 +181,11 @@ export class SeamHttpLocks { method: 'post', data: body, }) - if (waitForActionAttempt) { + if (waitForActionAttempt != null && waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, SeamHttpActionAttempts.fromClient(this.client), - { timeout, pollingInterval }, + typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } return data.action_attempt @@ -218,8 +214,8 @@ export type LocksLockDoorResponse = SetNonNullable< Required> > -export type LocksLockDoorOptions = Partial & { - waitForActionAttempt?: boolean +export interface LocksLockDoorOptions { + waitForActionAttempt?: boolean | Partial } export type LocksUnlockDoorBody = RouteRequestBody<'/locks/unlock_door'> @@ -228,6 +224,6 @@ export type LocksUnlockDoorResponse = SetNonNullable< Required> > -export type LocksUnlockDoorOptions = Partial & { - waitForActionAttempt?: boolean +export interface LocksUnlockDoorOptions { + waitForActionAttempt?: boolean | Partial } diff --git a/test/seam/connect/wait-for-action-attempt.test.ts b/test/seam/connect/wait-for-action-attempt.test.ts index bba718b5..4593fa97 100644 --- a/test/seam/connect/wait-for-action-attempt.test.ts +++ b/test/seam/connect/wait-for-action-attempt.test.ts @@ -107,8 +107,9 @@ test('waitForActionAttempt: times out while waiting for action attempt', async ( action_attempt_id: actionAttempt.action_attempt_id, }, { - waitForActionAttempt: true, - timeout: 100, + waitForActionAttempt: { + timeout: 100, + }, }, ), { instanceOf: SeamActionAttemptTimeoutError }, @@ -182,9 +183,10 @@ test('waitForActionAttempt: times out if waiting for polling interval', async (t action_attempt_id: actionAttempt.action_attempt_id, }, { - waitForActionAttempt: true, - timeout: 500, - pollingInterval: 10_000, + waitForActionAttempt: { + timeout: 500, + pollingInterval: 10_000, + }, }, ), { instanceOf: SeamActionAttemptTimeoutError },