@prismicio/client 6.7.3 → 6.9.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/types.d.ts CHANGED
@@ -1,156 +1,165 @@
1
- import * as prismicT from "@prismicio/types";
2
- /**
3
- * Create a union of the given object's values, and optionally specify which
4
- * keys to get the values from.
5
- *
6
- * Taken from the `type-fest` package.
7
- *
8
- * See:
9
- * https://github.com/sindresorhus/type-fest/blob/61c35052f09caa23de5eef96d95196375d8ed498/source/value-of.d.ts
10
- */
11
- export type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
12
- /**
13
- * Functions like TypeScript's native `Extract` utility type with added support
14
- * to fall back to a value if the resulting union is `never`.
15
- *
16
- * @example
17
- *
18
- * ```ts
19
- * ExtractOrFallback<"a" | "b", "a">; // => "a"
20
- * ExtractOrFallback<"a" | "b", "c">; // => "a" | "b"
21
- * ExtractOrFallback<"a" | "b", "c", "foo">; // => "foo"
22
- * ```
23
- *
24
- * @typeParam T - The union from which values will be extracted.
25
- * @typeParam U - The extraction condition.
26
- * @typeParam Fallback - The value to return if the resulting union is `never`.
27
- * Defaults to `T`.
28
- */
29
- type ExtractOrFallback<T, U, Fallback = T> = Extract<T, U> extends never ? Fallback : Extract<T, U>;
30
- /**
31
- * Extracts one or more Prismic document types that match a given Prismic
32
- * document type. If no matches are found, no extraction is performed and the
33
- * union of all provided Prismic document types are returned.
34
- *
35
- * @typeParam TDocuments - Prismic document types from which to extract.
36
- * @typeParam TDocumentType - Type(s) to match `TDocuments` against.
37
- */
38
- export type ExtractDocumentType<TDocuments extends prismicT.PrismicDocument, TDocumentType extends TDocuments["type"]> = ExtractOrFallback<TDocuments, {
39
- type: TDocumentType;
40
- }>;
41
- /**
42
- * A universal API to make network requests. A subset of the `fetch()` API.
43
- *
44
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
45
- */
46
- export type FetchLike = (input: string, init?: RequestInitLike) => Promise<ResponseLike>;
47
- /**
48
- * An object that allows you to abort a `fetch()` request if needed via an
49
- * `AbortController` object
50
- *
51
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
52
- */
53
- export type AbortSignalLike = any;
54
- /**
55
- * The minimum required properties from RequestInit.
56
- */
57
- export interface RequestInitLike {
58
- headers?: Record<string, string>;
59
- /**
60
- * An object that allows you to abort a `fetch()` request if needed via an
61
- * `AbortController` object
62
- *
63
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
64
- */
65
- signal?: AbortSignalLike;
66
- }
67
- /**
68
- * The minimum required properties from Response.
69
- */
70
- export interface ResponseLike {
71
- status: number;
72
- json(): Promise<any>;
73
- }
74
- /**
75
- * The minimum required properties to treat as an HTTP Request for automatic
76
- * Prismic preview support.
77
- */
78
- export type HttpRequestLike = /**
79
- * Web API Request
80
- *
81
- * @see http://developer.mozilla.org/en-US/docs/Web/API/Request
82
- */ {
83
- headers?: {
84
- get(name: string): string | null;
85
- };
86
- url?: string;
87
- }
88
- /**
89
- * Express-style Request
90
- */
91
- | {
92
- headers?: {
93
- cookie?: string;
94
- };
95
- query?: Record<string, unknown>;
96
- };
97
- /**
98
- * An `orderings` parameter that orders the results by the specified field.
99
- *
100
- * {@link https://prismic.io/docs/technologies/search-parameters-reference-rest-api#orderings}
101
- */
102
- export interface Ordering {
103
- field: string;
104
- direction?: "asc" | "desc";
105
- }
106
- /**
107
- * A `routes` parameter that determines how a document's URL field is resolved.
108
- *
109
- * @example With a document's UID field.
110
- *
111
- * ```ts
112
- * {
113
- * "type": "page",
114
- * "path": "/:uid"
115
- * }
116
- * ```
117
- *
118
- * @example With a Content Relationship `parent` field.
119
- *
120
- * ```ts
121
- * {
122
- * "type": "page",
123
- * "path": "/:parent?/:uid",
124
- * "resolvers": {
125
- * "parent": "parent"
126
- * }
127
- * }
128
- * ```
129
- *
130
- * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
131
- */
132
- export interface Route {
133
- /**
134
- * The Custom Type of the document.
135
- */
136
- type: string;
137
- /**
138
- * A specific UID to which this route definition is scoped. The route is only
139
- * defined for the document whose UID matches the given UID.
140
- */
141
- uid?: string;
142
- /**
143
- * A specific language to which this route definition is scoped. The route is
144
- * only defined for documents whose language matches the given language.
145
- */
146
- lang?: string;
147
- /**
148
- * The resolved path of the document with optional placeholders.
149
- */
150
- path: string;
151
- /**
152
- * An object that lists the API IDs of the Content Relationships in the route.
153
- */
154
- resolvers?: Record<string, string>;
155
- }
156
- export {};
1
+ import * as prismicT from "@prismicio/types";
2
+ /**
3
+ * Create a union of the given object's values, and optionally specify which
4
+ * keys to get the values from.
5
+ *
6
+ * Taken from the `type-fest` package.
7
+ *
8
+ * See:
9
+ * https://github.com/sindresorhus/type-fest/blob/61c35052f09caa23de5eef96d95196375d8ed498/source/value-of.d.ts
10
+ */
11
+ export type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
12
+ /**
13
+ * Functions like TypeScript's native `Extract` utility type with added support
14
+ * to fall back to a value if the resulting union is `never`.
15
+ *
16
+ * @example
17
+ *
18
+ * ```ts
19
+ * ExtractOrFallback<"a" | "b", "a">; // => "a"
20
+ * ExtractOrFallback<"a" | "b", "c">; // => "a" | "b"
21
+ * ExtractOrFallback<"a" | "b", "c", "foo">; // => "foo"
22
+ * ```
23
+ *
24
+ * @typeParam T - The union from which values will be extracted.
25
+ * @typeParam U - The extraction condition.
26
+ * @typeParam Fallback - The value to return if the resulting union is `never`.
27
+ * Defaults to `T`.
28
+ */
29
+ type ExtractOrFallback<T, U, Fallback = T> = Extract<T, U> extends never ? Fallback : Extract<T, U>;
30
+ /**
31
+ * Extracts one or more Prismic document types that match a given Prismic
32
+ * document type. If no matches are found, no extraction is performed and the
33
+ * union of all provided Prismic document types are returned.
34
+ *
35
+ * @typeParam TDocuments - Prismic document types from which to extract.
36
+ * @typeParam TDocumentType - Type(s) to match `TDocuments` against.
37
+ */
38
+ export type ExtractDocumentType<TDocuments extends prismicT.PrismicDocument, TDocumentType extends TDocuments["type"]> = ExtractOrFallback<TDocuments, {
39
+ type: TDocumentType;
40
+ }>;
41
+ /**
42
+ * A universal API to make network requests. A subset of the `fetch()` API.
43
+ *
44
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
45
+ */
46
+ export type FetchLike = (input: string, init?: RequestInitLike) => Promise<ResponseLike>;
47
+ /**
48
+ * An object that allows you to abort a `fetch()` request if needed via an
49
+ * `AbortController` object
50
+ *
51
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
52
+ */
53
+ export type AbortSignalLike = any;
54
+ /**
55
+ * A subset of RequestInit properties to configure a `fetch()` request.
56
+ */
57
+ export interface RequestInitLike extends Pick<RequestInit, "cache"> {
58
+ /**
59
+ * An object literal to set the `fetch()` request's headers.
60
+ */
61
+ headers?: Record<string, string>;
62
+ /**
63
+ * An AbortSignal to set the `fetch()` request's signal.
64
+ *
65
+ * See: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
66
+ */
67
+ signal?: AbortSignalLike;
68
+ }
69
+ /**
70
+ * The minimum required properties from Response.
71
+ */
72
+ export interface ResponseLike {
73
+ status: number;
74
+ headers: HeadersLike;
75
+ json(): Promise<any>;
76
+ }
77
+ /**
78
+ * The minimum required properties from Headers.
79
+ */
80
+ export interface HeadersLike {
81
+ get(name: string): string | null;
82
+ }
83
+ /**
84
+ * The minimum required properties to treat as an HTTP Request for automatic
85
+ * Prismic preview support.
86
+ */
87
+ export type HttpRequestLike = /**
88
+ * Web API Request
89
+ *
90
+ * @see http://developer.mozilla.org/en-US/docs/Web/API/Request
91
+ */ {
92
+ headers?: {
93
+ get(name: string): string | null;
94
+ };
95
+ url?: string;
96
+ }
97
+ /**
98
+ * Express-style Request
99
+ */
100
+ | {
101
+ headers?: {
102
+ cookie?: string;
103
+ };
104
+ query?: Record<string, unknown>;
105
+ };
106
+ /**
107
+ * An `orderings` parameter that orders the results by the specified field.
108
+ *
109
+ * {@link https://prismic.io/docs/technologies/search-parameters-reference-rest-api#orderings}
110
+ */
111
+ export interface Ordering {
112
+ field: string;
113
+ direction?: "asc" | "desc";
114
+ }
115
+ /**
116
+ * A `routes` parameter that determines how a document's URL field is resolved.
117
+ *
118
+ * @example With a document's UID field.
119
+ *
120
+ * ```ts
121
+ * {
122
+ * "type": "page",
123
+ * "path": "/:uid"
124
+ * }
125
+ * ```
126
+ *
127
+ * @example With a Content Relationship `parent` field.
128
+ *
129
+ * ```ts
130
+ * {
131
+ * "type": "page",
132
+ * "path": "/:parent?/:uid",
133
+ * "resolvers": {
134
+ * "parent": "parent"
135
+ * }
136
+ * }
137
+ * ```
138
+ *
139
+ * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
140
+ */
141
+ export interface Route {
142
+ /**
143
+ * The Custom Type of the document.
144
+ */
145
+ type: string;
146
+ /**
147
+ * A specific UID to which this route definition is scoped. The route is only
148
+ * defined for the document whose UID matches the given UID.
149
+ */
150
+ uid?: string;
151
+ /**
152
+ * A specific language to which this route definition is scoped. The route is
153
+ * only defined for documents whose language matches the given language.
154
+ */
155
+ lang?: string;
156
+ /**
157
+ * The resolved path of the document with optional placeholders.
158
+ */
159
+ path: string;
160
+ /**
161
+ * An object that lists the API IDs of the Content Relationships in the route.
162
+ */
163
+ resolvers?: Record<string, string>;
164
+ }
165
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/client",
3
- "version": "6.7.3",
3
+ "version": "6.9.0",
4
4
  "description": "The official JavaScript + TypeScript client library for Prismic",
5
5
  "keywords": [
6
6
  "prismic",
@@ -39,9 +39,9 @@
39
39
  "format": "prettier --write .",
40
40
  "lint": "eslint --ext .js,.ts .",
41
41
  "prepare": "npm run build",
42
- "release": "npm run build && npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
43
- "release:beta": "npm run build && npm run test && standard-version --release-as major --prerelease beta && git push --follow-tags && npm run build && npm publish --tag beta",
44
- "release:beta:dry": "standard-version --release-as major --prerelease beta --dry-run",
42
+ "release": "npm run build && npm run test && standard-version && git push --follow-tags && npm run build && npm publish --tag latest-v6",
43
+ "release:beta": "npm run build && npm run test && standard-version --release-as major --prerelease beta-v6 && git push --follow-tags && npm run build && npm publish --tag beta-v6",
44
+ "release:beta:dry": "standard-version --release-as major --prerelease beta-v6 --dry-run",
45
45
  "release:dry": "standard-version --dry-run",
46
46
  "size": "size-limit",
47
47
  "test": "npm run lint && npm run unit && npm run build && npm run size",
package/src/client.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  FetchLike,
16
16
  HttpRequestLike,
17
17
  ExtractDocumentType,
18
+ RequestInitLike,
18
19
  } from "./types";
19
20
  import { ForbiddenError } from "./ForbiddenError";
20
21
  import { NotFoundError } from "./NotFoundError";
@@ -48,6 +49,15 @@ export const REPOSITORY_CACHE_TTL = 5000;
48
49
  */
49
50
  export const GET_ALL_QUERY_DELAY = 500;
50
51
 
52
+ /**
53
+ * The default number of milliseconds to wait before retrying a rate-limited
54
+ * `fetch()` request (429 response code). The default value is only used if the
55
+ * response does not include a `retry-after` header.
56
+ *
57
+ * The API allows up to 200 requests per second.
58
+ */
59
+ const DEFUALT_RETRY_AFTER_MS = 1000;
60
+
51
61
  /**
52
62
  * Modes for client ref management.
53
63
  */
@@ -164,18 +174,33 @@ export type ClientConfig = {
164
174
  * Node.js, this function must be provided.
165
175
  */
166
176
  fetch?: FetchLike;
177
+
178
+ /**
179
+ * Options provided to the client's `fetch()` on all network requests. These
180
+ * options will be merged with internally required options. They can also be
181
+ * overriden on a per-query basis using the query's `fetchOptions` parameter.
182
+ */
183
+ fetchOptions?: RequestInitLike;
167
184
  };
168
185
 
169
186
  /**
170
- * Parameters for any client method that use `fetch()`. Only a subset of
171
- * `fetch()` parameters are exposed.
187
+ * Parameters for any client method that use `fetch()`.
172
188
  */
173
189
  type FetchParams = {
190
+ /**
191
+ * Options provided to the client's `fetch()` on all network requests. These
192
+ * options will be merged with internally required options. They can also be
193
+ * overriden on a per-query basis using the query's `fetchOptions` parameter.
194
+ */
195
+ fetchOptions?: RequestInitLike;
196
+
174
197
  /**
175
198
  * An `AbortSignal` provided by an `AbortController`. This allows the network
176
199
  * request to be cancelled if necessary.
177
200
  *
178
201
  * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
202
+ *
203
+ * @deprecated Move the `signal` parameter into `fetchOptions.signal`.
179
204
  */
180
205
  signal?: AbortSignalLike;
181
206
  };
@@ -341,6 +366,8 @@ export class Client<
341
366
  */
342
367
  fetchFn: FetchLike;
343
368
 
369
+ fetchOptions?: RequestInitLike;
370
+
344
371
  /**
345
372
  * Default parameters that will be sent with each query. These parameters can
346
373
  * be overridden on each query if needed.
@@ -404,6 +431,7 @@ export class Client<
404
431
  this.accessToken = options.accessToken;
405
432
  this.routes = options.routes;
406
433
  this.brokenRoute = options.brokenRoute;
434
+ this.fetchOptions = options.fetchOptions;
407
435
  this.defaultParams = options.defaultParams;
408
436
 
409
437
  if (options.ref) {
@@ -1269,12 +1297,15 @@ export class Client<
1269
1297
  */
1270
1298
  async buildQueryURL({
1271
1299
  signal,
1300
+ fetchOptions,
1272
1301
  ...params
1273
1302
  }: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
1274
- const ref = params.ref || (await this.getResolvedRefString());
1303
+ const ref =
1304
+ params.ref || (await this.getResolvedRefString({ signal, fetchOptions }));
1275
1305
  const integrationFieldsRef =
1276
1306
  params.integrationFieldsRef ||
1277
- (await this.getCachedRepository({ signal })).integrationFieldsRef ||
1307
+ (await this.getCachedRepository({ signal, fetchOptions }))
1308
+ .integrationFieldsRef ||
1278
1309
  undefined;
1279
1310
 
1280
1311
  return buildQueryURL(this.endpoint, {
@@ -1338,9 +1369,10 @@ export class Client<
1338
1369
 
1339
1370
  if (documentID != null && previewToken != null) {
1340
1371
  const document = await this.getByID(documentID, {
1341
- signal: args.signal,
1342
1372
  ref: previewToken,
1343
1373
  lang: "*",
1374
+ signal: args.signal,
1375
+ fetchOptions: args.fetchOptions,
1344
1376
  });
1345
1377
 
1346
1378
  const url = prismicH.asLink(document, args.linkResolver);
@@ -1688,7 +1720,16 @@ export class Client<
1688
1720
  // : {};
1689
1721
 
1690
1722
  const res = await this.fetchFn(url, {
1691
- signal: params.signal,
1723
+ ...this.fetchOptions,
1724
+ ...params.fetchOptions,
1725
+ headers: {
1726
+ ...this.fetchOptions?.headers,
1727
+ ...params.fetchOptions?.headers,
1728
+ },
1729
+ signal:
1730
+ params.fetchOptions?.signal ||
1731
+ params.signal ||
1732
+ this.fetchOptions?.signal,
1692
1733
  });
1693
1734
 
1694
1735
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1738,6 +1779,25 @@ export class Client<
1738
1779
  json,
1739
1780
  );
1740
1781
  }
1782
+
1783
+ // Too Many Requests
1784
+ // - Exceeded the maximum number of requests per second
1785
+ case 429: {
1786
+ const parsedRetryAfter = Number(res.headers.get("retry-after"));
1787
+ const delay = Number.isNaN(parsedRetryAfter)
1788
+ ? DEFUALT_RETRY_AFTER_MS
1789
+ : parsedRetryAfter;
1790
+
1791
+ return await new Promise((resolve, reject) => {
1792
+ setTimeout(async () => {
1793
+ try {
1794
+ resolve(await this.fetch(url, params));
1795
+ } catch (error) {
1796
+ reject(error);
1797
+ }
1798
+ }, delay);
1799
+ });
1800
+ }
1741
1801
  }
1742
1802
 
1743
1803
  throw new PrismicError(undefined, url, json);
package/src/types.ts CHANGED
@@ -72,16 +72,23 @@ export type FetchLike = (
72
72
  export type AbortSignalLike = any;
73
73
 
74
74
  /**
75
- * The minimum required properties from RequestInit.
75
+ * A subset of RequestInit properties to configure a `fetch()` request.
76
76
  */
77
- export interface RequestInitLike {
77
+ // Only options relevant to the client are included. Extending from the full
78
+ // RequestInit would cause issues, such as accepting Header objects.
79
+ //
80
+ // An interface is used to allow other libraries to augment the type with
81
+ // environment-specific types.
82
+ export interface RequestInitLike extends Pick<RequestInit, "cache"> {
83
+ /**
84
+ * An object literal to set the `fetch()` request's headers.
85
+ */
78
86
  headers?: Record<string, string>;
79
87
 
80
88
  /**
81
- * An object that allows you to abort a `fetch()` request if needed via an
82
- * `AbortController` object
89
+ * An AbortSignal to set the `fetch()` request's signal.
83
90
  *
84
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
91
+ * See: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
85
92
  */
86
93
  // NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`
87
94
  // for backwards compatibility (the type is exported) and to signal to
@@ -94,10 +101,18 @@ export interface RequestInitLike {
94
101
  */
95
102
  export interface ResponseLike {
96
103
  status: number;
104
+ headers: HeadersLike;
97
105
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
106
  json(): Promise<any>;
99
107
  }
100
108
 
109
+ /**
110
+ * The minimum required properties from Headers.
111
+ */
112
+ export interface HeadersLike {
113
+ get(name: string): string | null;
114
+ }
115
+
101
116
  /**
102
117
  * The minimum required properties to treat as an HTTP Request for automatic
103
118
  * Prismic preview support.