1 | |
2 | * create resource |
3 | * |
4 | */ |
5 | export async function main( |
6 | workspace: string, |
7 | update_if_exists: string | undefined, |
8 | body: { |
9 | path: string; |
10 | value: unknown; |
11 | description?: string; |
12 | resource_type: string; |
13 | [k: string]: unknown; |
14 | } |
15 | ) { |
16 | const url = new URL(`${BASE_URL}/api/w/${workspace}/resources/create`); |
17 | for (const [k, v] of [["update_if_exists", update_if_exists]]) { |
18 | if (v !== undefined && v !== "") { |
19 | url.searchParams.append(k, v); |
20 | } |
21 | } |
22 | const response = await fetch(url, { |
23 | method: "POST", |
24 | headers: { |
25 | "Content-Type": "application/json", |
26 | Authorization: "Bearer " + WM_TOKEN, |
27 | }, |
28 | body: JSON.stringify(body), |
29 | }); |
30 | if (!response.ok) { |
31 | const text = await response.text(); |
32 | throw new Error(`${response.status} ${text}`); |
33 | } |
34 | return await response.text(); |
35 | } |
36 |
|