1 | |
2 | type Confluence = { |
3 | email: string |
4 | apiToken: string |
5 | domain: string |
6 | } |
7 | |
8 | * Find source entities related to a target entity |
9 | * Returns all target entities that have a particular relationship to the |
10 | source entity. |
11 | */ |
12 | export async function main( |
13 | auth: Confluence, |
14 | relationName: string, |
15 | sourceType: 'user' | 'content' | 'space', |
16 | targetType: 'user' | 'content' | 'space', |
17 | targetKey: string, |
18 | sourceStatus: string | undefined, |
19 | targetStatus: string | undefined, |
20 | sourceVersion: string | undefined, |
21 | targetVersion: string | undefined, |
22 | expand: string | undefined, |
23 | start: string | undefined, |
24 | limit: string | undefined |
25 | ) { |
26 | const url = new URL( |
27 | `https://${auth.domain}/wiki/rest/api/relation/${relationName}/to/${targetType}/${targetKey}/from/${sourceType}` |
28 | ) |
29 | for (const [k, v] of [ |
30 | ['sourceStatus', sourceStatus], |
31 | ['targetStatus', targetStatus], |
32 | ['sourceVersion', sourceVersion], |
33 | ['targetVersion', targetVersion], |
34 | ['expand', expand], |
35 | ['start', start], |
36 | ['limit', limit] |
37 | ]) { |
38 | if (v !== undefined && v !== '' && k !== undefined) { |
39 | url.searchParams.append(k, v) |
40 | } |
41 | } |
42 | const response = await fetch(url, { |
43 | method: 'GET', |
44 | headers: { |
45 | Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`) |
46 | }, |
47 | body: undefined |
48 | }) |
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`${response.status} ${text}`) |
52 | } |
53 | return await response.json() |
54 | } |
55 |
|