@prismicio/client 6.2.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 +2 -3
- package/dist/index.cjs +56 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +58 -11
- package/dist/index.js +53 -15
- package/dist/index.js.map +1 -1
- package/package.json +10 -11
- package/src/client.ts +33 -16
- package/src/getGraphQLEndpoint.ts +14 -2
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -3
- package/src/isRepositoryEndpoint.ts +17 -0
- package/src/isRepositoryName.ts +10 -0
- package/src/getEndpoint.ts +0 -12
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { isRepositoryName } from "./isRepositoryName";
|
|
2
|
+
import { PrismicError } from "./PrismicError";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Get a repository's Prismic GraphQL endpoint.
|
|
3
6
|
*
|
|
@@ -8,5 +11,14 @@
|
|
|
8
11
|
*/
|
|
9
12
|
export const getGraphQLEndpoint = <RepositoryName extends string>(
|
|
10
13
|
repositoryName: RepositoryName,
|
|
11
|
-
): `https://${RepositoryName}.cdn.prismic.io/graphql` =>
|
|
12
|
-
|
|
14
|
+
): `https://${RepositoryName}.cdn.prismic.io/graphql` => {
|
|
15
|
+
if (isRepositoryName(repositoryName)) {
|
|
16
|
+
return `https://${repositoryName}.cdn.prismic.io/graphql` as const;
|
|
17
|
+
} else {
|
|
18
|
+
throw new PrismicError(
|
|
19
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
20
|
+
undefined,
|
|
21
|
+
undefined,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isRepositoryName } from "./isRepositoryName";
|
|
2
|
+
import { PrismicError } from "./PrismicError";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get a repository's Prismic Rest API V2 endpoint.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
8
|
+
* @param repositoryName - Name of the repository.
|
|
9
|
+
*
|
|
10
|
+
* @returns The repository's Prismic Rest API V2 endpoint
|
|
11
|
+
* @throws {@link Error} Thrown if an invalid repository name is provided.
|
|
12
|
+
*/
|
|
13
|
+
export const getRepositoryEndpoint = <RepositoryName extends string>(
|
|
14
|
+
repositoryName: RepositoryName,
|
|
15
|
+
): `https://${RepositoryName}.cdn.prismic.io/api/v2` => {
|
|
16
|
+
if (isRepositoryName(repositoryName)) {
|
|
17
|
+
return `https://${repositoryName}.cdn.prismic.io/api/v2` as const;
|
|
18
|
+
} else {
|
|
19
|
+
throw new PrismicError(
|
|
20
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
21
|
+
undefined,
|
|
22
|
+
undefined,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
@@ -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,7 +1,16 @@
|
|
|
1
1
|
// Primary library API.
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
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";
|
|
5
14
|
export { createClient, Client } from "./client";
|
|
6
15
|
|
|
7
16
|
// Predicates API.
|
|
@@ -29,6 +38,7 @@ export * as cookie from "./cookie";
|
|
|
29
38
|
|
|
30
39
|
// General types used throughout the project. These are made public to allow users to better type their projects.
|
|
31
40
|
export type { ClientConfig } from "./client";
|
|
41
|
+
export type { QueryParams, BuildQueryURLArgs } from "./buildQueryURL";
|
|
32
42
|
export type {
|
|
33
43
|
FetchLike,
|
|
34
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
|
+
};
|
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;
|