@prismicio/client 7.19.2 → 7.20.0-alpha.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/dist/BaseClient.cjs +108 -0
- package/dist/BaseClient.cjs.map +1 -0
- package/dist/BaseClient.d.ts +152 -0
- package/dist/BaseClient.js +108 -0
- package/dist/BaseClient.js.map +1 -0
- package/dist/Client.cjs +95 -111
- package/dist/Client.cjs.map +1 -1
- package/dist/Client.d.ts +27 -66
- package/dist/Client.js +95 -111
- package/dist/Client.js.map +1 -1
- package/dist/WriteClient.cjs +100 -151
- package/dist/WriteClient.cjs.map +1 -1
- package/dist/WriteClient.d.ts +26 -2
- package/dist/WriteClient.js +100 -151
- package/dist/WriteClient.js.map +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -1
- package/dist/errors/ParsingError.d.ts +4 -6
- package/dist/errors/ParsingError.js.map +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.d.ts +4 -4
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/integration.d.ts +28 -0
- package/dist/types/value/integration.d.ts +8 -2
- package/package.json +1 -1
- package/src/BaseClient.ts +330 -0
- package/src/Client.ts +171 -201
- package/src/WriteClient.ts +157 -214
- package/src/errors/ParsingError.ts +4 -6
- package/src/helpers/isFilled.ts +5 -4
- package/src/index.ts +13 -8
- package/src/types/integration.ts +34 -0
- package/src/types/value/integration.ts +9 -2
- package/dist/errors/InvalidDataError.cjs +0 -7
- package/dist/errors/InvalidDataError.cjs.map +0 -1
- package/dist/errors/InvalidDataError.d.ts +0 -3
- package/dist/errors/InvalidDataError.js +0 -7
- package/dist/errors/InvalidDataError.js.map +0 -1
- package/dist/lib/request.cjs +0 -43
- package/dist/lib/request.cjs.map +0 -1
- package/dist/lib/request.d.ts +0 -73
- package/dist/lib/request.js +0 -43
- package/dist/lib/request.js.map +0 -1
- package/src/errors/InvalidDataError.ts +0 -5
- package/src/lib/request.ts +0 -166
package/dist/lib/request.cjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const pLimit = require("./pLimit.cjs");
|
|
4
|
-
const DEFAULT_RETRY_AFTER = 1500;
|
|
5
|
-
const THROTTLED_RUNNERS = {};
|
|
6
|
-
const DEDUPLICATED_JOBS = {};
|
|
7
|
-
async function request(url, init, fetchFn) {
|
|
8
|
-
var _a, _b;
|
|
9
|
-
const stringURL = url.toString();
|
|
10
|
-
let job;
|
|
11
|
-
if (init == null ? void 0 : init.body) {
|
|
12
|
-
const runner = THROTTLED_RUNNERS[_a = url.hostname] || (THROTTLED_RUNNERS[_a] = pLimit.pLimit({
|
|
13
|
-
interval: DEFAULT_RETRY_AFTER
|
|
14
|
-
}));
|
|
15
|
-
job = runner(() => fetchFn(stringURL, init));
|
|
16
|
-
} else {
|
|
17
|
-
const existingJob = (_b = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _b.get(init == null ? void 0 : init.signal);
|
|
18
|
-
if (existingJob) {
|
|
19
|
-
job = existingJob;
|
|
20
|
-
} else {
|
|
21
|
-
job = fetchFn(stringURL, init).finally(() => {
|
|
22
|
-
var _a2, _b2;
|
|
23
|
-
(_a2 = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _a2.delete(init == null ? void 0 : init.signal);
|
|
24
|
-
if (((_b2 = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _b2.size) === 0) {
|
|
25
|
-
delete DEDUPLICATED_JOBS[stringURL];
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const map = DEDUPLICATED_JOBS[stringURL] || (DEDUPLICATED_JOBS[stringURL] = /* @__PURE__ */ new Map());
|
|
29
|
-
map.set(init == null ? void 0 : init.signal, job);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const response = await job;
|
|
33
|
-
if (response.status === 429) {
|
|
34
|
-
const retryAfter = Number(response.headers.get("retry-after"));
|
|
35
|
-
const resolvedRetryAfter = Number.isNaN(retryAfter) ? DEFAULT_RETRY_AFTER : retryAfter * 1e3;
|
|
36
|
-
await new Promise((resolve) => setTimeout(resolve, resolvedRetryAfter));
|
|
37
|
-
return request(url, init, fetchFn);
|
|
38
|
-
}
|
|
39
|
-
return response.clone();
|
|
40
|
-
}
|
|
41
|
-
exports.DEFAULT_RETRY_AFTER = DEFAULT_RETRY_AFTER;
|
|
42
|
-
exports.request = request;
|
|
43
|
-
//# sourceMappingURL=request.cjs.map
|
package/dist/lib/request.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request.cjs","sources":["../../../src/lib/request.ts"],"sourcesContent":["import { type LimitFunction, pLimit } from \"./pLimit\"\n\n/**\n * The default number of milliseconds to wait before retrying a rate-limited\n * `fetch()` request (429 response code). The default value is only used if the\n * response does not include a `retry-after` header.\n */\nexport const DEFAULT_RETRY_AFTER = 1500 // ms\n\n/**\n * A record of URLs mapped to throttled task runners.\n */\nconst THROTTLED_RUNNERS: Partial<Record<string, LimitFunction>> = {}\n\n/**\n * A record of URLs mapped to active deduplicated jobs. Jobs are keyed by their\n * optional signal.\n */\nconst DEDUPLICATED_JOBS: Partial<\n\tRecord<string, Map<AbortSignalLike | undefined, Promise<ResponseLike>>>\n> = {}\n\n/**\n * A universal API to make network requests. A subset of the `fetch()` API.\n *\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}\n */\nexport type FetchLike = (\n\tinput: string,\n\tinit?: RequestInitLike,\n) => Promise<ResponseLike>\n\n/**\n * An object that allows you to abort a `fetch()` request if needed via an\n * `AbortController` object\n *\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}\n */\n// `any` is used often here to ensure this type is universally valid among\n// different AbortSignal implementations. The types of each property are not\n// important to validate since it is blindly passed to a given `fetch()`\n// function.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AbortSignalLike = any\n\n/**\n * A subset of RequestInit properties to configure a `fetch()` request.\n */\n// Only options relevant to the client are included. Extending from the full\n// RequestInit would cause issues, such as accepting Header objects.\n//\n// An interface is used to allow other libraries to augment the type with\n// environment-specific types.\nexport interface RequestInitLike extends Pick<RequestInit, \"cache\"> {\n\t/**\n\t * The HTTP method to use for the request.\n\t */\n\tmethod?: string\n\n\t/**\n\t * The request body to send with the request.\n\t */\n\t// We want to keep the body type as compatible as possible, so\n\t// we only declare the type we need and accept anything else.\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tbody?: any | FormData | string\n\n\t/**\n\t * An object literal to set the `fetch()` request's headers.\n\t */\n\theaders?: Record<string, string>\n\n\t/**\n\t * An AbortSignal to set the `fetch()` request's signal.\n\t *\n\t * See:\n\t * [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\t */\n\t// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`\n\t// for backwards compatibility (the type is exported) and to signal to\n\t// other readers that this should be an AbortSignal-like object.\n\tsignal?: AbortSignalLike\n}\n\n/**\n * The minimum required properties from Response.\n */\nexport interface ResponseLike {\n\tok: boolean\n\tstatus: number\n\theaders: HeadersLike\n\turl: string\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tjson(): Promise<any>\n\ttext(): Promise<string>\n\tblob(): Promise<Blob>\n\tclone(): ResponseLike\n}\n\n/**\n * The minimum required properties from Headers.\n */\nexport interface HeadersLike {\n\tget(name: string): string | null\n}\n\n/**\n * Makes an HTTP request with automatic retry for rate limits and request\n * deduplication.\n *\n * @param url - The URL to request.\n * @param init - Fetch options.\n * @param fetchFn - The fetch function to use.\n *\n * @returns The response from the fetch request.\n */\nexport async function request(\n\turl: URL,\n\tinit: RequestInitLike | undefined,\n\tfetchFn: FetchLike,\n): Promise<ResponseLike> {\n\tconst stringURL = url.toString()\n\n\tlet job: Promise<ResponseLike>\n\n\t// Throttle requests with a body.\n\tif (init?.body) {\n\t\t// Rate limiting is done per hostname.\n\t\tconst runner = (THROTTLED_RUNNERS[url.hostname] ||= pLimit({\n\t\t\tinterval: DEFAULT_RETRY_AFTER,\n\t\t}))\n\n\t\tjob = runner(() => fetchFn(stringURL, init))\n\t} else {\n\t\t// Deduplicate all other requests.\n\t\tconst existingJob = DEDUPLICATED_JOBS[stringURL]?.get(init?.signal)\n\t\tif (existingJob) {\n\t\t\tjob = existingJob\n\t\t} else {\n\t\t\tjob = fetchFn(stringURL, init).finally(() => {\n\t\t\t\tDEDUPLICATED_JOBS[stringURL]?.delete(init?.signal)\n\t\t\t\tif (DEDUPLICATED_JOBS[stringURL]?.size === 0) {\n\t\t\t\t\tdelete DEDUPLICATED_JOBS[stringURL]\n\t\t\t\t}\n\t\t\t})\n\t\t\tconst map = (DEDUPLICATED_JOBS[stringURL] ||= new Map())\n\t\t\tmap.set(init?.signal, job)\n\t\t}\n\t}\n\n\tconst response = await job\n\n\t// Retry rate limited requests.\n\tif (response.status === 429) {\n\t\tconst retryAfter = Number(response.headers.get(\"retry-after\"))\n\t\tconst resolvedRetryAfter = Number.isNaN(retryAfter)\n\t\t\t? DEFAULT_RETRY_AFTER\n\t\t\t: retryAfter * 1000\n\n\t\tawait new Promise((resolve) => setTimeout(resolve, resolvedRetryAfter))\n\n\t\treturn request(url, init, fetchFn)\n\t}\n\n\treturn response.clone()\n}\n"],"names":["pLimit","_a","_b"],"mappings":";;;AAOO,MAAM,sBAAsB;AAKnC,MAAM,oBAA4D,CAAA;AAMlE,MAAM,oBAEF,CAAA;AAgGkB,eAAA,QACrB,KACA,MACA,SAAkB;;AAEZ,QAAA,YAAY,IAAI;AAElB,MAAA;AAGJ,MAAI,6BAAM,MAAM;AAEf,UAAM,SAAU,uBAAkB,IAAI,cAAtB,wBAAoCA,OAAAA,OAAO;AAAA,MAC1D,UAAU;AAAA,IAAA,CACV;AAED,UAAM,OAAO,MAAM,QAAQ,WAAW,IAAI,CAAC;AAAA,EAAA,OACrC;AAEN,UAAM,eAAc,uBAAkB,SAAS,MAA3B,mBAA8B,IAAI,6BAAM;AAC5D,QAAI,aAAa;AACV,YAAA;AAAA,IAAA,OACA;AACN,YAAM,QAAQ,WAAW,IAAI,EAAE,QAAQ,MAAK;;AAC3C,SAAAC,MAAA,kBAAkB,SAAS,MAA3B,gBAAAA,IAA8B,OAAO,6BAAM;AAC3C,cAAIC,MAAA,kBAAkB,SAAS,MAA3B,gBAAAA,IAA8B,UAAS,GAAG;AAC7C,iBAAO,kBAAkB,SAAS;AAAA,QAAA;AAAA,MACnC,CACA;AACD,YAAM,MAAO,oFAAqC,IAAK;AACnD,UAAA,IAAI,6BAAM,QAAQ,GAAG;AAAA,IAAA;AAAA,EAC1B;AAGD,QAAM,WAAW,MAAM;AAGnB,MAAA,SAAS,WAAW,KAAK;AAC5B,UAAM,aAAa,OAAO,SAAS,QAAQ,IAAI,aAAa,CAAC;AAC7D,UAAM,qBAAqB,OAAO,MAAM,UAAU,IAC/C,sBACA,aAAa;AAEhB,UAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,kBAAkB,CAAC;AAE/D,WAAA,QAAQ,KAAK,MAAM,OAAO;AAAA,EAAA;AAGlC,SAAO,SAAS,MAAK;AACtB;;;"}
|
package/dist/lib/request.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The default number of milliseconds to wait before retrying a rate-limited
|
|
3
|
-
* `fetch()` request (429 response code). The default value is only used if the
|
|
4
|
-
* response does not include a `retry-after` header.
|
|
5
|
-
*/
|
|
6
|
-
export declare const DEFAULT_RETRY_AFTER = 1500;
|
|
7
|
-
/**
|
|
8
|
-
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
9
|
-
*
|
|
10
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
|
|
11
|
-
*/
|
|
12
|
-
export type FetchLike = (input: string, init?: RequestInitLike) => Promise<ResponseLike>;
|
|
13
|
-
/**
|
|
14
|
-
* An object that allows you to abort a `fetch()` request if needed via an
|
|
15
|
-
* `AbortController` object
|
|
16
|
-
*
|
|
17
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
18
|
-
*/
|
|
19
|
-
export type AbortSignalLike = any;
|
|
20
|
-
/**
|
|
21
|
-
* A subset of RequestInit properties to configure a `fetch()` request.
|
|
22
|
-
*/
|
|
23
|
-
export interface RequestInitLike extends Pick<RequestInit, "cache"> {
|
|
24
|
-
/**
|
|
25
|
-
* The HTTP method to use for the request.
|
|
26
|
-
*/
|
|
27
|
-
method?: string;
|
|
28
|
-
/**
|
|
29
|
-
* The request body to send with the request.
|
|
30
|
-
*/
|
|
31
|
-
body?: any | FormData | string;
|
|
32
|
-
/**
|
|
33
|
-
* An object literal to set the `fetch()` request's headers.
|
|
34
|
-
*/
|
|
35
|
-
headers?: Record<string, string>;
|
|
36
|
-
/**
|
|
37
|
-
* An AbortSignal to set the `fetch()` request's signal.
|
|
38
|
-
*
|
|
39
|
-
* See:
|
|
40
|
-
* [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
41
|
-
*/
|
|
42
|
-
signal?: AbortSignalLike;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* The minimum required properties from Response.
|
|
46
|
-
*/
|
|
47
|
-
export interface ResponseLike {
|
|
48
|
-
ok: boolean;
|
|
49
|
-
status: number;
|
|
50
|
-
headers: HeadersLike;
|
|
51
|
-
url: string;
|
|
52
|
-
json(): Promise<any>;
|
|
53
|
-
text(): Promise<string>;
|
|
54
|
-
blob(): Promise<Blob>;
|
|
55
|
-
clone(): ResponseLike;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* The minimum required properties from Headers.
|
|
59
|
-
*/
|
|
60
|
-
export interface HeadersLike {
|
|
61
|
-
get(name: string): string | null;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Makes an HTTP request with automatic retry for rate limits and request
|
|
65
|
-
* deduplication.
|
|
66
|
-
*
|
|
67
|
-
* @param url - The URL to request.
|
|
68
|
-
* @param init - Fetch options.
|
|
69
|
-
* @param fetchFn - The fetch function to use.
|
|
70
|
-
*
|
|
71
|
-
* @returns The response from the fetch request.
|
|
72
|
-
*/
|
|
73
|
-
export declare function request(url: URL, init: RequestInitLike | undefined, fetchFn: FetchLike): Promise<ResponseLike>;
|
package/dist/lib/request.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { pLimit } from "./pLimit.js";
|
|
2
|
-
const DEFAULT_RETRY_AFTER = 1500;
|
|
3
|
-
const THROTTLED_RUNNERS = {};
|
|
4
|
-
const DEDUPLICATED_JOBS = {};
|
|
5
|
-
async function request(url, init, fetchFn) {
|
|
6
|
-
var _a, _b;
|
|
7
|
-
const stringURL = url.toString();
|
|
8
|
-
let job;
|
|
9
|
-
if (init == null ? void 0 : init.body) {
|
|
10
|
-
const runner = THROTTLED_RUNNERS[_a = url.hostname] || (THROTTLED_RUNNERS[_a] = pLimit({
|
|
11
|
-
interval: DEFAULT_RETRY_AFTER
|
|
12
|
-
}));
|
|
13
|
-
job = runner(() => fetchFn(stringURL, init));
|
|
14
|
-
} else {
|
|
15
|
-
const existingJob = (_b = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _b.get(init == null ? void 0 : init.signal);
|
|
16
|
-
if (existingJob) {
|
|
17
|
-
job = existingJob;
|
|
18
|
-
} else {
|
|
19
|
-
job = fetchFn(stringURL, init).finally(() => {
|
|
20
|
-
var _a2, _b2;
|
|
21
|
-
(_a2 = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _a2.delete(init == null ? void 0 : init.signal);
|
|
22
|
-
if (((_b2 = DEDUPLICATED_JOBS[stringURL]) == null ? void 0 : _b2.size) === 0) {
|
|
23
|
-
delete DEDUPLICATED_JOBS[stringURL];
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
const map = DEDUPLICATED_JOBS[stringURL] || (DEDUPLICATED_JOBS[stringURL] = /* @__PURE__ */ new Map());
|
|
27
|
-
map.set(init == null ? void 0 : init.signal, job);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
const response = await job;
|
|
31
|
-
if (response.status === 429) {
|
|
32
|
-
const retryAfter = Number(response.headers.get("retry-after"));
|
|
33
|
-
const resolvedRetryAfter = Number.isNaN(retryAfter) ? DEFAULT_RETRY_AFTER : retryAfter * 1e3;
|
|
34
|
-
await new Promise((resolve) => setTimeout(resolve, resolvedRetryAfter));
|
|
35
|
-
return request(url, init, fetchFn);
|
|
36
|
-
}
|
|
37
|
-
return response.clone();
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
DEFAULT_RETRY_AFTER,
|
|
41
|
-
request
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=request.js.map
|
package/dist/lib/request.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sources":["../../../src/lib/request.ts"],"sourcesContent":["import { type LimitFunction, pLimit } from \"./pLimit\"\n\n/**\n * The default number of milliseconds to wait before retrying a rate-limited\n * `fetch()` request (429 response code). The default value is only used if the\n * response does not include a `retry-after` header.\n */\nexport const DEFAULT_RETRY_AFTER = 1500 // ms\n\n/**\n * A record of URLs mapped to throttled task runners.\n */\nconst THROTTLED_RUNNERS: Partial<Record<string, LimitFunction>> = {}\n\n/**\n * A record of URLs mapped to active deduplicated jobs. Jobs are keyed by their\n * optional signal.\n */\nconst DEDUPLICATED_JOBS: Partial<\n\tRecord<string, Map<AbortSignalLike | undefined, Promise<ResponseLike>>>\n> = {}\n\n/**\n * A universal API to make network requests. A subset of the `fetch()` API.\n *\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}\n */\nexport type FetchLike = (\n\tinput: string,\n\tinit?: RequestInitLike,\n) => Promise<ResponseLike>\n\n/**\n * An object that allows you to abort a `fetch()` request if needed via an\n * `AbortController` object\n *\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}\n */\n// `any` is used often here to ensure this type is universally valid among\n// different AbortSignal implementations. The types of each property are not\n// important to validate since it is blindly passed to a given `fetch()`\n// function.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AbortSignalLike = any\n\n/**\n * A subset of RequestInit properties to configure a `fetch()` request.\n */\n// Only options relevant to the client are included. Extending from the full\n// RequestInit would cause issues, such as accepting Header objects.\n//\n// An interface is used to allow other libraries to augment the type with\n// environment-specific types.\nexport interface RequestInitLike extends Pick<RequestInit, \"cache\"> {\n\t/**\n\t * The HTTP method to use for the request.\n\t */\n\tmethod?: string\n\n\t/**\n\t * The request body to send with the request.\n\t */\n\t// We want to keep the body type as compatible as possible, so\n\t// we only declare the type we need and accept anything else.\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tbody?: any | FormData | string\n\n\t/**\n\t * An object literal to set the `fetch()` request's headers.\n\t */\n\theaders?: Record<string, string>\n\n\t/**\n\t * An AbortSignal to set the `fetch()` request's signal.\n\t *\n\t * See:\n\t * [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\t */\n\t// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`\n\t// for backwards compatibility (the type is exported) and to signal to\n\t// other readers that this should be an AbortSignal-like object.\n\tsignal?: AbortSignalLike\n}\n\n/**\n * The minimum required properties from Response.\n */\nexport interface ResponseLike {\n\tok: boolean\n\tstatus: number\n\theaders: HeadersLike\n\turl: string\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tjson(): Promise<any>\n\ttext(): Promise<string>\n\tblob(): Promise<Blob>\n\tclone(): ResponseLike\n}\n\n/**\n * The minimum required properties from Headers.\n */\nexport interface HeadersLike {\n\tget(name: string): string | null\n}\n\n/**\n * Makes an HTTP request with automatic retry for rate limits and request\n * deduplication.\n *\n * @param url - The URL to request.\n * @param init - Fetch options.\n * @param fetchFn - The fetch function to use.\n *\n * @returns The response from the fetch request.\n */\nexport async function request(\n\turl: URL,\n\tinit: RequestInitLike | undefined,\n\tfetchFn: FetchLike,\n): Promise<ResponseLike> {\n\tconst stringURL = url.toString()\n\n\tlet job: Promise<ResponseLike>\n\n\t// Throttle requests with a body.\n\tif (init?.body) {\n\t\t// Rate limiting is done per hostname.\n\t\tconst runner = (THROTTLED_RUNNERS[url.hostname] ||= pLimit({\n\t\t\tinterval: DEFAULT_RETRY_AFTER,\n\t\t}))\n\n\t\tjob = runner(() => fetchFn(stringURL, init))\n\t} else {\n\t\t// Deduplicate all other requests.\n\t\tconst existingJob = DEDUPLICATED_JOBS[stringURL]?.get(init?.signal)\n\t\tif (existingJob) {\n\t\t\tjob = existingJob\n\t\t} else {\n\t\t\tjob = fetchFn(stringURL, init).finally(() => {\n\t\t\t\tDEDUPLICATED_JOBS[stringURL]?.delete(init?.signal)\n\t\t\t\tif (DEDUPLICATED_JOBS[stringURL]?.size === 0) {\n\t\t\t\t\tdelete DEDUPLICATED_JOBS[stringURL]\n\t\t\t\t}\n\t\t\t})\n\t\t\tconst map = (DEDUPLICATED_JOBS[stringURL] ||= new Map())\n\t\t\tmap.set(init?.signal, job)\n\t\t}\n\t}\n\n\tconst response = await job\n\n\t// Retry rate limited requests.\n\tif (response.status === 429) {\n\t\tconst retryAfter = Number(response.headers.get(\"retry-after\"))\n\t\tconst resolvedRetryAfter = Number.isNaN(retryAfter)\n\t\t\t? DEFAULT_RETRY_AFTER\n\t\t\t: retryAfter * 1000\n\n\t\tawait new Promise((resolve) => setTimeout(resolve, resolvedRetryAfter))\n\n\t\treturn request(url, init, fetchFn)\n\t}\n\n\treturn response.clone()\n}\n"],"names":["_a","_b"],"mappings":";AAOO,MAAM,sBAAsB;AAKnC,MAAM,oBAA4D,CAAA;AAMlE,MAAM,oBAEF,CAAA;AAgGkB,eAAA,QACrB,KACA,MACA,SAAkB;;AAEZ,QAAA,YAAY,IAAI;AAElB,MAAA;AAGJ,MAAI,6BAAM,MAAM;AAEf,UAAM,SAAU,uBAAkB,IAAI,cAAtB,wBAAoC,OAAO;AAAA,MAC1D,UAAU;AAAA,IAAA,CACV;AAED,UAAM,OAAO,MAAM,QAAQ,WAAW,IAAI,CAAC;AAAA,EAAA,OACrC;AAEN,UAAM,eAAc,uBAAkB,SAAS,MAA3B,mBAA8B,IAAI,6BAAM;AAC5D,QAAI,aAAa;AACV,YAAA;AAAA,IAAA,OACA;AACN,YAAM,QAAQ,WAAW,IAAI,EAAE,QAAQ,MAAK;;AAC3C,SAAAA,MAAA,kBAAkB,SAAS,MAA3B,gBAAAA,IAA8B,OAAO,6BAAM;AAC3C,cAAIC,MAAA,kBAAkB,SAAS,MAA3B,gBAAAA,IAA8B,UAAS,GAAG;AAC7C,iBAAO,kBAAkB,SAAS;AAAA,QAAA;AAAA,MACnC,CACA;AACD,YAAM,MAAO,oFAAqC,IAAK;AACnD,UAAA,IAAI,6BAAM,QAAQ,GAAG;AAAA,IAAA;AAAA,EAC1B;AAGD,QAAM,WAAW,MAAM;AAGnB,MAAA,SAAS,WAAW,KAAK;AAC5B,UAAM,aAAa,OAAO,SAAS,QAAQ,IAAI,aAAa,CAAC;AAC7D,UAAM,qBAAqB,OAAO,MAAM,UAAU,IAC/C,sBACA,aAAa;AAEhB,UAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,kBAAkB,CAAC;AAE/D,WAAA,QAAQ,KAAK,MAAM,OAAO;AAAA,EAAA;AAGlC,SAAO,SAAS,MAAK;AACtB;"}
|
package/src/lib/request.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { type LimitFunction, pLimit } from "./pLimit"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The default number of milliseconds to wait before retrying a rate-limited
|
|
5
|
-
* `fetch()` request (429 response code). The default value is only used if the
|
|
6
|
-
* response does not include a `retry-after` header.
|
|
7
|
-
*/
|
|
8
|
-
export const DEFAULT_RETRY_AFTER = 1500 // ms
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A record of URLs mapped to throttled task runners.
|
|
12
|
-
*/
|
|
13
|
-
const THROTTLED_RUNNERS: Partial<Record<string, LimitFunction>> = {}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A record of URLs mapped to active deduplicated jobs. Jobs are keyed by their
|
|
17
|
-
* optional signal.
|
|
18
|
-
*/
|
|
19
|
-
const DEDUPLICATED_JOBS: Partial<
|
|
20
|
-
Record<string, Map<AbortSignalLike | undefined, Promise<ResponseLike>>>
|
|
21
|
-
> = {}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
25
|
-
*
|
|
26
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
|
|
27
|
-
*/
|
|
28
|
-
export type FetchLike = (
|
|
29
|
-
input: string,
|
|
30
|
-
init?: RequestInitLike,
|
|
31
|
-
) => Promise<ResponseLike>
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* An object that allows you to abort a `fetch()` request if needed via an
|
|
35
|
-
* `AbortController` object
|
|
36
|
-
*
|
|
37
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
38
|
-
*/
|
|
39
|
-
// `any` is used often here to ensure this type is universally valid among
|
|
40
|
-
// different AbortSignal implementations. The types of each property are not
|
|
41
|
-
// important to validate since it is blindly passed to a given `fetch()`
|
|
42
|
-
// function.
|
|
43
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
-
export type AbortSignalLike = any
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A subset of RequestInit properties to configure a `fetch()` request.
|
|
48
|
-
*/
|
|
49
|
-
// Only options relevant to the client are included. Extending from the full
|
|
50
|
-
// RequestInit would cause issues, such as accepting Header objects.
|
|
51
|
-
//
|
|
52
|
-
// An interface is used to allow other libraries to augment the type with
|
|
53
|
-
// environment-specific types.
|
|
54
|
-
export interface RequestInitLike extends Pick<RequestInit, "cache"> {
|
|
55
|
-
/**
|
|
56
|
-
* The HTTP method to use for the request.
|
|
57
|
-
*/
|
|
58
|
-
method?: string
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The request body to send with the request.
|
|
62
|
-
*/
|
|
63
|
-
// We want to keep the body type as compatible as possible, so
|
|
64
|
-
// we only declare the type we need and accept anything else.
|
|
65
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
-
body?: any | FormData | string
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* An object literal to set the `fetch()` request's headers.
|
|
70
|
-
*/
|
|
71
|
-
headers?: Record<string, string>
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* An AbortSignal to set the `fetch()` request's signal.
|
|
75
|
-
*
|
|
76
|
-
* See:
|
|
77
|
-
* [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
78
|
-
*/
|
|
79
|
-
// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`
|
|
80
|
-
// for backwards compatibility (the type is exported) and to signal to
|
|
81
|
-
// other readers that this should be an AbortSignal-like object.
|
|
82
|
-
signal?: AbortSignalLike
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The minimum required properties from Response.
|
|
87
|
-
*/
|
|
88
|
-
export interface ResponseLike {
|
|
89
|
-
ok: boolean
|
|
90
|
-
status: number
|
|
91
|
-
headers: HeadersLike
|
|
92
|
-
url: string
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
json(): Promise<any>
|
|
95
|
-
text(): Promise<string>
|
|
96
|
-
blob(): Promise<Blob>
|
|
97
|
-
clone(): ResponseLike
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* The minimum required properties from Headers.
|
|
102
|
-
*/
|
|
103
|
-
export interface HeadersLike {
|
|
104
|
-
get(name: string): string | null
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Makes an HTTP request with automatic retry for rate limits and request
|
|
109
|
-
* deduplication.
|
|
110
|
-
*
|
|
111
|
-
* @param url - The URL to request.
|
|
112
|
-
* @param init - Fetch options.
|
|
113
|
-
* @param fetchFn - The fetch function to use.
|
|
114
|
-
*
|
|
115
|
-
* @returns The response from the fetch request.
|
|
116
|
-
*/
|
|
117
|
-
export async function request(
|
|
118
|
-
url: URL,
|
|
119
|
-
init: RequestInitLike | undefined,
|
|
120
|
-
fetchFn: FetchLike,
|
|
121
|
-
): Promise<ResponseLike> {
|
|
122
|
-
const stringURL = url.toString()
|
|
123
|
-
|
|
124
|
-
let job: Promise<ResponseLike>
|
|
125
|
-
|
|
126
|
-
// Throttle requests with a body.
|
|
127
|
-
if (init?.body) {
|
|
128
|
-
// Rate limiting is done per hostname.
|
|
129
|
-
const runner = (THROTTLED_RUNNERS[url.hostname] ||= pLimit({
|
|
130
|
-
interval: DEFAULT_RETRY_AFTER,
|
|
131
|
-
}))
|
|
132
|
-
|
|
133
|
-
job = runner(() => fetchFn(stringURL, init))
|
|
134
|
-
} else {
|
|
135
|
-
// Deduplicate all other requests.
|
|
136
|
-
const existingJob = DEDUPLICATED_JOBS[stringURL]?.get(init?.signal)
|
|
137
|
-
if (existingJob) {
|
|
138
|
-
job = existingJob
|
|
139
|
-
} else {
|
|
140
|
-
job = fetchFn(stringURL, init).finally(() => {
|
|
141
|
-
DEDUPLICATED_JOBS[stringURL]?.delete(init?.signal)
|
|
142
|
-
if (DEDUPLICATED_JOBS[stringURL]?.size === 0) {
|
|
143
|
-
delete DEDUPLICATED_JOBS[stringURL]
|
|
144
|
-
}
|
|
145
|
-
})
|
|
146
|
-
const map = (DEDUPLICATED_JOBS[stringURL] ||= new Map())
|
|
147
|
-
map.set(init?.signal, job)
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const response = await job
|
|
152
|
-
|
|
153
|
-
// Retry rate limited requests.
|
|
154
|
-
if (response.status === 429) {
|
|
155
|
-
const retryAfter = Number(response.headers.get("retry-after"))
|
|
156
|
-
const resolvedRetryAfter = Number.isNaN(retryAfter)
|
|
157
|
-
? DEFAULT_RETRY_AFTER
|
|
158
|
-
: retryAfter * 1000
|
|
159
|
-
|
|
160
|
-
await new Promise((resolve) => setTimeout(resolve, resolvedRetryAfter))
|
|
161
|
-
|
|
162
|
-
return request(url, init, fetchFn)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return response.clone()
|
|
166
|
-
}
|