1 | |
2 | type Confluence = { |
3 | email: string |
4 | apiToken: string |
5 | domain: string |
6 | } |
7 | |
8 | * Find relationship from source to target |
9 | * Find whether a particular type of relationship exists from a source |
10 | entity to a target entity. Note, relationships are one way. |
11 |
|
12 | For example, you can use this method to find whether the current user has |
13 | selected a particular page as a favorite (i.e. 'save for later'): |
14 | `GET /wiki/rest/api/relation/favourite/from/user/current/to/content/123` |
15 |
|
16 | **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: |
17 | Permission to view both the target entity and source entity. |
18 | */ |
19 | export async function main( |
20 | auth: Confluence, |
21 | relationName: string, |
22 | sourceType: 'user' | 'content' | 'space', |
23 | sourceKey: string, |
24 | targetType: 'user' | 'content' | 'space', |
25 | targetKey: string, |
26 | sourceStatus: string | undefined, |
27 | targetStatus: string | undefined, |
28 | sourceVersion: string | undefined, |
29 | targetVersion: string | undefined, |
30 | expand: string | undefined |
31 | ) { |
32 | const url = new URL( |
33 | `https://${auth.domain}/wiki/rest/api/relation/${relationName}/from/${sourceType}/${sourceKey}/to/${targetType}/${targetKey}` |
34 | ) |
35 | for (const [k, v] of [ |
36 | ['sourceStatus', sourceStatus], |
37 | ['targetStatus', targetStatus], |
38 | ['sourceVersion', sourceVersion], |
39 | ['targetVersion', targetVersion], |
40 | ['expand', expand] |
41 | ]) { |
42 | if (v !== undefined && v !== '' && k !== undefined) { |
43 | url.searchParams.append(k, v) |
44 | } |
45 | } |
46 | const response = await fetch(url, { |
47 | method: 'GET', |
48 | headers: { |
49 | Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`) |
50 | }, |
51 | body: undefined |
52 | }) |
53 | if (!response.ok) { |
54 | const text = await response.text() |
55 | throw new Error(`${response.status} ${text}`) |
56 | } |
57 | return await response.json() |
58 | } |
59 |
|