From 5b3e6ca6119f1f3fce1432fcf5b616717f05ffe2 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 15:22:39 +0900 Subject: [PATCH 1/6] Remove client from ClientOptions --- src/lib/seam/connect/client.ts | 3 --- src/lib/seam/connect/parse-options.ts | 4 ++-- src/lib/seam/connect/routes/access-codes-unmanaged.ts | 4 ++-- src/lib/seam/connect/routes/access-codes.ts | 4 ++-- src/lib/seam/connect/routes/acs-access-groups.ts | 4 ++-- src/lib/seam/connect/routes/acs-credentials.ts | 4 ++-- src/lib/seam/connect/routes/acs-systems.ts | 4 ++-- src/lib/seam/connect/routes/acs-users.ts | 4 ++-- src/lib/seam/connect/routes/acs.ts | 4 ++-- src/lib/seam/connect/routes/action-attempts.ts | 4 ++-- src/lib/seam/connect/routes/client-sessions.ts | 4 ++-- src/lib/seam/connect/routes/connect-webviews.ts | 4 ++-- src/lib/seam/connect/routes/connected-accounts.ts | 4 ++-- src/lib/seam/connect/routes/devices-unmanaged.ts | 4 ++-- src/lib/seam/connect/routes/devices.ts | 4 ++-- src/lib/seam/connect/routes/events.ts | 4 ++-- src/lib/seam/connect/routes/locks.ts | 4 ++-- .../seam/connect/routes/noise-sensors-noise-thresholds.ts | 4 ++-- src/lib/seam/connect/routes/noise-sensors.ts | 4 ++-- .../routes/thermostats-climate-setting-schedules.ts | 4 ++-- src/lib/seam/connect/routes/thermostats.ts | 4 ++-- src/lib/seam/connect/routes/user-identities.ts | 4 ++-- src/lib/seam/connect/routes/webhooks.ts | 4 ++-- src/lib/seam/connect/routes/workspaces.ts | 4 ++-- src/lib/seam/connect/seam-http-multi-workspace.ts | 4 ++-- src/lib/seam/connect/seam-http.ts | 7 +++++-- 26 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/lib/seam/connect/client.ts b/src/lib/seam/connect/client.ts index f03a9c36..618336b7 100644 --- a/src/lib/seam/connect/client.ts +++ b/src/lib/seam/connect/client.ts @@ -12,14 +12,11 @@ export type Client = AxiosInstance export interface ClientOptions { axiosOptions?: AxiosRequestConfig axiosRetryOptions?: AxiosRetryConfig - client?: Client } type AxiosRetryConfig = Parameters[1] export const createClient = (options: ClientOptions): AxiosInstance => { - if (options.client != null) return options.client - const client = axios.create({ paramsSerializer, ...options.axiosOptions, diff --git a/src/lib/seam/connect/parse-options.ts b/src/lib/seam/connect/parse-options.ts index 9322e15d..6e082827 100644 --- a/src/lib/seam/connect/parse-options.ts +++ b/src/lib/seam/connect/parse-options.ts @@ -1,7 +1,7 @@ import version from 'lib/version.js' import { getAuthHeaders } from './auth.js' -import type { ClientOptions } from './client.js' +import type { Client, ClientOptions } from './client.js' import { isSeamHttpMultiWorkspaceOptionsWithClient, isSeamHttpOptionsWithClient, @@ -23,7 +23,7 @@ export type Options = export const parseOptions = ( apiKeyOrOptions: string | Options, -): ClientOptions => { +): ClientOptions | { client: Client } => { const options = getNormalizedOptions(apiKeyOrOptions) if (isSeamHttpOptionsWithClient(options)) return options diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 29cf2717..90b062df 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -31,8 +31,8 @@ export class SeamHttpAccessCodesUnmanaged { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index c9600c29..84cd7b54 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -32,8 +32,8 @@ export class SeamHttpAccessCodes { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/acs-access-groups.ts b/src/lib/seam/connect/routes/acs-access-groups.ts index c45f8985..9685bcd1 100644 --- a/src/lib/seam/connect/routes/acs-access-groups.ts +++ b/src/lib/seam/connect/routes/acs-access-groups.ts @@ -31,8 +31,8 @@ export class SeamHttpAcsAccessGroups { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/acs-credentials.ts b/src/lib/seam/connect/routes/acs-credentials.ts index 27f952e7..eae8b58f 100644 --- a/src/lib/seam/connect/routes/acs-credentials.ts +++ b/src/lib/seam/connect/routes/acs-credentials.ts @@ -31,8 +31,8 @@ export class SeamHttpAcsCredentials { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/acs-systems.ts b/src/lib/seam/connect/routes/acs-systems.ts index be5e714d..5a4a2f27 100644 --- a/src/lib/seam/connect/routes/acs-systems.ts +++ b/src/lib/seam/connect/routes/acs-systems.ts @@ -31,8 +31,8 @@ export class SeamHttpAcsSystems { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/acs-users.ts b/src/lib/seam/connect/routes/acs-users.ts index 3f7626d6..83788b88 100644 --- a/src/lib/seam/connect/routes/acs-users.ts +++ b/src/lib/seam/connect/routes/acs-users.ts @@ -31,8 +31,8 @@ export class SeamHttpAcsUsers { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/acs.ts b/src/lib/seam/connect/routes/acs.ts index b5e13aa1..5c33391d 100644 --- a/src/lib/seam/connect/routes/acs.ts +++ b/src/lib/seam/connect/routes/acs.ts @@ -32,8 +32,8 @@ export class SeamHttpAcs { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index fe20dffa..d80e4544 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -35,8 +35,8 @@ export class SeamHttpActionAttempts { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index 1d0f0117..c8297e1a 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -29,8 +29,8 @@ export class SeamHttpClientSessions { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/connect-webviews.ts b/src/lib/seam/connect/routes/connect-webviews.ts index 52a62506..716e5ea0 100644 --- a/src/lib/seam/connect/routes/connect-webviews.ts +++ b/src/lib/seam/connect/routes/connect-webviews.ts @@ -35,8 +35,8 @@ export class SeamHttpConnectWebviews { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/connected-accounts.ts b/src/lib/seam/connect/routes/connected-accounts.ts index 980ca370..93a2dd04 100644 --- a/src/lib/seam/connect/routes/connected-accounts.ts +++ b/src/lib/seam/connect/routes/connected-accounts.ts @@ -35,8 +35,8 @@ export class SeamHttpConnectedAccounts { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/devices-unmanaged.ts b/src/lib/seam/connect/routes/devices-unmanaged.ts index df5d06c9..3ef3f2cc 100644 --- a/src/lib/seam/connect/routes/devices-unmanaged.ts +++ b/src/lib/seam/connect/routes/devices-unmanaged.ts @@ -31,8 +31,8 @@ export class SeamHttpDevicesUnmanaged { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/devices.ts b/src/lib/seam/connect/routes/devices.ts index 6e5fd6fa..55b99273 100644 --- a/src/lib/seam/connect/routes/devices.ts +++ b/src/lib/seam/connect/routes/devices.ts @@ -32,8 +32,8 @@ export class SeamHttpDevices { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/events.ts b/src/lib/seam/connect/routes/events.ts index 7a3192f2..08faa414 100644 --- a/src/lib/seam/connect/routes/events.ts +++ b/src/lib/seam/connect/routes/events.ts @@ -31,8 +31,8 @@ export class SeamHttpEvents { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index 05345480..89a410e9 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -36,8 +36,8 @@ export class SeamHttpLocks { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts index 27b6956b..311151f0 100644 --- a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +++ b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts @@ -31,8 +31,8 @@ export class SeamHttpNoiseSensorsNoiseThresholds { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/noise-sensors.ts b/src/lib/seam/connect/routes/noise-sensors.ts index 17cacedb..487af945 100644 --- a/src/lib/seam/connect/routes/noise-sensors.ts +++ b/src/lib/seam/connect/routes/noise-sensors.ts @@ -29,8 +29,8 @@ export class SeamHttpNoiseSensors { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts index 05f5fd85..b39002b8 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -31,8 +31,8 @@ export class SeamHttpThermostatsClimateSettingSchedules { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/thermostats.ts b/src/lib/seam/connect/routes/thermostats.ts index 2eab9da0..be378d53 100644 --- a/src/lib/seam/connect/routes/thermostats.ts +++ b/src/lib/seam/connect/routes/thermostats.ts @@ -32,8 +32,8 @@ export class SeamHttpThermostats { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/user-identities.ts b/src/lib/seam/connect/routes/user-identities.ts index 96e55dec..6a0fbdbe 100644 --- a/src/lib/seam/connect/routes/user-identities.ts +++ b/src/lib/seam/connect/routes/user-identities.ts @@ -31,8 +31,8 @@ export class SeamHttpUserIdentities { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/webhooks.ts b/src/lib/seam/connect/routes/webhooks.ts index 81a1304b..a2d3993b 100644 --- a/src/lib/seam/connect/routes/webhooks.ts +++ b/src/lib/seam/connect/routes/webhooks.ts @@ -35,8 +35,8 @@ export class SeamHttpWebhooks { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index abbf5ee8..c988af86 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -35,8 +35,8 @@ export class SeamHttpWorkspaces { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( diff --git a/src/lib/seam/connect/seam-http-multi-workspace.ts b/src/lib/seam/connect/seam-http-multi-workspace.ts index 6384e78d..54474788 100644 --- a/src/lib/seam/connect/seam-http-multi-workspace.ts +++ b/src/lib/seam/connect/seam-http-multi-workspace.ts @@ -16,8 +16,8 @@ export class SeamHttpMultiWorkspace { client: Client constructor(options: SeamHttpMultiWorkspaceOptions) { - const clientOptions = parseOptions(options) - this.client = createClient(clientOptions) + const opts = parseOptions(options) + this.client = 'client' in opts ? opts.client : createClient(opts) } static fromClient( diff --git a/src/lib/seam/connect/seam-http.ts b/src/lib/seam/connect/seam-http.ts index b4a4cf88..b6f9cd35 100644 --- a/src/lib/seam/connect/seam-http.ts +++ b/src/lib/seam/connect/seam-http.ts @@ -37,8 +37,8 @@ export class SeamHttp { client: Client constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const clientOptions = parseOptions(apiKeyOrOptions) - this.client = createClient(clientOptions) + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) } static fromClient( @@ -84,6 +84,9 @@ export class SeamHttp { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ From 6fadd1e0924229ac2e1f39b371bea4ac0bf91f79 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 16:38:54 +0900 Subject: [PATCH 2/6] feat: Allow setting waitForActionAttempt as client option --- README.md | 15 ++++- generate-routes.ts | 22 ++++--- src/lib/seam/connect/options.ts | 21 +++++-- src/lib/seam/connect/parse-options.ts | 19 ++++-- .../connect/routes/access-codes-unmanaged.ts | 10 +++- src/lib/seam/connect/routes/access-codes.ts | 12 +++- .../seam/connect/routes/acs-access-groups.ts | 10 +++- .../seam/connect/routes/acs-credentials.ts | 10 +++- src/lib/seam/connect/routes/acs-systems.ts | 10 +++- src/lib/seam/connect/routes/acs-users.ts | 10 +++- src/lib/seam/connect/routes/acs.ts | 18 ++++-- .../seam/connect/routes/action-attempts.ts | 37 +++++++----- .../seam/connect/routes/client-sessions.ts | 10 +++- .../seam/connect/routes/connect-webviews.ts | 10 +++- .../seam/connect/routes/connected-accounts.ts | 10 +++- .../seam/connect/routes/devices-unmanaged.ts | 10 +++- src/lib/seam/connect/routes/devices.ts | 12 +++- src/lib/seam/connect/routes/events.ts | 10 +++- src/lib/seam/connect/routes/locks.ts | 59 +++++++++++-------- .../routes/noise-sensors-noise-thresholds.ts | 10 +++- src/lib/seam/connect/routes/noise-sensors.ts | 15 ++++- .../thermostats-climate-setting-schedules.ts | 10 +++- src/lib/seam/connect/routes/thermostats.ts | 15 ++++- .../seam/connect/routes/user-identities.ts | 10 +++- src/lib/seam/connect/routes/webhooks.ts | 10 +++- src/lib/seam/connect/routes/workspaces.ts | 10 +++- .../seam/connect/seam-http-multi-workspace.ts | 9 ++- src/lib/seam/connect/seam-http.ts | 35 ++++++----- .../connect/wait-for-action-attempt.test.ts | 46 +++++++++++++++ 29 files changed, 379 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 154a985c..ff18058d 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,9 @@ 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: +this library provides the `waitForActionAttempt` option. + +Pass the option per-request, ```ts await seam.locks.unlockDoor( @@ -162,6 +164,17 @@ await seam.locks.unlockDoor( ) ``` +or set the default option for the client: + +```ts +const seam = new SeamHttp({ + apiKey: 'your-api-key', + waitForActionAttempt: true, +}) + +await seam.locks.unlockDoor({ device_id }) +``` + Using the `waitForActionAttempt` option: - Polls the action attempt up to the `timeout` diff --git a/generate-routes.ts b/generate-routes.ts index ca0806bd..58fef54a 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -254,11 +254,11 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' import { resolveActionAttempt, - type ResolveActionAttemptOptions, } from 'lib/seam/connect/resolve-action-attempt.js' ${ @@ -291,6 +291,7 @@ const renderClass = ( ` export class SeamHttp${pascalCase(namespace)} { client: Client + readonly defaults: Required ${constructors .replaceAll(': SeamHttp ', `: SeamHttp${pascalCase(namespace)} `) @@ -342,10 +343,11 @@ const renderClassMethod = ({ }) ${ resource === 'action_attempt' - ? `if (waitForActionAttempt != null && waitForActionAttempt !== false) { + ? `const waitForActionAttempt = options.waitForActionAttempt ?? this.defaults.waitForActionAttempt + if (waitForActionAttempt !== false) { return resolveActionAttempt( data.${resource}, - SeamHttpActionAttempts.fromClient(this.client), + SeamHttpActionAttempts.fromClient(this.client, { ...this.defaults, waitForActionAttempt: false }), typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) }` @@ -359,9 +361,9 @@ const renderClassMethodOptions = ({ resource, }: Pick): string => { if (resource === 'action_attempt') { - return `{ waitForActionAttempt = false }: ${renderClassMethodOptionsTypeDef( - { resource }, - )} = {},` + return `options: ${renderClassMethodOptionsTypeDef({ + resource, + })} = {},` } return '' } @@ -376,11 +378,7 @@ const renderClassMethodOptionsTypeDef = ({ resource, }: Pick): string => { if (resource === 'action_attempt') { - return ` - { - waitForActionAttempt?: boolean | Partial - } - ` + return "Pick" } return 'never' } @@ -394,7 +392,7 @@ const renderSubresourceMethod = ( )}${pascalCase(subresource)} { return SeamHttp${pascalCase(namespace)}${pascalCase( subresource, - )}.fromClient(this.client) + )}.fromClient(this.client, this.defaults) } ` diff --git a/src/lib/seam/connect/options.ts b/src/lib/seam/connect/options.ts index 4b8f8d7a..4204e242 100644 --- a/src/lib/seam/connect/options.ts +++ b/src/lib/seam/connect/options.ts @@ -1,4 +1,5 @@ import type { Client, ClientOptions } from './client.js' +import type { ResolveActionAttemptOptions } from './resolve-action-attempt.js' export type SeamHttpMultiWorkspaceOptions = | SeamHttpMultiWorkspaceOptionsWithClient @@ -13,16 +14,28 @@ export type SeamHttpOptions = | SeamHttpOptionsWithConsoleSessionToken | SeamHttpOptionsWithPersonalAccessToken -interface SeamHttpCommonOptions extends ClientOptions { +interface SeamHttpCommonOptions extends ClientOptions, SeamHttpRequestOptions { endpoint?: string } +export interface SeamHttpRequestOptions { + waitForActionAttempt?: boolean | ResolveActionAttemptOptions +} + +const isSeamHttpRequestOption = (key: string): boolean => { + const keys: Record = { + waitForActionAttempt: true, + } + return Object.keys(keys).includes(key) +} + export interface SeamHttpFromPublishableKeyOptions extends SeamHttpCommonOptions {} export interface SeamHttpOptionsFromEnv extends SeamHttpCommonOptions {} -export interface SeamHttpMultiWorkspaceOptionsWithClient { +export interface SeamHttpMultiWorkspaceOptionsWithClient + extends SeamHttpRequestOptions { client: Client } @@ -31,7 +44,7 @@ export const isSeamHttpMultiWorkspaceOptionsWithClient = ( ): options is SeamHttpMultiWorkspaceOptionsWithClient => isSeamHttpOptionsWithClient(options) -export interface SeamHttpOptionsWithClient { +export interface SeamHttpOptionsWithClient extends SeamHttpRequestOptions { client: Client } @@ -42,7 +55,7 @@ export const isSeamHttpOptionsWithClient = ( if (options.client == null) return false const keys = Object.keys(options).filter((k) => k !== 'client') - if (keys.length > 0) { + if (keys.filter((k) => !isSeamHttpRequestOption(k)).length > 0) { throw new SeamHttpInvalidOptionsError( `The client option cannot be used with any other option, but received: ${keys.join( ', ', diff --git a/src/lib/seam/connect/parse-options.ts b/src/lib/seam/connect/parse-options.ts index 6e082827..d8235d37 100644 --- a/src/lib/seam/connect/parse-options.ts +++ b/src/lib/seam/connect/parse-options.ts @@ -1,13 +1,13 @@ import version from 'lib/version.js' import { getAuthHeaders } from './auth.js' -import type { Client, ClientOptions } from './client.js' import { isSeamHttpMultiWorkspaceOptionsWithClient, isSeamHttpOptionsWithClient, isSeamHttpOptionsWithClientSessionToken, type SeamHttpMultiWorkspaceOptions, type SeamHttpOptions, + type SeamHttpRequestOptions, } from './options.js' const defaultEndpoint = 'https://connect.getseam.com' @@ -23,13 +23,14 @@ export type Options = export const parseOptions = ( apiKeyOrOptions: string | Options, -): ClientOptions | { client: Client } => { +): SeamHttpOptions & Required => { const options = getNormalizedOptions(apiKeyOrOptions) if (isSeamHttpOptionsWithClient(options)) return options if (isSeamHttpMultiWorkspaceOptionsWithClient(options)) return options return { + ...options, axiosOptions: { baseURL: options.endpoint ?? getEndpointFromEnv() ?? defaultEndpoint, withCredentials: isSeamHttpOptionsWithClientSessionToken(options), @@ -48,13 +49,22 @@ export const parseOptions = ( const getNormalizedOptions = ( apiKeyOrOptions: string | Options, -): SeamHttpOptions => { +): SeamHttpOptions & Required => { const options = typeof apiKeyOrOptions === 'string' ? { apiKey: apiKeyOrOptions } : apiKeyOrOptions - if (isSeamHttpOptionsWithClient(options)) return options + const requestOptions = { + waitForActionAttempt: options.waitForActionAttempt ?? false, + } + + if (isSeamHttpOptionsWithClient(options)) { + return { + ...options, + ...requestOptions, + } + } const apiKey = 'apiKey' in options ? options.apiKey : getApiKeyFromEnv(options) @@ -62,6 +72,7 @@ const getNormalizedOptions = ( return { ...options, ...(apiKey != null ? { apiKey } : {}), + ...requestOptions, } } diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 90b062df..476ff1f2 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAccessCodesUnmanaged { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpAccessCodesUnmanaged { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index 84cd7b54..35ae0939 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -30,10 +31,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAccessCodes { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -79,6 +84,9 @@ export class SeamHttpAccessCodes { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -122,7 +130,7 @@ export class SeamHttpAccessCodes { } get unmanaged(): SeamHttpAccessCodesUnmanaged { - return SeamHttpAccessCodesUnmanaged.fromClient(this.client) + return SeamHttpAccessCodesUnmanaged.fromClient(this.client, this.defaults) } async create( diff --git a/src/lib/seam/connect/routes/acs-access-groups.ts b/src/lib/seam/connect/routes/acs-access-groups.ts index 9685bcd1..d5765f1f 100644 --- a/src/lib/seam/connect/routes/acs-access-groups.ts +++ b/src/lib/seam/connect/routes/acs-access-groups.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAcsAccessGroups { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpAcsAccessGroups { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/acs-credentials.ts b/src/lib/seam/connect/routes/acs-credentials.ts index eae8b58f..e3474bd0 100644 --- a/src/lib/seam/connect/routes/acs-credentials.ts +++ b/src/lib/seam/connect/routes/acs-credentials.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAcsCredentials { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpAcsCredentials { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/acs-systems.ts b/src/lib/seam/connect/routes/acs-systems.ts index 5a4a2f27..8aaae789 100644 --- a/src/lib/seam/connect/routes/acs-systems.ts +++ b/src/lib/seam/connect/routes/acs-systems.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAcsSystems { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpAcsSystems { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/acs-users.ts b/src/lib/seam/connect/routes/acs-users.ts index 83788b88..3bfb4c0c 100644 --- a/src/lib/seam/connect/routes/acs-users.ts +++ b/src/lib/seam/connect/routes/acs-users.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAcsUsers { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpAcsUsers { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/acs.ts b/src/lib/seam/connect/routes/acs.ts index 5c33391d..f5e46c8d 100644 --- a/src/lib/seam/connect/routes/acs.ts +++ b/src/lib/seam/connect/routes/acs.ts @@ -19,6 +19,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -30,10 +31,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpAcs { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -79,6 +84,9 @@ export class SeamHttpAcs { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -122,18 +130,18 @@ export class SeamHttpAcs { } get accessGroups(): SeamHttpAcsAccessGroups { - return SeamHttpAcsAccessGroups.fromClient(this.client) + return SeamHttpAcsAccessGroups.fromClient(this.client, this.defaults) } get credentials(): SeamHttpAcsCredentials { - return SeamHttpAcsCredentials.fromClient(this.client) + return SeamHttpAcsCredentials.fromClient(this.client, this.defaults) } get systems(): SeamHttpAcsSystems { - return SeamHttpAcsSystems.fromClient(this.client) + return SeamHttpAcsSystems.fromClient(this.client, this.defaults) } get users(): SeamHttpAcsUsers { - return SeamHttpAcsUsers.fromClient(this.client) + return SeamHttpAcsUsers.fromClient(this.client, this.defaults) } } diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index d80e4544..a3c80fe4 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -22,21 +22,23 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' -import { - resolveActionAttempt, - type ResolveActionAttemptOptions, -} from 'lib/seam/connect/resolve-action-attempt.js' +import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpActionAttempts { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -82,6 +84,9 @@ export class SeamHttpActionAttempts { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -126,21 +131,22 @@ export class SeamHttpActionAttempts { async get( body?: ActionAttemptsGetParams, - { - waitForActionAttempt = false, - }: { - waitForActionAttempt?: boolean | Partial - } = {}, + options: Pick = {}, ): Promise { const { data } = await this.client.request({ url: '/action_attempts/get', method: 'post', data: body, }) - if (waitForActionAttempt != null && waitForActionAttempt !== false) { + const waitForActionAttempt = + options.waitForActionAttempt ?? this.defaults.waitForActionAttempt + if (waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client), + SeamHttpActionAttempts.fromClient(this.client, { + ...this.defaults, + waitForActionAttempt: false, + }), typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } @@ -166,9 +172,10 @@ export type ActionAttemptsGetResponse = SetNonNullable< Required> > -export interface ActionAttemptsGetOptions { - waitForActionAttempt?: boolean | Partial -} +export type ActionAttemptsGetOptions = Pick< + SeamHttpRequestOptions, + 'waitForActionAttempt' +> export type ActionAttemptsListParams = RouteRequestBody<'/action_attempts/list'> diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index c8297e1a..f15f7008 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -22,15 +22,20 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' export class SeamHttpClientSessions { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -76,6 +81,9 @@ export class SeamHttpClientSessions { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/connect-webviews.ts b/src/lib/seam/connect/routes/connect-webviews.ts index 716e5ea0..2cbc052b 100644 --- a/src/lib/seam/connect/routes/connect-webviews.ts +++ b/src/lib/seam/connect/routes/connect-webviews.ts @@ -26,6 +26,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -33,10 +34,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpConnectWebviews { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -82,6 +87,9 @@ export class SeamHttpConnectWebviews { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/connected-accounts.ts b/src/lib/seam/connect/routes/connected-accounts.ts index 93a2dd04..f07fed38 100644 --- a/src/lib/seam/connect/routes/connected-accounts.ts +++ b/src/lib/seam/connect/routes/connected-accounts.ts @@ -26,6 +26,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -33,10 +34,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpConnectedAccounts { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -82,6 +87,9 @@ export class SeamHttpConnectedAccounts { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/devices-unmanaged.ts b/src/lib/seam/connect/routes/devices-unmanaged.ts index 3ef3f2cc..6aadde23 100644 --- a/src/lib/seam/connect/routes/devices-unmanaged.ts +++ b/src/lib/seam/connect/routes/devices-unmanaged.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpDevicesUnmanaged { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpDevicesUnmanaged { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/devices.ts b/src/lib/seam/connect/routes/devices.ts index 55b99273..1c798ee6 100644 --- a/src/lib/seam/connect/routes/devices.ts +++ b/src/lib/seam/connect/routes/devices.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -30,10 +31,14 @@ import { SeamHttpDevicesUnmanaged } from './devices-unmanaged.js' export class SeamHttpDevices { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -79,6 +84,9 @@ export class SeamHttpDevices { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -122,7 +130,7 @@ export class SeamHttpDevices { } get unmanaged(): SeamHttpDevicesUnmanaged { - return SeamHttpDevicesUnmanaged.fromClient(this.client) + return SeamHttpDevicesUnmanaged.fromClient(this.client, this.defaults) } async delete(body?: DevicesDeleteBody): Promise { diff --git a/src/lib/seam/connect/routes/events.ts b/src/lib/seam/connect/routes/events.ts index 08faa414..5872ee0a 100644 --- a/src/lib/seam/connect/routes/events.ts +++ b/src/lib/seam/connect/routes/events.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpEvents { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpEvents { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index 89a410e9..cf1f1462 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -22,22 +22,24 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' -import { - resolveActionAttempt, - type ResolveActionAttemptOptions, -} from 'lib/seam/connect/resolve-action-attempt.js' +import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' import { SeamHttpActionAttempts } from './action-attempts.js' import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpLocks { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -83,6 +85,9 @@ export class SeamHttpLocks { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -147,21 +152,22 @@ export class SeamHttpLocks { async lockDoor( body?: LocksLockDoorBody, - { - waitForActionAttempt = false, - }: { - waitForActionAttempt?: boolean | Partial - } = {}, + options: Pick = {}, ): Promise { const { data } = await this.client.request({ url: '/locks/lock_door', method: 'post', data: body, }) - if (waitForActionAttempt != null && waitForActionAttempt !== false) { + const waitForActionAttempt = + options.waitForActionAttempt ?? this.defaults.waitForActionAttempt + if (waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client), + SeamHttpActionAttempts.fromClient(this.client, { + ...this.defaults, + waitForActionAttempt: false, + }), typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } @@ -170,21 +176,22 @@ export class SeamHttpLocks { async unlockDoor( body?: LocksUnlockDoorBody, - { - waitForActionAttempt = false, - }: { - waitForActionAttempt?: boolean | Partial - } = {}, + options: Pick = {}, ): Promise { const { data } = await this.client.request({ url: '/locks/unlock_door', method: 'post', data: body, }) - if (waitForActionAttempt != null && waitForActionAttempt !== false) { + const waitForActionAttempt = + options.waitForActionAttempt ?? this.defaults.waitForActionAttempt + if (waitForActionAttempt !== false) { return await resolveActionAttempt( data.action_attempt, - SeamHttpActionAttempts.fromClient(this.client), + SeamHttpActionAttempts.fromClient(this.client, { + ...this.defaults, + waitForActionAttempt: false, + }), typeof waitForActionAttempt === 'boolean' ? {} : waitForActionAttempt, ) } @@ -214,9 +221,10 @@ export type LocksLockDoorResponse = SetNonNullable< Required> > -export interface LocksLockDoorOptions { - waitForActionAttempt?: boolean | Partial -} +export type LocksLockDoorOptions = Pick< + SeamHttpRequestOptions, + 'waitForActionAttempt' +> export type LocksUnlockDoorBody = RouteRequestBody<'/locks/unlock_door'> @@ -224,6 +232,7 @@ export type LocksUnlockDoorResponse = SetNonNullable< Required> > -export interface LocksUnlockDoorOptions { - waitForActionAttempt?: boolean | Partial -} +export type LocksUnlockDoorOptions = Pick< + SeamHttpRequestOptions, + 'waitForActionAttempt' +> diff --git a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts index 311151f0..e38d9c74 100644 --- a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +++ b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpNoiseSensorsNoiseThresholds { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpNoiseSensorsNoiseThresholds { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/noise-sensors.ts b/src/lib/seam/connect/routes/noise-sensors.ts index 487af945..f8379f45 100644 --- a/src/lib/seam/connect/routes/noise-sensors.ts +++ b/src/lib/seam/connect/routes/noise-sensors.ts @@ -19,6 +19,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -27,10 +28,14 @@ import { SeamHttpNoiseSensorsNoiseThresholds } from './noise-sensors-noise-thres export class SeamHttpNoiseSensors { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -76,6 +81,9 @@ export class SeamHttpNoiseSensors { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -119,6 +127,9 @@ export class SeamHttpNoiseSensors { } get noiseThresholds(): SeamHttpNoiseSensorsNoiseThresholds { - return SeamHttpNoiseSensorsNoiseThresholds.fromClient(this.client) + return SeamHttpNoiseSensorsNoiseThresholds.fromClient( + this.client, + this.defaults, + ) } } diff --git a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts index b39002b8..2ac46a07 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpThermostatsClimateSettingSchedules { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpThermostatsClimateSettingSchedules { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/thermostats.ts b/src/lib/seam/connect/routes/thermostats.ts index be378d53..91f00a3c 100644 --- a/src/lib/seam/connect/routes/thermostats.ts +++ b/src/lib/seam/connect/routes/thermostats.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -30,10 +31,14 @@ import { SeamHttpThermostatsClimateSettingSchedules } from './thermostats-climat export class SeamHttpThermostats { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -79,6 +84,9 @@ export class SeamHttpThermostats { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ @@ -122,7 +130,10 @@ export class SeamHttpThermostats { } get climateSettingSchedules(): SeamHttpThermostatsClimateSettingSchedules { - return SeamHttpThermostatsClimateSettingSchedules.fromClient(this.client) + return SeamHttpThermostatsClimateSettingSchedules.fromClient( + this.client, + this.defaults, + ) } async cool(body?: ThermostatsCoolBody): Promise { diff --git a/src/lib/seam/connect/routes/user-identities.ts b/src/lib/seam/connect/routes/user-identities.ts index 6a0fbdbe..26863406 100644 --- a/src/lib/seam/connect/routes/user-identities.ts +++ b/src/lib/seam/connect/routes/user-identities.ts @@ -22,6 +22,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -29,10 +30,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpUserIdentities { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -78,6 +83,9 @@ export class SeamHttpUserIdentities { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/webhooks.ts b/src/lib/seam/connect/routes/webhooks.ts index a2d3993b..caf4dd9c 100644 --- a/src/lib/seam/connect/routes/webhooks.ts +++ b/src/lib/seam/connect/routes/webhooks.ts @@ -26,6 +26,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -33,10 +34,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpWebhooks { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -82,6 +87,9 @@ export class SeamHttpWebhooks { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index c988af86..a2febe09 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -26,6 +26,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' import { parseOptions } from 'lib/seam/connect/parse-options.js' @@ -33,10 +34,14 @@ import { SeamHttpClientSessions } from './client-sessions.js' export class SeamHttpWorkspaces { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -82,6 +87,9 @@ export class SeamHttpWorkspaces { ): Promise { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new Error('Cannot pass a client when using fromPublishableKey') + } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) const { token } = await clientSessions.getOrCreate({ diff --git a/src/lib/seam/connect/seam-http-multi-workspace.ts b/src/lib/seam/connect/seam-http-multi-workspace.ts index 54474788..69d51af0 100644 --- a/src/lib/seam/connect/seam-http-multi-workspace.ts +++ b/src/lib/seam/connect/seam-http-multi-workspace.ts @@ -8,16 +8,21 @@ import { type SeamHttpMultiWorkspaceOptionsWithClient, type SeamHttpMultiWorkspaceOptionsWithConsoleSessionToken, type SeamHttpMultiWorkspaceOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from './options.js' import { parseOptions } from './parse-options.js' import { SeamHttpWorkspaces } from './routes/index.js' export class SeamHttpMultiWorkspace { client: Client + readonly defaults: Required constructor(options: SeamHttpMultiWorkspaceOptions) { - const opts = parseOptions(options) + const { waitForActionAttempt, ...opts } = parseOptions(options) this.client = 'client' in opts ? opts.client : createClient(opts) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -72,6 +77,6 @@ export class SeamHttpMultiWorkspace { } get workspaces(): SeamHttpWorkspaces { - return SeamHttpWorkspaces.fromClient(this.client) + return SeamHttpWorkspaces.fromClient(this.client, this.defaults) } } diff --git a/src/lib/seam/connect/seam-http.ts b/src/lib/seam/connect/seam-http.ts index b6f9cd35..c717a4d8 100644 --- a/src/lib/seam/connect/seam-http.ts +++ b/src/lib/seam/connect/seam-http.ts @@ -14,6 +14,7 @@ import { type SeamHttpOptionsWithClientSessionToken, type SeamHttpOptionsWithConsoleSessionToken, type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, } from './options.js' import { parseOptions } from './parse-options.js' import { @@ -35,10 +36,14 @@ import { export class SeamHttp { client: Client + readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const options = parseOptions(apiKeyOrOptions) + const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) + this.defaults = { + waitForActionAttempt, + } } static fromClient( @@ -130,58 +135,58 @@ export class SeamHttp { } get accessCodes(): SeamHttpAccessCodes { - return SeamHttpAccessCodes.fromClient(this.client) + return SeamHttpAccessCodes.fromClient(this.client, this.defaults) } get acs(): SeamHttpAcs { - return SeamHttpAcs.fromClient(this.client) + return SeamHttpAcs.fromClient(this.client, this.defaults) } get actionAttempts(): SeamHttpActionAttempts { - return SeamHttpActionAttempts.fromClient(this.client) + return SeamHttpActionAttempts.fromClient(this.client, this.defaults) } get clientSessions(): SeamHttpClientSessions { - return SeamHttpClientSessions.fromClient(this.client) + return SeamHttpClientSessions.fromClient(this.client, this.defaults) } get connectedAccounts(): SeamHttpConnectedAccounts { - return SeamHttpConnectedAccounts.fromClient(this.client) + return SeamHttpConnectedAccounts.fromClient(this.client, this.defaults) } get connectWebviews(): SeamHttpConnectWebviews { - return SeamHttpConnectWebviews.fromClient(this.client) + return SeamHttpConnectWebviews.fromClient(this.client, this.defaults) } get devices(): SeamHttpDevices { - return SeamHttpDevices.fromClient(this.client) + return SeamHttpDevices.fromClient(this.client, this.defaults) } get events(): SeamHttpEvents { - return SeamHttpEvents.fromClient(this.client) + return SeamHttpEvents.fromClient(this.client, this.defaults) } get locks(): SeamHttpLocks { - return SeamHttpLocks.fromClient(this.client) + return SeamHttpLocks.fromClient(this.client, this.defaults) } get noiseSensors(): SeamHttpNoiseSensors { - return SeamHttpNoiseSensors.fromClient(this.client) + return SeamHttpNoiseSensors.fromClient(this.client, this.defaults) } get thermostats(): SeamHttpThermostats { - return SeamHttpThermostats.fromClient(this.client) + return SeamHttpThermostats.fromClient(this.client, this.defaults) } get userIdentities(): SeamHttpUserIdentities { - return SeamHttpUserIdentities.fromClient(this.client) + return SeamHttpUserIdentities.fromClient(this.client, this.defaults) } get webhooks(): SeamHttpWebhooks { - return SeamHttpWebhooks.fromClient(this.client) + return SeamHttpWebhooks.fromClient(this.client, this.defaults) } get workspaces(): SeamHttpWorkspaces { - return SeamHttpWorkspaces.fromClient(this.client) + return SeamHttpWorkspaces.fromClient(this.client, this.defaults) } } diff --git a/test/seam/connect/wait-for-action-attempt.test.ts b/test/seam/connect/wait-for-action-attempt.test.ts index 4593fa97..1d60bf9b 100644 --- a/test/seam/connect/wait-for-action-attempt.test.ts +++ b/test/seam/connect/wait-for-action-attempt.test.ts @@ -211,3 +211,49 @@ test('waitForActionAttempt: waits directly on returned action attempt', async (t t.is(actionAttempt.status, 'success') }) + +test('waitForActionAttempt: does not wait by default', async (t) => { + const { seed, endpoint } = await getTestServer(t) + + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint, + }) + + const actionAttempt = await seam.locks.unlockDoor({ + device_id: seed.august_device_1, + }) + + t.is(actionAttempt.status, 'pending') +}) + +test('waitForActionAttempt: can set class default', async (t) => { + const { seed, endpoint } = await getTestServer(t) + + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint, + waitForActionAttempt: true, + }) + + const actionAttempt = await seam.locks.unlockDoor({ + device_id: seed.august_device_1, + }) + + t.is(actionAttempt.status, 'success') +}) + +test('waitForActionAttempt: can set class default with object', async (t) => { + const { seed, endpoint } = await getTestServer(t) + + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint, + waitForActionAttempt: { + timeout: 5000, + }, + }) + + const actionAttempt = await seam.locks.unlockDoor({ + device_id: seed.august_device_1, + }) + + t.is(actionAttempt.status, 'success') +}) From ebc3dfbfaa4fe174745604b316bb629883957347 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 16:54:32 +0900 Subject: [PATCH 3/6] Improve ParsedOptions --- src/lib/seam/connect/parse-options.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/seam/connect/parse-options.ts b/src/lib/seam/connect/parse-options.ts index d8235d37..da7efc49 100644 --- a/src/lib/seam/connect/parse-options.ts +++ b/src/lib/seam/connect/parse-options.ts @@ -1,6 +1,7 @@ import version from 'lib/version.js' import { getAuthHeaders } from './auth.js' +import type { Client, ClientOptions } from './client.js' import { isSeamHttpMultiWorkspaceOptionsWithClient, isSeamHttpOptionsWithClient, @@ -21,9 +22,13 @@ export type Options = | SeamHttpMultiWorkspaceOptions | (SeamHttpOptions & { publishableKey?: string }) +type ParsedOptions = Required< + (ClientOptions | { client: Client }) & SeamHttpRequestOptions +> + export const parseOptions = ( apiKeyOrOptions: string | Options, -): SeamHttpOptions & Required => { +): ParsedOptions => { const options = getNormalizedOptions(apiKeyOrOptions) if (isSeamHttpOptionsWithClient(options)) return options From 7027df9a096ee23d0d6b6aeb9ebe038f5aff9734 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 16:56:20 +0900 Subject: [PATCH 4/6] Fix error --- src/lib/seam/connect/seam-http.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/seam/connect/seam-http.ts b/src/lib/seam/connect/seam-http.ts index c717a4d8..5bd1353a 100644 --- a/src/lib/seam/connect/seam-http.ts +++ b/src/lib/seam/connect/seam-http.ts @@ -90,7 +90,9 @@ export class SeamHttp { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) From c143d784552125cead9c6ed84278f1f0126eea49 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 17:05:49 +0900 Subject: [PATCH 5/6] Use limitToSeamHttpRequestOptions --- generate-routes.ts | 5 ++++- src/lib/seam/connect/options.ts | 8 +------ src/lib/seam/connect/parse-options.ts | 21 +++++++++++++++++++ .../connect/routes/access-codes-unmanaged.ts | 15 +++++++------ src/lib/seam/connect/routes/access-codes.ts | 15 +++++++------ .../seam/connect/routes/acs-access-groups.ts | 15 +++++++------ .../seam/connect/routes/acs-credentials.ts | 15 +++++++------ src/lib/seam/connect/routes/acs-systems.ts | 15 +++++++------ src/lib/seam/connect/routes/acs-users.ts | 15 +++++++------ src/lib/seam/connect/routes/acs.ts | 15 +++++++------ .../seam/connect/routes/action-attempts.ts | 15 +++++++------ .../seam/connect/routes/client-sessions.ts | 15 +++++++------ .../seam/connect/routes/connect-webviews.ts | 15 +++++++------ .../seam/connect/routes/connected-accounts.ts | 15 +++++++------ .../seam/connect/routes/devices-unmanaged.ts | 15 +++++++------ src/lib/seam/connect/routes/devices.ts | 15 +++++++------ src/lib/seam/connect/routes/events.ts | 15 +++++++------ src/lib/seam/connect/routes/locks.ts | 15 +++++++------ .../routes/noise-sensors-noise-thresholds.ts | 15 +++++++------ src/lib/seam/connect/routes/noise-sensors.ts | 15 +++++++------ .../thermostats-climate-setting-schedules.ts | 15 +++++++------ src/lib/seam/connect/routes/thermostats.ts | 15 +++++++------ .../seam/connect/routes/user-identities.ts | 15 +++++++------ src/lib/seam/connect/routes/webhooks.ts | 15 +++++++------ src/lib/seam/connect/routes/workspaces.ts | 15 +++++++------ .../seam/connect/seam-http-multi-workspace.ts | 8 +++---- src/lib/seam/connect/seam-http.ts | 8 +++---- 27 files changed, 230 insertions(+), 150 deletions(-) diff --git a/generate-routes.ts b/generate-routes.ts index 58fef54a..29c98048 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -256,7 +256,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions +} from 'lib/seam/connect/parse-options.js' import { resolveActionAttempt, } from 'lib/seam/connect/resolve-action-attempt.js' diff --git a/src/lib/seam/connect/options.ts b/src/lib/seam/connect/options.ts index 4204e242..35fc63e7 100644 --- a/src/lib/seam/connect/options.ts +++ b/src/lib/seam/connect/options.ts @@ -1,4 +1,5 @@ import type { Client, ClientOptions } from './client.js' +import { isSeamHttpRequestOption } from './parse-options.js' import type { ResolveActionAttemptOptions } from './resolve-action-attempt.js' export type SeamHttpMultiWorkspaceOptions = @@ -22,13 +23,6 @@ export interface SeamHttpRequestOptions { waitForActionAttempt?: boolean | ResolveActionAttemptOptions } -const isSeamHttpRequestOption = (key: string): boolean => { - const keys: Record = { - waitForActionAttempt: true, - } - return Object.keys(keys).includes(key) -} - export interface SeamHttpFromPublishableKeyOptions extends SeamHttpCommonOptions {} diff --git a/src/lib/seam/connect/parse-options.ts b/src/lib/seam/connect/parse-options.ts index da7efc49..a1104071 100644 --- a/src/lib/seam/connect/parse-options.ts +++ b/src/lib/seam/connect/parse-options.ts @@ -96,3 +96,24 @@ const getEndpointFromEnv = (): string | null | undefined => { globalThis.process?.env?.SEAM_API_URL ) } + +export const limitToSeamHttpRequestOptions = ( + options: Required, +): Required => { + return Object.keys(options) + .filter(isSeamHttpRequestOption) + .reduce( + (obj, key) => ({ + ...obj, + [key]: options[key as keyof SeamHttpRequestOptions], + }), + {}, + ) as Required +} + +export const isSeamHttpRequestOption = (key: string): boolean => { + const keys: Record = { + waitForActionAttempt: true, + } + return Object.keys(keys).includes(key) +} diff --git a/src/lib/seam/connect/routes/access-codes-unmanaged.ts b/src/lib/seam/connect/routes/access-codes-unmanaged.ts index 476ff1f2..7d3c8521 100644 --- a/src/lib/seam/connect/routes/access-codes-unmanaged.ts +++ b/src/lib/seam/connect/routes/access-codes-unmanaged.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpAccessCodesUnmanaged { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpAccessCodesUnmanaged { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/access-codes.ts b/src/lib/seam/connect/routes/access-codes.ts index 35ae0939..757d5d5b 100644 --- a/src/lib/seam/connect/routes/access-codes.ts +++ b/src/lib/seam/connect/routes/access-codes.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpAccessCodesUnmanaged } from './access-codes-unmanaged.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -34,11 +37,9 @@ export class SeamHttpAccessCodes { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -85,7 +86,9 @@ export class SeamHttpAccessCodes { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/acs-access-groups.ts b/src/lib/seam/connect/routes/acs-access-groups.ts index d5765f1f..8829480e 100644 --- a/src/lib/seam/connect/routes/acs-access-groups.ts +++ b/src/lib/seam/connect/routes/acs-access-groups.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpAcsAccessGroups { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpAcsAccessGroups { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/acs-credentials.ts b/src/lib/seam/connect/routes/acs-credentials.ts index e3474bd0..d49ab14f 100644 --- a/src/lib/seam/connect/routes/acs-credentials.ts +++ b/src/lib/seam/connect/routes/acs-credentials.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpAcsCredentials { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpAcsCredentials { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/acs-systems.ts b/src/lib/seam/connect/routes/acs-systems.ts index 8aaae789..9e8a83d3 100644 --- a/src/lib/seam/connect/routes/acs-systems.ts +++ b/src/lib/seam/connect/routes/acs-systems.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpAcsSystems { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpAcsSystems { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/acs-users.ts b/src/lib/seam/connect/routes/acs-users.ts index 3bfb4c0c..beeb2c0d 100644 --- a/src/lib/seam/connect/routes/acs-users.ts +++ b/src/lib/seam/connect/routes/acs-users.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpAcsUsers { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpAcsUsers { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/acs.ts b/src/lib/seam/connect/routes/acs.ts index f5e46c8d..cdfceb14 100644 --- a/src/lib/seam/connect/routes/acs.ts +++ b/src/lib/seam/connect/routes/acs.ts @@ -21,7 +21,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpAcsAccessGroups } from './acs-access-groups.js' import { SeamHttpAcsCredentials } from './acs-credentials.js' @@ -34,11 +37,9 @@ export class SeamHttpAcs { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -85,7 +86,9 @@ export class SeamHttpAcs { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/action-attempts.ts b/src/lib/seam/connect/routes/action-attempts.ts index a3c80fe4..585fb674 100644 --- a/src/lib/seam/connect/routes/action-attempts.ts +++ b/src/lib/seam/connect/routes/action-attempts.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -34,11 +37,9 @@ export class SeamHttpActionAttempts { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -85,7 +86,9 @@ export class SeamHttpActionAttempts { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/client-sessions.ts b/src/lib/seam/connect/routes/client-sessions.ts index f15f7008..824bbce1 100644 --- a/src/lib/seam/connect/routes/client-sessions.ts +++ b/src/lib/seam/connect/routes/client-sessions.ts @@ -24,18 +24,19 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' export class SeamHttpClientSessions { client: Client readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -82,7 +83,9 @@ export class SeamHttpClientSessions { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/connect-webviews.ts b/src/lib/seam/connect/routes/connect-webviews.ts index 2cbc052b..f98678a0 100644 --- a/src/lib/seam/connect/routes/connect-webviews.ts +++ b/src/lib/seam/connect/routes/connect-webviews.ts @@ -28,7 +28,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -37,11 +40,9 @@ export class SeamHttpConnectWebviews { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -88,7 +89,9 @@ export class SeamHttpConnectWebviews { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/connected-accounts.ts b/src/lib/seam/connect/routes/connected-accounts.ts index f07fed38..fff6c7ac 100644 --- a/src/lib/seam/connect/routes/connected-accounts.ts +++ b/src/lib/seam/connect/routes/connected-accounts.ts @@ -28,7 +28,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -37,11 +40,9 @@ export class SeamHttpConnectedAccounts { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -88,7 +89,9 @@ export class SeamHttpConnectedAccounts { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/devices-unmanaged.ts b/src/lib/seam/connect/routes/devices-unmanaged.ts index 6aadde23..d25bc53d 100644 --- a/src/lib/seam/connect/routes/devices-unmanaged.ts +++ b/src/lib/seam/connect/routes/devices-unmanaged.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpDevicesUnmanaged { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpDevicesUnmanaged { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/devices.ts b/src/lib/seam/connect/routes/devices.ts index 1c798ee6..b2eff086 100644 --- a/src/lib/seam/connect/routes/devices.ts +++ b/src/lib/seam/connect/routes/devices.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpDevicesUnmanaged } from './devices-unmanaged.js' @@ -34,11 +37,9 @@ export class SeamHttpDevices { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -85,7 +86,9 @@ export class SeamHttpDevices { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/events.ts b/src/lib/seam/connect/routes/events.ts index 5872ee0a..89684634 100644 --- a/src/lib/seam/connect/routes/events.ts +++ b/src/lib/seam/connect/routes/events.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpEvents { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpEvents { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/locks.ts b/src/lib/seam/connect/routes/locks.ts index cf1f1462..5dfd7906 100644 --- a/src/lib/seam/connect/routes/locks.ts +++ b/src/lib/seam/connect/routes/locks.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { resolveActionAttempt } from 'lib/seam/connect/resolve-action-attempt.js' import { SeamHttpActionAttempts } from './action-attempts.js' @@ -35,11 +38,9 @@ export class SeamHttpLocks { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -86,7 +87,9 @@ export class SeamHttpLocks { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts index e38d9c74..6077bfdd 100644 --- a/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +++ b/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpNoiseSensorsNoiseThresholds { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpNoiseSensorsNoiseThresholds { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/noise-sensors.ts b/src/lib/seam/connect/routes/noise-sensors.ts index f8379f45..ccbda2aa 100644 --- a/src/lib/seam/connect/routes/noise-sensors.ts +++ b/src/lib/seam/connect/routes/noise-sensors.ts @@ -21,7 +21,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpNoiseSensorsNoiseThresholds } from './noise-sensors-noise-thresholds.js' @@ -31,11 +34,9 @@ export class SeamHttpNoiseSensors { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -82,7 +83,9 @@ export class SeamHttpNoiseSensors { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts index 2ac46a07..a5a2b9db 100644 --- a/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +++ b/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpThermostatsClimateSettingSchedules { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpThermostatsClimateSettingSchedules { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/thermostats.ts b/src/lib/seam/connect/routes/thermostats.ts index 91f00a3c..d50abf33 100644 --- a/src/lib/seam/connect/routes/thermostats.ts +++ b/src/lib/seam/connect/routes/thermostats.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' import { SeamHttpThermostatsClimateSettingSchedules } from './thermostats-climate-setting-schedules.js' @@ -34,11 +37,9 @@ export class SeamHttpThermostats { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -85,7 +86,9 @@ export class SeamHttpThermostats { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/user-identities.ts b/src/lib/seam/connect/routes/user-identities.ts index 26863406..da272feb 100644 --- a/src/lib/seam/connect/routes/user-identities.ts +++ b/src/lib/seam/connect/routes/user-identities.ts @@ -24,7 +24,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -33,11 +36,9 @@ export class SeamHttpUserIdentities { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -84,7 +85,9 @@ export class SeamHttpUserIdentities { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/webhooks.ts b/src/lib/seam/connect/routes/webhooks.ts index caf4dd9c..c7850bc3 100644 --- a/src/lib/seam/connect/routes/webhooks.ts +++ b/src/lib/seam/connect/routes/webhooks.ts @@ -28,7 +28,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -37,11 +40,9 @@ export class SeamHttpWebhooks { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -88,7 +89,9 @@ export class SeamHttpWebhooks { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index a2febe09..6a48b806 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -28,7 +28,10 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from 'lib/seam/connect/options.js' -import { parseOptions } from 'lib/seam/connect/parse-options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' @@ -37,11 +40,9 @@ export class SeamHttpWorkspaces { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( @@ -88,7 +89,9 @@ export class SeamHttpWorkspaces { warnOnInsecureuserIdentifierKey(userIdentifierKey) const clientOptions = parseOptions({ ...options, publishableKey }) if (isSeamHttpOptionsWithClient(clientOptions)) { - throw new Error('Cannot pass a client when using fromPublishableKey') + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) } const client = createClient(clientOptions) const clientSessions = SeamHttpClientSessions.fromClient(client) diff --git a/src/lib/seam/connect/seam-http-multi-workspace.ts b/src/lib/seam/connect/seam-http-multi-workspace.ts index 69d51af0..c4e65abe 100644 --- a/src/lib/seam/connect/seam-http-multi-workspace.ts +++ b/src/lib/seam/connect/seam-http-multi-workspace.ts @@ -10,7 +10,7 @@ import { type SeamHttpMultiWorkspaceOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from './options.js' -import { parseOptions } from './parse-options.js' +import { limitToSeamHttpRequestOptions, parseOptions } from './parse-options.js' import { SeamHttpWorkspaces } from './routes/index.js' export class SeamHttpMultiWorkspace { @@ -18,11 +18,9 @@ export class SeamHttpMultiWorkspace { readonly defaults: Required constructor(options: SeamHttpMultiWorkspaceOptions) { - const { waitForActionAttempt, ...opts } = parseOptions(options) + const opts = parseOptions(options) this.client = 'client' in opts ? opts.client : createClient(opts) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(opts) } static fromClient( diff --git a/src/lib/seam/connect/seam-http.ts b/src/lib/seam/connect/seam-http.ts index 5bd1353a..7e0bcb37 100644 --- a/src/lib/seam/connect/seam-http.ts +++ b/src/lib/seam/connect/seam-http.ts @@ -16,7 +16,7 @@ import { type SeamHttpOptionsWithPersonalAccessToken, type SeamHttpRequestOptions, } from './options.js' -import { parseOptions } from './parse-options.js' +import { limitToSeamHttpRequestOptions, parseOptions } from './parse-options.js' import { SeamHttpAccessCodes, SeamHttpAcs, @@ -39,11 +39,9 @@ export class SeamHttp { readonly defaults: Required constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { - const { waitForActionAttempt, ...options } = parseOptions(apiKeyOrOptions) + const options = parseOptions(apiKeyOrOptions) this.client = 'client' in options ? options.client : createClient(options) - this.defaults = { - waitForActionAttempt, - } + this.defaults = limitToSeamHttpRequestOptions(options) } static fromClient( From 6ab3cf9311425565de3f2a6cf9207313ef0adc57 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Thu, 16 Nov 2023 17:26:15 +0900 Subject: [PATCH 6/6] Use type guard --- src/lib/seam/connect/parse-options.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/seam/connect/parse-options.ts b/src/lib/seam/connect/parse-options.ts index a1104071..fea20cd4 100644 --- a/src/lib/seam/connect/parse-options.ts +++ b/src/lib/seam/connect/parse-options.ts @@ -105,13 +105,15 @@ export const limitToSeamHttpRequestOptions = ( .reduce( (obj, key) => ({ ...obj, - [key]: options[key as keyof SeamHttpRequestOptions], + [key]: options[key], }), {}, ) as Required } -export const isSeamHttpRequestOption = (key: string): boolean => { +export const isSeamHttpRequestOption = ( + key: string, +): key is keyof SeamHttpRequestOptions => { const keys: Record = { waitForActionAttempt: true, }