diff --git a/docs/classes/Seam.md b/docs/classes/Seam.md index 322949d2..ce4fe950 100644 --- a/docs/classes/Seam.md +++ b/docs/classes/Seam.md @@ -31,6 +31,7 @@ ### Methods - [makeRequest](Seam.md#makerequest) +- [getClientSessionToken](Seam.md#getclientsessiontoken) ## Constructors @@ -50,7 +51,7 @@ Routes.constructor #### Defined in -[src/seam-connect/client.ts:52](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L52) +[src/seam-connect/client.ts:98](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L98) ## Properties @@ -110,7 +111,7 @@ ___ #### Defined in -[src/seam-connect/client.ts:50](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L50) +[src/seam-connect/client.ts:96](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L96) ___ @@ -320,4 +321,24 @@ Routes.makeRequest #### Defined in -[src/seam-connect/client.ts:90](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L90) +[src/seam-connect/client.ts:130](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L130) + +___ + +### getClientSessionToken + +▸ `Static` **getClientSessionToken**(`ops`): `Promise`<[`APIResponse`](../modules.md#apiresponse)<[`ClientSessionResponse`](../interfaces/ClientSessionResponse.md)\>\> + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `ops` | `CSTParams` | + +#### Returns + +`Promise`<[`APIResponse`](../modules.md#apiresponse)<[`ClientSessionResponse`](../interfaces/ClientSessionResponse.md)\>\> + +#### Defined in + +[src/seam-connect/client.ts:155](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L155) diff --git a/docs/interfaces/AccessCodeCreateMultipleResponse.md b/docs/interfaces/AccessCodeCreateMultipleResponse.md index 4f48d8a8..0707faa7 100644 --- a/docs/interfaces/AccessCodeCreateMultipleResponse.md +++ b/docs/interfaces/AccessCodeCreateMultipleResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:85](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L85) +[src/types/route-responses.ts:86](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L86) diff --git a/docs/interfaces/AccessCodeCreateResponse.md b/docs/interfaces/AccessCodeCreateResponse.md index 8e4ad25f..a19663ae 100644 --- a/docs/interfaces/AccessCodeCreateResponse.md +++ b/docs/interfaces/AccessCodeCreateResponse.md @@ -29,7 +29,7 @@ #### Defined in -[src/types/route-responses.ts:78](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L78) +[src/types/route-responses.ts:79](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L79) ___ @@ -43,4 +43,4 @@ ___ #### Defined in -[src/types/route-responses.ts:122](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L122) +[src/types/route-responses.ts:123](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L123) diff --git a/docs/interfaces/AccessCodeGetResponse.md b/docs/interfaces/AccessCodeGetResponse.md index f4051e32..3015bbb0 100644 --- a/docs/interfaces/AccessCodeGetResponse.md +++ b/docs/interfaces/AccessCodeGetResponse.md @@ -22,4 +22,4 @@ #### Defined in -[src/types/route-responses.ts:78](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L78) +[src/types/route-responses.ts:79](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L79) diff --git a/docs/interfaces/AccessCodesListResponse.md b/docs/interfaces/AccessCodesListResponse.md index f66a09f5..a6a0f9fa 100644 --- a/docs/interfaces/AccessCodesListResponse.md +++ b/docs/interfaces/AccessCodesListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:75](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L75) +[src/types/route-responses.ts:76](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L76) diff --git a/docs/interfaces/ActionAttemptCreateResponse.md b/docs/interfaces/ActionAttemptCreateResponse.md index c4b4142b..30811438 100644 --- a/docs/interfaces/ActionAttemptCreateResponse.md +++ b/docs/interfaces/ActionAttemptCreateResponse.md @@ -22,4 +22,4 @@ #### Defined in -[src/types/route-responses.ts:118](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L118) +[src/types/route-responses.ts:119](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L119) diff --git a/docs/interfaces/ActionAttemptGetResponse.md b/docs/interfaces/ActionAttemptGetResponse.md index af3abe60..feb4d33f 100644 --- a/docs/interfaces/ActionAttemptGetResponse.md +++ b/docs/interfaces/ActionAttemptGetResponse.md @@ -28,4 +28,4 @@ #### Defined in -[src/types/route-responses.ts:122](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L122) +[src/types/route-responses.ts:123](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L123) diff --git a/docs/interfaces/ClientSession.md b/docs/interfaces/ClientSession.md new file mode 100644 index 00000000..52b46204 --- /dev/null +++ b/docs/interfaces/ClientSession.md @@ -0,0 +1,41 @@ +[seamapi](../README.md) / [Exports](../modules.md) / ClientSession + +# Interface: ClientSession + +## Table of contents + +### Properties + +- [created\_at](ClientSession.md#created_at) +- [token](ClientSession.md#token) +- [user\_id](ClientSession.md#user_id) + +## Properties + +### created\_at + +• **created\_at**: `string` + +#### Defined in + +[src/types/models.ts:308](https://github.com/seamapi/javascript/blob/main/src/types/models.ts#L308) + +___ + +### token + +• **token**: `string` + +#### Defined in + +[src/types/models.ts:306](https://github.com/seamapi/javascript/blob/main/src/types/models.ts#L306) + +___ + +### user\_id + +• **user\_id**: `string` + +#### Defined in + +[src/types/models.ts:307](https://github.com/seamapi/javascript/blob/main/src/types/models.ts#L307) diff --git a/docs/interfaces/ClientSessionResponse.md b/docs/interfaces/ClientSessionResponse.md new file mode 100644 index 00000000..33e395ee --- /dev/null +++ b/docs/interfaces/ClientSessionResponse.md @@ -0,0 +1,19 @@ +[seamapi](../README.md) / [Exports](../modules.md) / ClientSessionResponse + +# Interface: ClientSessionResponse + +## Table of contents + +### Properties + +- [client\_session](ClientSessionResponse.md#client_session) + +## Properties + +### client\_session + +• **client\_session**: [`ClientSession`](ClientSession.md) + +#### Defined in + +[src/types/route-responses.ts:132](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L132) diff --git a/docs/interfaces/ConnectWebviewCreateResponse.md b/docs/interfaces/ConnectWebviewCreateResponse.md index 35c30348..aace7b98 100644 --- a/docs/interfaces/ConnectWebviewCreateResponse.md +++ b/docs/interfaces/ConnectWebviewCreateResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:67](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L67) +[src/types/route-responses.ts:68](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L68) diff --git a/docs/interfaces/ConnectWebviewGetResponse.md b/docs/interfaces/ConnectWebviewGetResponse.md index 457fc9f9..d624eb0c 100644 --- a/docs/interfaces/ConnectWebviewGetResponse.md +++ b/docs/interfaces/ConnectWebviewGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:64](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L64) +[src/types/route-responses.ts:65](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L65) diff --git a/docs/interfaces/ConnectWebviewsListResponse.md b/docs/interfaces/ConnectWebviewsListResponse.md index e898fef5..6f044d0f 100644 --- a/docs/interfaces/ConnectWebviewsListResponse.md +++ b/docs/interfaces/ConnectWebviewsListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:61](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L61) +[src/types/route-responses.ts:62](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L62) diff --git a/docs/interfaces/ConnectedAccountsGetResponse.md b/docs/interfaces/ConnectedAccountsGetResponse.md index 8b63beec..4f540c33 100644 --- a/docs/interfaces/ConnectedAccountsGetResponse.md +++ b/docs/interfaces/ConnectedAccountsGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:101](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L101) +[src/types/route-responses.ts:102](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L102) diff --git a/docs/interfaces/ConnectedAccountsListResponse.md b/docs/interfaces/ConnectedAccountsListResponse.md index 6be83d7c..eccf0c32 100644 --- a/docs/interfaces/ConnectedAccountsListResponse.md +++ b/docs/interfaces/ConnectedAccountsListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:98](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L98) +[src/types/route-responses.ts:99](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L99) diff --git a/docs/interfaces/DeviceGetResponse.md b/docs/interfaces/DeviceGetResponse.md index f2f6d922..b7e2d530 100644 --- a/docs/interfaces/DeviceGetResponse.md +++ b/docs/interfaces/DeviceGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:46](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L46) +[src/types/route-responses.ts:47](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L47) diff --git a/docs/interfaces/DeviceProvidersListResponse.md b/docs/interfaces/DeviceProvidersListResponse.md index 28ec7c02..1618293f 100644 --- a/docs/interfaces/DeviceProvidersListResponse.md +++ b/docs/interfaces/DeviceProvidersListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:56](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L56) +[src/types/route-responses.ts:57](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L57) diff --git a/docs/interfaces/DevicesListResponse.md b/docs/interfaces/DevicesListResponse.md index e944f5cb..df5609c5 100644 --- a/docs/interfaces/DevicesListResponse.md +++ b/docs/interfaces/DevicesListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:43](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L43) +[src/types/route-responses.ts:44](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L44) diff --git a/docs/interfaces/EventsListResponse.md b/docs/interfaces/EventsListResponse.md index 4680a519..70fd4018 100644 --- a/docs/interfaces/EventsListResponse.md +++ b/docs/interfaces/EventsListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:127](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L127) +[src/types/route-responses.ts:128](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L128) diff --git a/docs/interfaces/LockGetResponse.md b/docs/interfaces/LockGetResponse.md index 437bc20e..60c8ac49 100644 --- a/docs/interfaces/LockGetResponse.md +++ b/docs/interfaces/LockGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:38](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L38) +[src/types/route-responses.ts:39](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L39) diff --git a/docs/interfaces/LocksListResponse.md b/docs/interfaces/LocksListResponse.md index 30e4bdea..a1832e68 100644 --- a/docs/interfaces/LocksListResponse.md +++ b/docs/interfaces/LocksListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:34](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L34) +[src/types/route-responses.ts:35](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L35) diff --git a/docs/interfaces/SeamClientOptions.md b/docs/interfaces/SeamClientOptions.md index 0ea2097d..c860bf8f 100644 --- a/docs/interfaces/SeamClientOptions.md +++ b/docs/interfaces/SeamClientOptions.md @@ -8,6 +8,7 @@ - [apiKey](SeamClientOptions.md#apikey) - [axiosOptions](SeamClientOptions.md#axiosoptions) +- [clientSessionToken](SeamClientOptions.md#clientsessiontoken) - [endpoint](SeamClientOptions.md#endpoint) - [workspaceId](SeamClientOptions.md#workspaceid) @@ -19,7 +20,7 @@ #### Defined in -[src/seam-connect/client.ts:10](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L10) +[src/seam-connect/client.ts:20](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L20) ___ @@ -31,7 +32,17 @@ Extended options to pass to Axios #### Defined in -[src/seam-connect/client.ts:23](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L23) +[src/seam-connect/client.ts:35](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L35) + +___ + +### clientSessionToken + +• `Optional` **clientSessionToken**: `string` + +#### Defined in + +[src/seam-connect/client.ts:22](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L22) ___ @@ -43,7 +54,7 @@ Seam Endpoint to use, defaults to https://connect.getseam.com #### Defined in -[src/seam-connect/client.ts:14](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L14) +[src/seam-connect/client.ts:26](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L26) ___ @@ -56,4 +67,4 @@ or undefined #### Defined in -[src/seam-connect/client.ts:19](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L19) +[src/seam-connect/client.ts:31](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L31) diff --git a/docs/interfaces/UnmanagedAccessCodesGetResponse.md b/docs/interfaces/UnmanagedAccessCodesGetResponse.md index c9578717..2eb69a47 100644 --- a/docs/interfaces/UnmanagedAccessCodesGetResponse.md +++ b/docs/interfaces/UnmanagedAccessCodesGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:93](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L93) +[src/types/route-responses.ts:94](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L94) diff --git a/docs/interfaces/UnmanagedAccessCodesListResponse.md b/docs/interfaces/UnmanagedAccessCodesListResponse.md index 1cad2330..037eac2e 100644 --- a/docs/interfaces/UnmanagedAccessCodesListResponse.md +++ b/docs/interfaces/UnmanagedAccessCodesListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:90](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L90) +[src/types/route-responses.ts:91](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L91) diff --git a/docs/interfaces/UnmanagedDeviceListResponse.md b/docs/interfaces/UnmanagedDeviceListResponse.md index ae530d76..14bd1a17 100644 --- a/docs/interfaces/UnmanagedDeviceListResponse.md +++ b/docs/interfaces/UnmanagedDeviceListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:51](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L51) +[src/types/route-responses.ts:52](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L52) diff --git a/docs/interfaces/WebhookGetResponse.md b/docs/interfaces/WebhookGetResponse.md index 95f276b2..3d476c3d 100644 --- a/docs/interfaces/WebhookGetResponse.md +++ b/docs/interfaces/WebhookGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:110](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L110) +[src/types/route-responses.ts:111](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L111) diff --git a/docs/interfaces/WebhookListResponse.md b/docs/interfaces/WebhookListResponse.md index 76fc36e2..619f1e42 100644 --- a/docs/interfaces/WebhookListResponse.md +++ b/docs/interfaces/WebhookListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:106](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L106) +[src/types/route-responses.ts:107](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L107) diff --git a/docs/interfaces/WorkspaceGetResponse.md b/docs/interfaces/WorkspaceGetResponse.md index 31aa9c57..be29e1b1 100644 --- a/docs/interfaces/WorkspaceGetResponse.md +++ b/docs/interfaces/WorkspaceGetResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:24](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L24) +[src/types/route-responses.ts:25](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L25) diff --git a/docs/interfaces/WorkspaceResetSandboxResponse.md b/docs/interfaces/WorkspaceResetSandboxResponse.md index 4ccf561a..11db3d15 100644 --- a/docs/interfaces/WorkspaceResetSandboxResponse.md +++ b/docs/interfaces/WorkspaceResetSandboxResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:29](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L29) +[src/types/route-responses.ts:30](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L30) diff --git a/docs/interfaces/WorkspacesListResponse.md b/docs/interfaces/WorkspacesListResponse.md index 8ff5a9bf..d813ab10 100644 --- a/docs/interfaces/WorkspacesListResponse.md +++ b/docs/interfaces/WorkspacesListResponse.md @@ -16,4 +16,4 @@ #### Defined in -[src/types/route-responses.ts:21](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L21) +[src/types/route-responses.ts:22](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L22) diff --git a/docs/modules.md b/docs/modules.md index 41ddfc79..71df5f22 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -39,6 +39,8 @@ - [ActionAttemptGetResponse](interfaces/ActionAttemptGetResponse.md) - [ActionAttemptResultTypeMap](interfaces/ActionAttemptResultTypeMap.md) - [ActionAttemptWithError](interfaces/ActionAttemptWithError.md) +- [ClientSession](interfaces/ClientSession.md) +- [ClientSessionResponse](interfaces/ClientSessionResponse.md) - [ConnectWebview](interfaces/ConnectWebview.md) - [ConnectWebviewCreateRequest](interfaces/ConnectWebviewCreateRequest.md) - [ConnectWebviewCreateResponse](interfaces/ConnectWebviewCreateResponse.md) @@ -486,7 +488,7 @@ ___ #### Defined in -[src/types/models.ts:305](https://github.com/seamapi/javascript/blob/main/src/types/models.ts#L305) +[src/types/models.ts:311](https://github.com/seamapi/javascript/blob/main/src/types/models.ts#L311) ___ @@ -518,7 +520,7 @@ ___ #### Defined in -[src/types/route-responses.ts:130](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L130) +[src/types/route-responses.ts:135](https://github.com/seamapi/javascript/blob/main/src/types/route-responses.ts#L135) ___ @@ -703,7 +705,7 @@ ___ #### Defined in -[src/seam-connect/client.ts:26](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L26) +[src/seam-connect/client.ts:38](https://github.com/seamapi/javascript/blob/main/src/seam-connect/client.ts#L38) ___ diff --git a/src/seam-connect/client.ts b/src/seam-connect/client.ts index 2537433a..c2eb87e4 100644 --- a/src/seam-connect/client.ts +++ b/src/seam-connect/client.ts @@ -1,13 +1,25 @@ -import axios, { AxiosInstance, AxiosRequestConfig } from "axios" +import axios, { + AxiosInstance, + AxiosRequestConfig, + AxiosRequestHeaders, + AxiosResponse, +} from "axios" import axiosRetry from "axios-retry" import { SeamAPIError, SeamMalformedInputError } from "../lib/api-error" import { Routes } from "./routes" -import { ErroredAPIResponse, SuccessfulAPIResponse } from "../types/globals" +import { + APIResponse, + ErroredAPIResponse, + SuccessfulAPIResponse, +} from "../types/globals" import { version } from "../../package.json" +import { ClientSession, ClientSessionResponse } from "../types" export interface SeamClientOptions { /* Seam API Key */ apiKey?: string + /* Seam Client Session Token */ + clientSessionToken?: string /** * Seam Endpoint to use, defaults to https://connect.getseam.com **/ @@ -46,39 +58,67 @@ export const getSeamClientOptionsWithDefaults = ( } } +const getBearerToken = ({ + clientSessionToken, + apiKey, + workspaceId, +}: { + clientSessionToken?: string + apiKey?: string + workspaceId?: string +}): string => { + if (apiKey && clientSessionToken) { + throw new Error("You can't use clientSessionToken AND specify apiKey.") + } + if (clientSessionToken) { + if (clientSessionToken.startsWith("seam_cst")) return clientSessionToken + else throw new Error("clientSessionToken must start with seam_cst") + } + if (apiKey) { + if (apiKey.startsWith("seam_cst")) { + console.warn( + "Using API Key as Client Session Token is deprecated. Please use the clientSessionToken option instead." + ) + return apiKey + } + if (!apiKey.startsWith("seam_at") && workspaceId) + throw new Error( + "You can't use API Key Authentication AND specify a workspace. Your API Key only works for the workspace it was created in. To use Session Key Authentication with multi-workspace support, contact Seam support." + ) + return apiKey + } + throw new Error( + "Must provide either clientSessionToken or apiKey (API Key or Access Token with Workspace ID)." + ) +} + export class Seam extends Routes { public client: AxiosInstance constructor(apiKeyOrOptions?: string | SeamClientOptions) { super() - const { apiKey, endpoint, workspaceId, axiosOptions } = + const { apiKey, endpoint, workspaceId, axiosOptions, clientSessionToken } = getSeamClientOptionsWithDefaults(apiKeyOrOptions) - const isRegularAPIKey = apiKey?.startsWith("seam_") - - if (isRegularAPIKey && workspaceId) - throw new Error( - "You can't use API Key Authentication AND specify a workspace. Your API Key only works for the workspace it was created in. To use Session Key Authentication with multi-workspace support, contact Seam support." - ) + const bearer = + `Bearer ` + getBearerToken({ clientSessionToken, apiKey, workspaceId }) - if (!apiKey) { - throw new Error( - "SEAM_API_KEY not found in environment, and apiKey not provided" - ) + const headers: AxiosRequestHeaders = { + ...axiosOptions?.headers, + Authorization: bearer, + ...(!workspaceId ? {} : { "Seam-Workspace": workspaceId }), // only needed for session key authentication + // 'seam-sdk-version': version // TODO: resolve error Access to XMLHttpRequest at 'http://localhost:3020/devices/list' from origin 'http://localhost:5173' has been blocked by CORS policy: Request header field seam-sdk-version is not allowed by Access-Control-Allow-Headers in preflight response. + } + if (typeof window === "undefined") { + headers[ + "User-Agent" + ] = `Javascript SDK v${version}, Node.js mode, (https://github.com/seamapi/javascript)` } - this.client = axios.create({ ...axiosOptions, baseURL: endpoint, - headers: { - ...axiosOptions?.headers, - Authorization: `Bearer ${apiKey}`, - ["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`, - - // only needed for session key authentication - ...(!workspaceId ? {} : { "Seam-Workspace": workspaceId }), - }, + headers, }) axiosRetry(this.client, { @@ -111,4 +151,57 @@ export class Seam extends Routes { throw error } } + + static async getClientSessionToken( + ops: CSTParams + ): Promise> { + const { apiKey, endpoint, axiosOptions } = + getSeamClientOptionsWithDefaults(ops) + let headers: AxiosRequestHeaders = { + ...axiosOptions?.headers, + } + + if (ops.publishableKey?.startsWith("seam_pk")) { + // frontend mode + headers["seam-publishable-key"] = ops.publishableKey + } else if (apiKey?.startsWith("seam_")) { + // backend mode + headers["seam-api-key"] = apiKey + } + if (ops.userIdentifierKey) { + headers["seam-user-identifier-key"] = ops.userIdentifierKey + } else { + throw new Error("userIdentifierKey is required") + } + const client = axios.create({ + ...axiosOptions, + baseURL: endpoint, + headers, + }) + try { + const response: AxiosResponse & { + data: { client_session: ClientSession } + } = await client.post( + "/internal/client_sessions/create", + {}, + // { headers: { "seam-sdk-version": version } } + {} + ) + return await response.data + } catch (error: any) { + throw new SeamAPIError( + error.response.status, + error.response.headers["seam-request-id"], + (error.response.data as ErroredAPIResponse).error + ) + } + } +} + +type CSTParams = { + publishableKey?: string + userIdentifierKey: string + endpoint?: string + workspaceId?: string + apiKey?: string } diff --git a/src/types/models.ts b/src/types/models.ts index 2af01f0d..603ca414 100644 --- a/src/types/models.ts +++ b/src/types/models.ts @@ -302,6 +302,12 @@ type Flatten = export type Event = Flatten +export interface ClientSession { + token: string + user_id: string + created_at: string +} + export type NoiseThresholds = { device_id: string noise_threshold_id: string diff --git a/src/types/route-responses.ts b/src/types/route-responses.ts index ce852daa..2e75b782 100644 --- a/src/types/route-responses.ts +++ b/src/types/route-responses.ts @@ -11,6 +11,7 @@ import { LockDevice, UnmanagedAccessCode, UnmanagedDevice, + ClientSession, Webhook, Workspace, NoiseThresholds, @@ -127,6 +128,10 @@ export interface EventsListResponse { events: Event[] } +export interface ClientSessionResponse { + client_session: ClientSession +} + export type NoiseThresholdsListResponse = { noise_thresholds: NoiseThresholds[] }