@salesforce/lds-network-fetch 1.412.1 → 1.414.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.
@@ -12,7 +12,7 @@
12
12
  * *******************************************************************************************
13
13
  */
14
14
  /* proxy-compat-disable */
15
- import { UIAPI_GET_LAYOUT, UIAPI_GET_LAYOUT_USER_STATE, UIAPI_OBJECT_INFO_BATCH_PATH, UIAPI_OBJECT_INFO_PATH, shouldForceRefresh, createOkResponse as createOkResponse$1, layoutUserStateStorage, layoutUserStateStorageStatsLogger, layoutStorage, layoutStorageStatsLogger, objectInfoStorage, objectInfoStorageStatsLogger, getTransactionKey as getTransactionKey$1 } from 'force/ldsNetwork';
15
+ import { UIAPI_GET_LAYOUT, UIAPI_GET_LAYOUT_USER_STATE, UIAPI_OBJECT_INFO_BATCH_PATH, UIAPI_OBJECT_INFO_PATH, shouldForceRefresh, layoutUserStateStorage, layoutUserStateStorageStatsLogger, layoutStorage, layoutStorageStatsLogger, objectInfoStorage, objectInfoStorageStatsLogger, getTransactionKey as getTransactionKey$1, createOkResponse as createOkResponse$1 } from 'force/ldsNetwork';
16
16
  import { HttpStatusCode as HttpStatusCode$1 } from 'force/luvioEngine';
17
17
 
18
18
  var SnapshotState;
@@ -270,7 +270,7 @@ const { join, push, unshift } = Array.prototype;
270
270
  const { isArray } = Array;
271
271
  const { entries, keys } = Object;
272
272
 
273
- const UI_API_BASE_URI = '/services/data/v66.0/ui-api';
273
+ const UI_API_BASE_URI = '/services/data/v67.0/ui-api';
274
274
 
275
275
  const LDS_RECORDS_AGGREGATE_UI = 'LDS_Records_AggregateUi';
276
276
  // Boundary which represents the limit that we start chunking at,
@@ -739,61 +739,24 @@ function getStorageAndLogger(path) {
739
739
  return null;
740
740
  }
741
741
  const platformFetchAdapter = platformNetworkAdapter(fetchNetworkAdapter);
742
- const platformFetchAdapterWithInterceptors = async (resourceRequest, resourceRequestContext, requestInterceptors) => {
743
- const pending = requestInterceptors.reduce((previousPromise, interceptor) => previousPromise.then(interceptor), resolvedPromiseLike(resourceRequest));
742
+ const platformFetchAdapterWithInterceptors = async (resourceRequest, resourceRequestContext, requestInterceptors, interceptorContext) => {
743
+ const pending = requestInterceptors.reduce((previousPromise, interceptor) => previousPromise.then((resourceRequest) => interceptor(resourceRequest, interceptorContext)), resolvedPromiseLike(resourceRequest));
744
744
  return pending.then((interceptedRequest) => platformFetchAdapter(interceptedRequest, resourceRequestContext));
745
745
  };
746
- /**
747
- * Helper function to fetch data, if available in aura storage cache returns that instead of going to network.
748
- * @param resourceRequest - {@link ResourceRequest}
749
- * @param resourceRequestContext {@link ResourceRequestContext}
750
- * @param storageAndLogger - Object containing a reference to aura storage/logger
751
- * @param transactionKey - The transaction key, derived from the resource request params.
752
- * @returns Promise<FetchResponse<any> - Promise that resolves to a fetch response.
753
- */
754
- function checkLdsStorageCacheOrFetch(resourceRequest, resourceRequestContext, storageAndLogger, transactionKey, requestInterceptors = []) {
746
+ async function getDurableCacheDataForIncomingRequest(resourceRequest, storageAndLogger, transactionKey) {
755
747
  if (!shouldForceRefresh(resourceRequest)) {
756
748
  if (storageAndLogger?.storage) {
757
749
  return storageAndLogger.storage.get(transactionKey).then((cacheResult) => {
758
750
  if (cacheResult !== undefined) {
759
751
  storageAndLogger.statsLogger.logHits();
760
- // This creates an AuraFetchResponse, should it just be FetchResponse?
761
- return createOkResponse$1(cacheResult);
762
752
  }
763
753
  storageAndLogger.statsLogger.logMisses();
764
- return platformFetchAdapterWithInterceptors(resourceRequest, resourceRequestContext, requestInterceptors);
765
- }, () => {
766
- return platformFetchAdapterWithInterceptors(resourceRequest, resourceRequestContext, requestInterceptors);
754
+ return cacheResult;
767
755
  });
768
756
  }
769
757
  }
770
- return platformFetchAdapterWithInterceptors(resourceRequest, resourceRequestContext, requestInterceptors);
771
- }
772
- // Copied from requestId generation on core
773
- function generateRequestId() {
774
- function guid() {
775
- function s4() {
776
- return Math.floor((1 + Math.random()) * 0x10000)
777
- .toString(16)
778
- .substring(1);
779
- }
780
- return s4() + s4();
781
- }
782
- return [
783
- Math.round(window.performance.now() * 1000000),
784
- guid(),
785
- Math.round(Math.random() * 100000000),
786
- ]
787
- .join('')
788
- .substring(0, 18);
789
758
  }
790
759
 
791
- // noop implementation for when one is not provided
792
- let internalRequestTracker = {
793
- hasTracker: () => false,
794
- registerHandler: (_request, _name, _loadedCheck) => { },
795
- markFinished: (_request) => { },
796
- };
797
760
  // noop implementation for when one is not provided
798
761
  let internalRequestLogger = {
799
762
  resolve: (_request, _response) => { },
@@ -815,18 +778,22 @@ function setupFetchNetworkAdapter() {
815
778
  * @param resourceRequestContext {@link ResourceRequestContext}
816
779
  * @returns Promise<FetchResponse<any>>
817
780
  */
818
- function handleSendFetchRequest(resourceRequest, resourceRequestContext, interceptors) {
781
+ async function handleSendFetchRequest(resourceRequest, resourceRequestContext, interceptors) {
819
782
  const { baseUri, basePath } = resourceRequest;
820
783
  const path = `${baseUri}${basePath}`;
821
784
  const storageAndLogger = getStorageAndLogger(path);
822
785
  const transactionKey = getTransactionKey$1(resourceRequest);
823
- const trackerId = generateRequestId();
824
- const response = checkLdsStorageCacheOrFetch(resourceRequest, resourceRequestContext, storageAndLogger, transactionKey, interceptors.request);
825
- internalRequestTracker.registerHandler(trackerId, 'lds-inflight-network-request', () => {
826
- // return false until we signal we are finished
827
- return false;
828
- });
829
- const interceptedResponse = Promise.resolve(interceptors.response.reduce((previousPromise, interceptor) => previousPromise.then(interceptor), resolvedPromiseLike(response)));
786
+ // Create context per request if factory is provided
787
+ const context = interceptors.createContext?.();
788
+ const { request: requestInterceptors = [], response: responseInterceptors = [], finally: finallyInterceptors = [], } = interceptors;
789
+ const cachedData = await getDurableCacheDataForIncomingRequest(resourceRequest, storageAndLogger, transactionKey);
790
+ if (cachedData !== undefined) {
791
+ return createOkResponse$1(cachedData);
792
+ }
793
+ const response = platformFetchAdapterWithInterceptors(resourceRequest, resourceRequestContext, requestInterceptors, context);
794
+ const interceptedResponse = Promise.resolve(responseInterceptors.reduce((previousPromise, interceptor) => previousPromise.then((response) => interceptor(response, context)), resolvedPromiseLike(response)));
795
+ // This can probably be refactored once we add an error interceptor pattern to onestore, and then
796
+ // the intial then here can also most likely be moved to a response interceptor as well
830
797
  interceptedResponse
831
798
  .then((value) => {
832
799
  if (storageAndLogger !== null && storageAndLogger.storage !== null) {
@@ -838,22 +805,19 @@ function handleSendFetchRequest(resourceRequest, resourceRequestContext, interce
838
805
  internalRequestLogger.reject(resourceRequest, error);
839
806
  });
840
807
  return interceptedResponse.finally(() => {
841
- internalRequestTracker.markFinished(trackerId);
808
+ return finallyInterceptors.reduce((previousPromise, interceptor) => previousPromise.then(() => interceptor(context)), Promise.resolve());
842
809
  });
843
810
  }
844
811
  /**
845
812
  * Wrapper around fetch network adapter from luvio
846
813
  *
847
- * @param requestTracker
848
814
  * @param requestLogger
849
815
  * @returns lexNetworkAdapter {@link NetworkAdapter}
850
816
  */
851
- function setupLexNetworkAdapter(requestTracker, requestLogger, interceptors) {
852
- internalRequestTracker =
853
- requestTracker && requestTracker.hasTracker() ? requestTracker : internalRequestTracker;
817
+ function setupLexNetworkAdapter(requestLogger, interceptors) {
854
818
  internalRequestLogger = requestLogger || internalRequestLogger;
855
819
  return async (resourceRequest, resourceRequestContext) => {
856
- return handleSendFetchRequest(resourceRequest, resourceRequestContext, interceptors ?? { request: [], response: [] });
820
+ return handleSendFetchRequest(resourceRequest, resourceRequestContext, interceptors ?? {});
857
821
  };
858
822
  }
859
823
 
@@ -875,5 +839,5 @@ function instrument(newInstrumentation) {
875
839
  Object.assign(instrumentation, newInstrumentation);
876
840
  }
877
841
 
878
- export { generateRequestId, instrument, internalRequestTracker, setupFetchNetworkAdapter, setupLexNetworkAdapter };
879
- // version: 1.412.1-054d9a1111
842
+ export { instrument, setupFetchNetworkAdapter, setupLexNetworkAdapter };
843
+ // version: 1.414.0-ff06871479
@@ -1,5 +1,4 @@
1
- export { internalRequestTracker, setupFetchNetworkAdapter, setupLexNetworkAdapter, } from './networkAdapter';
2
- export type { RequestLogger, RequestTracker } from './networkAdapter';
3
- export { generateRequestId } from './utils';
1
+ export { setupFetchNetworkAdapter, setupLexNetworkAdapter } from './networkAdapter';
2
+ export type { RequestLogger } from './networkAdapter';
4
3
  export type { RequestInterceptor, ResponseInterceptor, Interceptors } from './utils';
5
4
  export { instrument, FetchNetworkInstrumentation } from './instrumentation';
@@ -1,19 +1,9 @@
1
1
  import type { NetworkAdapter, ResourceRequest } from '@luvio/engine';
2
2
  import { Interceptors } from './utils';
3
- export type RequestTracker = {
4
- hasTracker: () => boolean;
5
- registerHandler: (request: any, name: string, loadedCheck: () => boolean) => void;
6
- markFinished: (request: any) => void;
7
- };
8
- export declare let internalRequestTracker: RequestTracker;
9
3
  export type RequestLogger = {
10
4
  resolve: (request: ResourceRequest, response: any) => void;
11
5
  reject: (request: ResourceRequest, error: Error) => void;
12
6
  };
13
- export declare function getTrackerAndLoggerForTest(): {
14
- tracker: RequestTracker;
15
- logger: RequestLogger;
16
- };
17
7
  /**
18
8
  * Wrapper around fetch network adapter from luvio
19
9
  *
@@ -23,8 +13,7 @@ export declare function setupFetchNetworkAdapter(): NetworkAdapter;
23
13
  /**
24
14
  * Wrapper around fetch network adapter from luvio
25
15
  *
26
- * @param requestTracker
27
16
  * @param requestLogger
28
17
  * @returns lexNetworkAdapter {@link NetworkAdapter}
29
18
  */
30
- export declare function setupLexNetworkAdapter(requestTracker?: RequestTracker, requestLogger?: RequestLogger, interceptors?: Interceptors): NetworkAdapter;
19
+ export declare function setupLexNetworkAdapter(requestLogger?: RequestLogger, interceptors?: Interceptors): NetworkAdapter;
@@ -11,20 +11,15 @@ type LdsStorageConfig = {
11
11
  * @returns LdsStorageConfig if we cache that resource, otherwise null.
12
12
  */
13
13
  export declare function getStorageAndLogger(path: string): LdsStorageConfig | null;
14
- export type RequestInterceptor = (fetchArgs: ResourceRequest) => PromiseLike<ResourceRequest>;
15
- export type ResponseInterceptor = (response: FetchResponse<any>) => PromiseLike<FetchResponse<any>>;
16
- export type Interceptors = {
17
- request: RequestInterceptor[];
18
- response: ResponseInterceptor[];
14
+ export type RequestInterceptor<Context = any> = (fetchArgs: ResourceRequest, context?: Context) => PromiseLike<ResourceRequest>;
15
+ export type ResponseInterceptor<Context = any> = (response: FetchResponse<any>, context?: Context) => PromiseLike<FetchResponse<any>>;
16
+ export type FinallyInterceptor<Context = any> = (context?: Context) => PromiseLike<void> | void;
17
+ export type Interceptors<Context = any> = {
18
+ createContext?: () => Context;
19
+ request?: RequestInterceptor<Context>[];
20
+ response?: ResponseInterceptor<Context>[];
21
+ finally?: FinallyInterceptor<Context>[];
19
22
  };
20
- /**
21
- * Helper function to fetch data, if available in aura storage cache returns that instead of going to network.
22
- * @param resourceRequest - {@link ResourceRequest}
23
- * @param resourceRequestContext {@link ResourceRequestContext}
24
- * @param storageAndLogger - Object containing a reference to aura storage/logger
25
- * @param transactionKey - The transaction key, derived from the resource request params.
26
- * @returns Promise<FetchResponse<any> - Promise that resolves to a fetch response.
27
- */
28
- export declare function checkLdsStorageCacheOrFetch(resourceRequest: ResourceRequest, resourceRequestContext: ResourceRequestContext, storageAndLogger: LdsStorageConfig | null, transactionKey: string, requestInterceptors?: RequestInterceptor[]): Promise<FetchResponse<any>>;
29
- export declare function generateRequestId(): string;
23
+ export declare const platformFetchAdapterWithInterceptors: (resourceRequest: ResourceRequest, resourceRequestContext: ResourceRequestContext, requestInterceptors: RequestInterceptor[], interceptorContext: any) => Promise<FetchResponse<any>>;
24
+ export declare function getDurableCacheDataForIncomingRequest(resourceRequest: ResourceRequest, storageAndLogger: LdsStorageConfig | null, transactionKey: string): Promise<unknown>;
30
25
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/lds-network-fetch",
3
- "version": "1.412.1",
3
+ "version": "1.414.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "description": "LDS Network Adapter using fetch",
6
6
  "main": "dist/ldsNetworkFetch.js",
@@ -38,6 +38,6 @@
38
38
  "@luvio/network-adapter-fetch": "0.158.7"
39
39
  },
40
40
  "devDependencies": {
41
- "@salesforce/lds-network-aura": "^1.412.1"
41
+ "@salesforce/lds-network-aura": "^1.414.0"
42
42
  }
43
43
  }