Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 24daf27

Browse files
authored
feat: Use client-session-token header (#188)
1 parent a8ef0e1 commit 24daf27

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

docs/classes/Seam.md

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/seam-connect/client.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,38 @@ export const getSeamClientOptionsWithDefaults = (
5858
}
5959
}
6060

61-
const getBearerToken = ({
61+
const getAuthHeaders = ({
6262
clientSessionToken,
6363
apiKey,
6464
workspaceId,
6565
}: {
6666
clientSessionToken?: string
6767
apiKey?: string
6868
workspaceId?: string
69-
}): string => {
69+
}): Record<string, string> => {
7070
if (apiKey && clientSessionToken) {
7171
throw new Error("You can't use clientSessionToken AND specify apiKey.")
7272
}
73+
7374
if (clientSessionToken) {
74-
if (clientSessionToken.startsWith("seam_cst")) return clientSessionToken
75-
else throw new Error("clientSessionToken must start with seam_cst")
75+
if (!clientSessionToken.startsWith("seam_cst")) {
76+
throw new Error("clientSessionToken must start with seam_cst")
77+
}
78+
return { "client-session-token": clientSessionToken }
7679
}
80+
7781
if (apiKey) {
7882
if (apiKey.startsWith("seam_cst")) {
7983
console.warn(
8084
"Using API Key as Client Session Token is deprecated. Please use the clientSessionToken option instead."
8185
)
82-
return apiKey
86+
return { "client-session-token": apiKey }
8387
}
8488
if (!apiKey.startsWith("seam_at") && workspaceId)
8589
throw new Error(
8690
"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."
8791
)
88-
return apiKey
92+
return { authorization: `Bearer ${apiKey}` }
8993
}
9094
throw new Error(
9195
"Must provide either clientSessionToken or apiKey (API Key or Access Token with Workspace ID)."
@@ -101,12 +105,15 @@ export class Seam extends Routes {
101105
const { apiKey, endpoint, workspaceId, axiosOptions, clientSessionToken } =
102106
getSeamClientOptionsWithDefaults(apiKeyOrOptions)
103107

104-
const bearer =
105-
`Bearer ` + getBearerToken({ clientSessionToken, apiKey, workspaceId })
108+
const authHeaders = getAuthHeaders({
109+
clientSessionToken,
110+
apiKey,
111+
workspaceId,
112+
})
106113

107114
const headers: AxiosRequestHeaders = {
108115
...axiosOptions?.headers,
109-
Authorization: bearer,
116+
...authHeaders,
110117
...(!workspaceId ? {} : { "Seam-Workspace": workspaceId }), // only needed for session key authentication
111118
// '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.
112119
}

0 commit comments

Comments
 (0)