@internetarchive/fetch-handler 1.1.0-webdev-7731.1 → 1.1.0-webdev-7731.3

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Build Status](https://github.com/internetarchive/iaux-fetch-handler-service/actions/workflows/ci.yml/badge.svg) [![codecov](https://codecov.io/gh/internetarchive/iaux-fetch-handler-service/graph/badge.svg?token=ZOYRJ2BV9W)](https://codecov.io/gh/internetarchive/iaux-fetch-handler-service)
1
+ ![Build Status](https://github.com/internetarchive/iaux-fetch-handler/actions/workflows/ci.yml/badge.svg) [![codecov](https://codecov.io/gh/internetarchive/iaux-fetch-handler/graph/badge.svg?token=ZOYRJ2BV9W)](https://codecov.io/gh/internetarchive/iaux-fetch-handler)
2
2
 
3
3
 
4
4
  # Internet Archive Fetch Handler library
@@ -10,14 +10,14 @@ A custom library for handling API requests.
10
10
  ## Installation
11
11
 
12
12
  ```bash
13
- npm install @internetarchive/iaux-fetch-handler
13
+ npm install @internetarchive/fetch-handler
14
14
  ```
15
15
 
16
16
 
17
17
  ## Sample Usage
18
18
 
19
19
  ```ts
20
- import { IaFetchHandler } from '../src/ia-fetch-handler';
20
+ import { IaFetchHandler } from '@internetarchive/fetch-handler';
21
21
 
22
22
  @property({ type: Object }) data: any = null;
23
23
  @property({ type: String }) error: string = '';
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export { DefaultRetryConfiguration } from './src/fetch-retry/configuration/defau
5
5
  export { NoRetryConfiguration } from './src/fetch-retry/configuration/no-retry-configuration';
6
6
  export type { RetryConfiguring } from './src/fetch-retry/configuration/retry-configuring';
7
7
  export type { FetchOptions } from './src/fetch-options';
8
+ export { RetryConfigs } from './src/fetch-retry/configuration/configurations';
package/dist/index.js CHANGED
@@ -2,4 +2,5 @@ export { IaFetchHandler } from './src/fetch-handler';
2
2
  export { FetchRetrier, } from './src/fetch-retry/fetch-retrier';
3
3
  export { DefaultRetryConfiguration } from './src/fetch-retry/configuration/default-retry-configuration';
4
4
  export { NoRetryConfiguration } from './src/fetch-retry/configuration/no-retry-configuration';
5
+ export { RetryConfigs } from './src/fetch-retry/configuration/configurations';
5
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,EACL,YAAY,GAEb,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6DAA6D,CAAC;AACxG,OAAO,EAAE,oBAAoB,EAAE,MAAM,wDAAwD,CAAC","sourcesContent":["export { IaFetchHandler } from './src/fetch-handler';\nexport { FetchHandlerInterface } from './src/fetch-handler-interface';\nexport {\n FetchRetrier,\n FetchRetrierInterface,\n} from './src/fetch-retry/fetch-retrier';\nexport { DefaultRetryConfiguration } from './src/fetch-retry/configuration/default-retry-configuration';\nexport { NoRetryConfiguration } from './src/fetch-retry/configuration/no-retry-configuration';\nexport type { RetryConfiguring } from './src/fetch-retry/configuration/retry-configuring';\nexport type { FetchOptions } from './src/fetch-options';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,EACL,YAAY,GAEb,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6DAA6D,CAAC;AACxG,OAAO,EAAE,oBAAoB,EAAE,MAAM,wDAAwD,CAAC;AAG9F,OAAO,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC","sourcesContent":["export { IaFetchHandler } from './src/fetch-handler';\nexport { FetchHandlerInterface } from './src/fetch-handler-interface';\nexport {\n FetchRetrier,\n FetchRetrierInterface,\n} from './src/fetch-retry/fetch-retrier';\nexport { DefaultRetryConfiguration } from './src/fetch-retry/configuration/default-retry-configuration';\nexport { NoRetryConfiguration } from './src/fetch-retry/configuration/no-retry-configuration';\nexport type { RetryConfiguring } from './src/fetch-retry/configuration/retry-configuring';\nexport type { FetchOptions } from './src/fetch-options';\nexport { RetryConfigs } from './src/fetch-retry/configuration/configurations';\n"]}
@@ -28,10 +28,19 @@ export interface FetchHandlerInterface {
28
28
  * of the full URL. If you need a full URL, use `fetchApiResponse` instead.
29
29
  *
30
30
  * @param path string
31
- * @param options?: { includeCredentials?: boolean, retryConfig?: RetryConfiguring }
31
+ * @param options?: {
32
+ * includeCredentials?: boolean;
33
+ * method?: string;
34
+ * body?: BodyInit;
35
+ * headers?: HeadersInit;
36
+ * retryConfig?: RetryConfiguring;
37
+ * }
32
38
  */
33
39
  fetchIAApiResponse<T>(path: string, options?: {
34
40
  includeCredentials?: boolean;
41
+ method?: string;
42
+ body?: BodyInit;
43
+ headers?: HeadersInit;
35
44
  retryConfig?: RetryConfiguring;
36
45
  }): Promise<T>;
37
46
  }
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-handler-interface.js","sourceRoot":"","sources":["../../src/fetch-handler-interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { FetchOptions } from './fetch-options';\nimport type { RetryConfiguring } from './fetch-retry/configuration/retry-configuring';\n\nexport interface FetchHandlerInterface {\n /**\n * Generic fetch function that handles retries and common IA parameters like `reCache=1`\n *\n * @param input RequestInfo\n * @param options RequestInit | FetchOptions\n */\n fetch(\n request: RequestInfo,\n options?: RequestInit | FetchOptions,\n ): Promise<Response>;\n\n /**\n * A helper function to fetch a response from an API and get a JSON object\n *\n * @param path string\n * @param options?: { includeCredentials?: boolean }\n */\n fetchApiResponse<T>(\n url: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T>;\n\n /**\n * A helper function to fetch a response from the IA API and get a JSON object\n *\n * This allows you to just pass the path to the API and get the response instead\n * of the full URL. If you need a full URL, use `fetchApiResponse` instead.\n *\n * @param path string\n * @param options?: { includeCredentials?: boolean, retryConfig?: RetryConfiguring }\n */\n fetchIAApiResponse<T>(\n path: string,\n options?: { includeCredentials?: boolean; retryConfig?: RetryConfiguring },\n ): Promise<T>;\n}\n"]}
1
+ {"version":3,"file":"fetch-handler-interface.js","sourceRoot":"","sources":["../../src/fetch-handler-interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { FetchOptions } from './fetch-options';\nimport type { RetryConfiguring } from './fetch-retry/configuration/retry-configuring';\n\nexport interface FetchHandlerInterface {\n /**\n * Generic fetch function that handles retries and common IA parameters like `reCache=1`\n *\n * @param input RequestInfo\n * @param options RequestInit | FetchOptions\n */\n fetch(\n request: RequestInfo,\n options?: RequestInit | FetchOptions,\n ): Promise<Response>;\n\n /**\n * A helper function to fetch a response from an API and get a JSON object\n *\n * @param path string\n * @param options?: { includeCredentials?: boolean }\n */\n fetchApiResponse<T>(\n url: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T>;\n\n /**\n * A helper function to fetch a response from the IA API and get a JSON object\n *\n * This allows you to just pass the path to the API and get the response instead\n * of the full URL. If you need a full URL, use `fetchApiResponse` instead.\n *\n * @param path string\n * @param options?: {\n * includeCredentials?: boolean;\n * method?: string;\n * body?: BodyInit;\n * headers?: HeadersInit;\n * retryConfig?: RetryConfiguring;\n * }\n */\n fetchIAApiResponse<T>(\n path: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T>;\n}\n"]}
@@ -21,6 +21,9 @@ export declare class IaFetchHandler implements FetchHandlerInterface {
21
21
  /** @inheritdoc */
22
22
  fetchIAApiResponse<T>(path: string, options?: {
23
23
  includeCredentials?: boolean;
24
+ method?: string;
25
+ body?: BodyInit;
26
+ headers?: HeadersInit;
24
27
  retryConfig?: RetryConfiguring;
25
28
  }): Promise<T>;
26
29
  /** @inheritdoc */
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-handler.js","sourceRoot":"","sources":["../../src/fetch-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAEb,MAAM,6BAA6B,CAAC;AAKrC;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IAOzB,YAAY,OAKX;QATO,iBAAY,GAA0B,IAAI,YAAY,EAAE,CAAC;QAU/D,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY;YAAE,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACpE,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY;YAAE,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACpE,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,EAAE,CAAC;YAC1B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QAC3C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;QAC7C,CAAC;IACH,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,kBAAkB,CACtB,IAAY,EACZ,OAGC;QAED,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,YAAY,GAAG,IAAI,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,gBAAgB,CACpB,GAAW,EACX,OAMC;QAED,MAAM,WAAW,GAAgB,EAAE,CAAC;QACpC,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,kBAAkB;YAAE,WAAW,CAAC,WAAW,GAAG,SAAS,CAAC;QACrE,IAAI,OAAO,aAAP,OAAO,uBA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M,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAErD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,GAAG,CAAC,IAAI,CAAC;IAClB,CAAC;CACF","sourcesContent":["import {\n FetchRetrier,\n FetchRetrierInterface,\n} from './fetch-retry/fetch-retrier';\nimport type { FetchHandlerInterface } from './fetch-handler-interface';\nimport type { FetchOptions } from './fetch-options';\nimport type { RetryConfiguring } from './fetch-retry/configuration/retry-configuring';\n\n/**\n * The FetchHandler adds some common helpers:\n * - retry the request if it fails\n * - add `reCache=1` to the request if it's in the current url so the backend sees it\n * - add convenience method for fetching/decoding an API response by just the path\n */\nexport class IaFetchHandler implements FetchHandlerInterface {\n private iaApiBaseUrl?: string;\n\n private fetchRetrier: FetchRetrierInterface = new FetchRetrier();\n\n private searchParams?: string;\n\n constructor(options?: {\n iaApiBaseUrl?: string;\n fetchRetrier?: FetchRetrierInterface;\n searchParams?: string;\n defaultRetryConfiguration?: RetryConfiguring;\n }) {\n if (options?.iaApiBaseUrl) this.iaApiBaseUrl = options.iaApiBaseUrl;\n if (options?.fetchRetrier) this.fetchRetrier = options.fetchRetrier;\n if (options?.searchParams) {\n this.searchParams = options.searchParams;\n } else {\n this.searchParams = window.location.search;\n }\n }\n\n /** @inheritdoc */\n async fetchIAApiResponse<T>(\n path: string,\n options?: {\n includeCredentials?: boolean;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T> {\n const url = `${this.iaApiBaseUrl}${path}`;\n return this.fetchApiResponse(url, options);\n }\n\n /** @inheritdoc */\n async fetchApiResponse<T>(\n url: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T> {\n const requestInit: RequestInit = {};\n if (options?.includeCredentials) requestInit.credentials = 'include';\n if (options?.method) requestInit.method = options.method;\n if (options?.body) requestInit.body = options.body;\n if (options?.headers) requestInit.headers = options.headers;\n const response = await this.fetch(url, {\n requestInit: requestInit,\n retryConfig: options?.retryConfig,\n });\n const json = await response.json();\n return json as T;\n }\n\n /** @inheritdoc */\n async fetch(\n request: RequestInfo,\n options?: RequestInit | FetchOptions,\n ): Promise<Response> {\n let finalRequest = request;\n const urlParams = new URLSearchParams(this.searchParams);\n if (urlParams.get('reCache') === '1') {\n const urlString = typeof request === 'string' ? request : request.url;\n finalRequest = this.addSearchParams(urlString, { reCache: '1' });\n }\n return this.fetchRetrier.fetchRetry(finalRequest, options);\n }\n\n /**\n * Since RequestInfo can be either a `Request` or `string`, we need to change\n * the way we add search params to it depending on the input.\n */\n private addSearchParams(\n urlString: string,\n params: Record<string, string>,\n ): string {\n const url = new URL(urlString, window.location.href);\n\n for (const [key, value] of Object.entries(params)) {\n url.searchParams.set(key, value);\n }\n\n return url.href;\n }\n}\n"]}
1
+ {"version":3,"file":"fetch-handler.js","sourceRoot":"","sources":["../../src/fetch-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAEb,MAAM,6BAA6B,CAAC;AAKrC;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IAOzB,YAAY,OAKX;QATO,iBAAY,GAA0B,IAAI,YAAY,EAAE,CAAC;QAU/D,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY;YAAE,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACpE,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY;YAAE,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACpE,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,EAAE,CAAC;YAC1B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC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C3B,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACzD,IAAI,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG,EAAE,CAAC;YACrC,MAAM,SAAS,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;YACtE,YAAY,GAAG,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;QACnE,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAC7D,CAAC;IAED;;;OAGG;IACK,eAAe,CACrB,SAAiB,EACjB,MAA8B;QAE9B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAErD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,GAAG,CAAC,IAAI,CAAC;IAClB,CAAC;CACF","sourcesContent":["import {\n FetchRetrier,\n FetchRetrierInterface,\n} from './fetch-retry/fetch-retrier';\nimport type { FetchHandlerInterface } from './fetch-handler-interface';\nimport type { FetchOptions } from './fetch-options';\nimport type { RetryConfiguring } from './fetch-retry/configuration/retry-configuring';\n\n/**\n * The FetchHandler adds some common helpers:\n * - retry the request if it fails\n * - add `reCache=1` to the request if it's in the current url so the backend sees it\n * - add convenience method for fetching/decoding an API response by just the path\n */\nexport class IaFetchHandler implements FetchHandlerInterface {\n private iaApiBaseUrl?: string;\n\n private fetchRetrier: FetchRetrierInterface = new FetchRetrier();\n\n private searchParams?: string;\n\n constructor(options?: {\n iaApiBaseUrl?: string;\n fetchRetrier?: FetchRetrierInterface;\n searchParams?: string;\n defaultRetryConfiguration?: RetryConfiguring;\n }) {\n if (options?.iaApiBaseUrl) this.iaApiBaseUrl = options.iaApiBaseUrl;\n if (options?.fetchRetrier) this.fetchRetrier = options.fetchRetrier;\n if (options?.searchParams) {\n this.searchParams = options.searchParams;\n } else {\n this.searchParams = window.location.search;\n }\n }\n\n /** @inheritdoc */\n async fetchIAApiResponse<T>(\n path: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T> {\n const url = `${this.iaApiBaseUrl}${path}`;\n return this.fetchApiResponse(url, options);\n }\n\n /** @inheritdoc */\n async fetchApiResponse<T>(\n url: string,\n options?: {\n includeCredentials?: boolean;\n method?: string;\n body?: BodyInit;\n headers?: HeadersInit;\n retryConfig?: RetryConfiguring;\n },\n ): Promise<T> {\n const requestInit: RequestInit = {};\n if (options?.includeCredentials) requestInit.credentials = 'include';\n if (options?.method) requestInit.method = options.method;\n if (options?.body) requestInit.body = options.body;\n if (options?.headers) requestInit.headers = options.headers;\n const response = await this.fetch(url, {\n requestInit: requestInit,\n retryConfig: options?.retryConfig,\n });\n const json = await response.json();\n return json as T;\n }\n\n /** @inheritdoc */\n async fetch(\n request: RequestInfo,\n options?: RequestInit | FetchOptions,\n ): Promise<Response> {\n let finalRequest = request;\n const urlParams = new URLSearchParams(this.searchParams);\n if (urlParams.get('reCache') === '1') {\n const urlString = typeof request === 'string' ? request : request.url;\n finalRequest = this.addSearchParams(urlString, { reCache: '1' });\n }\n return this.fetchRetrier.fetchRetry(finalRequest, options);\n }\n\n /**\n * Since RequestInfo can be either a `Request` or `string`, we need to change\n * the way we add search params to it depending on the input.\n */\n private addSearchParams(\n urlString: string,\n params: Record<string, string>,\n ): string {\n const url = new URL(urlString, window.location.href);\n\n for (const [key, value] of Object.entries(params)) {\n url.searchParams.set(key, value);\n }\n\n return url.href;\n }\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import type { RetryConfiguring } from './retry-configuring';
2
+ export declare class RetryConfigs {
3
+ static readonly default: Readonly<RetryConfiguring>;
4
+ static readonly noRetry: Readonly<RetryConfiguring>;
5
+ }
@@ -0,0 +1,7 @@
1
+ import { DefaultRetryConfiguration } from './default-retry-configuration';
2
+ import { NoRetryConfiguration } from './no-retry-configuration';
3
+ export class RetryConfigs {
4
+ }
5
+ RetryConfigs.default = DefaultRetryConfiguration.shared;
6
+ RetryConfigs.noRetry = NoRetryConfiguration.shared;
7
+ //# sourceMappingURL=configurations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configurations.js","sourceRoot":"","sources":["../../../../src/fetch-retry/configuration/configurations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAGhE,MAAM,OAAO,YAAY;;AACP,oBAAO,GACrB,yBAAyB,CAAC,MAAM,CAAC;AAEnB,oBAAO,GACrB,oBAAoB,CAAC,MAAM,CAAC","sourcesContent":["import { DefaultRetryConfiguration } from './default-retry-configuration';\nimport { NoRetryConfiguration } from './no-retry-configuration';\nimport type { RetryConfiguring } from './retry-configuring';\n\nexport class RetryConfigs {\n static readonly default: Readonly<RetryConfiguring> =\n DefaultRetryConfiguration.shared;\n\n static readonly noRetry: Readonly<RetryConfiguring> =\n NoRetryConfiguration.shared;\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { RetryConfiguring } from './retry-configuring';
2
2
  import type { Milliseconds } from './milliseconds';
3
3
  export declare class DefaultRetryConfiguration implements RetryConfiguring {
4
+ static readonly shared: Readonly<RetryConfiguring>;
4
5
  private readonly maxRetries;
5
6
  constructor(options?: {
6
7
  maxRetries?: number;
@@ -17,4 +17,5 @@ export class DefaultRetryConfiguration {
17
17
  return Math.min(500 * 2 ** retryNumber, 10000);
18
18
  }
19
19
  }
20
+ DefaultRetryConfiguration.shared = new DefaultRetryConfiguration();
20
21
  //# sourceMappingURL=default-retry-configuration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"default-retry-configuration.js","sourceRoot":"","sources":["../../../../src/fetch-retry/configuration/default-retry-configuration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,yBAAyB;IAGpC,YAAY,OAAiC;QAF5B,eAAU,GAAqB,CAAC,CAAC;QAGhD,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,MAAK,SAAS,EAAE,CAAC;YACtC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACvC,CAAC;IACH,CAAC;IAED,WAAW,CAAC,QAAyB,EAAE,WAAmB;QACxD,IAAI,QAAQ,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QACpC,IAAI,WAAW,GAAG,IAAI,CAAC,UAAU;YAAE,OAAO,KAAK,CAAC;QAChD,OAAO,QAAQ,CAAC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC;IACzD,CAAC;IAED,UAAU,CAAC,WAAmB;QAC5B,uCAAuC;QACvC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,WAAW,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;CACF","sourcesContent":["import type { RetryConfiguring } from './retry-configuring';\nimport type { Milliseconds } from './milliseconds';\n\nexport class DefaultRetryConfiguration implements RetryConfiguring {\n private readonly maxRetries: Readonly<number> = 2;\n\n constructor(options?: { maxRetries?: number }) {\n if (options?.maxRetries !== undefined) {\n this.maxRetries = options.maxRetries;\n }\n }\n\n shouldRetry(response: Response | null, retryNumber: number): boolean {\n if (response === null) return false;\n if (retryNumber > this.maxRetries) return false;\n return response.status >= 500 && response.status < 600;\n }\n\n retryDelay(retryNumber: number): Milliseconds {\n // Exponential backoff up to 10 seconds\n return Math.min(500 * 2 ** retryNumber, 10000);\n }\n}\n"]}
1
+ {"version":3,"file":"default-retry-configuration.js","sourceRoot":"","sources":["../../../../src/fetch-retry/configuration/default-retry-configuration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,yBAAyB;IAMpC,YAAY,OAAiC;QAF5B,eAAU,GAAqB,CAAC,CAAC;QAGhD,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,MAAK,SAAS,EAAE,CAAC;YACtC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACvC,CAAC;IACH,CAAC;IAED,WAAW,CAAC,QAAyB,EAAE,WAAmB;QACxD,IAAI,QAAQ,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QACpC,IAAI,WAAW,GAAG,IAAI,CAAC,UAAU;YAAE,OAAO,KAAK,CAAC;QAChD,OAAO,QAAQ,CAAC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC;IACzD,CAAC;IAED,UAAU,CAAC,WAAmB;QAC5B,uCAAuC;QACvC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,WAAW,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;;AApBe,gCAAM,GACpB,IAAI,yBAAyB,EAAE,AADX,CACY","sourcesContent":["import type { RetryConfiguring } from './retry-configuring';\nimport type { Milliseconds } from './milliseconds';\n\nexport class DefaultRetryConfiguration implements RetryConfiguring {\n static readonly shared: Readonly<RetryConfiguring> =\n new DefaultRetryConfiguration();\n\n private readonly maxRetries: Readonly<number> = 2;\n\n constructor(options?: { maxRetries?: number }) {\n if (options?.maxRetries !== undefined) {\n this.maxRetries = options.maxRetries;\n }\n }\n\n shouldRetry(response: Response | null, retryNumber: number): boolean {\n if (response === null) return false;\n if (retryNumber > this.maxRetries) return false;\n return response.status >= 500 && response.status < 600;\n }\n\n retryDelay(retryNumber: number): Milliseconds {\n // Exponential backoff up to 10 seconds\n return Math.min(500 * 2 ** retryNumber, 10000);\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { RetryConfiguring } from './retry-configuring';
2
2
  import type { Milliseconds } from './milliseconds';
3
3
  export declare class NoRetryConfiguration implements RetryConfiguring {
4
+ static readonly shared: Readonly<RetryConfiguring>;
4
5
  shouldRetry(): boolean;
5
6
  retryDelay(): Milliseconds;
6
7
  }
@@ -6,4 +6,5 @@ export class NoRetryConfiguration {
6
6
  return 0;
7
7
  }
8
8
  }
9
+ NoRetryConfiguration.shared = new NoRetryConfiguration();
9
10
  //# sourceMappingURL=no-retry-configuration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"no-retry-configuration.js","sourceRoot":"","sources":["../../../../src/fetch-retry/configuration/no-retry-configuration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,oBAAoB;IAC/B,WAAW;QACT,OAAO,KAAK,CAAC;IACf,CAAC;IAED,UAAU;QACR,OAAO,CAAC,CAAC;IACX,CAAC;CACF","sourcesContent":["import type { RetryConfiguring } from './retry-configuring';\nimport type { Milliseconds } from './milliseconds';\n\nexport class NoRetryConfiguration implements RetryConfiguring {\n shouldRetry(): boolean {\n return false;\n }\n\n retryDelay(): Milliseconds {\n return 0;\n }\n}\n"]}
1
+ {"version":3,"file":"no-retry-configuration.js","sourceRoot":"","sources":["../../../../src/fetch-retry/configuration/no-retry-configuration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,oBAAoB;IAI/B,WAAW;QACT,OAAO,KAAK,CAAC;IACf,CAAC;IAED,UAAU;QACR,OAAO,CAAC,CAAC;IACX,CAAC;;AATe,2BAAM,GACpB,IAAI,oBAAoB,EAAE,CAAC","sourcesContent":["import type { RetryConfiguring } from './retry-configuring';\nimport type { Milliseconds } from './milliseconds';\n\nexport class NoRetryConfiguration implements RetryConfiguring {\n static readonly shared: Readonly<RetryConfiguring> =\n new NoRetryConfiguration();\n\n shouldRetry(): boolean {\n return false;\n }\n\n retryDelay(): Milliseconds {\n return 0;\n }\n}\n"]}
package/index.ts CHANGED
@@ -8,3 +8,4 @@ export { DefaultRetryConfiguration } from './src/fetch-retry/configuration/defau
8
8
  export { NoRetryConfiguration } from './src/fetch-retry/configuration/no-retry-configuration';
9
9
  export type { RetryConfiguring } from './src/fetch-retry/configuration/retry-configuring';
10
10
  export type { FetchOptions } from './src/fetch-options';
11
+ export { RetryConfigs } from './src/fetch-retry/configuration/configurations';
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "license": "AGPL-3.0-only",
9
9
  "author": "Internet Archive",
10
- "version": "1.1.0-webdev-7731.1",
10
+ "version": "1.1.0-webdev-7731.3",
11
11
  "main": "dist/index.js",
12
12
  "module": "dist/index.js",
13
13
  "scripts": {
@@ -37,10 +37,22 @@ export interface FetchHandlerInterface {
37
37
  * of the full URL. If you need a full URL, use `fetchApiResponse` instead.
38
38
  *
39
39
  * @param path string
40
- * @param options?: { includeCredentials?: boolean, retryConfig?: RetryConfiguring }
40
+ * @param options?: {
41
+ * includeCredentials?: boolean;
42
+ * method?: string;
43
+ * body?: BodyInit;
44
+ * headers?: HeadersInit;
45
+ * retryConfig?: RetryConfiguring;
46
+ * }
41
47
  */
42
48
  fetchIAApiResponse<T>(
43
49
  path: string,
44
- options?: { includeCredentials?: boolean; retryConfig?: RetryConfiguring },
50
+ options?: {
51
+ includeCredentials?: boolean;
52
+ method?: string;
53
+ body?: BodyInit;
54
+ headers?: HeadersInit;
55
+ retryConfig?: RetryConfiguring;
56
+ },
45
57
  ): Promise<T>;
46
58
  }
@@ -39,6 +39,9 @@ export class IaFetchHandler implements FetchHandlerInterface {
39
39
  path: string,
40
40
  options?: {
41
41
  includeCredentials?: boolean;
42
+ method?: string;
43
+ body?: BodyInit;
44
+ headers?: HeadersInit;
42
45
  retryConfig?: RetryConfiguring;
43
46
  },
44
47
  ): Promise<T> {
@@ -0,0 +1,11 @@
1
+ import { DefaultRetryConfiguration } from './default-retry-configuration';
2
+ import { NoRetryConfiguration } from './no-retry-configuration';
3
+ import type { RetryConfiguring } from './retry-configuring';
4
+
5
+ export class RetryConfigs {
6
+ static readonly default: Readonly<RetryConfiguring> =
7
+ DefaultRetryConfiguration.shared;
8
+
9
+ static readonly noRetry: Readonly<RetryConfiguring> =
10
+ NoRetryConfiguration.shared;
11
+ }
@@ -2,6 +2,9 @@ import type { RetryConfiguring } from './retry-configuring';
2
2
  import type { Milliseconds } from './milliseconds';
3
3
 
4
4
  export class DefaultRetryConfiguration implements RetryConfiguring {
5
+ static readonly shared: Readonly<RetryConfiguring> =
6
+ new DefaultRetryConfiguration();
7
+
5
8
  private readonly maxRetries: Readonly<number> = 2;
6
9
 
7
10
  constructor(options?: { maxRetries?: number }) {
@@ -2,6 +2,9 @@ import type { RetryConfiguring } from './retry-configuring';
2
2
  import type { Milliseconds } from './milliseconds';
3
3
 
4
4
  export class NoRetryConfiguration implements RetryConfiguring {
5
+ static readonly shared: Readonly<RetryConfiguring> =
6
+ new NoRetryConfiguration();
7
+
5
8
  shouldRetry(): boolean {
6
9
  return false;
7
10
  }