@prismicio/client 6.6.3 → 6.7.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/client",
3
- "version": "6.6.3",
3
+ "version": "6.7.1",
4
4
  "description": "The official JavaScript + TypeScript client library for Prismic",
5
5
  "keywords": [
6
6
  "prismic",
@@ -45,34 +45,35 @@
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",
48
- "unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
48
+ "unit": "vitest run --coverage",
49
+ "unit:watch": "vitest watch"
49
50
  },
50
51
  "dependencies": {
51
- "@prismicio/helpers": "^2.3.2",
52
- "@prismicio/types": "^0.2.0"
52
+ "@prismicio/helpers": "^2.3.3",
53
+ "@prismicio/types": "^0.2.3"
53
54
  },
54
55
  "devDependencies": {
55
- "@size-limit/preset-small-lib": "^7.0.8",
56
+ "@prismicio/mock": "^0.1.1",
57
+ "@size-limit/preset-small-lib": "^8.0.1",
56
58
  "@types/sinon": "^10.0.13",
57
- "@typescript-eslint/eslint-plugin": "^5.31.0",
58
- "@typescript-eslint/parser": "^5.31.0",
59
+ "@typescript-eslint/eslint-plugin": "^5.34.0",
60
+ "@typescript-eslint/parser": "^5.34.0",
61
+ "@vitest/coverage-c8": "^0.22.1",
59
62
  "abort-controller": "^3.0.0",
60
- "ava": "^4.3.1",
61
- "esbuild-register": "^3.3.3",
62
- "eslint": "^8.20.0",
63
+ "eslint": "^8.22.0",
63
64
  "eslint-config-prettier": "^8.5.0",
64
65
  "eslint-plugin-prettier": "^4.2.1",
65
66
  "eslint-plugin-tsdoc": "^0.2.16",
66
- "msw": "^0.44.2",
67
- "node-fetch": "^3.2.9",
67
+ "msw": "^0.45.0",
68
+ "node-fetch": "^3.2.10",
68
69
  "nyc": "^15.1.0",
69
70
  "prettier": "^2.7.1",
70
71
  "prettier-plugin-jsdoc": "^0.3.38",
71
- "sinon": "^14.0.0",
72
72
  "siroc": "^0.16.0",
73
- "size-limit": "^7.0.8",
73
+ "size-limit": "^8.0.1",
74
74
  "standard-version": "^9.5.0",
75
- "typescript": "^4.7.4"
75
+ "typescript": "^4.7.4",
76
+ "vitest": "^0.22.1"
76
77
  },
77
78
  "engines": {
78
79
  "node": ">=12.13.0"
@@ -84,6 +84,16 @@ export interface QueryParams {
84
84
  * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
85
85
  */
86
86
  routes?: Route | string | (Route | string)[];
87
+
88
+ /**
89
+ * The `brokenRoute` option allows you to define the route populated in the
90
+ * `url` property for broken Link or Content Relationship fields. A broken
91
+ * link is a Link or Content Relationship field whose linked document has been
92
+ * unpublished or deleted.
93
+ *
94
+ * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
95
+ */
96
+ brokenRoute?: string;
87
97
  }
88
98
 
89
99
  /**
package/src/client.ts CHANGED
@@ -7,10 +7,9 @@ import { castThunk } from "./lib/castThunk";
7
7
  import { findMasterRef } from "./lib/findMasterRef";
8
8
  import { findRefByID } from "./lib/findRefByID";
9
9
  import { findRefByLabel } from "./lib/findRefByLabel";
10
- import { getCookie } from "./lib/getCookie";
10
+ import { getPreviewCookie } from "./lib/getPreviewCookie";
11
11
  import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery";
12
12
 
13
- import * as cookie from "./cookie";
14
13
  import {
15
14
  AbortSignalLike,
16
15
  FetchLike,
@@ -140,13 +139,23 @@ export type ClientConfig = {
140
139
  */
141
140
  routes?: NonNullable<BuildQueryURLArgs["routes"]>;
142
141
 
142
+ /**
143
+ * The `brokenRoute` option allows you to define the route populated in the
144
+ * `url` property for broken Link or Content Relationship fields. A broken
145
+ * link is a Link or Content Relationship field whose linked document has been
146
+ * unpublished or deleted.
147
+ *
148
+ * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
149
+ */
150
+ brokenRoute?: NonNullable<BuildQueryURLArgs["brokenRoute"]>;
151
+
143
152
  /**
144
153
  * Default parameters that will be sent with each query. These parameters can
145
154
  * be overridden on each query if needed.
146
155
  */
147
156
  defaultParams?: Omit<
148
157
  BuildQueryURLArgs,
149
- "ref" | "integrationFieldsRef" | "accessToken" | "routes"
158
+ "ref" | "integrationFieldsRef" | "accessToken" | "routes" | "brokenRoute"
150
159
  >;
151
160
 
152
161
  /**
@@ -315,6 +324,16 @@ export class Client<
315
324
  */
316
325
  routes?: NonNullable<BuildQueryURLArgs["routes"]>;
317
326
 
327
+ /**
328
+ * The `brokenRoute` option allows you to define the route populated in the
329
+ * `url` property for broken Link or Content Relationship fields. A broken
330
+ * link is a Link or Content Relationship field whose linked document has been
331
+ * unpublished or deleted.
332
+ *
333
+ * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
334
+ */
335
+ brokenRoute?: NonNullable<BuildQueryURLArgs["brokenRoute"]>;
336
+
318
337
  /**
319
338
  * The function used to make network requests to the Prismic REST API. In
320
339
  * environments where a global `fetch` function does not exist, such as
@@ -383,6 +402,7 @@ export class Client<
383
402
 
384
403
  this.accessToken = options.accessToken;
385
404
  this.routes = options.routes;
405
+ this.brokenRoute = options.brokenRoute;
386
406
  this.defaultParams = options.defaultParams;
387
407
 
388
408
  if (options.ref) {
@@ -531,12 +551,11 @@ export class Client<
531
551
  async getFirst<TDocument extends TDocuments>(
532
552
  params?: Partial<BuildQueryURLArgs> & FetchParams,
533
553
  ): Promise<TDocument> {
534
- const url = await this.buildQueryURL({
535
- pageSize:
536
- this.defaultParams?.pageSize ||
537
- (params && "page" in params ? undefined : 1),
538
- ...params,
539
- });
554
+ const actualParams = { ...params };
555
+ if (!(params && params.page) && !params?.pageSize) {
556
+ actualParams.pageSize = this.defaultParams?.pageSize ?? 1;
557
+ }
558
+ const url = await this.buildQueryURL(actualParams);
540
559
  const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
541
560
 
542
561
  const firstResult = result.results[0];
@@ -1237,6 +1256,7 @@ export class Client<
1237
1256
  ref,
1238
1257
  integrationFieldsRef,
1239
1258
  routes: params.routes || this.routes,
1259
+ brokenRoute: params.brokenRoute || this.brokenRoute,
1240
1260
  accessToken: params.accessToken || this.accessToken,
1241
1261
  });
1242
1262
  }
@@ -1395,7 +1415,7 @@ export class Client<
1395
1415
  /**
1396
1416
  * @deprecated Renamed to `graphQLFetch()` (note the capitalization of "QL").
1397
1417
  */
1398
- // TODO: Remove in v3
1418
+ // TODO: Remove in v7
1399
1419
  graphqlFetch = this.graphQLFetch.bind(this);
1400
1420
 
1401
1421
  /**
@@ -1427,15 +1447,13 @@ export class Client<
1427
1447
  */
1428
1448
  async graphQLFetch(
1429
1449
  input: RequestInfo,
1430
- init?: RequestInit,
1450
+ init?: Omit<RequestInit, "signal"> & { signal?: AbortSignalLike },
1431
1451
  ): Promise<Response> {
1432
1452
  const cachedRepository = await this.getCachedRepository();
1433
1453
  const ref = await this.getResolvedRefString();
1434
1454
 
1435
1455
  const unsanitizedHeaders: Record<string, string> = {
1436
1456
  "Prismic-ref": ref,
1437
- "Prismic-integration-field-ref":
1438
- cachedRepository.integrationFieldsRef || "",
1439
1457
  Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
1440
1458
  // Asserting `init.headers` is a Record since popular GraphQL
1441
1459
  // libraries pass this as a Record. Header objects as input
@@ -1443,6 +1461,11 @@ export class Client<
1443
1461
  ...(init ? (init.headers as Record<string, string>) : {}),
1444
1462
  };
1445
1463
 
1464
+ if (cachedRepository.integrationFieldsRef) {
1465
+ unsanitizedHeaders["Prismic-integration-field-ref"] =
1466
+ cachedRepository.integrationFieldsRef;
1467
+ }
1468
+
1446
1469
  // Normalize header keys to lowercase. This prevents header
1447
1470
  // conflicts between the Prismic client and the GraphQL
1448
1471
  // client.
@@ -1563,9 +1586,7 @@ export class Client<
1563
1586
 
1564
1587
  let cookieJar: string | null | undefined;
1565
1588
 
1566
- if (globalThis.document?.cookie) {
1567
- cookieJar = globalThis.document.cookie;
1568
- } else if (this.refState.httpRequest?.headers) {
1589
+ if (this.refState.httpRequest?.headers) {
1569
1590
  if (
1570
1591
  "get" in this.refState.httpRequest.headers &&
1571
1592
  typeof this.refState.httpRequest.headers.get === "function"
@@ -1576,10 +1597,12 @@ export class Client<
1576
1597
  // Express-style headers
1577
1598
  cookieJar = this.refState.httpRequest.headers.cookie;
1578
1599
  }
1600
+ } else if (globalThis.document?.cookie) {
1601
+ cookieJar = globalThis.document.cookie;
1579
1602
  }
1580
1603
 
1581
1604
  if (cookieJar) {
1582
- previewRef = getCookie(cookie.preview, cookieJar);
1605
+ previewRef = getPreviewCookie(cookieJar);
1583
1606
  }
1584
1607
 
1585
1608
  if (previewRef) {
package/src/index.ts CHANGED
@@ -40,6 +40,7 @@ export * as cookie from "./cookie";
40
40
  export type { CreateClient, ClientConfig } from "./client";
41
41
  export type { QueryParams, BuildQueryURLArgs } from "./buildQueryURL";
42
42
  export type {
43
+ AbortSignalLike,
43
44
  FetchLike,
44
45
  HttpRequestLike,
45
46
  Ordering,
@@ -0,0 +1,30 @@
1
+ import { preview as previewCookieName } from "../cookie";
2
+
3
+ const readValue = (value: string): string => {
4
+ return value.replace(/%3B/g, ";");
5
+ };
6
+
7
+ /**
8
+ * Returns the value of a cookie from a given cookie store.
9
+ *
10
+ * @param cookieJar - The stringified cookie store from which to read the cookie.
11
+ *
12
+ * @returns The value of the cookie, if it exists.
13
+ */
14
+ export const getPreviewCookie = (cookieJar: string): string | undefined => {
15
+ const cookies = cookieJar.split("; ");
16
+
17
+ let value: string | undefined;
18
+
19
+ for (const cookie of cookies) {
20
+ const parts = cookie.split("=");
21
+ const name = readValue(parts[0]).replace(/%3D/g, "=");
22
+
23
+ if (name === previewCookieName) {
24
+ value = readValue(parts.slice(1).join("="));
25
+ continue;
26
+ }
27
+ }
28
+
29
+ return value;
30
+ };
package/src/types.ts CHANGED
@@ -64,31 +64,29 @@ export type FetchLike = (
64
64
  *
65
65
  * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
66
66
  */
67
- // `any` is used often here to ensure this type is universally valid amond
67
+ // `any` is used often here to ensure this type is universally valid among
68
68
  // different AbortSignal implementations. The types of each property are not
69
69
  // important to validate since it is blindly passed to a given `fetch()`
70
70
  // function.
71
- export type AbortSignalLike = {
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- aborted: any;
74
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
- addEventListener: any;
76
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
- removeEventListener: any;
78
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
- dispatchEvent: any;
80
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
- onabort: any;
82
- };
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ export type AbortSignalLike = any;
83
73
 
84
74
  /**
85
75
  * The minimum required properties from RequestInit.
86
76
  */
87
77
  export interface RequestInitLike {
88
78
  headers?: Record<string, string>;
89
- // `null` is included to match TypeScript's `fetch()` type in `lib.dom.d.ts`.
90
- // See: https://github.com/microsoft/TypeScript/blob/3328feb7991f358e245088d48b64ad9da8f015e2/lib/lib.dom.d.ts#L1515-L1518
91
- signal?: AbortSignalLike | null;
79
+
80
+ /**
81
+ * An object that allows you to abort a `fetch()` request if needed via an
82
+ * `AbortController` object
83
+ *
84
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
85
+ */
86
+ // NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`
87
+ // for backwards compatibility (the type is exported) and to signal to
88
+ // other readers that this should be an AbortSignal-like object.
89
+ signal?: AbortSignalLike;
92
90
  }
93
91
 
94
92
  /**
@@ -140,6 +138,27 @@ export interface Ordering {
140
138
  /**
141
139
  * A `routes` parameter that determines how a document's URL field is resolved.
142
140
  *
141
+ * @example With a document's UID field.
142
+ *
143
+ * ```ts
144
+ * {
145
+ * "type": "page",
146
+ * "path": "/:uid"
147
+ * }
148
+ * ```
149
+ *
150
+ * @example With a Content Relationship `parent` field.
151
+ *
152
+ * ```ts
153
+ * {
154
+ * "type": "page",
155
+ * "path": "/:parent?/:uid",
156
+ * "resolvers": {
157
+ * "parent": "parent"
158
+ * }
159
+ * }
160
+ * ```
161
+ *
143
162
  * {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
144
163
  */
145
164
  export interface Route {
@@ -147,10 +166,24 @@ export interface Route {
147
166
  * The Custom Type of the document.
148
167
  */
149
168
  type: string;
169
+
170
+ /**
171
+ * A specific UID to which this route definition is scoped. The route is only
172
+ * defined for the document whose UID matches the given UID.
173
+ */
174
+ uid?: string;
175
+
176
+ /**
177
+ * A specific language to which this route definition is scoped. The route is
178
+ * only defined for documents whose language matches the given language.
179
+ */
180
+ lang?: string;
181
+
150
182
  /**
151
183
  * The resolved path of the document with optional placeholders.
152
184
  */
153
185
  path: string;
186
+
154
187
  /**
155
188
  * An object that lists the API IDs of the Content Relationships in the route.
156
189
  */
@@ -1,58 +0,0 @@
1
- /**
2
- * The following code is a modifed version of `es-cookie` taken from
3
- * https://github.com/theodorejb/es-cookie
4
- *
5
- * Copyright 2017 Theodore Brown
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in
15
- * all copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.*
24
- */
25
-
26
- const readValue = (value: string): string => {
27
- return value.replace(/%3B/g, ";");
28
- };
29
-
30
- export const parse = (cookieString: string): { [name: string]: string } => {
31
- const result: { [name: string]: string } = {};
32
- const cookies = cookieString.split("; ");
33
-
34
- for (const cookie of cookies) {
35
- const parts = cookie.split("=");
36
- const value = parts.slice(1).join("=");
37
- const name = readValue(parts[0]).replace(/%3D/g, "=");
38
- result[name] = readValue(value);
39
- }
40
-
41
- return result;
42
- };
43
-
44
- const getAll = (cookieStore: string): { [name: string]: string } =>
45
- parse(cookieStore);
46
-
47
- /**
48
- * Returns the value of a cookie from a given cookie store.
49
- *
50
- * @param name - Of the cookie.
51
- * @param cookieJar - The stringified cookie store from which to read the cookie.
52
- *
53
- * @returns The value of the cookie, if it exists.
54
- */
55
- export const getCookie = (
56
- name: string,
57
- cookieJar: string,
58
- ): string | undefined => getAll(cookieJar)[name];