|
| 1 | +client: Client |
| 2 | +readonly defaults: Required<SeamHttpRequestOptions> |
| 3 | +readonly ltsVersion = seamApiLtsVersion |
| 4 | +static ltsVersion = seamApiLtsVersion |
| 5 | + |
| 6 | +constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) { |
| 7 | + const options = parseOptions(apiKeyOrOptions) |
| 8 | + this.client = 'client' in options ? options.client : createClient(options) |
| 9 | + this.defaults = limitToSeamHttpRequestOptions(options) |
| 10 | +} |
| 11 | + |
| 12 | +static fromClient( |
| 13 | + client: SeamHttpOptionsWithClient['client'], |
| 14 | + options: Omit<SeamHttpOptionsWithClient, 'client'> = {}, |
| 15 | +): {{className}} { |
| 16 | + const constructorOptions = { ...options, client } |
| 17 | + if (!isSeamHttpOptionsWithClient(constructorOptions)) { |
| 18 | + throw new SeamHttpInvalidOptionsError('Missing client') |
| 19 | + } |
| 20 | + return new {{className}}(constructorOptions) |
| 21 | +} |
| 22 | + |
| 23 | +static fromApiKey( |
| 24 | + apiKey: SeamHttpOptionsWithApiKey['apiKey'], |
| 25 | + options: Omit<SeamHttpOptionsWithApiKey, 'apiKey'> = {}, |
| 26 | +): {{className}} { |
| 27 | + const constructorOptions = { ...options, apiKey } |
| 28 | + if (!isSeamHttpOptionsWithApiKey(constructorOptions)) { |
| 29 | + throw new SeamHttpInvalidOptionsError('Missing apiKey') |
| 30 | + } |
| 31 | + return new {{className}}(constructorOptions) |
| 32 | +} |
| 33 | + |
| 34 | +static fromClientSessionToken( |
| 35 | + clientSessionToken: SeamHttpOptionsWithClientSessionToken['clientSessionToken'], |
| 36 | + options: Omit< |
| 37 | + SeamHttpOptionsWithClientSessionToken, |
| 38 | + 'clientSessionToken' |
| 39 | + > = {}, |
| 40 | +): {{className}} { |
| 41 | + const constructorOptions = { ...options, clientSessionToken } |
| 42 | + if (!isSeamHttpOptionsWithClientSessionToken(constructorOptions)) { |
| 43 | + throw new SeamHttpInvalidOptionsError('Missing clientSessionToken') |
| 44 | + } |
| 45 | + return new {{className}}(constructorOptions) |
| 46 | +} |
| 47 | + |
| 48 | +static async fromPublishableKey( |
| 49 | + publishableKey: string, |
| 50 | + userIdentifierKey: string, |
| 51 | + options: SeamHttpFromPublishableKeyOptions = {}, |
| 52 | +): Promise<{{className}}> { |
| 53 | + warnOnInsecureuserIdentifierKey(userIdentifierKey) |
| 54 | + const clientOptions = parseOptions({ ...options, publishableKey }) |
| 55 | + if (isSeamHttpOptionsWithClient(clientOptions)) { |
| 56 | + throw new SeamHttpInvalidOptionsError( |
| 57 | + 'The client option cannot be used with {{className}}.fromPublishableKey', |
| 58 | + ) |
| 59 | + } |
| 60 | + const client = createClient(clientOptions) |
| 61 | + const clientSessions = SeamHttpClientSessions.fromClient(client) |
| 62 | + const { token } = await clientSessions.getOrCreate({ |
| 63 | + user_identifier_key: userIdentifierKey, |
| 64 | + }) |
| 65 | + return {{className}}.fromClientSessionToken(token, options) |
| 66 | +} |
| 67 | + |
| 68 | +static fromConsoleSessionToken( |
| 69 | + consoleSessionToken: SeamHttpOptionsWithConsoleSessionToken['consoleSessionToken'], |
| 70 | + workspaceId: SeamHttpOptionsWithConsoleSessionToken['workspaceId'], |
| 71 | + options: Omit< |
| 72 | + SeamHttpOptionsWithConsoleSessionToken, |
| 73 | + 'consoleSessionToken' | 'workspaceId' |
| 74 | + > = {}, |
| 75 | +): {{className}} { |
| 76 | + const constructorOptions = { ...options, consoleSessionToken, workspaceId } |
| 77 | + if (!isSeamHttpOptionsWithConsoleSessionToken(constructorOptions)) { |
| 78 | + throw new SeamHttpInvalidOptionsError( |
| 79 | + 'Missing consoleSessionToken or workspaceId', |
| 80 | + ) |
| 81 | + } |
| 82 | + return new {{className}}(constructorOptions) |
| 83 | +} |
| 84 | + |
| 85 | +static fromPersonalAccessToken( |
| 86 | + personalAccessToken: SeamHttpOptionsWithPersonalAccessToken['personalAccessToken'], |
| 87 | + workspaceId: SeamHttpOptionsWithPersonalAccessToken['workspaceId'], |
| 88 | + options: Omit< |
| 89 | + SeamHttpOptionsWithPersonalAccessToken, |
| 90 | + 'personalAccessToken' | 'workspaceId' |
| 91 | + > = {}, |
| 92 | +): {{className}} { |
| 93 | + const constructorOptions = { ...options, personalAccessToken, workspaceId } |
| 94 | + if (!isSeamHttpOptionsWithPersonalAccessToken(constructorOptions)) { |
| 95 | + throw new SeamHttpInvalidOptionsError( |
| 96 | + 'Missing personalAccessToken or workspaceId', |
| 97 | + ) |
| 98 | + } |
| 99 | + return new {{className}}(constructorOptions) |
| 100 | +} |
| 101 | + |
| 102 | +createPaginator<const TResponse, const TResponseKey extends keyof TResponse>( |
| 103 | + request: SeamHttpRequest<TResponse, TResponseKey>, |
| 104 | +): SeamPaginator<TResponse, TResponseKey> { |
| 105 | + return new SeamPaginator<TResponse, TResponseKey>(this, request) |
| 106 | +} |
| 107 | + |
| 108 | +async updateClientSessionToken( |
| 109 | + clientSessionToken: SeamHttpOptionsWithClientSessionToken['clientSessionToken'], |
| 110 | +): Promise<void> { |
| 111 | + const { headers } = this.client.defaults |
| 112 | + const authHeaders = getAuthHeadersForClientSessionToken({ |
| 113 | + clientSessionToken, |
| 114 | + }) |
| 115 | + for (const key of Object.keys(authHeaders)) { |
| 116 | + if (headers[key] == null) { |
| 117 | + throw new Error( |
| 118 | + 'Cannot update a clientSessionToken on a client created without a clientSessionToken', |
| 119 | + ) |
| 120 | + } |
| 121 | + } |
| 122 | + this.client.defaults.headers = { ...headers, ...authHeaders } |
| 123 | + const clientSessions = SeamHttpClientSessions.fromClient(this.client) |
| 124 | + await clientSessions.get() |
| 125 | +} |
0 commit comments