1 | |
2 |
|
3 | function authHeader(auth: RT.Servicenow) { |
4 | return auth.token |
5 | ? `Bearer ${auth.token}` |
6 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}` |
7 | } |
8 |
|
9 | |
10 | * Get Current User |
11 | * Return the sys_user record of the authenticated user (resolves current_user, then fetches the full record from sys_user). |
12 | */ |
13 | export async function main(auth: RT.Servicenow) { |
14 | const currentResponse = await fetch( |
15 | `${auth.instance_url}/api/now/ui/user/current_user`, |
16 | { |
17 | method: "GET", |
18 | headers: { |
19 | Authorization: authHeader(auth), |
20 | Accept: "application/json", |
21 | }, |
22 | } |
23 | ) |
24 | if (!currentResponse.ok) { |
25 | throw new Error(`${currentResponse.status} ${await currentResponse.text()}`) |
26 | } |
27 | const { result } = (await currentResponse.json()) as { |
28 | result: { user_sys_id: string } |
29 | } |
30 |
|
31 | const userResponse = await fetch( |
32 | `${auth.instance_url}/api/now/table/sys_user/${result.user_sys_id}`, |
33 | { |
34 | method: "GET", |
35 | headers: { |
36 | Authorization: authHeader(auth), |
37 | Accept: "application/json", |
38 | }, |
39 | } |
40 | ) |
41 | if (!userResponse.ok) { |
42 | throw new Error(`${userResponse.status} ${await userResponse.text()}`) |
43 | } |
44 |
|
45 | return await userResponse.json() |
46 | } |
47 |
|