Skip to content

Commit 884fdfd

Browse files
committed
Add SeamHttp.fromPublishableKey
1 parent e946ec3 commit 884fdfd

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/lib/seam/connect/auth.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import {
99

1010
type Headers = Record<string, string>
1111

12-
export const getAuthHeaders = (options: SeamHttpOptions): Headers => {
12+
export const getAuthHeaders = (
13+
options: SeamHttpOptions | { publishableKey: string },
14+
): Headers => {
15+
if ('publishableKey' in options) {
16+
return getAuthHeadersForPublishableKey(options.publishableKey)
17+
}
18+
1319
if (isSeamHttpOptionsWithApiKey(options)) {
1420
return getAuthHeadersForApiKey(options)
1521
}
@@ -19,7 +25,7 @@ export const getAuthHeaders = (options: SeamHttpOptions): Headers => {
1925
}
2026

2127
throw new SeamHttpInvalidOptionsError(
22-
'Must specify an apiKey or clientSessionToken',
28+
'Must specify an apiKey, clientSessionToken, or publishableKey',
2329
)
2430
}
2531

@@ -80,6 +86,12 @@ const getAuthHeadersForClientSessionToken = ({
8086
}
8187
}
8288

89+
const getAuthHeadersForPublishableKey = (publishableKey: string): Headers => {
90+
return {
91+
'seam-publishable-key': publishableKey,
92+
}
93+
}
94+
8395
export class SeamHttpInvalidTokenError extends Error {
8496
constructor(message: string) {
8597
super(`SeamHttp received an invalid token: ${message}`)

src/lib/seam/connect/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export interface ClientOptions {
2020

2121
type AxiosRetryConfig = Parameters<AxiosRetry>[1]
2222

23-
export const createClient = (options: Required<SeamHttpOptions>): Axios => {
23+
type Options =
24+
| Required<SeamHttpOptions>
25+
| (Required<ClientOptions> & { publishableKey: string })
26+
27+
export const createClient = (options: Options): Axios => {
2428
if (isSeamHttpOptionsWithClient(options)) return options.client
2529

2630
const client = axios.create({

src/lib/seam/connect/seam-http.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Client, createClient } from './client.js'
1+
import { type Client, type ClientOptions, createClient } from './client.js'
22
import {
33
isSeamHttpOptionsWithApiKey,
44
isSeamHttpOptionsWithClient,
@@ -70,6 +70,21 @@ export class SeamHttp {
7070
return new SeamHttp(opts)
7171
}
7272

73+
static async fromPublishableKey(
74+
publishableKey: string,
75+
userIdentifierKey: string,
76+
options: ClientOptions = {},
77+
): Promise<SeamHttp> {
78+
const opts = parseOptions(options)
79+
const client = createClient({ ...opts, publishableKey })
80+
const clientSessions = SeamHttpClientSessions.fromClient(client)
81+
// TODO: clientSessions.getOrCreate({ user_identifier_key: userIdentifierKey })
82+
const { token } = await clientSessions.create({
83+
user_identifier_key: userIdentifierKey,
84+
})
85+
return SeamHttp.fromClientSessionToken(token, options)
86+
}
87+
7388
get accessCodes(): SeamHttpAccessCodes {
7489
return SeamHttpAccessCodes.fromClient(this.client)
7590
}

0 commit comments

Comments
 (0)