@prismicio/client 6.2.0 → 6.4.1-alpha.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 CHANGED
@@ -18,8 +18,7 @@ The official JavaScript + TypeScript client library for [Prismic][prismic].
18
18
  import * as prismic from "@prismicio/client";
19
19
 
20
20
  // Create a client
21
- const endpoint = prismic.getEndpoint("my-repository");
22
- const client = prismic.createClient(endpoint);
21
+ const client = prismic.createClient("my-repository");
23
22
 
24
23
  // Then query for your content
25
24
  const blogPosts = await client.getAllByType("blog_post");
@@ -52,7 +51,7 @@ For more clarity on this project and its structure you can also check out the de
52
51
  ## License
53
52
 
54
53
  ```
55
- Copyright 2013-2021 Prismic <contact@prismic.io> (https://prismic.io)
54
+ Copyright 2013-2022 Prismic <contact@prismic.io> (https://prismic.io)
56
55
 
57
56
  Licensed under the Apache License, Version 2.0 (the "License");
58
57
  you may not use this file except in compliance with the License.
package/dist/index.cjs CHANGED
@@ -24,9 +24,50 @@ function _interopNamespace(e) {
24
24
 
25
25
  const prismicH__namespace = /*#__PURE__*/_interopNamespace(prismicH);
26
26
 
27
- const getEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/api/v2`;
27
+ const isRepositoryName = (input) => {
28
+ return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
29
+ };
30
+
31
+ class PrismicError extends Error {
32
+ constructor(message = "An invalid API response was returned", url, response) {
33
+ super(message);
34
+ this.url = url;
35
+ this.response = response;
36
+ }
37
+ }
28
38
 
29
- const getGraphQLEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/graphql`;
39
+ const getRepositoryEndpoint = (repositoryName) => {
40
+ if (isRepositoryName(repositoryName)) {
41
+ return `https://${repositoryName}.cdn.prismic.io/api/v2`;
42
+ } else {
43
+ throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
44
+ }
45
+ };
46
+
47
+ const getRepositoryName = (repositoryEndpoint) => {
48
+ try {
49
+ return new URL(repositoryEndpoint).hostname.split(".")[0];
50
+ } catch (e) {
51
+ throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
52
+ }
53
+ };
54
+
55
+ const getGraphQLEndpoint = (repositoryName) => {
56
+ if (isRepositoryName(repositoryName)) {
57
+ return `https://${repositoryName}.cdn.prismic.io/graphql`;
58
+ } else {
59
+ throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
60
+ }
61
+ };
62
+
63
+ const isRepositoryEndpoint = (input) => {
64
+ try {
65
+ new URL(input);
66
+ return true;
67
+ } catch (e) {
68
+ return false;
69
+ }
70
+ };
30
71
 
31
72
  const castArray = (a) => Array.isArray(a) ? a : [a];
32
73
 
@@ -79,14 +120,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
79
120
 
80
121
  const castThunk = (a) => typeof a === "function" ? a : () => a;
81
122
 
82
- class PrismicError extends Error {
83
- constructor(message = "An invalid API response was returned", url, response) {
84
- super(message);
85
- this.url = url;
86
- this.response = response;
87
- }
88
- }
89
-
90
123
  const findRef = (refs, predicate) => {
91
124
  const ref = refs.find((ref2) => predicate(ref2));
92
125
  if (!ref) {
@@ -217,16 +250,21 @@ const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags
217
250
  const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
218
251
  const createClient = (...args) => new Client(...args);
219
252
  class Client {
220
- constructor(endpoint, options = {}) {
253
+ constructor(repositoryNameOrEndpoint, options = {}) {
221
254
  this.refState = {
222
255
  mode: "Master" /* Master */,
223
256
  autoPreviewsEnabled: true
224
257
  };
225
258
  this.cachedRepositoryExpiration = 0;
226
- if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(endpoint)) {
227
- 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);
259
+ this.graphqlFetch = this.graphQLFetch;
260
+ if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
261
+ if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
262
+ 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);
263
+ }
264
+ this.endpoint = repositoryNameOrEndpoint;
265
+ } else {
266
+ this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
228
267
  }
229
- this.endpoint = endpoint;
230
268
  this.accessToken = options.accessToken;
231
269
  this.routes = options.routes;
232
270
  this.defaultParams = options.defaultParams;
@@ -243,7 +281,7 @@ class Client {
243
281
  if (this.fetchFn === globalThis.fetch) {
244
282
  this.fetchFn = this.fetchFn.bind(globalThis);
245
283
  }
246
- this.graphqlFetch = this.graphqlFetch.bind(this);
284
+ this.graphQLFetch = this.graphQLFetch.bind(this);
247
285
  }
248
286
  enableAutoPreviews() {
249
287
  this.refState.autoPreviewsEnabled = true;
@@ -273,10 +311,11 @@ class Client {
273
311
  throw new PrismicError("No documents were returned", url, void 0);
274
312
  }
275
313
  async dangerouslyGetAll(params = {}) {
314
+ var _a;
276
315
  const { limit = Infinity, ...actualParams } = params;
277
316
  const resolvedParams = {
278
317
  ...actualParams,
279
- pageSize: actualParams.pageSize || MAX_PAGE_SIZE
318
+ pageSize: actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
280
319
  };
281
320
  const documents = [];
282
321
  let latestResult;
@@ -415,16 +454,18 @@ class Client {
415
454
  documentID = documentID || this.refState.httpRequest.query.documentId;
416
455
  previewToken = previewToken || this.refState.httpRequest.query.token;
417
456
  }
418
- if (documentID != null) {
457
+ if (documentID != null && previewToken != null) {
419
458
  const document = await this.getByID(documentID, {
420
459
  signal: args.signal,
421
460
  ref: previewToken,
422
461
  lang: "*"
423
462
  });
424
- return prismicH__namespace.asLink(document, args.linkResolver);
425
- } else {
426
- return args.defaultURL;
463
+ const url = prismicH__namespace.asLink(document, args.linkResolver);
464
+ if (typeof url === "string") {
465
+ return url;
466
+ }
427
467
  }
468
+ return args.defaultURL;
428
469
  }
429
470
  queryLatestContent() {
430
471
  this.refState.mode = "Master" /* Master */;
@@ -450,7 +491,7 @@ class Client {
450
491
  ref
451
492
  };
452
493
  }
453
- async graphqlFetch(input, init) {
494
+ async graphQLFetch(input, init) {
454
495
  const cachedRepository = await this.getCachedRepository();
455
496
  const ref = await this.getResolvedRefString();
456
497
  const unsanitizedHeaders = {
@@ -466,6 +507,7 @@ class Client {
466
507
  }
467
508
  }
468
509
  const url = new URL(input);
510
+ url.searchParams.set("ref", ref);
469
511
  const query = url.searchParams.get("query");
470
512
  if (query) {
471
513
  url.searchParams.set("query", minifyGraphQLQuery(query));
@@ -547,6 +589,7 @@ class Client {
547
589
  }
548
590
  }
549
591
 
592
+ const getEndpoint = getRepositoryEndpoint;
550
593
  const predicates = predicate;
551
594
  const Predicates = predicate;
552
595
 
@@ -561,6 +604,10 @@ exports.cookie = cookie;
561
604
  exports.createClient = createClient;
562
605
  exports.getEndpoint = getEndpoint;
563
606
  exports.getGraphQLEndpoint = getGraphQLEndpoint;
607
+ exports.getRepositoryEndpoint = getRepositoryEndpoint;
608
+ exports.getRepositoryName = getRepositoryName;
609
+ exports.isRepositoryEndpoint = isRepositoryEndpoint;
610
+ exports.isRepositoryName = isRepositoryName;
564
611
  exports.predicate = predicate;
565
612
  exports.predicates = predicates;
566
613
  //# sourceMappingURL=index.cjs.map