@salesforce/lds-adapters-apex 0.131.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +82 -0
- package/dist/es/es2018/apex-service.js +668 -0
- package/dist/es/es2018/types/src/generated/adapters/adapter-utils.d.ts +66 -0
- package/dist/es/es2018/types/src/generated/resources/getByApexMethodAndApexClass.d.ts +16 -0
- package/dist/es/es2018/types/src/generated/resources/postByApexMethodAndApexClass.d.ts +14 -0
- package/dist/es/es2018/types/src/generated/types/ApexMethodExecuteErrorResponse.d.ts +30 -0
- package/dist/es/es2018/types/src/generated/types/ApexMethodExecuteRequest.d.ts +26 -0
- package/dist/es/es2018/types/src/generated/types/type-utils.d.ts +39 -0
- package/dist/es/es2018/types/src/lds-apex-static-utils.d.ts +8 -0
- package/dist/es/es2018/types/src/main.d.ts +3 -0
- package/dist/es/es2018/types/src/sfdc.d.ts +7 -0
- package/dist/es/es2018/types/src/types.d.ts +10 -0
- package/dist/es/es2018/types/src/util/language.d.ts +19 -0
- package/dist/es/es2018/types/src/util/shared.d.ts +44 -0
- package/dist/es/es2018/types/src/util/utils.d.ts +26 -0
- package/dist/es/es2018/types/src/wire/getApex/index.d.ts +13 -0
- package/dist/es/es2018/types/src/wire/postApex/index.d.ts +10 -0
- package/package.json +56 -0
- package/sfdc/index.d.ts +1 -0
- package/sfdc/index.js +659 -0
- package/sfdc/lds-apex-static-utils.js +63 -0
- package/src/raml/api.raml +139 -0
- package/src/raml/luvio.raml +7 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* For full license text, see the LICENSE.txt file
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { serializeStructuredKey, StoreKeyMap, StoreKeySet } from '@luvio/engine';
|
|
8
|
+
|
|
9
|
+
const { keys: ObjectKeys$1, freeze: ObjectFreeze$1, create: ObjectCreate$1 } = Object;
|
|
10
|
+
const { stringify: JSONStringify } = JSON;
|
|
11
|
+
const { isArray: ArrayIsArray$1 } = Array;
|
|
12
|
+
function untrustedIsObject(untrusted) {
|
|
13
|
+
return typeof untrusted === 'object' && untrusted !== null && ArrayIsArray$1(untrusted) === false;
|
|
14
|
+
}
|
|
15
|
+
const snapshotRefreshOptions = {
|
|
16
|
+
overrides: {
|
|
17
|
+
headers: {
|
|
18
|
+
'Cache-Control': 'no-cache',
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A deterministic JSON stringify implementation. Heavily adapted from https://github.com/epoberezkin/fast-json-stable-stringify.
|
|
24
|
+
* This is needed because insertion order for JSON.stringify(object) affects output:
|
|
25
|
+
* JSON.stringify({a: 1, b: 2})
|
|
26
|
+
* "{"a":1,"b":2}"
|
|
27
|
+
* JSON.stringify({b: 2, a: 1})
|
|
28
|
+
* "{"b":2,"a":1}"
|
|
29
|
+
* @param data Data to be JSON-stringified.
|
|
30
|
+
* @returns JSON.stringified value with consistent ordering of keys.
|
|
31
|
+
*/
|
|
32
|
+
function stableJSONStringify$1(node) {
|
|
33
|
+
// This is for Date values.
|
|
34
|
+
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
35
|
+
// eslint-disable-next-line no-param-reassign
|
|
36
|
+
node = node.toJSON();
|
|
37
|
+
}
|
|
38
|
+
if (node === undefined) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (typeof node === 'number') {
|
|
42
|
+
return isFinite(node) ? '' + node : 'null';
|
|
43
|
+
}
|
|
44
|
+
if (typeof node !== 'object') {
|
|
45
|
+
return JSONStringify(node);
|
|
46
|
+
}
|
|
47
|
+
let i;
|
|
48
|
+
let out;
|
|
49
|
+
if (ArrayIsArray$1(node)) {
|
|
50
|
+
out = '[';
|
|
51
|
+
for (i = 0; i < node.length; i++) {
|
|
52
|
+
if (i) {
|
|
53
|
+
out += ',';
|
|
54
|
+
}
|
|
55
|
+
out += stableJSONStringify$1(node[i]) || 'null';
|
|
56
|
+
}
|
|
57
|
+
return out + ']';
|
|
58
|
+
}
|
|
59
|
+
if (node === null) {
|
|
60
|
+
return 'null';
|
|
61
|
+
}
|
|
62
|
+
const keys = ObjectKeys$1(node).sort();
|
|
63
|
+
out = '';
|
|
64
|
+
for (i = 0; i < keys.length; i++) {
|
|
65
|
+
const key = keys[i];
|
|
66
|
+
const value = stableJSONStringify$1(node[key]);
|
|
67
|
+
if (!value) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (out) {
|
|
71
|
+
out += ',';
|
|
72
|
+
}
|
|
73
|
+
out += JSONStringify(key) + ':' + value;
|
|
74
|
+
}
|
|
75
|
+
return '{' + out + '}';
|
|
76
|
+
}
|
|
77
|
+
const keyPrefix = 'Apex';
|
|
78
|
+
|
|
79
|
+
function createResourceRequest$1(config) {
|
|
80
|
+
const headers = {};
|
|
81
|
+
const header_xSFDCAllowContinuation = config.headers.xSFDCAllowContinuation;
|
|
82
|
+
if (header_xSFDCAllowContinuation !== undefined) {
|
|
83
|
+
headers['X-SFDC-Allow-Continuation'] = header_xSFDCAllowContinuation;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
baseUri: '/lwr/apex/v58.0',
|
|
87
|
+
basePath: '/' + config.urlParams.apexClass + '/' + config.urlParams.apexMethod + '',
|
|
88
|
+
method: 'get',
|
|
89
|
+
body: null,
|
|
90
|
+
urlParams: config.urlParams,
|
|
91
|
+
queryParams: config.queryParams,
|
|
92
|
+
headers,
|
|
93
|
+
priority: 'normal',
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const { create, keys, values } = Object;
|
|
98
|
+
const { hasOwnProperty } = Object.prototype;
|
|
99
|
+
const { isArray } = Array;
|
|
100
|
+
const { stringify } = JSON;
|
|
101
|
+
|
|
102
|
+
const { freeze: ObjectFreeze, keys: ObjectKeys, create: ObjectCreate, assign: ObjectAssign } = Object;
|
|
103
|
+
const { isArray: ArrayIsArray } = Array;
|
|
104
|
+
function deepFreeze(value) {
|
|
105
|
+
// No need to freeze primitives
|
|
106
|
+
if (typeof value !== 'object' || value === null) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (ArrayIsArray(value)) {
|
|
110
|
+
for (let i = 0, len = value.length; i < len; i += 1) {
|
|
111
|
+
deepFreeze(value[i]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const keys = ObjectKeys(value);
|
|
116
|
+
for (let i = 0, len = keys.length; i < len; i += 1) {
|
|
117
|
+
deepFreeze(value[keys[i]]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
ObjectFreeze(value);
|
|
121
|
+
}
|
|
122
|
+
function createLink(ref) {
|
|
123
|
+
return {
|
|
124
|
+
__ref: serializeStructuredKey(ref),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const CACHE_CONTROL = 'cache-control';
|
|
129
|
+
const SHARED_ADAPTER_CONTEXT_ID = 'apex__shared';
|
|
130
|
+
// eslint-disable-next-line @salesforce/lds/no-invalid-todo
|
|
131
|
+
// TODO: APEX_TTL, apexResponseEquals, apexResponseIngest, and validateAdapterConfig should have been code generated
|
|
132
|
+
// however compiler does not support response body type any so hand roll for now
|
|
133
|
+
/**
|
|
134
|
+
* Time to live for the Apex cache value. 5 minutes.
|
|
135
|
+
*/
|
|
136
|
+
const APEX_TTL = 5 * 60 * 1000;
|
|
137
|
+
// apex is essentially versionless, we can never know the shape of apex data
|
|
138
|
+
// so we will rely on components to code defensively. All apex data will be ingested
|
|
139
|
+
// and looked up with this version
|
|
140
|
+
const APEX_VERSION = 'APEX_V_1';
|
|
141
|
+
const APEX_STORE_METADATA_PARAMS = {
|
|
142
|
+
ttl: APEX_TTL,
|
|
143
|
+
namespace: keyPrefix,
|
|
144
|
+
representationName: '',
|
|
145
|
+
version: APEX_VERSION,
|
|
146
|
+
};
|
|
147
|
+
function apexResponseEquals(existing, incoming) {
|
|
148
|
+
return stringify(incoming) === stringify(existing);
|
|
149
|
+
}
|
|
150
|
+
const apexResponseIngest = (input, path, luvio, store) => {
|
|
151
|
+
// skip validation and normalization, since input type is any
|
|
152
|
+
const key = path.fullPath;
|
|
153
|
+
const incomingRecord = input;
|
|
154
|
+
const existingRecord = store.readEntry(key);
|
|
155
|
+
// freeze on ingest (luvio.opaque)
|
|
156
|
+
deepFreeze(incomingRecord);
|
|
157
|
+
if (existingRecord === undefined ||
|
|
158
|
+
apexResponseEquals(existingRecord, incomingRecord) === false) {
|
|
159
|
+
luvio.storePublish(key, incomingRecord);
|
|
160
|
+
}
|
|
161
|
+
luvio.publishStoreMetadata(key, APEX_STORE_METADATA_PARAMS);
|
|
162
|
+
return createLink(key);
|
|
163
|
+
};
|
|
164
|
+
function validateAdapterConfig(untrustedConfig) {
|
|
165
|
+
if (untrustedIsObject(untrustedConfig)) {
|
|
166
|
+
const values$1 = values(untrustedConfig);
|
|
167
|
+
return values$1.indexOf(undefined) === -1 ? untrustedConfig : null;
|
|
168
|
+
}
|
|
169
|
+
return untrustedConfig;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A standard delimiter when producing cache keys.
|
|
173
|
+
*/
|
|
174
|
+
const KEY_DELIM = ':';
|
|
175
|
+
function isEmptyParam(param) {
|
|
176
|
+
return (param === undefined ||
|
|
177
|
+
param === null ||
|
|
178
|
+
(typeof param === 'object' && keys(param).length === 0));
|
|
179
|
+
}
|
|
180
|
+
function keyBuilder(classname, method, isContinuation, params) {
|
|
181
|
+
return [
|
|
182
|
+
classname.replace('__', KEY_DELIM),
|
|
183
|
+
method,
|
|
184
|
+
isContinuation,
|
|
185
|
+
isEmptyParam(params) ? '' : stableJSONStringify$1(params),
|
|
186
|
+
].join(KEY_DELIM);
|
|
187
|
+
}
|
|
188
|
+
function configBuilder(config, classname, method, isContinuation) {
|
|
189
|
+
return {
|
|
190
|
+
apexMethod: method,
|
|
191
|
+
apexClass: classname,
|
|
192
|
+
methodParams: config,
|
|
193
|
+
xSFDCAllowContinuation: isContinuation + '',
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function apexClassnameBuilder(namespace, classname) {
|
|
197
|
+
return namespace !== '' ? `${namespace}__${classname}` : classname;
|
|
198
|
+
}
|
|
199
|
+
function isCacheControlValueCacheable(value) {
|
|
200
|
+
if (value === undefined || value === null || typeof value !== 'string') {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
return value.indexOf('no-cache') < 0 && value.indexOf('no-store') < 0;
|
|
204
|
+
}
|
|
205
|
+
function getCacheControlHeaderValue(headers) {
|
|
206
|
+
if (headers === undefined) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
// header fields are case-insensitive according to
|
|
210
|
+
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
|
|
211
|
+
const headerKeys = keys(headers);
|
|
212
|
+
for (let i = 0, len = headerKeys.length; i < len; i += 1) {
|
|
213
|
+
const key = headerKeys[i];
|
|
214
|
+
if (key.toLowerCase() === CACHE_CONTROL) {
|
|
215
|
+
return headers[key];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
function shouldCache(response) {
|
|
221
|
+
const { headers } = response;
|
|
222
|
+
const headerValue = getCacheControlHeaderValue(headers);
|
|
223
|
+
return isCacheControlValueCacheable(headerValue);
|
|
224
|
+
}
|
|
225
|
+
function getCacheableKey(recordId) {
|
|
226
|
+
return `${recordId}_cacheable`;
|
|
227
|
+
}
|
|
228
|
+
function set(context, recordId, value) {
|
|
229
|
+
const key = getCacheableKey(recordId);
|
|
230
|
+
context.set(key, value);
|
|
231
|
+
}
|
|
232
|
+
function get(context, key) {
|
|
233
|
+
return context.get(getCacheableKey(key));
|
|
234
|
+
}
|
|
235
|
+
function setCacheControlAdapterContext(context, recordId, response) {
|
|
236
|
+
const { headers } = response;
|
|
237
|
+
const headerValue = getCacheControlHeaderValue(headers);
|
|
238
|
+
if (headerValue !== undefined) {
|
|
239
|
+
set(context, recordId, headerValue);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function isCacheable(config, context) {
|
|
243
|
+
const { apexClass, apexMethod, xSFDCAllowContinuation, methodParams } = config;
|
|
244
|
+
const recordId = keyBuilder(apexClass, apexMethod, xSFDCAllowContinuation, methodParams);
|
|
245
|
+
return checkAdapterContext(context, recordId);
|
|
246
|
+
}
|
|
247
|
+
function checkAdapterContext(context, recordId) {
|
|
248
|
+
const contextValue = get(context, recordId);
|
|
249
|
+
return isCacheControlValueCacheable(contextValue);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function createResourceParams$1(config) {
|
|
253
|
+
const queryParams = create(null);
|
|
254
|
+
if (!isEmptyParam(config.methodParams)) {
|
|
255
|
+
queryParams.methodParams = config.methodParams;
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
queryParams,
|
|
259
|
+
urlParams: {
|
|
260
|
+
apexMethod: config.apexMethod,
|
|
261
|
+
apexClass: config.apexClass,
|
|
262
|
+
},
|
|
263
|
+
headers: {
|
|
264
|
+
xSFDCAllowContinuation: config.xSFDCAllowContinuation,
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function keyBuilderFromResourceParams$1(params) {
|
|
269
|
+
let classname = params.urlParams.apexClass.replace('__', KEY_DELIM);
|
|
270
|
+
return [
|
|
271
|
+
classname,
|
|
272
|
+
params.urlParams.apexMethod,
|
|
273
|
+
params.headers.xSFDCAllowContinuation,
|
|
274
|
+
isEmptyParam(params.queryParams.methodParams)
|
|
275
|
+
? ''
|
|
276
|
+
: stableJSONStringify$1(params.queryParams.methodParams),
|
|
277
|
+
].join(KEY_DELIM);
|
|
278
|
+
}
|
|
279
|
+
function ingestSuccess$1(luvio, resourceParams, response, snapshotRefresh) {
|
|
280
|
+
const { body } = response;
|
|
281
|
+
const recordId = keyBuilderFromResourceParams$1(resourceParams);
|
|
282
|
+
const select = {
|
|
283
|
+
recordId,
|
|
284
|
+
node: { kind: 'Fragment', opaque: true, private: [], version: APEX_VERSION },
|
|
285
|
+
variables: {},
|
|
286
|
+
};
|
|
287
|
+
luvio.storeIngest(recordId, apexResponseIngest, body);
|
|
288
|
+
const snapshot = luvio.storeLookup(select, snapshotRefresh);
|
|
289
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
290
|
+
if (response.headers !== undefined && snapshot.state !== 'Fulfilled') {
|
|
291
|
+
throw new Error('Invalid network response. Expected resource response to result in Fulfilled snapshot');
|
|
292
|
+
}
|
|
293
|
+
if (!(snapshot.state === 'Fulfilled' || snapshot.state === 'Stale')) {
|
|
294
|
+
throw new Error('Invalid resource response. Expected resource response to result in Fulfilled or Stale snapshot');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return snapshot;
|
|
298
|
+
}
|
|
299
|
+
function buildCachedSnapshotCachePolicy$1(buildSnapshotContext, storeLookup) {
|
|
300
|
+
const { luvio, config, adapterContext } = buildSnapshotContext;
|
|
301
|
+
const { apexClass, apexMethod, xSFDCAllowContinuation, methodParams } = config;
|
|
302
|
+
const recordId = keyBuilder(apexClass, apexMethod, xSFDCAllowContinuation, methodParams);
|
|
303
|
+
return storeLookup({
|
|
304
|
+
recordId: recordId,
|
|
305
|
+
node: {
|
|
306
|
+
kind: 'Fragment',
|
|
307
|
+
opaque: true,
|
|
308
|
+
private: [],
|
|
309
|
+
version: APEX_VERSION,
|
|
310
|
+
},
|
|
311
|
+
variables: {},
|
|
312
|
+
}, {
|
|
313
|
+
config,
|
|
314
|
+
resolve: () => buildNetworkSnapshot$1(luvio, adapterContext, config, snapshotRefreshOptions),
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
function onFetchResponseSuccess$1(luvio, context, config, resourceParams, response) {
|
|
318
|
+
const recordId = keyBuilderFromResourceParams$1(resourceParams);
|
|
319
|
+
const select = {
|
|
320
|
+
recordId,
|
|
321
|
+
node: { kind: 'Fragment', opaque: true, private: [], version: APEX_VERSION },
|
|
322
|
+
variables: {},
|
|
323
|
+
};
|
|
324
|
+
setCacheControlAdapterContext(context, recordId, response);
|
|
325
|
+
if (shouldCache(response)) {
|
|
326
|
+
const snapshot = ingestSuccess$1(luvio, resourceParams, response, {
|
|
327
|
+
config,
|
|
328
|
+
resolve: () => buildNetworkSnapshot$1(luvio, context, config, snapshotRefreshOptions),
|
|
329
|
+
});
|
|
330
|
+
return luvio.storeBroadcast().then(() => snapshot);
|
|
331
|
+
}
|
|
332
|
+
// if Cache-Control is not set or set to 'no-cache', return a synthetic snapshot
|
|
333
|
+
return Promise.resolve({
|
|
334
|
+
recordId,
|
|
335
|
+
variables: {},
|
|
336
|
+
seenRecords: new StoreKeySet(),
|
|
337
|
+
select,
|
|
338
|
+
state: 'Fulfilled',
|
|
339
|
+
data: response.body,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
function onFetchResponseError$1(luvio, context, config, _resourceParams, response) {
|
|
343
|
+
return Promise.resolve(luvio.errorSnapshot(response, {
|
|
344
|
+
config,
|
|
345
|
+
resolve: () => buildNetworkSnapshot$1(luvio, context, config, snapshotRefreshOptions),
|
|
346
|
+
}));
|
|
347
|
+
}
|
|
348
|
+
function buildNetworkSnapshot$1(luvio, context, config, options) {
|
|
349
|
+
const resourceParams = createResourceParams$1(config);
|
|
350
|
+
const request = createResourceRequest$1(resourceParams);
|
|
351
|
+
return luvio.dispatchResourceRequest(request, options).then((response) => {
|
|
352
|
+
return luvio.handleSuccessResponse(() => onFetchResponseSuccess$1(luvio, context, config, resourceParams, response),
|
|
353
|
+
// TODO [W-10490362]: Properly generate the response cache keys
|
|
354
|
+
() => {
|
|
355
|
+
return new StoreKeyMap();
|
|
356
|
+
});
|
|
357
|
+
}, (response) => {
|
|
358
|
+
return luvio.handleErrorResponse(() => onFetchResponseError$1(luvio, context, config, resourceParams, response));
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
function buildNetworkSnapshotCachePolicy$1(context, coercedAdapterRequestContext) {
|
|
362
|
+
const { luvio, config, adapterContext } = context;
|
|
363
|
+
const { networkPriority, requestCorrelator, eventObservers } = coercedAdapterRequestContext;
|
|
364
|
+
const dispatchOptions = {
|
|
365
|
+
resourceRequestContext: {
|
|
366
|
+
requestCorrelator,
|
|
367
|
+
},
|
|
368
|
+
eventObservers,
|
|
369
|
+
};
|
|
370
|
+
if (networkPriority !== 'normal') {
|
|
371
|
+
dispatchOptions.overrides = {
|
|
372
|
+
priority: networkPriority,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
return buildNetworkSnapshot$1(luvio, adapterContext, config, dispatchOptions);
|
|
376
|
+
}
|
|
377
|
+
const factory = (luvio, invokerParams) => {
|
|
378
|
+
const { namespace, classname, method, isContinuation } = invokerParams;
|
|
379
|
+
return getApexAdapterFactory(luvio, namespace, classname, method, isContinuation);
|
|
380
|
+
};
|
|
381
|
+
function getApexAdapterFactory(luvio, namespace, classname, method, isContinuation) {
|
|
382
|
+
return luvio.withContext(function apex__getApex(untrustedConfig, context, requestContext) {
|
|
383
|
+
// Even though the config is of type `any`,
|
|
384
|
+
// validation is required here because `undefined`
|
|
385
|
+
// values on a wire mean that properties on the component
|
|
386
|
+
// used in the config have not been loaded yet.
|
|
387
|
+
const config = validateAdapterConfig(untrustedConfig);
|
|
388
|
+
// Invalid or incomplete config
|
|
389
|
+
if (config === null) {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
const configPlus = configBuilder(config, apexClassnameBuilder(namespace, classname), method, isContinuation);
|
|
393
|
+
return luvio.applyCachePolicy(requestContext || {}, { config: configPlus, luvio, adapterContext: context }, buildCachedSnapshotCachePolicy$1, buildNetworkSnapshotCachePolicy$1);
|
|
394
|
+
}, { contextId: SHARED_ADAPTER_CONTEXT_ID });
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* A deterministic JSON stringify implementation. Heavily adapted from https://github.com/epoberezkin/fast-json-stable-stringify.
|
|
399
|
+
* This is needed because insertion order for JSON.stringify(object) affects output:
|
|
400
|
+
* JSON.stringify({a: 1, b: 2})
|
|
401
|
+
* "{"a":1,"b":2}"
|
|
402
|
+
* JSON.stringify({b: 2, a: 1})
|
|
403
|
+
* "{"b":2,"a":1}"
|
|
404
|
+
* @param data Data to be JSON-stringified.
|
|
405
|
+
* @returns JSON.stringified value with consistent ordering of keys.
|
|
406
|
+
*/
|
|
407
|
+
function stableJSONStringify(node) {
|
|
408
|
+
// This is for Date values.
|
|
409
|
+
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
|
410
|
+
// eslint-disable-next-line no-param-reassign
|
|
411
|
+
node = node.toJSON();
|
|
412
|
+
}
|
|
413
|
+
if (node === undefined) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (typeof node === 'number') {
|
|
417
|
+
return isFinite(node) ? '' + node : 'null';
|
|
418
|
+
}
|
|
419
|
+
if (typeof node !== 'object') {
|
|
420
|
+
return stringify(node);
|
|
421
|
+
}
|
|
422
|
+
let i;
|
|
423
|
+
let out;
|
|
424
|
+
if (isArray(node)) {
|
|
425
|
+
out = '[';
|
|
426
|
+
for (i = 0; i < node.length; i++) {
|
|
427
|
+
if (i) {
|
|
428
|
+
out += ',';
|
|
429
|
+
}
|
|
430
|
+
out += stableJSONStringify(node[i]) || 'null';
|
|
431
|
+
}
|
|
432
|
+
return out + ']';
|
|
433
|
+
}
|
|
434
|
+
if (node === null) {
|
|
435
|
+
return 'null';
|
|
436
|
+
}
|
|
437
|
+
const keys$1 = keys(node).sort();
|
|
438
|
+
out = '';
|
|
439
|
+
for (i = 0; i < keys$1.length; i++) {
|
|
440
|
+
const key = keys$1[i];
|
|
441
|
+
const value = stableJSONStringify(node[key]);
|
|
442
|
+
if (!value) {
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (out) {
|
|
446
|
+
out += ',';
|
|
447
|
+
}
|
|
448
|
+
out += stringify(key) + ':' + value;
|
|
449
|
+
}
|
|
450
|
+
return '{' + out + '}';
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Returns the field API name, qualified with an object name if possible.
|
|
454
|
+
* @param value The value from which to get the qualified field API name.
|
|
455
|
+
* @return The qualified field API name.
|
|
456
|
+
*/
|
|
457
|
+
function getFieldApiName(value) {
|
|
458
|
+
if (typeof value === 'string') {
|
|
459
|
+
return value;
|
|
460
|
+
}
|
|
461
|
+
else if (value &&
|
|
462
|
+
typeof value.objectApiName === 'string' &&
|
|
463
|
+
typeof value.fieldApiName === 'string') {
|
|
464
|
+
return value.objectApiName + '.' + value.fieldApiName;
|
|
465
|
+
}
|
|
466
|
+
throw new TypeError('Value is not a string or FieldId.');
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Split the object API name and field API name from a qualified field name.
|
|
470
|
+
* Eg: Opportunity.Title returns ['Opportunity', 'Title']
|
|
471
|
+
* Eg: Opportunity.Account.Name returns ['Opportunity', 'Account.Name']
|
|
472
|
+
* @param fieldApiName The qualified field name.
|
|
473
|
+
* @return The object and field API names.
|
|
474
|
+
*/
|
|
475
|
+
function splitQualifiedFieldApiName(fieldApiName) {
|
|
476
|
+
const idx = fieldApiName.indexOf('.');
|
|
477
|
+
if (idx < 1) {
|
|
478
|
+
// object api name must non-empty
|
|
479
|
+
throw new TypeError('Value does not include an object API name.');
|
|
480
|
+
}
|
|
481
|
+
return [fieldApiName.substring(0, idx), fieldApiName.substring(idx + 1)];
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function createResourceRequest(config) {
|
|
485
|
+
const headers = {};
|
|
486
|
+
const header_xSFDCAllowContinuation = config.headers.xSFDCAllowContinuation;
|
|
487
|
+
if (header_xSFDCAllowContinuation !== undefined) {
|
|
488
|
+
headers['X-SFDC-Allow-Continuation'] = header_xSFDCAllowContinuation;
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
baseUri: '/lwr/apex/v58.0',
|
|
492
|
+
basePath: '/' + config.urlParams.apexClass + '/' + config.urlParams.apexMethod + '',
|
|
493
|
+
method: 'post',
|
|
494
|
+
body: config.body,
|
|
495
|
+
urlParams: config.urlParams,
|
|
496
|
+
queryParams: {},
|
|
497
|
+
headers,
|
|
498
|
+
priority: 'normal',
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function createResourceParams(config) {
|
|
503
|
+
return {
|
|
504
|
+
urlParams: {
|
|
505
|
+
apexMethod: config.apexMethod,
|
|
506
|
+
apexClass: config.apexClass,
|
|
507
|
+
},
|
|
508
|
+
body: config.methodParams,
|
|
509
|
+
headers: {
|
|
510
|
+
xSFDCAllowContinuation: config.xSFDCAllowContinuation,
|
|
511
|
+
},
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
function keyBuilderFromResourceParams(params) {
|
|
515
|
+
let classname = params.urlParams.apexClass.replace('__', KEY_DELIM);
|
|
516
|
+
return [
|
|
517
|
+
classname,
|
|
518
|
+
params.urlParams.apexMethod,
|
|
519
|
+
params.headers.xSFDCAllowContinuation,
|
|
520
|
+
isEmptyParam(params.body) ? '' : stableJSONStringify(params.body),
|
|
521
|
+
].join(KEY_DELIM);
|
|
522
|
+
}
|
|
523
|
+
function ingestSuccess(luvio, resourceParams, response, snapshotRefresh) {
|
|
524
|
+
const { body } = response;
|
|
525
|
+
const recordId = keyBuilderFromResourceParams(resourceParams);
|
|
526
|
+
const select = {
|
|
527
|
+
recordId,
|
|
528
|
+
node: { kind: 'Fragment', opaque: true, private: [], version: APEX_VERSION },
|
|
529
|
+
variables: {},
|
|
530
|
+
};
|
|
531
|
+
luvio.storeIngest(recordId, apexResponseIngest, body);
|
|
532
|
+
const snapshot = luvio.storeLookup(select, snapshotRefresh);
|
|
533
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
534
|
+
if (response.headers !== undefined && snapshot.state !== 'Fulfilled') {
|
|
535
|
+
throw new Error('Invalid network response. Expected resource response to result in Fulfilled snapshot');
|
|
536
|
+
}
|
|
537
|
+
if (!(snapshot.state === 'Fulfilled' || snapshot.state === 'Stale')) {
|
|
538
|
+
throw new Error('Invalid resource response. Expected resource response to result in Fulfilled or Stale snapshot');
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return snapshot;
|
|
542
|
+
}
|
|
543
|
+
function buildCachedSnapshotCachePolicy(buildSnapshotContext, storeLookup) {
|
|
544
|
+
const { config } = buildSnapshotContext;
|
|
545
|
+
const { apexClass, apexMethod, xSFDCAllowContinuation, methodParams } = config;
|
|
546
|
+
const recordId = keyBuilder(apexClass, apexMethod, xSFDCAllowContinuation, methodParams);
|
|
547
|
+
return storeLookup({
|
|
548
|
+
recordId: recordId,
|
|
549
|
+
node: {
|
|
550
|
+
kind: 'Fragment',
|
|
551
|
+
opaque: true,
|
|
552
|
+
private: [],
|
|
553
|
+
version: APEX_VERSION,
|
|
554
|
+
},
|
|
555
|
+
variables: {},
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function onFetchResponseSuccess(luvio, context, _config, resourceParams, response) {
|
|
559
|
+
const recordId = keyBuilderFromResourceParams(resourceParams);
|
|
560
|
+
const select = {
|
|
561
|
+
recordId,
|
|
562
|
+
node: { kind: 'Fragment', opaque: true, private: [], version: APEX_VERSION },
|
|
563
|
+
variables: {},
|
|
564
|
+
};
|
|
565
|
+
setCacheControlAdapterContext(context, recordId, response);
|
|
566
|
+
if (shouldCache(response)) {
|
|
567
|
+
const snapshot = ingestSuccess(luvio, resourceParams, response);
|
|
568
|
+
return luvio.storeBroadcast().then(() => snapshot);
|
|
569
|
+
}
|
|
570
|
+
// if Cache-Control is not set or set to 'no-cache', return a synthetic snapshot
|
|
571
|
+
return Promise.resolve({
|
|
572
|
+
recordId,
|
|
573
|
+
variables: {},
|
|
574
|
+
seenRecords: new StoreKeySet(),
|
|
575
|
+
select,
|
|
576
|
+
state: 'Fulfilled',
|
|
577
|
+
data: response.body,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
function onFetchResponseError(luvio, _context, _config, _resourceParams, response) {
|
|
581
|
+
return Promise.resolve(luvio.errorSnapshot(response));
|
|
582
|
+
}
|
|
583
|
+
function buildNetworkSnapshot(luvio, context, config, options) {
|
|
584
|
+
const resourceParams = createResourceParams(config);
|
|
585
|
+
const request = createResourceRequest(resourceParams);
|
|
586
|
+
return luvio.dispatchResourceRequest(request, options).then((response) => {
|
|
587
|
+
return luvio.handleSuccessResponse(() => onFetchResponseSuccess(luvio, context, config, resourceParams, response),
|
|
588
|
+
// TODO [W-10490362]: Properly generate response cache keys
|
|
589
|
+
() => {
|
|
590
|
+
return new StoreKeyMap();
|
|
591
|
+
});
|
|
592
|
+
}, (response) => {
|
|
593
|
+
return luvio.handleErrorResponse(() => onFetchResponseError(luvio, context, config, resourceParams, response));
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
function buildNetworkSnapshotCachePolicy(context, coercedAdapterRequestContext) {
|
|
597
|
+
const { luvio, config, adapterContext } = context;
|
|
598
|
+
const { networkPriority, requestCorrelator, eventObservers } = coercedAdapterRequestContext;
|
|
599
|
+
const dispatchOptions = {
|
|
600
|
+
resourceRequestContext: {
|
|
601
|
+
requestCorrelator,
|
|
602
|
+
},
|
|
603
|
+
eventObservers,
|
|
604
|
+
};
|
|
605
|
+
if (networkPriority !== 'normal') {
|
|
606
|
+
dispatchOptions.overrides = {
|
|
607
|
+
priority: networkPriority,
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
return buildNetworkSnapshot(luvio, adapterContext, config, dispatchOptions);
|
|
611
|
+
}
|
|
612
|
+
const invoker = (luvio, invokerParams) => {
|
|
613
|
+
const { namespace, classname, method, isContinuation } = invokerParams;
|
|
614
|
+
const ldsAdapter = postApexAdapterFactory(luvio, namespace, classname, method, isContinuation);
|
|
615
|
+
return getInvoker(ldsAdapter);
|
|
616
|
+
};
|
|
617
|
+
function getInvoker(ldsAdapter) {
|
|
618
|
+
return (config, requestContext) => {
|
|
619
|
+
const snapshotOrPromise = ldsAdapter(config, requestContext);
|
|
620
|
+
return Promise.resolve(snapshotOrPromise).then((snapshot) => {
|
|
621
|
+
if (snapshot.state === 'Error') {
|
|
622
|
+
throw snapshot.error;
|
|
623
|
+
}
|
|
624
|
+
return snapshot.data;
|
|
625
|
+
});
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
function postApexAdapterFactory(luvio, namespace, classname, method, isContinuation) {
|
|
629
|
+
return luvio.withContext(function apex__postApex(config, context, requestContext) {
|
|
630
|
+
// config validation is unnecessary for this imperative adapter
|
|
631
|
+
// due to the config being of type `any`.
|
|
632
|
+
// however, we have special config validation for the wire adapter,
|
|
633
|
+
// explanation in getApex
|
|
634
|
+
const configPlus = configBuilder(config, apexClassnameBuilder(namespace, classname), method, isContinuation);
|
|
635
|
+
// if this response isn't meant to be cached then pass a buildCached
|
|
636
|
+
// func that returns undefined, that way cache policy impls will know
|
|
637
|
+
// not to do any cache lookups
|
|
638
|
+
const buildCached = isCacheable(configPlus, context)
|
|
639
|
+
? buildCachedSnapshotCachePolicy
|
|
640
|
+
: () => undefined;
|
|
641
|
+
return luvio.applyCachePolicy(requestContext || {}, { config: configPlus, luvio, adapterContext: context }, buildCached, buildNetworkSnapshotCachePolicy);
|
|
642
|
+
}, { contextId: SHARED_ADAPTER_CONTEXT_ID });
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Gets a field value from an Apex sObject.
|
|
647
|
+
* @param sobject The sObject holding the field.
|
|
648
|
+
* @param field The qualified API name of the field to return.
|
|
649
|
+
* @returns The field's value. If it doesn't exist, undefined is returned.
|
|
650
|
+
*/
|
|
651
|
+
function getSObjectValue(sObject, field) {
|
|
652
|
+
if (untrustedIsObject(sObject) === false) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
const unqualifiedField = splitQualifiedFieldApiName(getFieldApiName(field))[1];
|
|
656
|
+
const fields = unqualifiedField.split('.');
|
|
657
|
+
let ret = sObject;
|
|
658
|
+
for (let i = 0, fieldsLength = fields.length; i < fieldsLength; i++) {
|
|
659
|
+
const nextField = fields[i];
|
|
660
|
+
if (!hasOwnProperty.call(ret, nextField)) {
|
|
661
|
+
return undefined;
|
|
662
|
+
}
|
|
663
|
+
ret = ret[nextField];
|
|
664
|
+
}
|
|
665
|
+
return ret;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
export { invoker as GetApexInvoker, factory as GetApexWireAdapterFactory, getSObjectValue };
|