@prismicio/client 6.7.2 → 6.8.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,158 @@
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
+ json(): Promise<any>;
75
+ }
76
+ /**
77
+ * The minimum required properties to treat as an HTTP Request for automatic
78
+ * Prismic preview support.
79
+ */
80
+ export type HttpRequestLike = /**
81
+ * Web API Request
82
+ *
83
+ * @see http://developer.mozilla.org/en-US/docs/Web/API/Request
84
+ */ {
85
+ headers?: {
86
+ get(name: string): string | null;
87
+ };
88
+ url?: string;
89
+ }
90
+ /**
91
+ * Express-style Request
92
+ */
93
+ | {
94
+ headers?: {
95
+ cookie?: string;
96
+ };
97
+ query?: Record<string, unknown>;
98
+ };
99
+ /**
100
+ * An `orderings` parameter that orders the results by the specified field.
101
+ *
102
+ * {@link https://prismic.io/docs/technologies/search-parameters-reference-rest-api#orderings}
103
+ */
104
+ export interface Ordering {
105
+ field: string;
106
+ direction?: "asc" | "desc";
107
+ }
108
+ /**
109
+ * A `routes` parameter that determines how a document's URL field is resolved.
110
+ *
111
+ * @example With a document's UID field.
112
+ *
113
+ * ```ts
114
+ * {
115
+ * "type": "page",
116
+ * "path": "/:uid"
117
+ * }
118
+ * ```
119
+ *
120
+ * @example With a Content Relationship `parent` field.
121
+ *
122
+ * ```ts
123
+ * {
124
+ * "type": "page",
125
+ * "path": "/:parent?/:uid",
126
+ * "resolvers": {
127
+ * "parent": "parent"
128
+ * }
129
+ * }
130
+ * ```
131
+ *
132
+ * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
133
+ */
134
+ export interface Route {
135
+ /**
136
+ * The Custom Type of the document.
137
+ */
138
+ type: string;
139
+ /**
140
+ * A specific UID to which this route definition is scoped. The route is only
141
+ * defined for the document whose UID matches the given UID.
142
+ */
143
+ uid?: string;
144
+ /**
145
+ * A specific language to which this route definition is scoped. The route is
146
+ * only defined for documents whose language matches the given language.
147
+ */
148
+ lang?: string;
149
+ /**
150
+ * The resolved path of the document with optional placeholders.
151
+ */
152
+ path: string;
153
+ /**
154
+ * An object that lists the API IDs of the Content Relationships in the route.
155
+ */
156
+ resolvers?: Record<string, string>;
157
+ }
158
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/client",
3
- "version": "6.7.2",
3
+ "version": "6.8.0",
4
4
  "description": "The official JavaScript + TypeScript client library for Prismic",
5
5
  "keywords": [
6
6
  "prismic",
@@ -49,7 +49,7 @@
49
49
  "unit:watch": "vitest watch"
50
50
  },
51
51
  "dependencies": {
52
- "@prismicio/helpers": "^2.3.7",
52
+ "@prismicio/helpers": "^2.3.8",
53
53
  "@prismicio/types": "^0.2.7"
54
54
  },
55
55
  "devDependencies": {
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";
@@ -164,18 +165,33 @@ export type ClientConfig = {
164
165
  * Node.js, this function must be provided.
165
166
  */
166
167
  fetch?: FetchLike;
168
+
169
+ /**
170
+ * Options provided to the client's `fetch()` on all network requests. These
171
+ * options will be merged with internally required options. They can also be
172
+ * overriden on a per-query basis using the query's `fetchOptions` parameter.
173
+ */
174
+ fetchOptions?: RequestInitLike;
167
175
  };
168
176
 
169
177
  /**
170
- * Parameters for any client method that use `fetch()`. Only a subset of
171
- * `fetch()` parameters are exposed.
178
+ * Parameters for any client method that use `fetch()`.
172
179
  */
173
180
  type FetchParams = {
181
+ /**
182
+ * Options provided to the client's `fetch()` on all network requests. These
183
+ * options will be merged with internally required options. They can also be
184
+ * overriden on a per-query basis using the query's `fetchOptions` parameter.
185
+ */
186
+ fetchOptions?: RequestInitLike;
187
+
174
188
  /**
175
189
  * An `AbortSignal` provided by an `AbortController`. This allows the network
176
190
  * request to be cancelled if necessary.
177
191
  *
178
192
  * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
193
+ *
194
+ * @deprecated Move the `signal` parameter into `fetchOptions.signal`.
179
195
  */
180
196
  signal?: AbortSignalLike;
181
197
  };
@@ -341,6 +357,8 @@ export class Client<
341
357
  */
342
358
  fetchFn: FetchLike;
343
359
 
360
+ fetchOptions?: RequestInitLike;
361
+
344
362
  /**
345
363
  * Default parameters that will be sent with each query. These parameters can
346
364
  * be overridden on each query if needed.
@@ -404,6 +422,7 @@ export class Client<
404
422
  this.accessToken = options.accessToken;
405
423
  this.routes = options.routes;
406
424
  this.brokenRoute = options.brokenRoute;
425
+ this.fetchOptions = options.fetchOptions;
407
426
  this.defaultParams = options.defaultParams;
408
427
 
409
428
  if (options.ref) {
@@ -1269,12 +1288,15 @@ export class Client<
1269
1288
  */
1270
1289
  async buildQueryURL({
1271
1290
  signal,
1291
+ fetchOptions,
1272
1292
  ...params
1273
1293
  }: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
1274
- const ref = params.ref || (await this.getResolvedRefString());
1294
+ const ref =
1295
+ params.ref || (await this.getResolvedRefString({ signal, fetchOptions }));
1275
1296
  const integrationFieldsRef =
1276
1297
  params.integrationFieldsRef ||
1277
- (await this.getCachedRepository({ signal })).integrationFieldsRef ||
1298
+ (await this.getCachedRepository({ signal, fetchOptions }))
1299
+ .integrationFieldsRef ||
1278
1300
  undefined;
1279
1301
 
1280
1302
  return buildQueryURL(this.endpoint, {
@@ -1338,9 +1360,10 @@ export class Client<
1338
1360
 
1339
1361
  if (documentID != null && previewToken != null) {
1340
1362
  const document = await this.getByID(documentID, {
1341
- signal: args.signal,
1342
1363
  ref: previewToken,
1343
1364
  lang: "*",
1365
+ signal: args.signal,
1366
+ fetchOptions: args.fetchOptions,
1344
1367
  });
1345
1368
 
1346
1369
  const url = prismicH.asLink(document, args.linkResolver);
@@ -1688,7 +1711,16 @@ export class Client<
1688
1711
  // : {};
1689
1712
 
1690
1713
  const res = await this.fetchFn(url, {
1691
- signal: params.signal,
1714
+ ...this.fetchOptions,
1715
+ ...params.fetchOptions,
1716
+ headers: {
1717
+ ...this.fetchOptions?.headers,
1718
+ ...params.fetchOptions?.headers,
1719
+ },
1720
+ signal:
1721
+ params.fetchOptions?.signal ||
1722
+ params.signal ||
1723
+ this.fetchOptions?.signal,
1692
1724
  });
1693
1725
 
1694
1726
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
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