1 | |
2 |
|
3 | export type DynSelect_table = string |
4 |
|
5 | function authHeader(auth: RT.Servicenow) { |
6 | return auth.token |
7 | ? `Bearer ${auth.token}` |
8 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}` |
9 | } |
10 |
|
11 | |
12 | export async function table(auth: RT.Servicenow) { |
13 | const response = await fetch( |
14 | `${auth.instance_url}/api/now/table/sys_db_object?sysparm_fields=name,label&sysparm_limit=10000`, |
15 | { |
16 | headers: { |
17 | Authorization: authHeader(auth), |
18 | Accept: "application/json", |
19 | }, |
20 | } |
21 | ) |
22 | if (!response.ok) { |
23 | throw new Error(`${response.status} ${await response.text()}`) |
24 | } |
25 | const { result } = (await response.json()) as { |
26 | result: { name: string; label: string }[] |
27 | } |
28 | return result |
29 | .filter((t) => t.name) |
30 | .map((t) => ({ value: t.name, label: `${t.label} (${t.name})` })) |
31 | .sort((a, b) => a.label.localeCompare(b.label)) |
32 | } |
33 |
|
34 | |
35 | * Delete Record |
36 | * Delete a record by sys_id. ServiceNow returns 204 No Content on success, so this returns {success, sys_id}. |
37 | */ |
38 | export async function main( |
39 | auth: RT.Servicenow, |
40 | table: DynSelect_table, |
41 | sys_id: string |
42 | ) { |
43 | const url = new URL(`${auth.instance_url}/api/now/table/${table}/${sys_id}`) |
44 |
|
45 | const response = await fetch(url, { |
46 | method: "DELETE", |
47 | headers: { |
48 | Authorization: authHeader(auth), |
49 | Accept: "application/json", |
50 | }, |
51 | }) |
52 |
|
53 | if (!response.ok) { |
54 | throw new Error(`${response.status} ${await response.text()}`) |
55 | } |
56 |
|
57 | return { success: true, sys_id } |
58 | } |
59 |
|