@prismicio/client 6.6.2 → 6.7.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.cjs CHANGED
@@ -40,7 +40,11 @@ const getRepositoryEndpoint = (repositoryName) => {
40
40
  if (isRepositoryName(repositoryName)) {
41
41
  return `https://${repositoryName}.cdn.prismic.io/api/v2`;
42
42
  } else {
43
- throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
43
+ throw new PrismicError(
44
+ `An invalid Prismic repository name was given: ${repositoryName}`,
45
+ void 0,
46
+ void 0
47
+ );
44
48
  }
45
49
  };
46
50
 
@@ -48,7 +52,11 @@ const getRepositoryName = (repositoryEndpoint) => {
48
52
  try {
49
53
  return new URL(repositoryEndpoint).hostname.split(".")[0];
50
54
  } catch (e) {
51
- throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
55
+ throw new PrismicError(
56
+ `An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`,
57
+ void 0,
58
+ void 0
59
+ );
52
60
  }
53
61
  };
54
62
 
@@ -56,7 +64,11 @@ const getGraphQLEndpoint = (repositoryName) => {
56
64
  if (isRepositoryName(repositoryName)) {
57
65
  return `https://${repositoryName}.cdn.prismic.io/graphql`;
58
66
  } else {
59
- throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
67
+ throw new PrismicError(
68
+ `An invalid Prismic repository name was given: ${repositoryName}`,
69
+ void 0,
70
+ void 0
71
+ );
60
72
  }
61
73
  };
62
74
 
@@ -140,34 +152,37 @@ const findRefByLabel = (refs, label) => {
140
152
  return findRef(refs, (ref) => ref.label === label);
141
153
  };
142
154
 
155
+ const preview = "io.prismic.preview";
156
+
157
+ const cookie = /*#__PURE__*/Object.freeze({
158
+ __proto__: null,
159
+ preview: preview
160
+ });
161
+
143
162
  const readValue = (value) => {
144
163
  return value.replace(/%3B/g, ";");
145
164
  };
146
- const parse = (cookieString) => {
147
- const result = {};
148
- const cookies = cookieString.split("; ");
165
+ const getPreviewCookie = (cookieJar) => {
166
+ const cookies = cookieJar.split("; ");
167
+ let value;
149
168
  for (const cookie of cookies) {
150
169
  const parts = cookie.split("=");
151
- const value = parts.slice(1).join("=");
152
170
  const name = readValue(parts[0]).replace(/%3D/g, "=");
153
- result[name] = readValue(value);
171
+ if (name === preview) {
172
+ value = readValue(parts.slice(1).join("="));
173
+ continue;
174
+ }
154
175
  }
155
- return result;
176
+ return value;
156
177
  };
157
- const getAll = (cookieStore) => parse(cookieStore);
158
- const getCookie = (name, cookieJar) => getAll(cookieJar)[name];
159
178
 
160
179
  const minifyGraphQLQuery = (query) => {
161
- return query.replace(/(\n| )*( |{|})(\n| )*/gm, (_chars, _spaces, brackets) => brackets);
180
+ return query.replace(
181
+ /(\n| )*( |{|})(\n| )*/gm,
182
+ (_chars, _spaces, brackets) => brackets
183
+ );
162
184
  };
163
185
 
164
- const preview = "io.prismic.preview";
165
-
166
- const cookie = /*#__PURE__*/Object.freeze({
167
- __proto__: null,
168
- preview: preview
169
- });
170
-
171
186
  class ForbiddenError extends PrismicError {
172
187
  }
173
188
 
@@ -214,7 +229,9 @@ const argsPredicate = (name) => {
214
229
  const predicate = {
215
230
  at: pathWithArgsPredicate("at"),
216
231
  not: pathWithArgsPredicate("not"),
217
- any: pathWithArgsPredicate("any"),
232
+ any: pathWithArgsPredicate(
233
+ "any"
234
+ ),
218
235
  in: pathWithArgsPredicate("in"),
219
236
  fulltext: pathWithArgsPredicate("fulltext"),
220
237
  has: pathPredicate("has"),
@@ -223,16 +240,26 @@ const predicate = {
223
240
  geopointNear: pathWithArgsPredicate("geopoint.near"),
224
241
  numberLessThan: pathWithArgsPredicate("number.lt"),
225
242
  numberGreaterThan: pathWithArgsPredicate("number.gt"),
226
- numberInRange: pathWithArgsPredicate("number.inRange"),
243
+ numberInRange: pathWithArgsPredicate(
244
+ "number.inRange"
245
+ ),
227
246
  dateAfter: pathWithArgsPredicate("date.after"),
228
247
  dateBefore: pathWithArgsPredicate("date.before"),
229
248
  dateBetween: pathWithArgsPredicate("date.between"),
230
249
  dateDayOfMonth: pathWithArgsPredicate("date.day-of-month"),
231
- dateDayOfMonthAfter: pathWithArgsPredicate("date.day-of-month-after"),
232
- dateDayOfMonthBefore: pathWithArgsPredicate("date.day-of-month-before"),
250
+ dateDayOfMonthAfter: pathWithArgsPredicate(
251
+ "date.day-of-month-after"
252
+ ),
253
+ dateDayOfMonthBefore: pathWithArgsPredicate(
254
+ "date.day-of-month-before"
255
+ ),
233
256
  dateDayOfWeek: pathWithArgsPredicate("date.day-of-week"),
234
- dateDayOfWeekAfter: pathWithArgsPredicate("date.day-of-week-after"),
235
- dateDayOfWeekBefore: pathWithArgsPredicate("date.day-of-week-before"),
257
+ dateDayOfWeekAfter: pathWithArgsPredicate(
258
+ "date.day-of-week-after"
259
+ ),
260
+ dateDayOfWeekBefore: pathWithArgsPredicate(
261
+ "date.day-of-week-before"
262
+ ),
236
263
  dateMonth: pathWithArgsPredicate("date.month"),
237
264
  dateMonthAfter: pathWithArgsPredicate("date.month-after"),
238
265
  dateMonthBefore: pathWithArgsPredicate("date.month-before"),
@@ -259,7 +286,11 @@ class Client {
259
286
  this.graphqlFetch = this.graphQLFetch.bind(this);
260
287
  if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
261
288
  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);
289
+ throw new PrismicError(
290
+ "@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.",
291
+ void 0,
292
+ void 0
293
+ );
263
294
  }
264
295
  this.endpoint = repositoryNameOrEndpoint;
265
296
  } else {
@@ -267,6 +298,7 @@ class Client {
267
298
  }
268
299
  this.accessToken = options.accessToken;
269
300
  this.routes = options.routes;
301
+ this.brokenRoute = options.brokenRoute;
270
302
  this.defaultParams = options.defaultParams;
271
303
  if (options.ref) {
272
304
  this.queryContentFromRef(options.ref);
@@ -276,7 +308,11 @@ class Client {
276
308
  } else if (typeof globalThis.fetch === "function") {
277
309
  this.fetchFn = globalThis.fetch;
278
310
  } else {
279
- throw new PrismicError("A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.", void 0, void 0);
311
+ throw new PrismicError(
312
+ "A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
313
+ void 0,
314
+ void 0
315
+ );
280
316
  }
281
317
  if (this.fetchFn === globalThis.fetch) {
282
318
  this.fetchFn = this.fetchFn.bind(globalThis);
@@ -302,11 +338,12 @@ class Client {
302
338
  return await this.fetch(url, params);
303
339
  }
304
340
  async getFirst(params) {
305
- var _a;
306
- const url = await this.buildQueryURL({
307
- pageSize: ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || (params && "page" in params ? void 0 : 1),
308
- ...params
309
- });
341
+ var _a, _b;
342
+ const actualParams = { ...params };
343
+ if (!(params && params.page) && !(params == null ? void 0 : params.pageSize)) {
344
+ actualParams.pageSize = (_b = (_a = this.defaultParams) == null ? void 0 : _a.pageSize) != null ? _b : 1;
345
+ }
346
+ const url = await this.buildQueryURL(actualParams);
310
347
  const result = await this.fetch(url, params);
311
348
  const firstResult = result.results[0];
312
349
  if (firstResult) {
@@ -319,7 +356,10 @@ class Client {
319
356
  const { limit = Infinity, ...actualParams } = params;
320
357
  const resolvedParams = {
321
358
  ...actualParams,
322
- pageSize: Math.min(limit, actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE)
359
+ pageSize: Math.min(
360
+ limit,
361
+ actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
362
+ )
323
363
  };
324
364
  const documents = [];
325
365
  let latestResult;
@@ -334,58 +374,86 @@ class Client {
334
374
  return documents.slice(0, limit);
335
375
  }
336
376
  async getByID(id, params) {
337
- return await this.getFirst(appendPredicates(params, predicate.at("document.id", id)));
377
+ return await this.getFirst(
378
+ appendPredicates(params, predicate.at("document.id", id))
379
+ );
338
380
  }
339
381
  async getByIDs(ids, params) {
340
- return await this.get(appendPredicates(params, predicate.in("document.id", ids)));
382
+ return await this.get(
383
+ appendPredicates(params, predicate.in("document.id", ids))
384
+ );
341
385
  }
342
386
  async getAllByIDs(ids, params) {
343
- return await this.dangerouslyGetAll(appendPredicates(params, predicate.in("document.id", ids)));
387
+ return await this.dangerouslyGetAll(
388
+ appendPredicates(params, predicate.in("document.id", ids))
389
+ );
344
390
  }
345
391
  async getByUID(documentType, uid, params) {
346
- return await this.getFirst(appendPredicates(params, [
347
- typePredicate(documentType),
348
- predicate.at(`my.${documentType}.uid`, uid)
349
- ]));
392
+ return await this.getFirst(
393
+ appendPredicates(params, [
394
+ typePredicate(documentType),
395
+ predicate.at(`my.${documentType}.uid`, uid)
396
+ ])
397
+ );
350
398
  }
351
399
  async getByUIDs(documentType, uids, params) {
352
- return await this.get(appendPredicates(params, [
353
- typePredicate(documentType),
354
- predicate.in(`my.${documentType}.uid`, uids)
355
- ]));
400
+ return await this.get(
401
+ appendPredicates(params, [
402
+ typePredicate(documentType),
403
+ predicate.in(`my.${documentType}.uid`, uids)
404
+ ])
405
+ );
356
406
  }
357
407
  async getAllByUIDs(documentType, uids, params) {
358
- return await this.dangerouslyGetAll(appendPredicates(params, [
359
- typePredicate(documentType),
360
- predicate.in(`my.${documentType}.uid`, uids)
361
- ]));
408
+ return await this.dangerouslyGetAll(
409
+ appendPredicates(params, [
410
+ typePredicate(documentType),
411
+ predicate.in(`my.${documentType}.uid`, uids)
412
+ ])
413
+ );
362
414
  }
363
415
  async getSingle(documentType, params) {
364
- return await this.getFirst(appendPredicates(params, typePredicate(documentType)));
416
+ return await this.getFirst(
417
+ appendPredicates(params, typePredicate(documentType))
418
+ );
365
419
  }
366
420
  async getByType(documentType, params) {
367
- return await this.get(appendPredicates(params, typePredicate(documentType)));
421
+ return await this.get(
422
+ appendPredicates(params, typePredicate(documentType))
423
+ );
368
424
  }
369
425
  async getAllByType(documentType, params) {
370
426
  return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
371
427
  }
372
428
  async getByTag(tag, params) {
373
- return await this.get(appendPredicates(params, someTagsPredicate(tag)));
429
+ return await this.get(
430
+ appendPredicates(params, someTagsPredicate(tag))
431
+ );
374
432
  }
375
433
  async getAllByTag(tag, params) {
376
- return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
434
+ return await this.dangerouslyGetAll(
435
+ appendPredicates(params, someTagsPredicate(tag))
436
+ );
377
437
  }
378
438
  async getByEveryTag(tags, params) {
379
- return await this.get(appendPredicates(params, everyTagPredicate(tags)));
439
+ return await this.get(
440
+ appendPredicates(params, everyTagPredicate(tags))
441
+ );
380
442
  }
381
443
  async getAllByEveryTag(tags, params) {
382
- return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tags)));
444
+ return await this.dangerouslyGetAll(
445
+ appendPredicates(params, everyTagPredicate(tags))
446
+ );
383
447
  }
384
448
  async getBySomeTags(tags, params) {
385
- return await this.get(appendPredicates(params, someTagsPredicate(tags)));
449
+ return await this.get(
450
+ appendPredicates(params, someTagsPredicate(tags))
451
+ );
386
452
  }
387
453
  async getAllBySomeTags(tags, params) {
388
- return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
454
+ return await this.dangerouslyGetAll(
455
+ appendPredicates(params, someTagsPredicate(tags))
456
+ );
389
457
  }
390
458
  async getRepository(params) {
391
459
  const url = new URL(this.endpoint);
@@ -447,6 +515,7 @@ class Client {
447
515
  ref,
448
516
  integrationFieldsRef,
449
517
  routes: params.routes || this.routes,
518
+ brokenRoute: params.brokenRoute || this.brokenRoute,
450
519
  accessToken: params.accessToken || this.accessToken
451
520
  });
452
521
  }
@@ -510,21 +579,28 @@ class Client {
510
579
  const ref = await this.getResolvedRefString();
511
580
  const unsanitizedHeaders = {
512
581
  "Prismic-ref": ref,
513
- "Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
514
582
  Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
515
583
  ...init ? init.headers : {}
516
584
  };
585
+ if (cachedRepository.integrationFieldsRef) {
586
+ unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
587
+ }
517
588
  const headers = {};
518
589
  for (const key in unsanitizedHeaders) {
519
590
  if (unsanitizedHeaders[key]) {
520
591
  headers[key.toLowerCase()] = unsanitizedHeaders[key];
521
592
  }
522
593
  }
523
- const url = new URL(input);
594
+ const url = new URL(
595
+ input
596
+ );
524
597
  url.searchParams.set("ref", ref);
525
598
  const query = url.searchParams.get("query");
526
599
  if (query) {
527
- url.searchParams.set("query", minifyGraphQLQuery(query));
600
+ url.searchParams.set(
601
+ "query",
602
+ minifyGraphQLQuery(query)
603
+ );
528
604
  }
529
605
  return await this.fetchFn(url.toString(), {
530
606
  ...init,
@@ -542,7 +618,11 @@ class Client {
542
618
  const cachedRepository = await this.getCachedRepository(params);
543
619
  const form = cachedRepository.forms[name];
544
620
  if (!form) {
545
- throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
621
+ throw new PrismicError(
622
+ `Form with name "${name}" could not be found`,
623
+ void 0,
624
+ void 0
625
+ );
546
626
  }
547
627
  return form;
548
628
  }
@@ -551,17 +631,17 @@ class Client {
551
631
  if (this.refState.autoPreviewsEnabled) {
552
632
  let previewRef;
553
633
  let cookieJar;
554
- if ((_a = globalThis.document) == null ? void 0 : _a.cookie) {
555
- cookieJar = globalThis.document.cookie;
556
- } else if ((_b = this.refState.httpRequest) == null ? void 0 : _b.headers) {
634
+ if ((_a = this.refState.httpRequest) == null ? void 0 : _a.headers) {
557
635
  if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") {
558
636
  cookieJar = this.refState.httpRequest.headers.get("cookie");
559
637
  } else if ("cookie" in this.refState.httpRequest.headers) {
560
638
  cookieJar = this.refState.httpRequest.headers.cookie;
561
639
  }
640
+ } else if ((_b = globalThis.document) == null ? void 0 : _b.cookie) {
641
+ cookieJar = globalThis.document.cookie;
562
642
  }
563
643
  if (cookieJar) {
564
- previewRef = getCookie(preview, cookieJar);
644
+ previewRef = getPreviewCookie(cookieJar);
565
645
  }
566
646
  if (previewRef) {
567
647
  return previewRef;
@@ -590,7 +670,11 @@ class Client {
590
670
  json = await res.json();
591
671
  } catch (e) {
592
672
  if (res.status === 404) {
593
- throw new NotFoundError(`Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`, url, void 0);
673
+ throw new NotFoundError(
674
+ `Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
675
+ url,
676
+ void 0
677
+ );
594
678
  } else {
595
679
  throw new PrismicError(void 0, url, void 0);
596
680
  }
@@ -604,7 +688,11 @@ class Client {
604
688
  }
605
689
  case 401:
606
690
  case 403: {
607
- throw new ForbiddenError("error" in json ? json.error : json.message, url, json);
691
+ throw new ForbiddenError(
692
+ "error" in json ? json.error : json.message,
693
+ url,
694
+ json
695
+ );
608
696
  }
609
697
  }
610
698
  throw new PrismicError(void 0, url, json);