Line data Source code
1 : import type { ClientFunctionBinding } from '../connector_descriptor.js';
2 : import { ClientFunctionError } from '../clfn/clfn_spec.js';
3 : import { loadClientFunctionSpec } from '../clfn/clfn_specs.js';
4 : import { requireChannel } from '../query_channel.js';
5 : import type { SalesforceConnectionStateDetails } from './salesforce_connection_state.js';
6 :
7 : /// The API version of the Data 360 spec
8 1 : const DATA_360_API_VERSION = '68.0';
9 :
10 : /// The host and the token of a request
11 1 : function target(instanceUrl: string | undefined, token: string | undefined, host: string) {
12 1 : if (!instanceUrl || !token) {
13 0 : throw new ClientFunctionError(`the Salesforce connection has no URL or token for the host ${host}`);
14 : }
15 1 : return {
16 1 : baseUrl: new URL(instanceUrl),
17 1 : headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
18 1 : };
19 1 : }
20 :
21 : /// The Data 360 API on Salesforce connections, see extensions/d360-api/clfn/docs/overview.md.
22 : ///
23 : /// The sources conflict on `connect`. It takes the core host and token, as the metadata calls of the connection do,
24 : /// until a check on an org settles it.
25 1 : export const SALESFORCE_CLIENT_FUNCTIONS: ClientFunctionBinding<SalesforceConnectionStateDetails> = {
26 1 : loadSpec: () => loadClientFunctionSpec('d360-api'),
27 1 : variables: () => ({ api_version: DATA_360_API_VERSION }),
28 1 : resolveHost: async (details, host) => {
29 1 : const { coreToken, dataCloudToken } = requireChannel(details.channel, 'Salesforce');
30 1 : switch (host) {
31 1 : case 'core':
32 1 : case 'connect':
33 1 : return target(coreToken.instanceUrl, coreToken.accessToken, host);
34 1 : case 'tenant':
35 : // The service would only answer with a 401
36 1 : if (Date.parse(dataCloudToken.expiresAt) <= Date.now()) {
37 1 : throw new ClientFunctionError('the Data 360 token of the Salesforce connection has expired, connect again');
38 : }
39 1 : return target(dataCloudToken.instanceUrl, dataCloudToken.jwt?.raw, host);
40 1 : default:
41 1 : throw new ClientFunctionError(`Salesforce connections have no host ${host}`);
42 1 : }
43 1 : },
44 1 : };
|