Skip to content

Commit 8decfeb

Browse files
committed
Setup basic structure
1 parent eb4a66d commit 8decfeb

File tree

5 files changed

+72
-47
lines changed

5 files changed

+72
-47
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
},
7373
"dependencies": {
7474
"@seamapi/types": "^1.14.0",
75-
"axios": "^1.5.0"
75+
"axios": "^1.5.0",
76+
"type-fest": "^4.3.1"
7677
},
7778
"devDependencies": {
7879
"@types/node": "^18.11.18",

src/lib/seam/connect/auth.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
import type {
2-
SeamHttpOptionsWithApiKey,
3-
SeamHttpOptionsWithClientSessionToken,
1+
import {
2+
InvalidSeamHttpOptionsError,
3+
isSeamHttpOptionsWithApiKey,
4+
isSeamHttpOptionsWithClientSessionToken,
5+
type SeamHttpOptions,
6+
type SeamHttpOptionsWithApiKey,
7+
type SeamHttpOptionsWithClientSessionToken,
48
} from './client-options.js'
59

610
type Headers = Record<string, string>
711

8-
export const getAuthHeadersForApiKey = ({
12+
export const getAuthHeaders = (options: SeamHttpOptions): Headers => {
13+
if (isSeamHttpOptionsWithApiKey(options)) {
14+
return getAuthHeadersForApiKey(options)
15+
}
16+
17+
if (isSeamHttpOptionsWithClientSessionToken(options)) {
18+
return getAuthHeadersForClientSessionToken(options)
19+
}
20+
21+
throw new InvalidSeamHttpOptionsError(
22+
'Must specify an apiKey or clientSessionToken',
23+
)
24+
}
25+
26+
const getAuthHeadersForApiKey = ({
927
apiKey,
1028
}: SeamHttpOptionsWithApiKey): Headers => {
1129
if (isClientSessionToken(apiKey)) {
@@ -31,7 +49,7 @@ export const getAuthHeadersForApiKey = ({
3149
}
3250
}
3351

34-
export const getAuthHeadersForClientSessionToken = ({
52+
const getAuthHeadersForClientSessionToken = ({
3553
clientSessionToken,
3654
}: SeamHttpOptionsWithClientSessionToken): Headers => {
3755
if (!isClientSessionToken(clientSessionToken)) {

src/lib/seam/connect/client.ts

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import axios, { type Axios } from 'axios'
22

3-
import {
4-
getAuthHeadersForApiKey,
5-
getAuthHeadersForClientSessionToken,
6-
} from './auth.js'
3+
import { getAuthHeaders } from './auth.js'
74
import {
85
InvalidSeamHttpOptionsError,
96
isSeamHttpOptionsWithApiKey,
@@ -12,6 +9,7 @@ import {
129
type SeamHttpOptionsWithApiKey,
1310
type SeamHttpOptionsWithClientSessionToken,
1411
} from './client-options.js'
12+
import { Workspaces } from './routes/workspaces.js'
1513

1614
export class SeamHttp {
1715
client: Axios
@@ -23,37 +21,14 @@ export class SeamHttp {
2321
: apiKeyOrOptions,
2422
)
2523

26-
const axiosOptions = {
24+
this.client = axios.create({
2725
baseURL: options.endpoint,
2826
...options.axiosOptions,
29-
headers: options.axiosOptions.headers ?? {},
30-
}
31-
32-
if (isSeamHttpOptionsWithApiKey(options)) {
33-
this.client = axios.create({
34-
...axiosOptions,
35-
headers: {
36-
...getAuthHeadersForApiKey(options),
37-
...axiosOptions.headers,
38-
},
39-
})
40-
return
41-
}
42-
43-
if (isSeamHttpOptionsWithClientSessionToken(options)) {
44-
this.client = axios.create({
45-
...axiosOptions,
46-
headers: {
47-
...getAuthHeadersForClientSessionToken(options),
48-
...axiosOptions.headers,
49-
},
50-
})
51-
return
52-
}
53-
54-
throw new InvalidSeamHttpOptionsError(
55-
'Must specify an apiKey or clientSessionToken',
56-
)
27+
headers: {
28+
...getAuthHeaders(options),
29+
...options.axiosOptions.headers,
30+
},
31+
})
5732
}
5833

5934
static fromApiKey(
@@ -81,13 +56,8 @@ export class SeamHttp {
8156
return new SeamHttp(opts)
8257
}
8358

84-
public readonly workspaces = {
85-
get: async (): Promise<{ workspace_id: string }> => {
86-
const {
87-
data: { workspace },
88-
} = await this.client.get('/workspaces/get')
89-
return workspace
90-
},
59+
get workspaces(): Workspaces {
60+
return new Workspaces(this.client)
9161
}
9262
}
9363

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Routes } from '@seamapi/types/connect'
2+
import type { Axios } from 'axios'
3+
import type { SetNonNullable } from 'type-fest'
4+
// import type { Workspace } from @seamapi/types
5+
6+
export class Workspaces {
7+
#client: Axios
8+
9+
constructor(client: Axios) {
10+
this.#client = client
11+
}
12+
13+
async get(params: WorkspacesGetParams = {}): Promise<Workspace> {
14+
const {
15+
data: { workspace },
16+
} = await this.#client.get<WorkspacesGetResponse>('/workspaces/get', {
17+
params,
18+
})
19+
return workspace
20+
}
21+
}
22+
23+
export type WorkspacesGetParams = SetNonNullable<
24+
Required<Routes['/workspaces/get']['commonParams']>
25+
>
26+
27+
export type WorkspacesGetResponse = SetNonNullable<
28+
Required<Routes['/workspaces/get']['jsonResponse']>
29+
>
30+
31+
// UPSTREAM: Should come from @seamapi/types/connect
32+
// export type { Workspace } from '@seamapi/types/connect'
33+
export interface Workspace {
34+
workspace_id: string
35+
}

0 commit comments

Comments
 (0)