File tree Expand file tree Collapse file tree 3 files changed +35
-4
lines changed Expand file tree Collapse file tree 3 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,13 @@ import {
99
1010type 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+
8395export class SeamHttpInvalidTokenError extends Error {
8496 constructor ( message : string ) {
8597 super ( `SeamHttp received an invalid token: ${ message } ` )
Original file line number Diff line number Diff line change @@ -20,7 +20,11 @@ export interface ClientOptions {
2020
2121type 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 ( {
Original file line number Diff line number Diff line change 1- import { type Client , createClient } from './client.js'
1+ import { type Client , type ClientOptions , createClient } from './client.js'
22import {
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 }
You can’t perform that action at this time.
0 commit comments