Skip to content

Commit

Permalink
Add support for legacy behavior override
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 21, 2023
1 parent 95eefea commit 187a36e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib/seam/connect/client-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type SeamHttpOptions =
interface SeamHttpCommonOptions {
endpoint?: string
axiosOptions?: AxiosRequestConfig
enableLegacyMethodBehaivor?: boolean
}

export interface SeamHttpOptionsWithApiKey extends SeamHttpCommonOptions {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/seam/connect/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ 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'
? { apiKey: apiKeyOrOptions }
: apiKeyOrOptions,
)

this.#legacy = options.enableLegacyMethodBehaivor

// TODO: axiosRetry? Allow options to configure this if so
this.client = axios.create({
baseURL: options.endpoint,
Expand Down Expand Up @@ -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
}
}

Expand All @@ -88,5 +95,6 @@ const parseOptions = (options: SeamHttpOptions): Required<SeamHttpOptions> => {
...(apiKey != null ? { apiKey } : {}),
endpoint,
axiosOptions: options.axiosOptions ?? {},
enableLegacyMethodBehaivor: false,
}
}
31 changes: 31 additions & 0 deletions src/lib/seam/connect/legacy/workspaces.ts
Original file line number Diff line number Diff line change
@@ -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<Workspace> {
const {
data: { workspace },
} = await this.client.get<WorkspacesGetResponse>('/workspaces/get', {
params,
})
return workspace
}
}

export type WorkspacesGetParams = SetNonNullable<
Required<Routes['/workspaces/get']['commonParams']>
>

export type WorkspacesGetResponse = SetNonNullable<
Required<Routes['/workspaces/get']['jsonResponse']>
>

// 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
}
6 changes: 3 additions & 3 deletions src/lib/seam/connect/routes/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Workspace> {
const {
data: { workspace },
} = await this.#client.get<WorkspacesGetResponse>('/workspaces/get', {
} = await this.client.get<WorkspacesGetResponse>('/workspaces/get', {
params,
})
return workspace
Expand Down

0 comments on commit 187a36e

Please sign in to comment.