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