@internetarchive/fetch-handler 1.1.0-webdev-7731.1 → 1.1.0-webdev-7731.2
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 +3 -3
- package/dist/src/fetch-handler-interface.d.ts +10 -1
- package/dist/src/fetch-handler-interface.js.map +1 -1
- package/dist/src/fetch-handler.d.ts +3 -0
- package/dist/src/fetch-handler.js.map +1 -1
- package/package.json +1 -1
- package/src/fetch-handler-interface.ts +14 -2
- package/src/fetch-handler.ts +3 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
 [](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/
|
|
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 '
|
|
20
|
+
import { IaFetchHandler } from '@internetarchive/fetch-handler';
|
|
21
21
|
|
|
22
22
|
@property({ type: Object }) data: any = null;
|
|
23
23
|
@property({ type: String }) error: string = '';
|
|
@@ -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?: {
|
|
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?: {
|
|
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,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C,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"]}
|
package/package.json
CHANGED
|
@@ -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?: {
|
|
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?: {
|
|
50
|
+
options?: {
|
|
51
|
+
includeCredentials?: boolean;
|
|
52
|
+
method?: string;
|
|
53
|
+
body?: BodyInit;
|
|
54
|
+
headers?: HeadersInit;
|
|
55
|
+
retryConfig?: RetryConfiguring;
|
|
56
|
+
},
|
|
45
57
|
): Promise<T>;
|
|
46
58
|
}
|
package/src/fetch-handler.ts
CHANGED