Skip to content

Commit

Permalink
Move waitForActionAttempt options into object
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Nov 16, 2023
1 parent f1809ba commit fa9141d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 41 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
14 changes: 10 additions & 4 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`}
Expand All @@ -353,7 +359,7 @@ const renderClassMethodOptions = ({
resource,
}: Pick<Endpoint, 'resource'>): string => {
if (resource === 'action_attempt') {
return `{ waitForActionAttempt = false, timeout = 5000, pollingInterval = 500 }: ${renderClassMethodOptionsTypeDef(
return `{ waitForActionAttempt = false }: ${renderClassMethodOptionsTypeDef(
{ resource },
)} = {},`
}
Expand All @@ -371,8 +377,8 @@ const renderClassMethodOptionsTypeDef = ({
}: Pick<Endpoint, 'resource'>): string => {
if (resource === 'action_attempt') {
return `
Partial<ResolveActionAttemptOptions> & {
waitForActionAttempt?: boolean
{
waitForActionAttempt?: boolean | Partial<ResolveActionAttemptOptions>
}
`
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/seam/connect/resolve-action-attempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T extends ActionAttempt>(
actionAttempt: T,
actionAttempts: SeamHttpActionAttempts,
{ timeout, pollingInterval }: ResolveActionAttemptOptions,
{ timeout = 5000, pollingInterval = 500 }: ResolveActionAttemptOptions,
): Promise<SuccessfulActionAttempt<T>> => {
let timeoutRef
const timeoutPromise = new Promise<SuccessfulActionAttempt<T>>(
Expand Down
14 changes: 6 additions & 8 deletions src/lib/seam/connect/routes/action-attempts.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions src/lib/seam/connect/routes/locks.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions test/seam/connect/wait-for-action-attempt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit fa9141d

Please sign in to comment.