From 8e821b7f2ba4f30732dd39e00476813165427053 Mon Sep 17 00:00:00 2001 From: itelo Date: Thu, 7 Mar 2024 14:15:09 -0300 Subject: [PATCH] feat: Add phones.simulate.createSandboxPhone * remove phones/simulate/create_sandbox_phone from ignoredEndpointPaths list * add /phones/simulate to routePaths and routePathSubresources * ci: Generate code --------- Co-authored-by: Seam Bot --- generate-routes.ts | 3 +- src/lib/seam/connect/routes/index.ts | 1 + .../seam/connect/routes/phones-simulate.ts | 177 ++++++++++++++++++ src/lib/seam/connect/routes/phones.ts | 5 + 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/lib/seam/connect/routes/phones-simulate.ts diff --git a/generate-routes.ts b/generate-routes.ts index 151b792f..a3085b71 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -32,6 +32,7 @@ const routePaths = [ '/noise_sensors', '/noise_sensors/noise_thresholds', '/phones', + '/phones/simulate', '/thermostats', '/thermostats/climate_setting_schedules', '/user_identities', @@ -53,6 +54,7 @@ const routePathSubresources: Partial< 'systems', 'users', ], + '/phones': ['simulate'], '/devices': ['unmanaged'], '/noise_sensors': ['noise_thresholds'], '/thermostats': ['climate_setting_schedules'], @@ -67,7 +69,6 @@ const ignoredEndpointPaths = [ '/health/get_service_health', '/health/service/[service_name]', '/noise_sensors/simulate/trigger_noise_threshold', - '/phones/simulate/create_sandbox_phone', '/workspaces/reset_sandbox', ] as const diff --git a/src/lib/seam/connect/routes/index.ts b/src/lib/seam/connect/routes/index.ts index 416f9192..b580e487 100644 --- a/src/lib/seam/connect/routes/index.ts +++ b/src/lib/seam/connect/routes/index.ts @@ -20,6 +20,7 @@ export * from './networks.js' export * from './noise-sensors.js' export * from './noise-sensors-noise-thresholds.js' export * from './phones.js' +export * from './phones-simulate.js' export * from './thermostats.js' export * from './thermostats-climate-setting-schedules.js' export * from './user-identities.js' diff --git a/src/lib/seam/connect/routes/phones-simulate.ts b/src/lib/seam/connect/routes/phones-simulate.ts new file mode 100644 index 00000000..c75450ad --- /dev/null +++ b/src/lib/seam/connect/routes/phones-simulate.ts @@ -0,0 +1,177 @@ +/* + * Automatically generated by generate-routes.ts. + * Do not edit this file or add other files to this directory. + */ + +import type { RouteRequestBody, RouteResponse } from '@seamapi/types/connect' +import type { SetNonNullable } from 'type-fest' + +import { + getAuthHeadersForClientSessionToken, + warnOnInsecureuserIdentifierKey, +} from 'lib/seam/connect/auth.js' +import { type Client, createClient } from 'lib/seam/connect/client.js' +import { + isSeamHttpOptionsWithApiKey, + isSeamHttpOptionsWithClient, + isSeamHttpOptionsWithClientSessionToken, + isSeamHttpOptionsWithConsoleSessionToken, + isSeamHttpOptionsWithPersonalAccessToken, + type SeamHttpFromPublishableKeyOptions, + SeamHttpInvalidOptionsError, + type SeamHttpOptions, + type SeamHttpOptionsWithApiKey, + type SeamHttpOptionsWithClient, + type SeamHttpOptionsWithClientSessionToken, + type SeamHttpOptionsWithConsoleSessionToken, + type SeamHttpOptionsWithPersonalAccessToken, + type SeamHttpRequestOptions, +} from 'lib/seam/connect/options.js' +import { + limitToSeamHttpRequestOptions, + parseOptions, +} from 'lib/seam/connect/parse-options.js' + +import { SeamHttpClientSessions } from './client-sessions.js' + +export class SeamHttpPhonesSimulate { + client: Client + readonly defaults: Required + + constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { + const options = parseOptions(apiKeyOrOptions) + this.client = 'client' in options ? options.client : createClient(options) + this.defaults = limitToSeamHttpRequestOptions(options) + } + + static fromClient( + client: SeamHttpOptionsWithClient['client'], + options: Omit = {}, + ): SeamHttpPhonesSimulate { + const constructorOptions = { ...options, client } + if (!isSeamHttpOptionsWithClient(constructorOptions)) { + throw new SeamHttpInvalidOptionsError('Missing client') + } + return new SeamHttpPhonesSimulate(constructorOptions) + } + + static fromApiKey( + apiKey: SeamHttpOptionsWithApiKey['apiKey'], + options: Omit = {}, + ): SeamHttpPhonesSimulate { + const constructorOptions = { ...options, apiKey } + if (!isSeamHttpOptionsWithApiKey(constructorOptions)) { + throw new SeamHttpInvalidOptionsError('Missing apiKey') + } + return new SeamHttpPhonesSimulate(constructorOptions) + } + + static fromClientSessionToken( + clientSessionToken: SeamHttpOptionsWithClientSessionToken['clientSessionToken'], + options: Omit< + SeamHttpOptionsWithClientSessionToken, + 'clientSessionToken' + > = {}, + ): SeamHttpPhonesSimulate { + const constructorOptions = { ...options, clientSessionToken } + if (!isSeamHttpOptionsWithClientSessionToken(constructorOptions)) { + throw new SeamHttpInvalidOptionsError('Missing clientSessionToken') + } + return new SeamHttpPhonesSimulate(constructorOptions) + } + + static async fromPublishableKey( + publishableKey: string, + userIdentifierKey: string, + options: SeamHttpFromPublishableKeyOptions = {}, + ): Promise { + warnOnInsecureuserIdentifierKey(userIdentifierKey) + const clientOptions = parseOptions({ ...options, publishableKey }) + if (isSeamHttpOptionsWithClient(clientOptions)) { + throw new SeamHttpInvalidOptionsError( + 'The client option cannot be used with SeamHttp.fromPublishableKey', + ) + } + const client = createClient(clientOptions) + const clientSessions = SeamHttpClientSessions.fromClient(client) + const { token } = await clientSessions.getOrCreate({ + user_identifier_key: userIdentifierKey, + }) + return SeamHttpPhonesSimulate.fromClientSessionToken(token, options) + } + + static fromConsoleSessionToken( + consoleSessionToken: SeamHttpOptionsWithConsoleSessionToken['consoleSessionToken'], + workspaceId: SeamHttpOptionsWithConsoleSessionToken['workspaceId'], + options: Omit< + SeamHttpOptionsWithConsoleSessionToken, + 'consoleSessionToken' | 'workspaceId' + > = {}, + ): SeamHttpPhonesSimulate { + const constructorOptions = { ...options, consoleSessionToken, workspaceId } + if (!isSeamHttpOptionsWithConsoleSessionToken(constructorOptions)) { + throw new SeamHttpInvalidOptionsError( + 'Missing consoleSessionToken or workspaceId', + ) + } + return new SeamHttpPhonesSimulate(constructorOptions) + } + + static fromPersonalAccessToken( + personalAccessToken: SeamHttpOptionsWithPersonalAccessToken['personalAccessToken'], + workspaceId: SeamHttpOptionsWithPersonalAccessToken['workspaceId'], + options: Omit< + SeamHttpOptionsWithPersonalAccessToken, + 'personalAccessToken' | 'workspaceId' + > = {}, + ): SeamHttpPhonesSimulate { + const constructorOptions = { ...options, personalAccessToken, workspaceId } + if (!isSeamHttpOptionsWithPersonalAccessToken(constructorOptions)) { + throw new SeamHttpInvalidOptionsError( + 'Missing personalAccessToken or workspaceId', + ) + } + return new SeamHttpPhonesSimulate(constructorOptions) + } + + async updateClientSessionToken( + clientSessionToken: SeamHttpOptionsWithClientSessionToken['clientSessionToken'], + ): Promise { + const { headers } = this.client.defaults + const authHeaders = getAuthHeadersForClientSessionToken({ + clientSessionToken, + }) + for (const key of Object.keys(authHeaders)) { + if (headers[key] == null) { + throw new Error( + 'Cannot update a clientSessionToken on a client created without a clientSessionToken', + ) + } + } + this.client.defaults.headers = { ...headers, ...authHeaders } + const clientSessions = SeamHttpClientSessions.fromClient(this.client) + await clientSessions.get() + } + + async createSandboxPhone( + body?: PhonesSimulateCreateSandboxPhoneBody, + ): Promise { + const { data } = + await this.client.request({ + url: '/phones/simulate/create_sandbox_phone', + method: 'post', + data: body, + }) + + return data.phone + } +} + +export type PhonesSimulateCreateSandboxPhoneBody = + RouteRequestBody<'/phones/simulate/create_sandbox_phone'> + +export type PhonesSimulateCreateSandboxPhoneResponse = SetNonNullable< + Required> +> + +export type PhonesSimulateCreateSandboxPhoneOptions = never diff --git a/src/lib/seam/connect/routes/phones.ts b/src/lib/seam/connect/routes/phones.ts index e0ce67df..526ec7ce 100644 --- a/src/lib/seam/connect/routes/phones.ts +++ b/src/lib/seam/connect/routes/phones.ts @@ -33,6 +33,7 @@ import { } from 'lib/seam/connect/parse-options.js' import { SeamHttpClientSessions } from './client-sessions.js' +import { SeamHttpPhonesSimulate } from './phones-simulate.js' export class SeamHttpPhones { client: Client @@ -153,6 +154,10 @@ export class SeamHttpPhones { await clientSessions.get() } + get simulate(): SeamHttpPhonesSimulate { + return SeamHttpPhonesSimulate.fromClient(this.client, this.defaults) + } + async deactivate(body?: PhonesDeactivateBody): Promise { await this.client.request({ url: '/phones/deactivate',