Skip to content

Commit d450e3e

Browse files
authored
feat: Default waitForActionAttempt to true (#121)
* feat: Default waitForActionAttempt to true * Update tests * Update unlock.ts
1 parent 2572e8c commit d450e3e

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

README.md

+39-33
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ or create a new empty client session.
170170

171171
A Personal Access Token is scoped to a Seam Console user.
172172
Obtain one from the Seam Console.
173-
A workspace id must be provided when using this method
173+
A workspace ID must be provided when using this method
174174
and all requests will be scoped to that workspace.
175175

176176
```ts
@@ -192,7 +192,7 @@ const seam = SeamHttp.fromPersonalAccessToken(
192192

193193
A Console Session Token is used by the Seam Console.
194194
This authentication method is only used by internal Seam applications.
195-
A workspace id must be provided when using this method
195+
A workspace ID must be provided when using this method
196196
and all requests will be scoped to that workspace.
197197

198198
```ts
@@ -212,53 +212,62 @@ const seam = SeamHttp.fromConsoleSessionToken(
212212
### Action Attempts
213213

214214
Some asynchronous operations, e.g., unlocking a door, return an [action attempt].
215-
Seam tracks the progress of requested operation and updates the action attempt.
215+
Seam tracks the progress of the requested operation and updates the action attempt
216+
when it succeeds or fails.
216217

217218
To make working with action attempts more convenient for applications,
218-
this library provides the `waitForActionAttempt` option.
219+
this library provides the `waitForActionAttempt` option and enables it by default.
219220

220-
Pass the option per-request,
221+
When the `waitForActionAttempt` option is enabled, the SDK:
222+
223+
- Polls the action attempt up to the `timeout`
224+
at the `pollingInterval` (both in milliseconds).
225+
- Resolves with a fresh copy of the successful action attempt.
226+
- Rejects with a `SeamActionAttemptFailedError` if the action attempt is unsuccessful.
227+
- Rejects with a `SeamActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
228+
- Both errors expose an `actionAttempt` property.
229+
230+
If you already have an action attempt ID
231+
and want to wait for it to resolve, simply use
221232

222233
```ts
223-
await seam.locks.unlockDoor(
224-
{ device_id },
234+
await seam.actionAttempts.get({ action_attempt_id })
235+
```
236+
237+
Or, to get the current state of an action attempt by ID without waiting,
238+
239+
```ts
240+
await seam.actionAttempts.get(
241+
{ action_attempt_id },
225242
{
226-
waitForActionAttempt: true,
243+
waitForActionAttempt: false,
227244
},
228245
)
229246
```
230247

231-
or set the default option for the client:
248+
To disable this behavior, set the default option for the client,
232249

233250
```ts
234251
const seam = new SeamHttp({
235252
apiKey: 'your-api-key',
236-
waitForActionAttempt: true,
253+
waitForActionAttempt: false,
237254
})
238255

239256
await seam.locks.unlockDoor({ device_id })
240257
```
241258

242-
If you have already have an action attempt id
243-
and want to wait for it to resolve, simply use
259+
or the behavior may be configured per-request,
244260

245261
```ts
246-
await seam.actionAttempts.get(
247-
{ action_attempt_id },
262+
await seam.locks.unlockDoor(
263+
{ device_id },
248264
{
249-
waitForActionAttempt: true,
265+
waitForActionAttempt: false,
250266
},
251267
)
252268
```
253269

254-
Using the `waitForActionAttempt` option:
255-
256-
- Polls the action attempt up to the `timeout`
257-
at the `pollingInterval` (both in milliseconds).
258-
- Resolves with a fresh copy of the successful action attempt.
259-
- Rejects with a `SeamActionAttemptFailedError` if the action attempt is unsuccessful.
260-
- Rejects with a `SeamActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
261-
- Both errors expose an `actionAttempt` property.
270+
The `pollingInterval` and `timeout` may be configured for the client or per-request, for example
262271

263272
```ts
264273
import {
@@ -267,22 +276,19 @@ import {
267276
isSeamActionAttemptTimeoutError,
268277
} from '@seamapi/http/connect'
269278

270-
const seam = new SeamHttp('your-api-key')
279+
const seam = new SeamHttp('your-api-key', {
280+
waitForActionAttempt: {
281+
pollingInterval: 1000,
282+
timeout: 5000,
283+
},
284+
})
271285

272286
const [lock] = await seam.locks.list()
273287

274288
if (lock == null) throw new Error('No locks in this workspace')
275289

276290
try {
277-
await seam.locks.unlockDoor(
278-
{ device_id: lock.device_id },
279-
{
280-
waitForActionAttempt: {
281-
pollingInterval: 1000,
282-
timeout: 5000,
283-
},
284-
},
285-
)
291+
await seam.locks.unlockDoor({ device_id: lock.device_id })
286292
console.log('Door unlocked')
287293
} catch (err: unknown) {
288294
if (isSeamActionAttemptFailedError(err)) {

examples/unlock.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ export const builder: Builder = {
2525

2626
export const handler: Handler<Options> = async ({ deviceId, seam, logger }) => {
2727
try {
28-
const actionAttempt = await seam.locks.unlockDoor(
29-
{
30-
device_id: deviceId,
31-
},
32-
{ waitForActionAttempt: true },
33-
)
28+
const actionAttempt = await seam.locks.unlockDoor({ device_id: deviceId })
3429
logger.info({ actionAttempt }, 'unlocked')
3530
} catch (err: unknown) {
3631
if (isSeamActionAttemptFailedError<UnlockDoorActionAttempt>(err)) {

src/lib/seam/connect/parse-options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const getNormalizedOptions = (
6363
: apiKeyOrOptions
6464

6565
const requestOptions = {
66-
waitForActionAttempt: options.waitForActionAttempt ?? false,
66+
waitForActionAttempt: options.waitForActionAttempt ?? true,
6767
}
6868

6969
if (isSeamHttpOptionsWithClient(options)) {

test/seam/connect/wait-for-action-attempt.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test('waitForActionAttempt: waits for pending action attempt', async (t) => {
1212

1313
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
1414
endpoint,
15+
waitForActionAttempt: false,
1516
})
1617

1718
const actionAttempt = await seam.locks.unlockDoor({
@@ -48,6 +49,7 @@ test('waitForActionAttempt: returns successful action attempt', async (t) => {
4849

4950
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
5051
endpoint,
52+
waitForActionAttempt: false,
5153
})
5254

5355
const actionAttempt = await seam.locks.unlockDoor({
@@ -87,6 +89,7 @@ test('waitForActionAttempt: times out while waiting for action attempt', async (
8789

8890
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
8991
endpoint,
92+
waitForActionAttempt: false,
9093
})
9194

9295
const actionAttempt = await seam.locks.unlockDoor({
@@ -123,6 +126,7 @@ test('waitForActionAttempt: rejects when action attempt fails', async (t) => {
123126

124127
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
125128
endpoint,
129+
waitForActionAttempt: false,
126130
})
127131

128132
const actionAttempt = await seam.locks.unlockDoor({
@@ -163,6 +167,7 @@ test('waitForActionAttempt: times out if waiting for polling interval', async (t
163167

164168
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
165169
endpoint,
170+
waitForActionAttempt: false,
166171
})
167172

168173
const actionAttempt = await seam.locks.unlockDoor({
@@ -200,6 +205,7 @@ test('waitForActionAttempt: waits directly on returned action attempt', async (t
200205

201206
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
202207
endpoint,
208+
waitForActionAttempt: false,
203209
})
204210

205211
const actionAttempt = await seam.locks.unlockDoor(
@@ -212,7 +218,7 @@ test('waitForActionAttempt: waits directly on returned action attempt', async (t
212218
t.is(actionAttempt.status, 'success')
213219
})
214220

215-
test('waitForActionAttempt: does not wait by default', async (t) => {
221+
test('waitForActionAttempt: waits by default', async (t) => {
216222
const { seed, endpoint } = await getTestServer(t)
217223

218224
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
@@ -223,22 +229,22 @@ test('waitForActionAttempt: does not wait by default', async (t) => {
223229
device_id: seed.august_device_1,
224230
})
225231

226-
t.is(actionAttempt.status, 'pending')
232+
t.is(actionAttempt.status, 'success')
227233
})
228234

229235
test('waitForActionAttempt: can set class default', async (t) => {
230236
const { seed, endpoint } = await getTestServer(t)
231237

232238
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
233239
endpoint,
234-
waitForActionAttempt: true,
240+
waitForActionAttempt: false,
235241
})
236242

237243
const actionAttempt = await seam.locks.unlockDoor({
238244
device_id: seed.august_device_1,
239245
})
240246

241-
t.is(actionAttempt.status, 'success')
247+
t.is(actionAttempt.status, 'pending')
242248
})
243249

244250
test('waitForActionAttempt: can set class default with object', async (t) => {

0 commit comments

Comments
 (0)