@prismicio/client 6.6.1 → 6.6.4
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/index.cjs +152 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +152 -62
- package/dist/index.js.map +1 -1
- package/package.json +18 -17
- package/src/client.ts +18 -12
- package/src/index.ts +1 -0
- package/src/lib/getPreviewCookie.ts +30 -0
- package/src/types.ts +1 -5
- package/src/lib/getCookie.ts +0 -58
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/client",
|
|
3
|
-
"version": "6.6.
|
|
3
|
+
"version": "6.6.4",
|
|
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": "
|
|
48
|
+
"unit": "vitest run --coverage",
|
|
49
|
+
"unit:watch": "vitest watch"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"@prismicio/helpers": "^2.3.
|
|
52
|
-
"@prismicio/types": "^0.1
|
|
52
|
+
"@prismicio/helpers": "^2.3.3",
|
|
53
|
+
"@prismicio/types": "^0.2.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"@typescript-eslint/
|
|
56
|
+
"@prismicio/mock": "^0.1.1",
|
|
57
|
+
"@size-limit/preset-small-lib": "^8.0.0",
|
|
58
|
+
"@types/sinon": "^10.0.13",
|
|
59
|
+
"@typescript-eslint/eslint-plugin": "^5.32.0",
|
|
60
|
+
"@typescript-eslint/parser": "^5.32.0",
|
|
59
61
|
"abort-controller": "^3.0.0",
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"eslint": "^8.17.0",
|
|
62
|
+
"c8": "^7.12.0",
|
|
63
|
+
"eslint": "^8.21.0",
|
|
63
64
|
"eslint-config-prettier": "^8.5.0",
|
|
64
|
-
"eslint-plugin-prettier": "^4.
|
|
65
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
65
66
|
"eslint-plugin-tsdoc": "^0.2.16",
|
|
66
|
-
"msw": "^0.
|
|
67
|
-
"node-fetch": "^3.2.
|
|
67
|
+
"msw": "^0.44.2",
|
|
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": "^
|
|
73
|
+
"size-limit": "^8.0.0",
|
|
74
74
|
"standard-version": "^9.5.0",
|
|
75
|
-
"typescript": "^4.7.4"
|
|
75
|
+
"typescript": "^4.7.4",
|
|
76
|
+
"vitest": "^0.21.0"
|
|
76
77
|
},
|
|
77
78
|
"engines": {
|
|
78
79
|
"node": ">=12.13.0"
|
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 {
|
|
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,
|
|
@@ -392,7 +391,7 @@ export class Client<
|
|
|
392
391
|
if (typeof options.fetch === "function") {
|
|
393
392
|
this.fetchFn = options.fetch;
|
|
394
393
|
} else if (typeof globalThis.fetch === "function") {
|
|
395
|
-
this.fetchFn = globalThis.fetch;
|
|
394
|
+
this.fetchFn = globalThis.fetch as FetchLike;
|
|
396
395
|
} else {
|
|
397
396
|
throw new PrismicError(
|
|
398
397
|
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
|
|
@@ -531,7 +530,11 @@ export class Client<
|
|
|
531
530
|
async getFirst<TDocument extends TDocuments>(
|
|
532
531
|
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
533
532
|
): Promise<TDocument> {
|
|
534
|
-
const
|
|
533
|
+
const actualParams = { ...params };
|
|
534
|
+
if (!(params && params.page) && !params?.pageSize) {
|
|
535
|
+
actualParams.pageSize = this.defaultParams?.pageSize ?? 1;
|
|
536
|
+
}
|
|
537
|
+
const url = await this.buildQueryURL(actualParams);
|
|
535
538
|
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
536
539
|
|
|
537
540
|
const firstResult = result.results[0];
|
|
@@ -1390,7 +1393,7 @@ export class Client<
|
|
|
1390
1393
|
/**
|
|
1391
1394
|
* @deprecated Renamed to `graphQLFetch()` (note the capitalization of "QL").
|
|
1392
1395
|
*/
|
|
1393
|
-
// TODO: Remove in
|
|
1396
|
+
// TODO: Remove in v7
|
|
1394
1397
|
graphqlFetch = this.graphQLFetch.bind(this);
|
|
1395
1398
|
|
|
1396
1399
|
/**
|
|
@@ -1422,15 +1425,13 @@ export class Client<
|
|
|
1422
1425
|
*/
|
|
1423
1426
|
async graphQLFetch(
|
|
1424
1427
|
input: RequestInfo,
|
|
1425
|
-
init?: RequestInit,
|
|
1428
|
+
init?: Omit<RequestInit, "signal"> & { signal?: AbortSignalLike },
|
|
1426
1429
|
): Promise<Response> {
|
|
1427
1430
|
const cachedRepository = await this.getCachedRepository();
|
|
1428
1431
|
const ref = await this.getResolvedRefString();
|
|
1429
1432
|
|
|
1430
1433
|
const unsanitizedHeaders: Record<string, string> = {
|
|
1431
1434
|
"Prismic-ref": ref,
|
|
1432
|
-
"Prismic-integration-field-ref":
|
|
1433
|
-
cachedRepository.integrationFieldsRef || "",
|
|
1434
1435
|
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
1435
1436
|
// Asserting `init.headers` is a Record since popular GraphQL
|
|
1436
1437
|
// libraries pass this as a Record. Header objects as input
|
|
@@ -1438,6 +1439,11 @@ export class Client<
|
|
|
1438
1439
|
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1439
1440
|
};
|
|
1440
1441
|
|
|
1442
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
1443
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] =
|
|
1444
|
+
cachedRepository.integrationFieldsRef;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1441
1447
|
// Normalize header keys to lowercase. This prevents header
|
|
1442
1448
|
// conflicts between the Prismic client and the GraphQL
|
|
1443
1449
|
// client.
|
|
@@ -1558,9 +1564,7 @@ export class Client<
|
|
|
1558
1564
|
|
|
1559
1565
|
let cookieJar: string | null | undefined;
|
|
1560
1566
|
|
|
1561
|
-
if (
|
|
1562
|
-
cookieJar = globalThis.document.cookie;
|
|
1563
|
-
} else if (this.refState.httpRequest?.headers) {
|
|
1567
|
+
if (this.refState.httpRequest?.headers) {
|
|
1564
1568
|
if (
|
|
1565
1569
|
"get" in this.refState.httpRequest.headers &&
|
|
1566
1570
|
typeof this.refState.httpRequest.headers.get === "function"
|
|
@@ -1571,10 +1575,12 @@ export class Client<
|
|
|
1571
1575
|
// Express-style headers
|
|
1572
1576
|
cookieJar = this.refState.httpRequest.headers.cookie;
|
|
1573
1577
|
}
|
|
1578
|
+
} else if (globalThis.document?.cookie) {
|
|
1579
|
+
cookieJar = globalThis.document.cookie;
|
|
1574
1580
|
}
|
|
1575
1581
|
|
|
1576
1582
|
if (cookieJar) {
|
|
1577
|
-
previewRef =
|
|
1583
|
+
previewRef = getPreviewCookie(cookieJar);
|
|
1578
1584
|
}
|
|
1579
1585
|
|
|
1580
1586
|
if (previewRef) {
|
package/src/index.ts
CHANGED
|
@@ -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,7 +64,7 @@ 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
|
|
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.
|
|
@@ -79,10 +79,6 @@ export type AbortSignalLike = {
|
|
|
79
79
|
dispatchEvent: any;
|
|
80
80
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
81
|
onabort: any;
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
-
reason: any;
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
-
throwIfAborted: any;
|
|
86
82
|
};
|
|
87
83
|
|
|
88
84
|
/**
|
package/src/lib/getCookie.ts
DELETED
|
@@ -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];
|