Skip to content

Commit c4d6df7

Browse files
committed
Ignore SEAM_API_KEY when clientSessionToken is defined
1 parent d3796ef commit c4d6df7

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

src/lib/seam/connect/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const isSeamHttpOptionsWithApiKey = (
4444

4545
if ('clientSessionToken' in options && options.clientSessionToken != null) {
4646
throw new SeamHttpInvalidOptionsError(
47-
'The clientSessionToken option cannot be used with the apiKey option.',
47+
'The clientSessionToken option cannot be used with the apiKey option',
4848
)
4949
}
5050

@@ -64,7 +64,7 @@ export const isSeamHttpOptionsWithClientSessionToken = (
6464

6565
if ('apiKey' in options && options.apiKey != null) {
6666
throw new SeamHttpInvalidOptionsError(
67-
'The clientSessionToken option cannot be used with the apiKey option.',
67+
'The clientSessionToken option cannot be used with the apiKey option',
6868
)
6969
}
7070

src/lib/seam/connect/parse-options.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isSeamHttpOptionsWithClient, type SeamHttpOptions } from './options.js'
22

3+
const defaultEndpoint = 'https://connect.getseam.com'
4+
35
export const parseOptions = (
46
apiKeyOrOptions: string | SeamHttpOptions,
57
): Required<SeamHttpOptions> => {
@@ -10,14 +12,10 @@ export const parseOptions = (
1012

1113
if (isSeamHttpOptionsWithClient(options)) return options
1214

13-
const endpoint =
14-
options.endpoint ??
15-
globalThis.process?.env?.SEAM_ENDPOINT ??
16-
globalThis.process?.env?.SEAM_API_URL ??
17-
'https://connect.getseam.com'
15+
const endpoint = options.endpoint ?? getEndpointFromEnv() ?? defaultEndpoint
1816

1917
const apiKey =
20-
'apiKey' in options ? options.apiKey : globalThis.process?.env?.SEAM_API_KEY
18+
'apiKey' in options ? options.apiKey : getApiKeyFromEnv(options)
2119

2220
return {
2321
...options,
@@ -27,3 +25,19 @@ export const parseOptions = (
2725
axiosRetryOptions: options.axiosRetryOptions ?? {},
2826
}
2927
}
28+
29+
const getApiKeyFromEnv = (
30+
options: SeamHttpOptions,
31+
): string | null | undefined => {
32+
if ('clientSessionToken' in options && options.clientSessionToken != null) {
33+
return null
34+
}
35+
return globalThis.process?.env?.SEAM_API_KEY
36+
}
37+
38+
const getEndpointFromEnv = (): string | null | undefined => {
39+
return (
40+
globalThis.process?.env?.SEAM_ENDPOINT ??
41+
globalThis.process?.env?.SEAM_API_URL
42+
)
43+
}

test/seam/connect/env.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { SeamHttp, SeamHttpInvalidOptionsError } from '@seamapi/http/connect'
88
/*
99
* Tests in this file must run serially to ensure a clean environment for each test.
1010
*/
11-
test.afterEach(() => {
11+
const cleanupEnv = (): void => {
1212
delete env.SEAM_API_KEY
1313
delete env.SEAM_ENDPOINT
1414
delete env.SEAM_API_URL
15-
})
15+
}
16+
test.afterEach(cleanupEnv)
17+
test.beforeEach(cleanupEnv)
1618

1719
test.serial(
1820
'SeamHttp: constructor uses SEAM_API_KEY environment variable',
@@ -32,7 +34,7 @@ test.serial(
3234
'SeamHttp: apiKey option overrides environment variables',
3335
async (t) => {
3436
const { seed, endpoint } = await getTestServer(t)
35-
env.SEAM_API_KEY = 'some-invalid-api-key'
37+
env.SEAM_API_KEY = 'some-invalid-api-key-1'
3638
const seam = new SeamHttp({ apiKey: seed.seam_apikey1_token, endpoint })
3739
const device = await seam.devices.get({
3840
device_id: seed.august_device_1,
@@ -45,7 +47,7 @@ test.serial(
4547
test.serial(
4648
'SeamHttp: apiKey option as first argument overrides environment variables',
4749
(t) => {
48-
env.SEAM_API_KEY = 'some-invalid-api-key'
50+
env.SEAM_API_KEY = 'some-invalid-api-key-2'
4951
const seam = new SeamHttp('seam_apikey_token')
5052
t.truthy(seam)
5153
},
@@ -70,17 +72,6 @@ test.serial(
7072
},
7173
)
7274

73-
test.serial(
74-
'SeamHttp: constructor throws if SEAM_API_KEY environment variable is used with clientSessionToken',
75-
(t) => {
76-
env.SEAM_API_KEY = 'seam_apikey_token'
77-
t.throws(() => new SeamHttp({ clientSessionToken: 'seam_cst1_token' }), {
78-
instanceOf: SeamHttpInvalidOptionsError,
79-
message: /apiKey/,
80-
})
81-
},
82-
)
83-
8475
test.serial(
8576
'SeamHttp: SEAM_ENDPOINT environment variable is used first',
8677
async (t) => {
@@ -140,6 +131,39 @@ test.serial(
140131
},
141132
)
142133

134+
test.serial(
135+
'SeamHttp: constructor ignores SEAM_API_KEY environment variable if used with clientSessionToken',
136+
async (t) => {
137+
const { seed, endpoint } = await getTestServer(t)
138+
env.SEAM_API_KEY = 'some-invalid-api-key-3'
139+
const seam = new SeamHttp({
140+
clientSessionToken: seed.seam_cst1_token,
141+
endpoint,
142+
})
143+
const device = await seam.devices.get({
144+
device_id: seed.august_device_1,
145+
})
146+
t.is(device.workspace_id, seed.seed_workspace_1)
147+
t.is(device.device_id, seed.august_device_1)
148+
},
149+
)
150+
151+
test.serial(
152+
'SeamHttp: SEAM_API_KEY environment variable is ignored with fromClientSessionToken',
153+
async (t) => {
154+
const { seed, endpoint } = await getTestServer(t)
155+
env.SEAM_API_KEY = 'some-invalid-api-key-4'
156+
const seam = SeamHttp.fromClientSessionToken(seed.seam_cst1_token, {
157+
endpoint,
158+
})
159+
const device = await seam.devices.get({
160+
device_id: seed.august_device_1,
161+
})
162+
t.is(device.workspace_id, seed.seed_workspace_1)
163+
t.is(device.device_id, seed.august_device_1)
164+
},
165+
)
166+
143167
test.serial(
144168
'SeamHttp: SEAM_ENDPOINT environment variable is used with fromClientSessionToken',
145169
async (t) => {

0 commit comments

Comments
 (0)