@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/dist/index.d.ts CHANGED
@@ -2,14 +2,27 @@ import * as prismicT from '@prismicio/types';
2
2
  import * as prismicH from '@prismicio/helpers';
3
3
 
4
4
  /**
5
- * Get a repository's Prismic REST API V2 endpoint.
5
+ * Get a repository's Prismic Rest API V2 endpoint.
6
6
  *
7
7
  * @typeParam RepositoryName - Name of the Prismic repository.
8
8
  * @param repositoryName - Name of the repository.
9
9
  *
10
- * @returns The repository's Prismic REST API V2 endpoint
10
+ * @returns The repository's Prismic Rest API V2 endpoint
11
+ * @throws {@link Error} Thrown if an invalid repository name is provided.
11
12
  */
12
- declare const getEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/api/v2`;
13
+ declare const getRepositoryEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/api/v2`;
14
+
15
+ /**
16
+ * Get a Prismic repository's name from its standard Prismic Rest API V2 or
17
+ * GraphQL endpoint.
18
+ *
19
+ * @typeParam RepositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
20
+ * @param repositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
21
+ *
22
+ * @returns The Prismic repository's name.
23
+ * @throws {@link Error} Thrown if an invalid Prismic Rest API V2 endpoint is provided.
24
+ */
25
+ declare const getRepositoryName: (repositoryEndpoint: string) => string;
13
26
 
14
27
  /**
15
28
  * Get a repository's Prismic GraphQL endpoint.
@@ -21,6 +34,25 @@ declare const getEndpoint: <RepositoryName extends string>(repositoryName: Repos
21
34
  */
22
35
  declare const getGraphQLEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/graphql`;
23
36
 
37
+ /**
38
+ * Determines if an input is a valid Prismic repository name.
39
+ *
40
+ * @param input - Input to test.
41
+ *
42
+ * @returns `true` if `input` is a valid Prismic repository name, `false` otherwise.
43
+ */
44
+ declare const isRepositoryName: (input: string) => boolean;
45
+
46
+ /**
47
+ * Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid
48
+ * URL is a valid endpoint to support network proxies.
49
+ *
50
+ * @param input - Input to test.
51
+ *
52
+ * @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false` otherwise.
53
+ */
54
+ declare const isRepositoryEndpoint: (input: string) => boolean;
55
+
24
56
  /**
25
57
  * A universal API to make network requests. A subset of the `fetch()` API.
26
58
  *
@@ -299,14 +331,24 @@ declare type ResolvePreviewArgs = {
299
331
  /**
300
332
  * Creates a Prismic client that can be used to query a repository.
301
333
  *
302
- * @param endpoint - The Prismic REST API V2 endpoint for the repository (use
303
- * `prismic.getEndpoint` for the default endpoint).
334
+ * @example
335
+ *
336
+ * ```ts
337
+ * // With a repository name.
338
+ * createClient("qwerty");
339
+ *
340
+ * // Or with a full Prismic Rest API V2 endpoint.
341
+ * createClient("https://qwerty.cdn.prismic.io/api/v2");
342
+ * ```
343
+ *
344
+ * @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
345
+ * API V2 endpoint for the repository.
304
346
  * @param options - Configuration that determines how content will be queried
305
347
  * from the Prismic repository.
306
348
  *
307
349
  * @returns A client that can query content from the repository.
308
350
  */
309
- declare const createClient: (endpoint: string, options?: ClientConfig | undefined) => Client;
351
+ declare const createClient: (repositoryNameOrEndpoint: string, options?: ClientConfig | undefined) => Client;
310
352
  /**
311
353
  * A client that allows querying content from a Prismic repository.
312
354
  *
@@ -317,7 +359,7 @@ declare const createClient: (endpoint: string, options?: ClientConfig | undefine
317
359
  declare class Client {
318
360
  /**
319
361
  * The Prismic REST API V2 endpoint for the repository (use
320
- * `prismic.getEndpoint` for the default endpoint).
362
+ * `prismic.getRepositoryEndpoint` for the default endpoint).
321
363
  */
322
364
  endpoint: string;
323
365
  /**
@@ -364,14 +406,14 @@ declare class Client {
364
406
  * such as Node.js, the `fetch` option must be provided as part of the
365
407
  * `options` parameter.
366
408
  *
367
- * @param endpoint - The Prismic REST API V2 endpoint for the repository (use
368
- * `prismic.getEndpoint` to get the default endpoint).
409
+ * @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
410
+ * API V2 endpoint for the repository.
369
411
  * @param options - Configuration that determines how content will be queried
370
412
  * from the Prismic repository.
371
413
  *
372
414
  * @returns A client that can query content from the repository.
373
415
  */
374
- constructor(endpoint: string, options?: ClientConfig);
416
+ constructor(repositoryNameOrEndpoint: string, options?: ClientConfig);
375
417
  /**
376
418
  * Enables the client to automatically query content from a preview session if
377
419
  * one is active in browser environments. This is enabled by default in the browser.
@@ -1289,6 +1331,11 @@ declare namespace cookie {
1289
1331
  };
1290
1332
  }
1291
1333
 
1334
+ /**
1335
+ * @deprecated Renamed to `getRepositoryEndpoint`.
1336
+ */
1337
+ declare const getEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/api/v2`;
1338
+
1292
1339
  /**
1293
1340
  * @deprecated Renamed to `predicate` (without an "s").
1294
1341
  */
@@ -1356,4 +1403,4 @@ declare const Predicates: {
1356
1403
  dateHourBefore: (path: string, hour: number) => string;
1357
1404
  };
1358
1405
 
1359
- export { BuildQueryURLArgs, Client, ClientConfig, FetchLike, ForbiddenError, HttpRequestLike, NotFoundError, Ordering, ParsingError, Predicates, PrismicError, QueryParams, RequestInitLike, ResponseLike, Route, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, predicate, predicates };
1406
+ export { BuildQueryURLArgs, Client, ClientConfig, FetchLike, ForbiddenError, HttpRequestLike, NotFoundError, Ordering, ParsingError, Predicates, PrismicError, QueryParams, RequestInitLike, ResponseLike, Route, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
package/dist/index.js CHANGED
@@ -1,8 +1,49 @@
1
1
  import * as prismicH from '@prismicio/helpers';
2
2
 
3
- const getEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/api/v2`;
3
+ const isRepositoryName = (input) => {
4
+ return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
5
+ };
6
+
7
+ class PrismicError extends Error {
8
+ constructor(message = "An invalid API response was returned", url, response) {
9
+ super(message);
10
+ this.url = url;
11
+ this.response = response;
12
+ }
13
+ }
14
+
15
+ const getRepositoryEndpoint = (repositoryName) => {
16
+ if (isRepositoryName(repositoryName)) {
17
+ return `https://${repositoryName}.cdn.prismic.io/api/v2`;
18
+ } else {
19
+ throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
20
+ }
21
+ };
22
+
23
+ const getRepositoryName = (repositoryEndpoint) => {
24
+ try {
25
+ return new URL(repositoryEndpoint).hostname.split(".")[0];
26
+ } catch (e) {
27
+ throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
28
+ }
29
+ };
4
30
 
5
- const getGraphQLEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/graphql`;
31
+ const getGraphQLEndpoint = (repositoryName) => {
32
+ if (isRepositoryName(repositoryName)) {
33
+ return `https://${repositoryName}.cdn.prismic.io/graphql`;
34
+ } else {
35
+ throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
36
+ }
37
+ };
38
+
39
+ const isRepositoryEndpoint = (input) => {
40
+ try {
41
+ new URL(input);
42
+ return true;
43
+ } catch (e) {
44
+ return false;
45
+ }
46
+ };
6
47
 
7
48
  const castArray = (a) => Array.isArray(a) ? a : [a];
8
49
 
@@ -55,14 +96,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
55
96
 
56
97
  const castThunk = (a) => typeof a === "function" ? a : () => a;
57
98
 
58
- class PrismicError extends Error {
59
- constructor(message = "An invalid API response was returned", url, response) {
60
- super(message);
61
- this.url = url;
62
- this.response = response;
63
- }
64
- }
65
-
66
99
  const findRef = (refs, predicate) => {
67
100
  const ref = refs.find((ref2) => predicate(ref2));
68
101
  if (!ref) {
@@ -193,16 +226,20 @@ const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags
193
226
  const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
194
227
  const createClient = (...args) => new Client(...args);
195
228
  class Client {
196
- constructor(endpoint, options = {}) {
229
+ constructor(repositoryNameOrEndpoint, options = {}) {
197
230
  this.refState = {
198
231
  mode: "Master" /* Master */,
199
232
  autoPreviewsEnabled: true
200
233
  };
201
234
  this.cachedRepositoryExpiration = 0;
202
- if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(endpoint)) {
203
- throw new PrismicError("@prismicio/client only supports Prismic Rest API V2. Please use the getEndpoint helper to generate a valid Rest API V2 endpoint URL.", void 0, void 0);
235
+ if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
236
+ if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
237
+ throw new PrismicError("@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.", void 0, void 0);
238
+ }
239
+ this.endpoint = repositoryNameOrEndpoint;
240
+ } else {
241
+ this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
204
242
  }
205
- this.endpoint = endpoint;
206
243
  this.accessToken = options.accessToken;
207
244
  this.routes = options.routes;
208
245
  this.defaultParams = options.defaultParams;
@@ -523,8 +560,9 @@ class Client {
523
560
  }
524
561
  }
525
562
 
563
+ const getEndpoint = getRepositoryEndpoint;
526
564
  const predicates = predicate;
527
565
  const Predicates = predicate;
528
566
 
529
- export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, predicate, predicates };
567
+ export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
530
568
  //# sourceMappingURL=index.js.map