From 187a36e828029bb511668c3e35f3409ff4621e77 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Wed, 20 Sep 2023 20:01:21 -0700 Subject: [PATCH] Add support for legacy behavior override --- src/lib/seam/connect/client-options.ts | 1 + src/lib/seam/connect/client.ts | 10 +++++++- src/lib/seam/connect/legacy/workspaces.ts | 31 +++++++++++++++++++++++ src/lib/seam/connect/routes/workspaces.ts | 6 ++--- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/lib/seam/connect/legacy/workspaces.ts diff --git a/src/lib/seam/connect/client-options.ts b/src/lib/seam/connect/client-options.ts index 1abec908..df3bbefa 100644 --- a/src/lib/seam/connect/client-options.ts +++ b/src/lib/seam/connect/client-options.ts @@ -7,6 +7,7 @@ export type SeamHttpOptions = interface SeamHttpCommonOptions { endpoint?: string axiosOptions?: AxiosRequestConfig + enableLegacyMethodBehaivor?: boolean } export interface SeamHttpOptionsWithApiKey extends SeamHttpCommonOptions { diff --git a/src/lib/seam/connect/client.ts b/src/lib/seam/connect/client.ts index 6cdb58b1..10e5105d 100644 --- a/src/lib/seam/connect/client.ts +++ b/src/lib/seam/connect/client.ts @@ -9,11 +9,14 @@ import { type SeamHttpOptionsWithApiKey, type SeamHttpOptionsWithClientSessionToken, } from './client-options.js' +import { LegacyWorkspaces } from './legacy/workspaces.js' import { Workspaces } from './routes/workspaces.js' export class SeamHttp { client: Axios + #legacy: boolean + constructor(apiKeyOrOptions: string | SeamHttpOptions) { const options = parseOptions( typeof apiKeyOrOptions === 'string' @@ -21,6 +24,8 @@ export class SeamHttp { : apiKeyOrOptions, ) + this.#legacy = options.enableLegacyMethodBehaivor + // TODO: axiosRetry? Allow options to configure this if so this.client = axios.create({ baseURL: options.endpoint, @@ -67,7 +72,9 @@ export class SeamHttp { // makeRequest get workspaces(): Workspaces { - return new Workspaces(this.client) + const workspaces = new Workspaces(this.client) + if (this.#legacy) return new LegacyWorkspaces(this.client) + return workspaces } } @@ -88,5 +95,6 @@ const parseOptions = (options: SeamHttpOptions): Required => { ...(apiKey != null ? { apiKey } : {}), endpoint, axiosOptions: options.axiosOptions ?? {}, + enableLegacyMethodBehaivor: false, } } diff --git a/src/lib/seam/connect/legacy/workspaces.ts b/src/lib/seam/connect/legacy/workspaces.ts new file mode 100644 index 00000000..d870a9c9 --- /dev/null +++ b/src/lib/seam/connect/legacy/workspaces.ts @@ -0,0 +1,31 @@ +// TODO: Example of non-generated overrides to methods to preserve legacy behavior +import type { Routes } from '@seamapi/types/connect' +import type { SetNonNullable } from 'type-fest' + +import { Workspaces } from 'lib/seam/connect/routes/workspaces.js' + +export class LegacyWorkspaces extends Workspaces { + override async get(params: WorkspacesGetParams = {}): Promise { + const { + data: { workspace }, + } = await this.client.get('/workspaces/get', { + params, + }) + return workspace + } +} + +export type WorkspacesGetParams = SetNonNullable< + Required +> + +export type WorkspacesGetResponse = SetNonNullable< + Required +> + +// UPSTREAM: Should come from @seamapi/types/connect +// import type { Workspace } from @seamapi/types +// export type { Workspace } from '@seamapi/types/connect' +export interface Workspace { + workspace_id: string +} diff --git a/src/lib/seam/connect/routes/workspaces.ts b/src/lib/seam/connect/routes/workspaces.ts index 00ff241a..b21ae010 100644 --- a/src/lib/seam/connect/routes/workspaces.ts +++ b/src/lib/seam/connect/routes/workspaces.ts @@ -4,16 +4,16 @@ import type { Axios } from 'axios' import type { SetNonNullable } from 'type-fest' export class Workspaces { - #client: Axios + client: Axios constructor(client: Axios) { - this.#client = client + this.client = client } async get(params: WorkspacesGetParams = {}): Promise { const { data: { workspace }, - } = await this.#client.get('/workspaces/get', { + } = await this.client.get('/workspaces/get', { params, }) return workspace