@prismicio/client 6.0.0-alpha.9 → 6.0.0-beta.3
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 +226 -300
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +364 -433
- package/dist/index.mjs +220 -292
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -25
- package/src/ForbiddenError.ts +8 -41
- package/src/ParsingError.ts +4 -34
- package/src/PrismicError.ts +11 -12
- package/src/buildQueryURL.ts +1 -6
- package/src/client.ts +391 -413
- package/src/index.ts +1 -7
- package/src/lib/appendPredicates.ts +15 -15
- 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/predicate.ts +231 -237
- package/src/types.ts +0 -63
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,14 +18,8 @@ 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
25
|
Route,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { castArray } from "./castArray";
|
|
2
|
+
|
|
1
3
|
interface WithPredicates {
|
|
2
4
|
predicates?: string | string[];
|
|
3
5
|
}
|
|
@@ -7,22 +9,20 @@ interface WithPredicates {
|
|
|
7
9
|
* Appended predicates are added to the end of the existing list.
|
|
8
10
|
*
|
|
9
11
|
* @typeParam T - Object to which predicates will be append.
|
|
12
|
+
* @param objWithPredicates - Object to append predicates on the `predicates` property.
|
|
10
13
|
* @param predicates - One or more predicates to append.
|
|
11
14
|
*
|
|
12
|
-
* @returns
|
|
13
|
-
* `predicates` list will be appended to that object.
|
|
15
|
+
* @returns The object with the appended predicates.
|
|
14
16
|
*/
|
|
15
|
-
export const appendPredicates =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
*
|
|
21
|
-
* @param objWithPredicates - Object to append predicates on the `predicates` property.
|
|
22
|
-
*
|
|
23
|
-
* @returns The object with the appended predicates.
|
|
24
|
-
*/
|
|
25
|
-
(objWithPredicates: T = {} as T): T => ({
|
|
17
|
+
export const appendPredicates = <T extends WithPredicates>(
|
|
18
|
+
objWithPredicates: T = {} as T,
|
|
19
|
+
predicates: string | string[],
|
|
20
|
+
) => {
|
|
21
|
+
return {
|
|
26
22
|
...objWithPredicates,
|
|
27
|
-
predicates: [
|
|
28
|
-
|
|
23
|
+
predicates: [
|
|
24
|
+
...(objWithPredicates.predicates || []),
|
|
25
|
+
...castArray(predicates),
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -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
|
+
};
|