@prismicio/client 6.0.0-alpha.8 → 6.0.0-beta.2
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/README.md +38 -34
- package/dist/index.cjs +218 -278
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +662 -571
- package/dist/index.mjs +482 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +29 -37
- package/src/ForbiddenError.ts +8 -41
- package/src/ParsingError.ts +4 -34
- package/src/PrismicError.ts +11 -12
- package/src/buildQueryURL.ts +29 -16
- package/src/client.ts +687 -569
- package/src/getEndpoint.ts +3 -3
- package/src/index.ts +2 -8
- package/src/lib/appendPredicates.ts +19 -16
- package/src/lib/castArray.ts +5 -3
- package/src/lib/castThunk.ts +11 -0
- package/src/lib/findMasterRef.ts +15 -0
- package/src/lib/findRef.ts +27 -0
- package/src/lib/findRefByID.ts +16 -0
- package/src/lib/findRefByLabel.ts +19 -0
- package/src/lib/getCookie.ts +20 -7
- package/src/lib/orElseThrow.ts +4 -5
- package/src/predicate.ts +247 -214
- package/src/types.ts +13 -68
- package/dist/index.es.js +0 -540
package/src/getEndpoint.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get a repository's Prismic REST API V2 endpoint.
|
|
3
3
|
*
|
|
4
|
-
* @typeParam RepositoryName Name of the Prismic repository.
|
|
5
|
-
* @param repositoryName Name of the repository.
|
|
4
|
+
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
5
|
+
* @param repositoryName - Name of the repository.
|
|
6
6
|
*
|
|
7
7
|
* @returns The repository's Prismic REST API V2 endpoint
|
|
8
8
|
*/
|
|
9
9
|
export const getEndpoint = <RepositoryName extends string>(
|
|
10
|
-
repositoryName: RepositoryName
|
|
10
|
+
repositoryName: RepositoryName,
|
|
11
11
|
): `https://${RepositoryName}.cdn.prismic.io/api/v2` =>
|
|
12
12
|
`https://${repositoryName}.cdn.prismic.io/api/v2` as const;
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ export * from "./buildQueryURL";
|
|
|
4
4
|
export { createClient, Client } from "./client";
|
|
5
5
|
|
|
6
6
|
// Predicates API.
|
|
7
|
-
export
|
|
7
|
+
export { predicate } from "./predicate";
|
|
8
8
|
|
|
9
9
|
// Custom errors used by Client.
|
|
10
10
|
export { PrismicError } from "./PrismicError";
|
|
@@ -18,15 +18,9 @@ export * as cookie from "./cookie";
|
|
|
18
18
|
export type { ClientConfig } from "./client";
|
|
19
19
|
export type {
|
|
20
20
|
FetchLike,
|
|
21
|
-
Form,
|
|
22
|
-
FormField,
|
|
23
21
|
HttpRequestLike,
|
|
24
|
-
Language,
|
|
25
22
|
Ordering,
|
|
26
|
-
Query,
|
|
27
|
-
Ref,
|
|
28
|
-
Repository,
|
|
29
23
|
RequestInitLike,
|
|
30
24
|
ResponseLike,
|
|
31
|
-
Route
|
|
25
|
+
Route,
|
|
32
26
|
} from "./types";
|
|
@@ -1,25 +1,28 @@
|
|
|
1
|
+
import { castArray } from "./castArray";
|
|
2
|
+
|
|
1
3
|
interface WithPredicates {
|
|
2
4
|
predicates?: string | string[];
|
|
3
5
|
}
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
* Adds one or more predicates to an object with a `predicates` property.
|
|
8
|
+
* Adds one or more predicates to an object with a `predicates` property.
|
|
9
|
+
* Appended predicates are added to the end of the existing list.
|
|
7
10
|
*
|
|
8
|
-
* @typeParam T Object to which predicates will be append.
|
|
9
|
-
* @param
|
|
11
|
+
* @typeParam T - Object to which predicates will be append.
|
|
12
|
+
* @param objWithPredicates - Object to append predicates on the `predicates` property.
|
|
13
|
+
* @param predicates - One or more predicates to append.
|
|
10
14
|
*
|
|
11
|
-
* @returns
|
|
15
|
+
* @returns The object with the appended predicates.
|
|
12
16
|
*/
|
|
13
|
-
export const appendPredicates =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* @param objWithPredicates Object to append predicates on the `predicates` property.
|
|
19
|
-
*
|
|
20
|
-
* @returns The object with the appended predicates.
|
|
21
|
-
*/
|
|
22
|
-
(objWithPredicates: T = {} as T): T => ({
|
|
17
|
+
export const appendPredicates = <T extends WithPredicates>(
|
|
18
|
+
objWithPredicates: T = {} as T,
|
|
19
|
+
predicates: string | string[],
|
|
20
|
+
) => {
|
|
21
|
+
return {
|
|
23
22
|
...objWithPredicates,
|
|
24
|
-
predicates: [
|
|
25
|
-
|
|
23
|
+
predicates: [
|
|
24
|
+
...(objWithPredicates.predicates || []),
|
|
25
|
+
...castArray(predicates),
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
};
|
package/src/lib/castArray.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Ensures that a value is an array. If it is already an array, it is returned
|
|
2
|
+
* Ensures that a value is an array. If it is already an array, it is returned
|
|
3
|
+
* as is. If it is not an array, it is converted to an array with itself as its
|
|
4
|
+
* only element.
|
|
3
5
|
*
|
|
4
|
-
* @typeParam A Element of the array.
|
|
5
|
-
* @param a Value to ensure is an array.
|
|
6
|
+
* @typeParam A - Element of the array.
|
|
7
|
+
* @param a - Value to ensure is an array.
|
|
6
8
|
*
|
|
7
9
|
* @returns `a` as an array.
|
|
8
10
|
*/
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that a value is a thunk. If it is already a thunk, it is returned as
|
|
3
|
+
* is. If it is not a thunk, it is converted to a thunk.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam A - Value returned by the thunk.
|
|
6
|
+
* @param a - Value to ensure is a thunk.
|
|
7
|
+
*
|
|
8
|
+
* @returns `a` as a a thunk.
|
|
9
|
+
*/
|
|
10
|
+
export const castThunk = <A>(a: A | (() => A)): (() => A) =>
|
|
11
|
+
typeof a === "function" ? (a as () => A) : () => a;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as prismicT from "@prismicio/types";
|
|
2
|
+
|
|
3
|
+
import { findRef } from "./findRef";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the master ref from a list of given refs.
|
|
7
|
+
*
|
|
8
|
+
* @param refs - A list of refs to search.
|
|
9
|
+
*
|
|
10
|
+
* @returns The master ref from the list.
|
|
11
|
+
* @throws If a matching ref cannot be found.
|
|
12
|
+
*/
|
|
13
|
+
export const findMasterRef = (refs: prismicT.Ref[]): prismicT.Ref => {
|
|
14
|
+
return findRef(refs, (ref) => ref.isMasterRef);
|
|
15
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as prismicT from "@prismicio/types";
|
|
2
|
+
|
|
3
|
+
import { PrismicError } from "../PrismicError";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the first ref from a list that passes a predicate (a function that
|
|
7
|
+
* returns true).
|
|
8
|
+
*
|
|
9
|
+
* @param refs - A list of refs to search.
|
|
10
|
+
* @param predicate - A function that determines if a ref from the list matches
|
|
11
|
+
* the criteria.
|
|
12
|
+
*
|
|
13
|
+
* @returns The first matching ref.
|
|
14
|
+
* @throws If a matching ref cannot be found.
|
|
15
|
+
*/
|
|
16
|
+
export const findRef = (
|
|
17
|
+
refs: prismicT.Ref[],
|
|
18
|
+
predicate: (ref: prismicT.Ref) => boolean,
|
|
19
|
+
): prismicT.Ref => {
|
|
20
|
+
const ref = refs.find((ref) => predicate(ref));
|
|
21
|
+
|
|
22
|
+
if (!ref) {
|
|
23
|
+
throw new PrismicError("Ref could not be found.", undefined, undefined);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return ref;
|
|
27
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as prismicT from "@prismicio/types";
|
|
2
|
+
|
|
3
|
+
import { findRef } from "./findRef";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the ref from a list of given refs with a matching ID.
|
|
7
|
+
*
|
|
8
|
+
* @param refs - A list of refs to search.
|
|
9
|
+
* @param id - The ID of the ref to find.
|
|
10
|
+
*
|
|
11
|
+
* @returns The ref with a matching ID from the list.
|
|
12
|
+
* @throws If a matching ref cannot be found.
|
|
13
|
+
*/
|
|
14
|
+
export const findRefByID = (refs: prismicT.Ref[], id: string): prismicT.Ref => {
|
|
15
|
+
return findRef(refs, (ref) => ref.id === id);
|
|
16
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as prismicT from "@prismicio/types";
|
|
2
|
+
|
|
3
|
+
import { findRef } from "./findRef";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the ref from a list of given refs with a matching label.
|
|
7
|
+
*
|
|
8
|
+
* @param refs - A list of refs to search.
|
|
9
|
+
* @param id - The label of the ref to find.
|
|
10
|
+
*
|
|
11
|
+
* @returns The ref with a matching label from the list.
|
|
12
|
+
* @throws If a matching ref cannot be found.
|
|
13
|
+
*/
|
|
14
|
+
export const findRefByLabel = (
|
|
15
|
+
refs: prismicT.Ref[],
|
|
16
|
+
label: string,
|
|
17
|
+
): prismicT.Ref => {
|
|
18
|
+
return findRef(refs, (ref) => ref.label === label);
|
|
19
|
+
};
|
package/src/lib/getCookie.ts
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The following code is a modifed version of `es-cookie` taken from
|
|
2
|
+
* The following code is a modifed version of `es-cookie` taken from
|
|
3
|
+
* https://github.com/theodorejb/es-cookie
|
|
3
4
|
*
|
|
4
5
|
* Copyright 2017 Theodore Brown
|
|
5
6
|
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
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:
|
|
7
13
|
*
|
|
8
|
-
* The above copyright notice and this permission notice shall be included in
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
|
15
|
+
* all copies or substantial portions of the Software.
|
|
9
16
|
*
|
|
10
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
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.*
|
|
11
24
|
*/
|
|
12
25
|
|
|
13
26
|
const readValue = (value: string): string => {
|
|
@@ -34,12 +47,12 @@ const getAll = (cookieStore: string): { [name: string]: string } =>
|
|
|
34
47
|
/**
|
|
35
48
|
* Returns the value of a cookie from a given cookie store.
|
|
36
49
|
*
|
|
37
|
-
* @param Name
|
|
38
|
-
* @param cookieStore The stringified cookie store from which to read the cookie.
|
|
50
|
+
* @param Name - Of the cookie.
|
|
51
|
+
* @param cookieStore - The stringified cookie store from which to read the cookie.
|
|
39
52
|
*
|
|
40
53
|
* @returns The value of the cookie, if it exists.
|
|
41
54
|
*/
|
|
42
55
|
export const getCookie = (
|
|
43
56
|
name: string,
|
|
44
|
-
cookieStore: string
|
|
57
|
+
cookieStore: string,
|
|
45
58
|
): string | undefined => getAll(cookieStore)[name];
|
package/src/lib/orElseThrow.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Throws an error if a given value is nullish.
|
|
3
3
|
*
|
|
4
|
-
* @typeParam Nullable
|
|
5
|
-
* @param a Nullable value to check.
|
|
6
|
-
* @param error Error to throw if `a` is nullish.
|
|
7
|
-
*
|
|
8
|
-
* @throws Throws the given error if `a` is nullish.
|
|
4
|
+
* @typeParam Nullable - Value to check.
|
|
5
|
+
* @param a -Nullable value to check.
|
|
6
|
+
* @param error - Error to throw if `a` is nullish.
|
|
9
7
|
*
|
|
10
8
|
* @returns `a`, whose type is narrowed to be non-nullable.
|
|
9
|
+
* @throws Throws the given error if `a` is nullish.
|
|
11
10
|
*/
|
|
12
11
|
export const orElseThrow = <T>(a: T | null | undefined, error: Error): T => {
|
|
13
12
|
if (a == null) {
|