1 | |
2 | type Gitbook = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get a space computed document |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Gitbook, |
11 | spaceId: string, |
12 | schema: "current" | "next" | undefined, |
13 | body: { |
14 | source: |
15 | | ({ |
16 | type: "builtin:openapi"; |
17 | dependencies: { |
18 | spec: |
19 | | { ref: { kind: "openapi"; spec: string } } |
20 | | { |
21 | ref: { kind: "openapi"; spec: string }; |
22 | value: { |
23 | object: "openapi-spec"; |
24 | id: string; |
25 | slug: string; |
26 | lastVersion?: string; |
27 | }; |
28 | }; |
29 | }; |
30 | } & { |
31 | props: |
32 | | { doc: "models" } |
33 | | { doc: "operations" | "info"; page: string }; |
34 | }) |
35 | | { |
36 | type: "builtin:translation"; |
37 | props: { space: string; document: string }; |
38 | } |
39 | | { type: string; props: {}; dependencies?: {} }; |
40 | seed: string; |
41 | }, |
42 | ) { |
43 | const url = new URL(`https://api.gitbook.com/v1/spaces/${spaceId}/content/computed/document`); |
44 | for (const [k, v] of [["schema", schema]]) { |
45 | if (v !== undefined && v !== "" && k !== undefined) { |
46 | url.searchParams.append(k, v); |
47 | } |
48 | } |
49 | const response = await fetch(url, { |
50 | method: "POST", |
51 | headers: { |
52 | "Content-Type": "application/json", |
53 | Authorization: "Bearer " + auth.token, |
54 | }, |
55 | body: JSON.stringify(body), |
56 | }); |
57 | if (!response.ok) { |
58 | const text = await response.text(); |
59 | throw new Error(`${response.status} ${text}`); |
60 | } |
61 | return await response.json(); |
62 | } |
63 |
|