@salesforce/lds-adapters-apex 1.100.1

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