Skip to content

Commit

Permalink
ci: Generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
seambot committed Mar 28, 2024
1 parent 010e941 commit c9606ce
Showing 1 changed file with 358 additions and 0 deletions.
358 changes: 358 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,364 @@ const devices = await seam.client.get<DevicesListResponse>('/devices/list')
An Axios compatible client may be provided to create a `Seam` instance.
This API is used internally and is not directly supported.

First, create a webhook using the Seam API or Seam Console
and obtain a Seam webhook secret.

_This example is for [Express], see the [Svix docs for more examples in specific frameworks](https://docs.svix.com/receiving/verifying-payloads/how)._

```js
import { SeamWebhook } from 'seam'
import express from 'express'
import bodyParser from 'body-parser'

import { storeEvent } from './store-event.js'

const app = express()

const webhook = new SeamWebhook(process.env.SEAM_WEBHOOK_SECRET)

app.post(
'/webhook',
bodyParser.raw({ type: 'application/json' }),
(req, res) => {
let data
try {
data = webhook.verify(payload, headers)
} catch {
return res.status(400).send()
}

storeEvent(data, (err) => {
if (err != null) {
return res.status(500).send()
}
res.status(204).send()
})
},
)
```

[Express]: https://expressjs.com/
```ts
import { Seam } from 'seam'

const seam = new Seam('your-api-key')
const devices = await seam.devices.list()
```

### Authentication Methods

The SDK supports several authentication mechanisms.
Authentication may be configured by passing the corresponding
options directly to the `Seam` constructor,
or with the more ergonomic static factory methods.

> Publishable Key authentication is not supported by the constructor
> and must be configured using `Seam.fromPublishableKey`.
#### API Key

An API key is scoped to a single workspace and should only be used on the server.
Obtain one from the Seam Console.

```ts
// Set the `SEAM_API_KEY` environment variable
const seam = new Seam()

// Pass as the first argument to the constructor
const seam = new Seam('your-api-key')

// Pass as an option the constructor
const seam = new Seam({ apiKey: 'your-api-key' })

// Use the factory method
const seam = Seam.fromApiKey('your-api-key')
```

#### Client Session Token

A Client Session Token is scoped to a client session and should only be used on the client.

```ts
// Pass as an option the constructor
const seam = new Seam({ clientSessionToken: 'some-client-session-token' })

// Use the factory method
const seam = Seam.fromClientSessionToken('some-client-session-token')
```

The client session token may be updated using

```
const seam = Seam.fromClientSessionToken('some-client-session-token')
await seam.updateClientSessionToken('some-new-client-session-token')
```

#### Publishable Key

A Publishable Key is used by the client to acquire Client Session Token for a workspace.
Obtain one from the Seam Console.

Use the async factory method to return a client authenticated with a client session token:

```ts
const seam = await Seam.fromPublishableKey(
'your-publishable-key',
'some-user-identifier-key',
)
```

This will get an existing client session matching the user identifier key,
or create a new empty client session.

#### Personal Access Token

A Personal Access Token is scoped to a Seam Console user.
Obtain one from the Seam Console.
A workspace id must be provided when using this method
and all requests will be scoped to that workspace.

```ts
// Pass as an option the constructor

const seam = new Seam({
personalAccessToken: 'your-personal-access-token',
workspaceId: 'your-workspace-id',
})

// Use the factory method
const seam = Seam.fromPersonalAccessToken(
'some-console-session-token',
'your-workspace-id',
)
```

#### Console Session Token

A Console Session Token is used by the Seam Console.
This authentication method is only used by internal Seam applications.
A workspace id must be provided when using this method
and all requests will be scoped to that workspace.

```ts
// Pass as an option the constructor
const seam = new Seam({
consoleSessionToken: 'some-console-session-token',
workspaceId: 'your-workspace-id',
})

// Use the factory method
const seam = Seam.fromConsoleSessionToken(
'some-console-session-token',
'your-workspace-id',
)
```

### Action Attempts

Some asynchronous operations, e.g., unlocking a door, return an [action attempt].
Seam tracks the progress of requested operation and updates the action attempt.

To make working with action attempts more convenient for applications,
this library provides the `waitForActionAttempt` option.

Pass the option per-request,

```ts
await seam.locks.unlockDoor(
{ device_id },
{
waitForActionAttempt: true,
},
)
```

or set the default option for the client:

```ts
const seam = new Seam({
apiKey: 'your-api-key',
waitForActionAttempt: true,
})

await seam.locks.unlockDoor({ device_id })
```

If you have already have an action attempt id
and want to wait for it to resolve, simply use

```ts
await seam.actionAttempts.get(
{ action_attempt_id },
{
waitForActionAttempt: true,
},
)
```

Using the `waitForActionAttempt` option:

- Polls the action attempt up to the `timeout`
at the `pollingInterval` (both in milliseconds).
- Resolves with a fresh copy of the successful action attempt.
- Rejects with a `SeamActionAttemptFailedError` if the action attempt is unsuccessful.
- Rejects with a `SeamActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
- Both errors expose an `actionAttempt` property.

```ts
import {
Seam,
isSeamActionAttemptFailedError,
isSeamActionAttemptTimeoutError,
} from 'seam'

const seam = new Seam('your-api-key')

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: {
pollingInterval: 1000,
timeout: 5000,
},
},
)
console.log('Door unlocked')
} catch (err: unknown) {
if (isSeamActionAttemptFailedError(err)) {
console.log('Could not unlock the door')
return
}

if (isSeamActionAttemptTimeoutError(err)) {
console.log('Door took too long to unlock')
return
}

throw err
}
```

[action attempt]: https://docs.seam.co/latest/core-concepts/action-attempts

### Interacting with Multiple Workspaces

Some Seam API endpoints interact with multiple workspaces.
The `SeamMultiWorkspace` client is not bound to a specific workspace
and may use those endpoints with an appropriate authentication method.

#### Personal Access Token

A Personal Access Token is scoped to a Seam Console user.
Obtain one from the Seam Console.

```ts
// Pass as an option the constructor
const seam = new SeamMultiWorkspace({
personalAccessToken: 'your-personal-access-token',
})

// Use the factory method
const seam = SeamMultiWorkspace.fromPersonalAccessToken(
'some-console-session-token',
)

// List workspaces authorized for this Personal Access Token
const workspaces = await seam.workspaces.list()
```

#### Console Session Token

A Console Session Token is used by the Seam Console.
This authentication method is only used by internal Seam applications.

```ts
// Pass as an option the constructor
const seam = new SeamMultiWorkspace({
consoleSessionToken: 'some-console-session-token',
})

// Use the factory method
const seam = SeamMultiWorkspace.fromConsoleSessionToken(
'some-console-session-token',
)

// List workspaces authorized for this Seam Console user
const workspaces = await seam.workspaces.list()
```

### Advanced Usage

#### Additional Options

In addition the various authentication options,
the constructor takes some advanced options that affect behavior.

```ts
const seam = new Seam({
apiKey: 'your-api-key',
endpoint: 'https://example.com',
axiosOptions: {},
axiosRetryOptions: {},
})
```

When using the static factory methods,
these options may be passed in as the last argument.

```ts
const seam = Seam.fromApiKey('some-api-key', {
endpoint: 'https://example.com',
axiosOptions: {},
axiosRetryOptions: {},
})
```

#### Setting the endpoint

Some contexts may need to override the API endpoint,
e.g., testing or proxy setups.
This option corresponds to the Axios `baseURL` setting.

Either pass the `endpoint` option, or set the `SEAM_ENDPOINT` environment variable.

#### Configuring the Axios Client

The Axios client and retry behavior may be configured with custom initiation options
via [`axiosOptions`][axiosOptions] and [`axiosRetryOptions`][axiosRetryOptions].
Options are deep merged with the default options.

[axiosOptions]: https://axios-http.com/docs/config_defaults
[axiosRetryOptions]: https://github.com/softonic/axios-retry

#### Using the Axios Client

The Axios client is exposed and may be used or configured directly:

```ts
import { Seam, DevicesListResponse } from 'seam'

const seam = new Seam()

seam.client.interceptors.response.use((response) => {
console.log(response)
return response
})

const devices = await seam.client.get<DevicesListResponse>('/devices/list')
```

#### Overriding the Client

An Axios compatible client may be provided to create a `Seam` instance.
This API is used internally and is not directly supported.

## Development and Testing

### Quickstart
Expand Down

0 comments on commit c9606ce

Please sign in to comment.