Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
76 lines (68 loc) · 2.11 KB
/
Copy pathapi.ts
File metadata and controls
76 lines (68 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {
type SeamHttp,
SeamHttpRequest,
type SeamHttpRequestOptions,
} from '@seamapi/http'
import type { AuthContext } from 'lib/context.js'
import { getSeam } from './client.js'
export interface ApiRequestOptions extends Pick<
SeamHttpRequestOptions,
'waitForActionAttempt'
> {
path: string
params: Record<string, unknown>
/** Response key documented for the endpoint, e.g., `devices`. */
responseKey?: string | null | undefined
}
/**
* A prepared call to the Seam API: inspectable before it is sent, e.g., to
* report the URL, then executed through the SDK.
*/
export interface SeamApiRequest {
readonly url: URL
readonly method: string
readonly body: unknown
/** Return the full response body without automatic waiting. */
fetchResponse: () => Promise<unknown>
/** Return the response resource with SDK automatic action-attempt waiting. */
execute: () => Promise<unknown>
}
/**
* How the blueprint-driven CLI reaches the Seam API: prepare a request for
* an endpoint path. Tests fake at this port with `createMemorySeamApi()` —
* the in-process mirror of the e2e suite's HTTP server.
*/
export interface SeamApi {
createRequest: (options: ApiRequestOptions) => SeamApiRequest
}
/** The only place `SeamHttp` appears for raw requests. */
export class HttpSeamApi implements SeamApi {
constructor(private readonly seam: SeamHttp) {}
createRequest = ({
path,
params,
responseKey,
waitForActionAttempt,
}: ApiRequestOptions): SeamApiRequest =>
new SeamHttpRequest<Record<string, unknown>, string | undefined>(
this.seam,
{
pathname: path,
method: 'POST',
body: params,
responseKey: responseKey ?? undefined,
options:
waitForActionAttempt === undefined ? {} : { waitForActionAttempt },
actionAttempts: {
get: (params) =>
this.seam.actionAttempts.get(params, {
waitForActionAttempt: false,
}),
},
},
)
}
export const createSeamApi = async (auth?: AuthContext): Promise<SeamApi> => {
const seam = await getSeam(auth)
return new HttpSeamApi(seam)
}