@prismicio/client 6.6.3 → 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 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +152 -66
- package/dist/index.js.map +1 -1
- package/package.json +15 -14
- package/src/client.ts +17 -16
- package/src/index.ts +1 -0
- package/src/lib/getPreviewCookie.ts +30 -0
- package/src/types.ts +1 -1
- 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.2.
|
|
52
|
+
"@prismicio/helpers": "^2.3.3",
|
|
53
|
+
"@prismicio/types": "^0.2.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
+
"@prismicio/mock": "^0.1.1",
|
|
57
|
+
"@size-limit/preset-small-lib": "^8.0.0",
|
|
56
58
|
"@types/sinon": "^10.0.13",
|
|
57
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
58
|
-
"@typescript-eslint/parser": "^5.
|
|
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.20.0",
|
|
62
|
+
"c8": "^7.12.0",
|
|
63
|
+
"eslint": "^8.21.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
67
|
"msw": "^0.44.2",
|
|
67
|
-
"node-fetch": "^3.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,
|
|
@@ -531,12 +530,11 @@ export class Client<
|
|
|
531
530
|
async getFirst<TDocument extends TDocuments>(
|
|
532
531
|
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
533
532
|
): Promise<TDocument> {
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
});
|
|
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);
|
|
540
538
|
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
541
539
|
|
|
542
540
|
const firstResult = result.results[0];
|
|
@@ -1395,7 +1393,7 @@ export class Client<
|
|
|
1395
1393
|
/**
|
|
1396
1394
|
* @deprecated Renamed to `graphQLFetch()` (note the capitalization of "QL").
|
|
1397
1395
|
*/
|
|
1398
|
-
// TODO: Remove in
|
|
1396
|
+
// TODO: Remove in v7
|
|
1399
1397
|
graphqlFetch = this.graphQLFetch.bind(this);
|
|
1400
1398
|
|
|
1401
1399
|
/**
|
|
@@ -1427,15 +1425,13 @@ export class Client<
|
|
|
1427
1425
|
*/
|
|
1428
1426
|
async graphQLFetch(
|
|
1429
1427
|
input: RequestInfo,
|
|
1430
|
-
init?: RequestInit,
|
|
1428
|
+
init?: Omit<RequestInit, "signal"> & { signal?: AbortSignalLike },
|
|
1431
1429
|
): Promise<Response> {
|
|
1432
1430
|
const cachedRepository = await this.getCachedRepository();
|
|
1433
1431
|
const ref = await this.getResolvedRefString();
|
|
1434
1432
|
|
|
1435
1433
|
const unsanitizedHeaders: Record<string, string> = {
|
|
1436
1434
|
"Prismic-ref": ref,
|
|
1437
|
-
"Prismic-integration-field-ref":
|
|
1438
|
-
cachedRepository.integrationFieldsRef || "",
|
|
1439
1435
|
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
1440
1436
|
// Asserting `init.headers` is a Record since popular GraphQL
|
|
1441
1437
|
// libraries pass this as a Record. Header objects as input
|
|
@@ -1443,6 +1439,11 @@ export class Client<
|
|
|
1443
1439
|
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1444
1440
|
};
|
|
1445
1441
|
|
|
1442
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
1443
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] =
|
|
1444
|
+
cachedRepository.integrationFieldsRef;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1446
1447
|
// Normalize header keys to lowercase. This prevents header
|
|
1447
1448
|
// conflicts between the Prismic client and the GraphQL
|
|
1448
1449
|
// client.
|
|
@@ -1563,9 +1564,7 @@ export class Client<
|
|
|
1563
1564
|
|
|
1564
1565
|
let cookieJar: string | null | undefined;
|
|
1565
1566
|
|
|
1566
|
-
if (
|
|
1567
|
-
cookieJar = globalThis.document.cookie;
|
|
1568
|
-
} else if (this.refState.httpRequest?.headers) {
|
|
1567
|
+
if (this.refState.httpRequest?.headers) {
|
|
1569
1568
|
if (
|
|
1570
1569
|
"get" in this.refState.httpRequest.headers &&
|
|
1571
1570
|
typeof this.refState.httpRequest.headers.get === "function"
|
|
@@ -1576,10 +1575,12 @@ export class Client<
|
|
|
1576
1575
|
// Express-style headers
|
|
1577
1576
|
cookieJar = this.refState.httpRequest.headers.cookie;
|
|
1578
1577
|
}
|
|
1578
|
+
} else if (globalThis.document?.cookie) {
|
|
1579
|
+
cookieJar = globalThis.document.cookie;
|
|
1579
1580
|
}
|
|
1580
1581
|
|
|
1581
1582
|
if (cookieJar) {
|
|
1582
|
-
previewRef =
|
|
1583
|
+
previewRef = getPreviewCookie(cookieJar);
|
|
1583
1584
|
}
|
|
1584
1585
|
|
|
1585
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.
|
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];
|