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

Commit 16fbaf0

Browse files
authored
feat: Merge pull request #24 from seamapi/dashboard-options
2 parents 0b14cd6 + adbfa4f commit 16fbaf0

File tree

10 files changed

+178
-25
lines changed

10 files changed

+178
-25
lines changed

docs/classes/default.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,21 @@
3333

3434
### constructor
3535

36-
**new default**(`apiKey?`, `endpoint?`)
36+
**new default**(`apiKeyOrOptions?`)
3737

3838
#### Parameters
3939

4040
| Name | Type |
4141
| :------ | :------ |
42-
| `apiKey?` | `string` |
43-
| `endpoint` | `string` |
42+
| `apiKeyOrOptions?` | `string` \| [`SeamClientOptions`](../interfaces/SeamClientOptions.md) |
4443

4544
#### Overrides
4645

4746
Routes.constructor
4847

4948
#### Defined in
5049

51-
[src/index.ts:9](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L9)
50+
[src/index.ts:38](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L38)
5251

5352
## Properties
5453

@@ -100,7 +99,7 @@ ___
10099

101100
#### Defined in
102101

103-
[src/index.ts:7](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L7)
102+
[src/index.ts:36](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L36)
104103

105104
___
106105

@@ -239,4 +238,4 @@ Routes.makeRequest
239238

240239
#### Defined in
241240

242-
[src/index.ts:33](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L33)
241+
[src/index.ts:68](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L68)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[seamapi](../README.md) / [Exports](../modules.md) / SeamClientOptions
2+
3+
# Interface: SeamClientOptions
4+
5+
## Table of contents
6+
7+
### Properties
8+
9+
- [apiKey](SeamClientOptions.md#apikey)
10+
- [endpoint](SeamClientOptions.md#endpoint)
11+
- [workspaceId](SeamClientOptions.md#workspaceid)
12+
13+
## Properties
14+
15+
### apiKey
16+
17+
`Optional` **apiKey**: `string`
18+
19+
#### Defined in
20+
21+
[src/index.ts:8](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L8)
22+
23+
___
24+
25+
### endpoint
26+
27+
`Optional` **endpoint**: `string`
28+
29+
Seam Endpoint to use, defaults to https://connect.getseam.com
30+
31+
#### Defined in
32+
33+
[src/index.ts:12](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L12)
34+
35+
___
36+
37+
### workspaceId
38+
39+
`Optional` **workspaceId**: `string`
40+
41+
Workspace if using session authentication, defaults to SEAM_WORKSPACE_ID
42+
or undefined
43+
44+
#### Defined in
45+
46+
[src/index.ts:17](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L17)

docs/modules.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [OngoingAccessCode](interfaces/OngoingAccessCode.md)
4444
- [PendingActionAttempt](interfaces/PendingActionAttempt.md)
4545
- [SeamAPIErrorMetadata](interfaces/SeamAPIErrorMetadata.md)
46+
- [SeamClientOptions](interfaces/SeamClientOptions.md)
4647
- [SuccessfulActionAttempt](interfaces/SuccessfulActionAttempt.md)
4748
- [TimeBoundAccessCode](interfaces/TimeBoundAccessCode.md)
4849
- [Workspace](interfaces/Workspace.md)
@@ -66,6 +67,10 @@
6667
- [NoiseDetectionDeviceType](modules.md#noisedetectiondevicetype)
6768
- [SuccessfulAPIResponse](modules.md#successfulapiresponse)
6869

70+
### Functions
71+
72+
- [getSeamClientOptionsWithDefaults](modules.md#getseamclientoptionswithdefaults)
73+
6974
## Type aliases
7075

7176
### APIResponse
@@ -220,3 +225,23 @@ ___
220225
#### Defined in
221226

222227
[src/types/globals.ts:7](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/globals.ts#L7)
228+
229+
## Functions
230+
231+
### getSeamClientOptionsWithDefaults
232+
233+
**getSeamClientOptionsWithDefaults**(`apiKeyOrOptions?`): [`SeamClientOptions`](interfaces/SeamClientOptions.md)
234+
235+
#### Parameters
236+
237+
| Name | Type |
238+
| :------ | :------ |
239+
| `apiKeyOrOptions?` | `string` \| [`SeamClientOptions`](interfaces/SeamClientOptions.md) |
240+
241+
#### Returns
242+
243+
[`SeamClientOptions`](interfaces/SeamClientOptions.md)
244+
245+
#### Defined in
246+
247+
[src/index.ts:20](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L20)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"axios": "0.25.0",
3939
"change-case": "4.1.2",
4040
"eventemitter3": "4.0.7",
41+
"lodash": "^4.17.21",
4142
"node-inspect-extracted": "1.1.0",
4243
"ora": "5.4.1",
4344
"p-retry": "4.6.1",
@@ -50,6 +51,7 @@
5051
"@semantic-release/npm": "9.0.0",
5152
"@semantic-release/release-notes-generator": "10.0.3",
5253
"@swc/core": "1.2.133",
54+
"@types/lodash": "^4.14.179",
5355
"@types/node": "17.0.10",
5456
"@types/yargs": "17.0.8",
5557
"ajv": "8.9.0",

src/cli/browser.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type Y from "yargs"
44
import { Yargs } from "https://unpkg.com/[email protected]/browser.mjs"
55
import EventEmitter from "eventemitter3"
66
import TypedEmitter from "typed-emitter"
7+
import { getSeamClientOptionsWithDefaults, SeamClientOptions } from "../"
78

89
const yargsInstance = Yargs() as ReturnType<typeof Y>
910

@@ -13,12 +14,14 @@ type CLIEvents = {
1314

1415
class BrowserCLI extends (EventEmitter as unknown as new () => TypedEmitter<CLIEvents>) {
1516
private instance = getCLI(yargsInstance).scriptName("seam")
17+
private seamClientOptions: SeamClientOptions
1618

1719
/**
1820
* Use the Seam CLI in the browser!
1921
*/
20-
constructor(private apiKey?: string) {
22+
constructor(apiKeyOrOptions?: string | SeamClientOptions) {
2123
super()
24+
this.seamClientOptions = getSeamClientOptionsWithDefaults(apiKeyOrOptions)
2225
this.setUpShims()
2326
}
2427

@@ -34,15 +37,21 @@ class BrowserCLI extends (EventEmitter as unknown as new () => TypedEmitter<CLIE
3437
async parse(input: string) {
3538
this.setUpShims()
3639

37-
const inputWithKey = input.includes("--api-key")
40+
const { apiKey, endpoint, workspaceId } = this.seamClientOptions
41+
42+
input = input.includes("--api-key") ? input : `${input} --api-key ${apiKey}`
43+
input = input.includes("--endpoint")
44+
? input
45+
: `${input} --endpoint ${endpoint}`
46+
input = input.includes("--workspace-id")
3847
? input
39-
: `${input} --api-key ${this.apiKey}`
48+
: `${input} --workspace-id ${workspaceId}`
4049

41-
const inputWithKeyAndWithoutPrefix = inputWithKey.startsWith("seam ")
42-
? inputWithKey.replace("seam ", "")
43-
: inputWithKey.startsWith("seamapi ")
44-
? inputWithKey.replace("seamapi ", "")
45-
: inputWithKey
50+
input = input.startsWith("seam ")
51+
? input.replace("seam ", "")
52+
: input.startsWith("seamapi ")
53+
? input.replace("seamapi ", "")
54+
: input
4655

4756
await new Promise<void>((resolve, reject) => {
4857
// .parseAsync isn't available in v16, so we listen for the ending newline instead
@@ -55,7 +64,7 @@ class BrowserCLI extends (EventEmitter as unknown as new () => TypedEmitter<CLIE
5564
this.on("data", onData)
5665

5766
this.instance.parse(
58-
inputWithKeyAndWithoutPrefix,
67+
input,
5968
(error: Error, _argv: any, output?: string) => {
6069
if (error) {
6170
this.removeListener("data", onData)

src/cli/lib/execute-command.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Get } from "type-fest"
33
import { paramCase } from "change-case"
44
import Seam, { SeamAPIError } from "../.."
55
import { GlobalOptions } from "./global-options"
6+
import _ from "lodash"
67

78
type ParametersByPath<Path extends string> = Parameters<
89
Exclude<Get<Seam, Path>, Seam>
@@ -32,7 +33,16 @@ const executeCommand = async <MethodPath extends string>(
3233
.start()
3334
}
3435

35-
const seam = new Seam(executeArgs["api-key"])
36+
const seam = new Seam(
37+
_.omitBy(
38+
{
39+
apiKey: executeArgs["api-key"],
40+
endpoint: executeArgs["endpoint"],
41+
workspaceId: executeArgs["workspace-id"],
42+
},
43+
_.isUndefined
44+
)
45+
)
3646

3747
let method: any = seam
3848
for (const path of methodName.split(".")) {

src/cli/lib/global-options.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,29 @@ export const getParserWithOptions = (yargsInstance: Argv) =>
1515
describe: "Output JSON",
1616
type: "boolean",
1717
})
18-
.group(["api-key", "quiet", "json", "help", "version"], "Global Options:")
18+
.option("endpoint", {
19+
describe: "Seam API Endpoint (defaults to https://connect.getseam.com)",
20+
hidden: true,
21+
type: "string",
22+
})
23+
.option("workspace-id", {
24+
describe:
25+
"Workspace Id to perform action against (only if using session key auth)",
26+
hidden: true,
27+
type: "string",
28+
})
29+
.group(
30+
[
31+
"api-key",
32+
"endpoint",
33+
"workspace-id",
34+
"quiet",
35+
"json",
36+
"help",
37+
"version",
38+
],
39+
"Global Options:"
40+
)
1941

2042
export type YargsWithGlobalOptions = ReturnType<typeof getParserWithOptions>
2143
type ExtractGeneric<T> = T extends Argv<infer X> ? X : never

src/index.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,50 @@ import { SeamAPIError } from "./lib/api-error"
33
import { Routes } from "./routes"
44
import { ErroredAPIResponse, SuccessfulAPIResponse } from "./types/globals"
55

6+
export interface SeamClientOptions {
7+
/* Seam API Key */
8+
apiKey?: string
9+
/**
10+
* Seam Endpoint to use, defaults to https://connect.getseam.com
11+
**/
12+
endpoint?: string
13+
/**
14+
* Workspace if using session authentication, defaults to SEAM_WORKSPACE_ID
15+
* or undefined
16+
**/
17+
workspaceId?: string
18+
}
19+
20+
export const getSeamClientOptionsWithDefaults = (
21+
apiKeyOrOptions?: string | SeamClientOptions
22+
): SeamClientOptions => {
23+
const seamClientDefaults: SeamClientOptions = {
24+
apiKey: process?.env?.SEAM_API_KEY,
25+
endpoint: process?.env?.SEAM_API_URL || "https://connect.getseam.com",
26+
workspaceId: process?.env?.SEAM_WORKSPACE_ID,
27+
}
28+
if (typeof apiKeyOrOptions === "string") {
29+
return { ...seamClientDefaults, apiKey: apiKeyOrOptions }
30+
} else {
31+
return { ...seamClientDefaults, ...apiKeyOrOptions }
32+
}
33+
}
34+
635
class Seam extends Routes {
736
private client: AxiosInstance
837

9-
constructor(
10-
apiKey?: string,
11-
endpoint = process?.env?.SEAM_API_URL || "https://connect.getseam.com"
12-
) {
38+
constructor(apiKeyOrOptions?: string | SeamClientOptions) {
1339
super()
1440

15-
if (!apiKey) {
16-
apiKey = process.env.SEAM_API_KEY
17-
}
41+
const options = getSeamClientOptionsWithDefaults(apiKeyOrOptions)
42+
const { apiKey, endpoint, workspaceId } = options
43+
44+
const isRegularAPIKey = apiKey?.startsWith("seam_")
45+
46+
if (isRegularAPIKey && workspaceId)
47+
throw new Error(
48+
"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."
49+
)
1850

1951
if (!apiKey) {
2052
throw new Error(
@@ -26,6 +58,9 @@ class Seam extends Routes {
2658
baseURL: endpoint,
2759
headers: {
2860
Authorization: `Bearer ${apiKey}`,
61+
62+
// only needed for session key authentication
63+
...(!workspaceId ? {} : { "Seam-Workspace": workspaceId }),
2964
},
3065
})
3166
}

tests

Submodule tests updated from 2f23a39 to 70adac6

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@
417417
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
418418
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==
419419

420+
"@types/lodash@^4.14.179":
421+
version "4.14.179"
422+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.179.tgz#490ec3288088c91295780237d2497a3aa9dfb5c5"
423+
integrity sha512-uwc1x90yCKqGcIOAT6DwOSuxnrAbpkdPsUOZtwrXb4D/6wZs+6qG7QnIawDuZWg0sWpxl+ltIKCaLoMlna678w==
424+
420425
"@types/minimatch@*":
421426
version "3.0.5"
422427
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"

0 commit comments

Comments
 (0)