seam 0.47.0 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +376 -5
  2. package/package.json +2 -1
package/README.md CHANGED
@@ -46,7 +46,59 @@ $ npm install seam
46
46
 
47
47
  [npm]: https://www.npmjs.com/
48
48
 
49
- ### Usage
49
+ ## Usage
50
+
51
+ First, create a webhook using the Seam API or Seam Console
52
+ and obtain a Seam webhook secret.
53
+
54
+ _This example is for [Express], see the [Svix docs for more examples in specific frameworks](https://docs.svix.com/receiving/verifying-payloads/how)._
55
+
56
+ ```js
57
+ import { SeamWebhook } from 'seam'
58
+ import express from 'express'
59
+ import bodyParser from 'body-parser'
60
+
61
+ import { storeEvent } from './store-event.js'
62
+
63
+ const app = express()
64
+
65
+ const webhook = new SeamWebhook(process.env.SEAM_WEBHOOK_SECRET)
66
+
67
+ app.post(
68
+ '/webhook',
69
+ bodyParser.raw({ type: 'application/json' }),
70
+ (req, res) => {
71
+ let data
72
+ try {
73
+ data = webhook.verify(payload, headers)
74
+ } catch {
75
+ return res.status(400).send()
76
+ }
77
+
78
+ storeEvent(data, (err) => {
79
+ if (err != null) {
80
+ return res.status(500).send()
81
+ }
82
+ res.status(204).send()
83
+ })
84
+ },
85
+ )
86
+ ```
87
+
88
+ [Express]: https://expressjs.com/
89
+
90
+ ### Examples
91
+
92
+ _These examples assume `SEAM_API_KEY` is set in your environment._
93
+
94
+ #### List devices
95
+
96
+ ```ts
97
+ import { Seam } from 'seam'
98
+
99
+ const seam = new Seam()
100
+ const devices = await seam.devices.list()
101
+ ```
50
102
 
51
103
  #### Unlock a door
52
104
 
@@ -58,13 +110,332 @@ const lock = await seam.locks.get({ name: 'Front Door' })
58
110
  await seam.locks.unlockDoor({ device_id: lock.device_id })
59
111
  ```
60
112
 
61
- #### Parse and validate a webhook
113
+ ### Authentication Methods
114
+
115
+ The SDK supports several authentication mechanisms.
116
+ Authentication may be configured by passing the corresponding
117
+ options directly to the `Seam` constructor,
118
+ or with the more ergonomic static factory methods.
119
+
120
+ > Publishable Key authentication is not supported by the constructor
121
+ > and must be configured using `Seam.fromPublishableKey`.
122
+
123
+ #### API Key
124
+
125
+ An API key is scoped to a single workspace and should only be used on the server.
126
+ Obtain one from the Seam Console.
62
127
 
63
128
  ```ts
64
- import { SeamWebhook } from 'seam'
129
+ // Set the `SEAM_API_KEY` environment variable
130
+ const seam = new Seam()
131
+
132
+ // Pass as the first argument to the constructor
133
+ const seam = new Seam('your-api-key')
134
+
135
+ // Pass as an option the constructor
136
+ const seam = new Seam({ apiKey: 'your-api-key' })
137
+
138
+ // Use the factory method
139
+ const seam = Seam.fromApiKey('your-api-key')
140
+ ```
141
+
142
+ #### Client Session Token
143
+
144
+ A Client Session Token is scoped to a client session and should only be used on the client.
145
+
146
+ ```ts
147
+ // Pass as an option the constructor
148
+ const seam = new Seam({ clientSessionToken: 'some-client-session-token' })
149
+
150
+ // Use the factory method
151
+ const seam = Seam.fromClientSessionToken('some-client-session-token')
152
+ ```
153
+
154
+ The client session token may be updated using
155
+
156
+ ```ts
157
+ const seam = Seam.fromClientSessionToken('some-client-session-token')
158
+
159
+ await seam.updateClientSessionToken('some-new-client-session-token')
160
+ ```
161
+
162
+ #### Publishable Key
163
+
164
+ A Publishable Key is used by the client to acquire Client Session Token for a workspace.
165
+ Obtain one from the Seam Console.
166
+
167
+ Use the async factory method to return a client authenticated with a client session token:
168
+
169
+ ```ts
170
+ const seam = await Seam.fromPublishableKey(
171
+ 'your-publishable-key',
172
+ 'some-user-identifier-key',
173
+ )
174
+ ```
175
+
176
+ This will get an existing client session matching the user identifier key,
177
+ or create a new empty client session.
178
+
179
+ #### Personal Access Token
180
+
181
+ A Personal Access Token is scoped to a Seam Console user.
182
+ Obtain one from the Seam Console.
183
+ A workspace id must be provided when using this method
184
+ and all requests will be scoped to that workspace.
185
+
186
+ ```ts
187
+ // Pass as an option the constructor
188
+
189
+ const seam = new Seam({
190
+ personalAccessToken: 'your-personal-access-token',
191
+ workspaceId: 'your-workspace-id',
192
+ })
193
+
194
+ // Use the factory method
195
+ const seam = Seam.fromPersonalAccessToken(
196
+ 'some-console-session-token',
197
+ 'your-workspace-id',
198
+ )
199
+ ```
200
+
201
+ #### Console Session Token
202
+
203
+ A Console Session Token is used by the Seam Console.
204
+ This authentication method is only used by internal Seam applications.
205
+ A workspace id must be provided when using this method
206
+ and all requests will be scoped to that workspace.
207
+
208
+ ```ts
209
+ // Pass as an option the constructor
210
+ const seam = new Seam({
211
+ consoleSessionToken: 'some-console-session-token',
212
+ workspaceId: 'your-workspace-id',
213
+ })
214
+
215
+ // Use the factory method
216
+ const seam = Seam.fromConsoleSessionToken(
217
+ 'some-console-session-token',
218
+ 'your-workspace-id',
219
+ )
220
+ ```
221
+
222
+ ### Action Attempts
223
+
224
+ Some asynchronous operations, e.g., unlocking a door, return an [action attempt].
225
+ Seam tracks the progress of requested operation and updates the action attempt.
226
+
227
+ To make working with action attempts more convenient for applications,
228
+ this library provides the `waitForActionAttempt` option.
229
+
230
+ Pass the option per-request,
231
+
232
+ ```ts
233
+ await seam.locks.unlockDoor(
234
+ { device_id },
235
+ {
236
+ waitForActionAttempt: true,
237
+ },
238
+ )
239
+ ```
240
+
241
+ or set the default option for the client:
242
+
243
+ ```ts
244
+ const seam = new Seam({
245
+ apiKey: 'your-api-key',
246
+ waitForActionAttempt: true,
247
+ })
248
+
249
+ await seam.locks.unlockDoor({ device_id })
250
+ ```
251
+
252
+ If you have already have an action attempt id
253
+ and want to wait for it to resolve, simply use
254
+
255
+ ```ts
256
+ await seam.actionAttempts.get(
257
+ { action_attempt_id },
258
+ {
259
+ waitForActionAttempt: true,
260
+ },
261
+ )
262
+ ```
263
+
264
+ Using the `waitForActionAttempt` option:
265
+
266
+ - Polls the action attempt up to the `timeout`
267
+ at the `pollingInterval` (both in milliseconds).
268
+ - Resolves with a fresh copy of the successful action attempt.
269
+ - Rejects with a `SeamActionAttemptFailedError` if the action attempt is unsuccessful.
270
+ - Rejects with a `SeamActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
271
+ - Both errors expose an `actionAttempt` property.
272
+
273
+ ```ts
274
+ import {
275
+ Seam,
276
+ isSeamActionAttemptFailedError,
277
+ isSeamActionAttemptTimeoutError,
278
+ } from 'seam'
279
+
280
+ const seam = new Seam('your-api-key')
281
+
282
+ const [lock] = await seam.locks.list()
283
+
284
+ if (lock == null) throw new Error('No locks in this workspace')
285
+
286
+ try {
287
+ await seam.locks.unlockDoor(
288
+ { device_id: lock.device_id },
289
+ {
290
+ waitForActionAttempt: {
291
+ pollingInterval: 1000,
292
+ timeout: 5000,
293
+ },
294
+ },
295
+ )
296
+ console.log('Door unlocked')
297
+ } catch (err: unknown) {
298
+ if (isSeamActionAttemptFailedError(err)) {
299
+ console.log('Could not unlock the door')
300
+ return
301
+ }
302
+
303
+ if (isSeamActionAttemptTimeoutError(err)) {
304
+ console.log('Door took too long to unlock')
305
+ return
306
+ }
307
+
308
+ throw err
309
+ }
310
+ ```
311
+
312
+ [action attempt]: https://docs.seam.co/latest/core-concepts/action-attempts
313
+
314
+ ### Interacting with Multiple Workspaces
315
+
316
+ Some Seam API endpoints interact with multiple workspaces.
317
+ The `SeamMultiWorkspace` client is not bound to a specific workspace
318
+ and may use those endpoints with an appropriate authentication method.
319
+
320
+ #### Personal Access Token
321
+
322
+ A Personal Access Token is scoped to a Seam Console user.
323
+ Obtain one from the Seam Console.
324
+
325
+ ```ts
326
+ // Pass as an option the constructor
327
+ const seam = new SeamMultiWorkspace({
328
+ personalAccessToken: 'your-personal-access-token',
329
+ })
330
+
331
+ // Use the factory method
332
+ const seam = SeamMultiWorkspace.fromPersonalAccessToken(
333
+ 'some-console-session-token',
334
+ )
335
+
336
+ // List workspaces authorized for this Personal Access Token
337
+ const workspaces = await seam.workspaces.list()
338
+ ```
339
+
340
+ #### Console Session Token
341
+
342
+ A Console Session Token is used by the Seam Console.
343
+ This authentication method is only used by internal Seam applications.
344
+
345
+ ```ts
346
+ // Pass as an option the constructor
347
+ const seam = new SeamMultiWorkspace({
348
+ consoleSessionToken: 'some-console-session-token',
349
+ })
350
+
351
+ // Use the factory method
352
+ const seam = SeamMultiWorkspace.fromConsoleSessionToken(
353
+ 'some-console-session-token',
354
+ )
355
+
356
+ // List workspaces authorized for this Seam Console user
357
+ const workspaces = await seam.workspaces.list()
358
+ ```
359
+
360
+ ### Advanced Usage
361
+
362
+ #### Additional Options
363
+
364
+ In addition the various authentication options,
365
+ the constructor takes some advanced options that affect behavior.
366
+
367
+ ```ts
368
+ const seam = new Seam({
369
+ apiKey: 'your-api-key',
370
+ endpoint: 'https://example.com',
371
+ axiosOptions: {},
372
+ axiosRetryOptions: {},
373
+ })
374
+ ```
375
+
376
+ When using the static factory methods,
377
+ these options may be passed in as the last argument.
378
+
379
+ ```ts
380
+ const seam = Seam.fromApiKey('some-api-key', {
381
+ endpoint: 'https://example.com',
382
+ axiosOptions: {},
383
+ axiosRetryOptions: {},
384
+ })
385
+ ```
386
+
387
+ #### Setting the endpoint
388
+
389
+ Some contexts may need to override the API endpoint,
390
+ e.g., testing or proxy setups.
391
+ This option corresponds to the Axios `baseURL` setting.
392
+
393
+ Either pass the `endpoint` option, or set the `SEAM_ENDPOINT` environment variable.
394
+
395
+ #### Configuring the Axios Client
396
+
397
+ The Axios client and retry behavior may be configured with custom initiation options
398
+ via [`axiosOptions`][axiosOptions] and [`axiosRetryOptions`][axiosRetryOptions].
399
+ Options are deep merged with the default options.
400
+
401
+ [axiosOptions]: https://axios-http.com/docs/config_defaults
402
+ [axiosRetryOptions]: https://github.com/softonic/axios-retry
403
+
404
+ #### Using the Axios Client
405
+
406
+ The Axios client is exposed and may be used or configured directly:
407
+
408
+ ```ts
409
+ import { Seam, DevicesListResponse } from 'seam'
410
+
411
+ const seam = new Seam()
412
+
413
+ seam.client.interceptors.response.use((response) => {
414
+ console.log(response)
415
+ return response
416
+ })
417
+
418
+ const devices = await seam.client.get<DevicesListResponse>('/devices/list')
419
+ ```
420
+
421
+ #### Overriding the Client
422
+
423
+ An Axios compatible client may be provided to create a `Seam` instance.
424
+ This API is used internally and is not directly supported.
425
+
426
+ #### Inspecting the Request
427
+
428
+ All client methods return an instance of `SeamRequest`.
429
+ Inspect the request before it is sent to the server by intentionally not awaiting the `SeamRequest`:
430
+
431
+ ```ts
432
+ const seam = new Seam('your-api-key')
433
+
434
+ const request = seam.devices.list()
435
+
436
+ console.log(`${request.method} ${request.url}`, JSON.stringify(request.body))
65
437
 
66
- const webhook = new SeamWebhook('webhook-secret')
67
- const data = webhook.verify(payload, headers)
438
+ const devices = await request.execute()
68
439
  ```
69
440
 
70
441
  ## Development and Testing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "seam",
3
- "version": "0.47.0",
3
+ "version": "0.48.0",
4
4
  "description": "JavaScript SDK for the Seam API written in TypeScript.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -52,6 +52,7 @@
52
52
  "lint": "eslint --ignore-path .gitignore .",
53
53
  "prelint": "prettier --check --ignore-path .gitignore .",
54
54
  "postversion": "git push --follow-tags",
55
+ "generate:readme": "tsx ./generate-readme.ts",
55
56
  "example": "tsx examples",
56
57
  "example:inspect": "tsx --inspect examples",
57
58
  "format": "eslint --ignore-path .gitignore --fix .",