1 | |
2 |
|
3 | export type DynSelect_sobject = string |
4 | export type DynMultiselect_fields = string[] |
5 |
|
6 | |
7 | export async function sobject(auth: RT.Salesforce) { |
8 | const apiVersion = auth.api_version || "v60.0" |
9 | const response = await fetch( |
10 | `${auth.instance_url}/services/data/${apiVersion}/sobjects/`, |
11 | { |
12 | headers: { |
13 | Authorization: `Bearer ${auth.token}`, |
14 | Accept: "application/json", |
15 | }, |
16 | } |
17 | ) |
18 | if (!response.ok) { |
19 | throw new Error(`${response.status} ${await response.text()}`) |
20 | } |
21 | const { sobjects } = (await response.json()) as { |
22 | sobjects: { name: string; label: string }[] |
23 | } |
24 | return sobjects |
25 | .map((o) => ({ value: o.name, label: `${o.label} (${o.name})` })) |
26 | .sort((a, b) => a.label.localeCompare(b.label)) |
27 | } |
28 |
|
29 | |
30 | export async function fields(auth: RT.Salesforce, sobject: DynSelect_sobject) { |
31 | if (!sobject) return [] |
32 | const apiVersion = auth.api_version || "v60.0" |
33 | const response = await fetch( |
34 | `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`, |
35 | { |
36 | headers: { |
37 | Authorization: `Bearer ${auth.token}`, |
38 | Accept: "application/json", |
39 | }, |
40 | } |
41 | ) |
42 | if (!response.ok) { |
43 | throw new Error(`${response.status} ${await response.text()}`) |
44 | } |
45 | const { fields: fieldList } = (await response.json()) as { |
46 | fields: { name: string; label: string }[] |
47 | } |
48 | return fieldList |
49 | .map((f) => ({ value: f.name, label: `${f.label} (${f.name})` })) |
50 | .sort((a, b) => a.label.localeCompare(b.label)) |
51 | } |
52 |
|
53 | |
54 | * Get Record |
55 | * Retrieve a single record by ID. Optionally restrict the returned columns to a chosen set of fields. |
56 | */ |
57 | export async function main( |
58 | auth: RT.Salesforce, |
59 | sobject: DynSelect_sobject, |
60 | record_id: string, |
61 | fields: DynMultiselect_fields | undefined |
62 | ) { |
63 | const apiVersion = auth.api_version || "v60.0" |
64 | const url = new URL( |
65 | `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/${record_id}` |
66 | ) |
67 | if (fields && fields.length > 0) { |
68 | url.searchParams.append("fields", fields.join(",")) |
69 | } |
70 |
|
71 | const response = await fetch(url, { |
72 | method: "GET", |
73 | headers: { |
74 | Authorization: `Bearer ${auth.token}`, |
75 | Accept: "application/json", |
76 | }, |
77 | }) |
78 |
|
79 | if (!response.ok) { |
80 | throw new Error(`${response.status} ${await response.text()}`) |
81 | } |
82 |
|
83 | return await response.json() |
84 | } |
85 |
|