@conduit-client/salesforce-lightning-service-worker 3.6.3 → 3.7.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.
@@ -0,0 +1,12 @@
1
+ import type { RequestInterceptor } from '@conduit-client/service-fetch-network/v1';
2
+ import { CsrfTokenManager } from './token-manager';
3
+ export interface HeaderInterceptorConfig {
4
+ protectedUrls?: string[];
5
+ }
6
+ /**
7
+ * Builds a configured interceptor for applying CSRF header to requests
8
+ *
9
+ * @param csrfTokenManager
10
+ * @param config
11
+ */
12
+ export declare function buildInterceptor(csrfTokenManager: CsrfTokenManager, config?: HeaderInterceptorConfig): RequestInterceptor;
@@ -0,0 +1,7 @@
1
+ import type { FetchParameters } from '@conduit-client/service-fetch-network/v1';
2
+ import { type RetryService } from '@conduit-client/service-retry/v1';
3
+ import { HeaderInterceptorConfig } from './header.interceptor';
4
+ import { CsrfTokenManager } from './token-manager';
5
+ export interface RetryInterceptorConfig extends HeaderInterceptorConfig {
6
+ }
7
+ export declare function buildInterceptor(csrfTokenManager: CsrfTokenManager, config?: RetryInterceptorConfig): (fetchArgs: FetchParameters, retryService?: RetryService<Response>) => Promise<Response>;
@@ -0,0 +1,37 @@
1
+ import { RetryPolicy, type RetryContext } from '@conduit-client/service-retry/v1';
2
+ import { CsrfTokenManager } from './token-manager';
3
+ /**
4
+ * Retry policy that handles CSRF token expiration errors.
5
+ *
6
+ * When a request fails with a CSRF token error this policy will:
7
+ * 1. Detect the error condition in shouldRetry
8
+ * 2. Refresh the CSRF token in prepareRetry hook
9
+ * 3. Retry the request once (maxRetries = 1)
10
+ *
11
+ */
12
+ export declare class CsrfTokenRetryPolicy extends RetryPolicy<Response> {
13
+ private csrfTokenManager;
14
+ constructor(csrfTokenManager: CsrfTokenManager);
15
+ /**
16
+ * Determines if a failed request should be retried due to CSRF token issues.
17
+ */
18
+ shouldRetry(result: Response, context: RetryContext<Response>): Promise<boolean>;
19
+ /**
20
+ * CSRF token refresh should happen immediately with no delay.
21
+ */
22
+ calculateDelay(_result: Response, _context: RetryContext<Response>): Promise<number>;
23
+ /**
24
+ * Called by retry service before each retry attempt.
25
+ *
26
+ * @param _result - The failed response that triggered the retry (unused, already validated)
27
+ * @param _context - Current retry context (unused but part of interface)
28
+ */
29
+ prepareRetry(_result: Response, _context: RetryContext<Response>): Promise<void>;
30
+ }
31
+ /**
32
+ * Helper to check if a response indicates a CSRF token error.
33
+ *
34
+ * @param response - The response to check
35
+ * @returns true if the response indicates a CSRF token error (INVALID_ACCESS_TOKEN)
36
+ */
37
+ export declare function isCsrfError(response: Response): Promise<boolean>;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Manages CSRF token fetching and caching for secure requests.
3
+ * Implements a singleton pattern to ensure consistent token management across the application.
4
+ */
5
+ declare class CsrfTokenManager {
6
+ private endpoint;
7
+ private cacheName?;
8
+ private tokenPromise;
9
+ constructor(endpoint: string, cacheName?: string | undefined);
10
+ /**
11
+ * Returns the current token value as a Promise.
12
+ * Lazy-loads the token on first call (from cache or by fetching).
13
+ */
14
+ getToken(): Promise<string>;
15
+ /**
16
+ * Obtains and returns a new token value as a promise.
17
+ * This will clear the cached token and fetch a fresh one.
18
+ */
19
+ refreshToken(): Promise<string | undefined>;
20
+ /**
21
+ * Obtains a CSRF token, using cache when available or fetching a new one.
22
+ *
23
+ * @returns Promise that resolves to the CSRF token string
24
+ */
25
+ private obtainToken;
26
+ /**
27
+ * Provides a safe way to interact with the Cache API with fallback for unsupported environments.
28
+ *
29
+ * @param callback - Function that receives the cache instance and returns a promise
30
+ * @returns The result of the callback, or undefined if caches API is not available
31
+ */
32
+ private withCache;
33
+ }
34
+ export { CsrfTokenManager };
@@ -1,31 +1,19 @@
1
- import { Interceptors } from '@conduit-client/service-fetch-network/v1';
2
- /**
3
- * Configuration options for the Lightning fetch creation.
4
- */
5
- export interface LightningFetchConfig {
6
- interceptors?: Interceptors<void>;
7
- /**
8
- * Optional source for CSRF tokens. Can be:
9
- * - string: URL path to token endpoint (e.g., '/custom/csrf-endpoint')
10
- * - URL: Full URL object for token endpoint
11
- * - function: Custom async function that returns a token string
12
- *
13
- * As a string or URL, default fetching and caching (if Cache API is
14
- * available) will be used to obtain tokens
15
- */
16
- csrfTokenSource?: string | URL | (() => Promise<string>);
17
- /**
18
- * Optional callback for firing events related to fetch operations.
19
- * Can be used for instrumentation, logging, and monitoring.
20
- */
21
- fireEvent?: (eventName: string, id: string, data?: unknown) => void;
1
+ import { FetchService } from '@conduit-client/service-fetch-network/v1';
2
+ import { RetryInterceptorConfig } from './csrf/retry.interceptor';
3
+ interface CsrfConfig extends RetryInterceptorConfig {
4
+ endpoint: string;
5
+ cacheName: string;
6
+ }
7
+ export interface FetchConfig {
8
+ csrf: CsrfConfig;
22
9
  }
23
10
  /**
24
11
  * Creates an enhanced fetch function with automatic CSRF token handling.
25
12
  * The returned function automatically adds CSRF tokens to protected requests
26
13
  * and handles token refresh when tokens become invalid.
27
14
  *
28
- * @param config - Optional configuration object
15
+ * @param config - Optional configuration for CSRF handling
29
16
  * @returns An enhanced fetch function that handles CSRF protection
30
17
  */
31
- export declare function createLightningFetch(config?: LightningFetchConfig): typeof fetch;
18
+ export declare function createFetchService(config: FetchConfig): FetchService;
19
+ export {};
@@ -1,3 +1,7 @@
1
+ import { FetchConfig } from './fetch';
2
+ type ConduitClientConfig = {
3
+ serviceWorkerUrl?: string;
4
+ } & FetchConfig;
1
5
  /**
2
6
  * A client for making HTTP requests with CSRF protection. By default, protection is provided by
3
7
  * wrapping the native `fetch` API with functionality that will apply a CSRF token to appropriate
@@ -8,7 +12,6 @@
8
12
  * to `registerServiceWorker` and `defineServiceWorker`
9
13
  */
10
14
  export declare class ConduitClient {
11
- private constructor();
12
15
  /**
13
16
  * Makes an HTTP request
14
17
  *
@@ -18,17 +21,17 @@ export declare class ConduitClient {
18
21
  */
19
22
  fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
20
23
  /**
21
- * Factory method to create a new ConduitClient instance
24
+ * Configures global settings for all ConduitClient instances.
25
+ * This will recreate the internal fetch service with the new configuration.
22
26
  *
23
- * @returns A new ConduitClient instance
27
+ * @param config - Configuration options for CSRF handling and other features
24
28
  */
25
- static create(): ConduitClient;
29
+ static initialize(config: ConduitClientConfig): Promise<void>;
26
30
  /**
27
- * Registers a service worker for enhanced CSRF protection and caching.
28
- * When successfully registered, the client will switch to using native fetch
29
- * as the service worker will handle CSRF protection.
31
+ * Factory method to create a new ConduitClient instance
30
32
  *
31
- * @param scriptURL - path to the service worker script
33
+ * @returns A new ConduitClient instance
32
34
  */
33
- static registerServiceWorker(scriptURL: string): Promise<void>;
35
+ static instance(): ConduitClient;
34
36
  }
37
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@conduit-client/salesforce-lightning-service-worker",
3
- "version": "3.6.3",
3
+ "version": "3.7.0",
4
4
  "private": false,
5
5
  "description": "Service worker for accessing Salesforce data",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "watch": "npm run build --watch"
33
33
  },
34
34
  "dependencies": {
35
- "@conduit-client/service-fetch-network": "3.6.3"
35
+ "@conduit-client/service-fetch-network": "3.7.0"
36
36
  },
37
37
  "volta": {
38
38
  "extends": "../../../package.json"
@@ -40,7 +40,7 @@
40
40
  "size-limit": [
41
41
  {
42
42
  "path": "dist/index.js",
43
- "limit": "2.82 kB"
43
+ "limit": "3.14 kB"
44
44
  }
45
45
  ]
46
46
  }