1 | |
2 | type Gitbook = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Search in a site |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Gitbook, |
11 | organizationId: string, |
12 | siteId: string, |
13 | page: string | undefined, |
14 | limit: string | undefined, |
15 | body: |
16 | | ({ query: string } & { |
17 | scope: |
18 | | { mode: "default"; includedSiteSpaces?: string[] } |
19 | | { mode: "specific"; siteSpaceIds: string[] } |
20 | | { mode: "all" }; |
21 | }) |
22 | | ({ query: string } & { mode?: "specific"; siteSpaceIds: string[] }) |
23 | | ({ query: string } & { mode: "current"; siteSpaceId: string }) |
24 | | ({ query: string } & { mode: "all" }), |
25 | ) { |
26 | const url = new URL( |
27 | `https://api.gitbook.com/v1/orgs/${organizationId}/sites/${siteId}/search`, |
28 | ); |
29 | for (const [k, v] of [ |
30 | ["page", page], |
31 | ["limit", limit], |
32 | ]) { |
33 | if (v !== undefined && v !== "" && k !== undefined) { |
34 | url.searchParams.append(k, v); |
35 | } |
36 | } |
37 | const response = await fetch(url, { |
38 | method: "POST", |
39 | headers: { |
40 | "Content-Type": "application/json", |
41 | Authorization: "Bearer " + auth.token, |
42 | }, |
43 | body: JSON.stringify(body), |
44 | }); |
45 | if (!response.ok) { |
46 | const text = await response.text(); |
47 | throw new Error(`${response.status} ${text}`); |
48 | } |
49 | return await response.json(); |
50 | } |
51 |
|