@prismicio/client 6.0.0 → 6.3.0
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 +3 -4
- package/dist/index.cjs +147 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +154 -40
- package/dist/index.js +143 -73
- package/dist/index.js.map +1 -1
- package/package.json +19 -16
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +210 -75
- package/src/getGraphQLEndpoint.ts +24 -0
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -2
- package/src/isRepositoryEndpoint.ts +17 -0
- package/src/isRepositoryName.ts +10 -0
- package/src/lib/minifyGraphQLQuery.ts +13 -0
- package/src/types.ts +42 -0
- package/src/getEndpoint.ts +0 -12
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PrismicError } from "./PrismicError";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get a Prismic repository's name from its standard Prismic Rest API V2 or
|
|
5
|
+
* GraphQL endpoint.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam RepositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
|
|
8
|
+
* @param repositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
|
|
9
|
+
*
|
|
10
|
+
* @returns The Prismic repository's name.
|
|
11
|
+
* @throws {@link Error} Thrown if an invalid Prismic Rest API V2 endpoint is provided.
|
|
12
|
+
*/
|
|
13
|
+
export const getRepositoryName = (repositoryEndpoint: string): string => {
|
|
14
|
+
try {
|
|
15
|
+
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
16
|
+
} catch {
|
|
17
|
+
throw new PrismicError(
|
|
18
|
+
`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`,
|
|
19
|
+
undefined,
|
|
20
|
+
undefined,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
// Primary library API.
|
|
2
|
-
|
|
3
|
-
export
|
|
2
|
+
import { getRepositoryEndpoint } from "./getRepositoryEndpoint";
|
|
3
|
+
export { getRepositoryEndpoint };
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated Renamed to `getRepositoryEndpoint`.
|
|
6
|
+
*/
|
|
7
|
+
// TODO: Remove in v3.
|
|
8
|
+
export const getEndpoint = getRepositoryEndpoint;
|
|
9
|
+
export { getRepositoryName } from "./getRepositoryName";
|
|
10
|
+
export { getGraphQLEndpoint } from "./getGraphQLEndpoint";
|
|
11
|
+
export { isRepositoryName } from "./isRepositoryName";
|
|
12
|
+
export { isRepositoryEndpoint } from "./isRepositoryEndpoint";
|
|
13
|
+
export { buildQueryURL } from "./buildQueryURL";
|
|
4
14
|
export { createClient, Client } from "./client";
|
|
5
15
|
|
|
6
16
|
// Predicates API.
|
|
@@ -28,6 +38,7 @@ export * as cookie from "./cookie";
|
|
|
28
38
|
|
|
29
39
|
// General types used throughout the project. These are made public to allow users to better type their projects.
|
|
30
40
|
export type { ClientConfig } from "./client";
|
|
41
|
+
export type { QueryParams, BuildQueryURLArgs } from "./buildQueryURL";
|
|
31
42
|
export type {
|
|
32
43
|
FetchLike,
|
|
33
44
|
HttpRequestLike,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid
|
|
3
|
+
* URL is a valid endpoint to support network proxies.
|
|
4
|
+
*
|
|
5
|
+
* @param input - Input to test.
|
|
6
|
+
*
|
|
7
|
+
* @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false` otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export const isRepositoryEndpoint = (input: string): boolean => {
|
|
10
|
+
try {
|
|
11
|
+
new URL(input);
|
|
12
|
+
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if an input is a valid Prismic repository name.
|
|
3
|
+
*
|
|
4
|
+
* @param input - Input to test.
|
|
5
|
+
*
|
|
6
|
+
* @returns `true` if `input` is a valid Prismic repository name, `false` otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export const isRepositoryName = (input: string): boolean => {
|
|
9
|
+
return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
|
|
10
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minifies a GraphQL query by removing whitespace where possible.
|
|
3
|
+
*
|
|
4
|
+
* @param query - GraphQL query to minify.
|
|
5
|
+
*
|
|
6
|
+
* @returns A minified version of `query`.
|
|
7
|
+
*/
|
|
8
|
+
export const minifyGraphQLQuery = (query: string): string => {
|
|
9
|
+
return query.replace(
|
|
10
|
+
/(\n| )*( |{|})(\n| )*/gm,
|
|
11
|
+
(_chars, _spaces, brackets) => brackets,
|
|
12
|
+
);
|
|
13
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,16 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a union of the given object's values, and optionally specify which
|
|
3
|
+
* keys to get the values from.
|
|
4
|
+
*
|
|
5
|
+
* Taken from the `type-fest` package.
|
|
6
|
+
*
|
|
7
|
+
* See:
|
|
8
|
+
* https://github.com/sindresorhus/type-fest/blob/61c35052f09caa23de5eef96d95196375d8ed498/source/value-of.d.ts
|
|
9
|
+
*/
|
|
10
|
+
export type ValueOf<
|
|
11
|
+
ObjectType,
|
|
12
|
+
ValueType extends keyof ObjectType = keyof ObjectType,
|
|
13
|
+
> = ObjectType[ValueType];
|
|
14
|
+
|
|
1
15
|
/**
|
|
2
16
|
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
17
|
+
*
|
|
18
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
|
|
3
19
|
*/
|
|
4
20
|
export type FetchLike = (
|
|
5
21
|
input: string,
|
|
6
22
|
init?: RequestInitLike,
|
|
7
23
|
) => Promise<ResponseLike>;
|
|
8
24
|
|
|
25
|
+
/**
|
|
26
|
+
* An object that allows you to abort a `fetch()` request if needed via an
|
|
27
|
+
* `AbortController` object
|
|
28
|
+
*
|
|
29
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
30
|
+
*/
|
|
31
|
+
// `any` is used often here to ensure this type is universally valid amond
|
|
32
|
+
// different AbortSignal implementations. The types of each property are not
|
|
33
|
+
// important to validate since it is blindly passed to a given `fetch()`
|
|
34
|
+
// function.
|
|
35
|
+
export type AbortSignalLike = {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
aborted: any;
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
addEventListener: any;
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
removeEventListener: any;
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
dispatchEvent: any;
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
+
onabort: any;
|
|
46
|
+
};
|
|
47
|
+
|
|
9
48
|
/**
|
|
10
49
|
* The minimum required properties from RequestInit.
|
|
11
50
|
*/
|
|
12
51
|
export interface RequestInitLike {
|
|
13
52
|
headers?: Record<string, string>;
|
|
53
|
+
// `null` is included to match TypeScript's `fetch()` type in `lib.dom.d.ts`.
|
|
54
|
+
// See: https://github.com/microsoft/TypeScript/blob/3328feb7991f358e245088d48b64ad9da8f015e2/lib/lib.dom.d.ts#L1515-L1518
|
|
55
|
+
signal?: AbortSignalLike | null;
|
|
14
56
|
}
|
|
15
57
|
|
|
16
58
|
/**
|
package/src/getEndpoint.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get a repository's Prismic REST API V2 endpoint.
|
|
3
|
-
*
|
|
4
|
-
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
5
|
-
* @param repositoryName - Name of the repository.
|
|
6
|
-
*
|
|
7
|
-
* @returns The repository's Prismic REST API V2 endpoint
|
|
8
|
-
*/
|
|
9
|
-
export const getEndpoint = <RepositoryName extends string>(
|
|
10
|
-
repositoryName: RepositoryName,
|
|
11
|
-
): `https://${RepositoryName}.cdn.prismic.io/api/v2` =>
|
|
12
|
-
`https://${repositoryName}.cdn.prismic.io/api/v2` as const;
|