@prismicio/client 6.6.1 → 6.6.4

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 {
@@ -276,7 +307,11 @@ class Client {
276
307
  } else if (typeof globalThis.fetch === "function") {
277
308
  this.fetchFn = globalThis.fetch;
278
309
  } 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);
310
+ throw new PrismicError(
311
+ "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.",
312
+ void 0,
313
+ void 0
314
+ );
280
315
  }
281
316
  if (this.fetchFn === globalThis.fetch) {
282
317
  this.fetchFn = this.fetchFn.bind(globalThis);
@@ -302,7 +337,12 @@ class Client {
302
337
  return await this.fetch(url, params);
303
338
  }
304
339
  async getFirst(params) {
305
- const url = await this.buildQueryURL(params);
340
+ var _a, _b;
341
+ const actualParams = { ...params };
342
+ if (!(params && params.page) && !(params == null ? void 0 : params.pageSize)) {
343
+ actualParams.pageSize = (_b = (_a = this.defaultParams) == null ? void 0 : _a.pageSize) != null ? _b : 1;
344
+ }
345
+ const url = await this.buildQueryURL(actualParams);
306
346
  const result = await this.fetch(url, params);
307
347
  const firstResult = result.results[0];
308
348
  if (firstResult) {
@@ -315,7 +355,10 @@ class Client {
315
355
  const { limit = Infinity, ...actualParams } = params;
316
356
  const resolvedParams = {
317
357
  ...actualParams,
318
- pageSize: Math.min(limit, actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE)
358
+ pageSize: Math.min(
359
+ limit,
360
+ actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
361
+ )
319
362
  };
320
363
  const documents = [];
321
364
  let latestResult;
@@ -330,58 +373,86 @@ class Client {
330
373
  return documents.slice(0, limit);
331
374
  }
332
375
  async getByID(id, params) {
333
- return await this.getFirst(appendPredicates(params, predicate.at("document.id", id)));
376
+ return await this.getFirst(
377
+ appendPredicates(params, predicate.at("document.id", id))
378
+ );
334
379
  }
335
380
  async getByIDs(ids, params) {
336
- return await this.get(appendPredicates(params, predicate.in("document.id", ids)));
381
+ return await this.get(
382
+ appendPredicates(params, predicate.in("document.id", ids))
383
+ );
337
384
  }
338
385
  async getAllByIDs(ids, params) {
339
- return await this.dangerouslyGetAll(appendPredicates(params, predicate.in("document.id", ids)));
386
+ return await this.dangerouslyGetAll(
387
+ appendPredicates(params, predicate.in("document.id", ids))
388
+ );
340
389
  }
341
390
  async getByUID(documentType, uid, params) {
342
- return await this.getFirst(appendPredicates(params, [
343
- typePredicate(documentType),
344
- predicate.at(`my.${documentType}.uid`, uid)
345
- ]));
391
+ return await this.getFirst(
392
+ appendPredicates(params, [
393
+ typePredicate(documentType),
394
+ predicate.at(`my.${documentType}.uid`, uid)
395
+ ])
396
+ );
346
397
  }
347
398
  async getByUIDs(documentType, uids, params) {
348
- return await this.get(appendPredicates(params, [
349
- typePredicate(documentType),
350
- predicate.in(`my.${documentType}.uid`, uids)
351
- ]));
399
+ return await this.get(
400
+ appendPredicates(params, [
401
+ typePredicate(documentType),
402
+ predicate.in(`my.${documentType}.uid`, uids)
403
+ ])
404
+ );
352
405
  }
353
406
  async getAllByUIDs(documentType, uids, params) {
354
- return await this.dangerouslyGetAll(appendPredicates(params, [
355
- typePredicate(documentType),
356
- predicate.in(`my.${documentType}.uid`, uids)
357
- ]));
407
+ return await this.dangerouslyGetAll(
408
+ appendPredicates(params, [
409
+ typePredicate(documentType),
410
+ predicate.in(`my.${documentType}.uid`, uids)
411
+ ])
412
+ );
358
413
  }
359
414
  async getSingle(documentType, params) {
360
- return await this.getFirst(appendPredicates(params, typePredicate(documentType)));
415
+ return await this.getFirst(
416
+ appendPredicates(params, typePredicate(documentType))
417
+ );
361
418
  }
362
419
  async getByType(documentType, params) {
363
- return await this.get(appendPredicates(params, typePredicate(documentType)));
420
+ return await this.get(
421
+ appendPredicates(params, typePredicate(documentType))
422
+ );
364
423
  }
365
424
  async getAllByType(documentType, params) {
366
425
  return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
367
426
  }
368
427
  async getByTag(tag, params) {
369
- return await this.get(appendPredicates(params, someTagsPredicate(tag)));
428
+ return await this.get(
429
+ appendPredicates(params, someTagsPredicate(tag))
430
+ );
370
431
  }
371
432
  async getAllByTag(tag, params) {
372
- return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
433
+ return await this.dangerouslyGetAll(
434
+ appendPredicates(params, someTagsPredicate(tag))
435
+ );
373
436
  }
374
437
  async getByEveryTag(tags, params) {
375
- return await this.get(appendPredicates(params, everyTagPredicate(tags)));
438
+ return await this.get(
439
+ appendPredicates(params, everyTagPredicate(tags))
440
+ );
376
441
  }
377
442
  async getAllByEveryTag(tags, params) {
378
- return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tags)));
443
+ return await this.dangerouslyGetAll(
444
+ appendPredicates(params, everyTagPredicate(tags))
445
+ );
379
446
  }
380
447
  async getBySomeTags(tags, params) {
381
- return await this.get(appendPredicates(params, someTagsPredicate(tags)));
448
+ return await this.get(
449
+ appendPredicates(params, someTagsPredicate(tags))
450
+ );
382
451
  }
383
452
  async getAllBySomeTags(tags, params) {
384
- return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
453
+ return await this.dangerouslyGetAll(
454
+ appendPredicates(params, someTagsPredicate(tags))
455
+ );
385
456
  }
386
457
  async getRepository(params) {
387
458
  const url = new URL(this.endpoint);
@@ -506,21 +577,28 @@ class Client {
506
577
  const ref = await this.getResolvedRefString();
507
578
  const unsanitizedHeaders = {
508
579
  "Prismic-ref": ref,
509
- "Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
510
580
  Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
511
581
  ...init ? init.headers : {}
512
582
  };
583
+ if (cachedRepository.integrationFieldsRef) {
584
+ unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
585
+ }
513
586
  const headers = {};
514
587
  for (const key in unsanitizedHeaders) {
515
588
  if (unsanitizedHeaders[key]) {
516
589
  headers[key.toLowerCase()] = unsanitizedHeaders[key];
517
590
  }
518
591
  }
519
- const url = new URL(input);
592
+ const url = new URL(
593
+ input
594
+ );
520
595
  url.searchParams.set("ref", ref);
521
596
  const query = url.searchParams.get("query");
522
597
  if (query) {
523
- url.searchParams.set("query", minifyGraphQLQuery(query));
598
+ url.searchParams.set(
599
+ "query",
600
+ minifyGraphQLQuery(query)
601
+ );
524
602
  }
525
603
  return await this.fetchFn(url.toString(), {
526
604
  ...init,
@@ -538,7 +616,11 @@ class Client {
538
616
  const cachedRepository = await this.getCachedRepository(params);
539
617
  const form = cachedRepository.forms[name];
540
618
  if (!form) {
541
- throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
619
+ throw new PrismicError(
620
+ `Form with name "${name}" could not be found`,
621
+ void 0,
622
+ void 0
623
+ );
542
624
  }
543
625
  return form;
544
626
  }
@@ -547,17 +629,17 @@ class Client {
547
629
  if (this.refState.autoPreviewsEnabled) {
548
630
  let previewRef;
549
631
  let cookieJar;
550
- if ((_a = globalThis.document) == null ? void 0 : _a.cookie) {
551
- cookieJar = globalThis.document.cookie;
552
- } else if ((_b = this.refState.httpRequest) == null ? void 0 : _b.headers) {
632
+ if ((_a = this.refState.httpRequest) == null ? void 0 : _a.headers) {
553
633
  if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") {
554
634
  cookieJar = this.refState.httpRequest.headers.get("cookie");
555
635
  } else if ("cookie" in this.refState.httpRequest.headers) {
556
636
  cookieJar = this.refState.httpRequest.headers.cookie;
557
637
  }
638
+ } else if ((_b = globalThis.document) == null ? void 0 : _b.cookie) {
639
+ cookieJar = globalThis.document.cookie;
558
640
  }
559
641
  if (cookieJar) {
560
- previewRef = getCookie(preview, cookieJar);
642
+ previewRef = getPreviewCookie(cookieJar);
561
643
  }
562
644
  if (previewRef) {
563
645
  return previewRef;
@@ -586,7 +668,11 @@ class Client {
586
668
  json = await res.json();
587
669
  } catch (e) {
588
670
  if (res.status === 404) {
589
- throw new NotFoundError(`Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`, url, void 0);
671
+ throw new NotFoundError(
672
+ `Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
673
+ url,
674
+ void 0
675
+ );
590
676
  } else {
591
677
  throw new PrismicError(void 0, url, void 0);
592
678
  }
@@ -600,7 +686,11 @@ class Client {
600
686
  }
601
687
  case 401:
602
688
  case 403: {
603
- throw new ForbiddenError("error" in json ? json.error : json.message, url, json);
689
+ throw new ForbiddenError(
690
+ "error" in json ? json.error : json.message,
691
+ url,
692
+ json
693
+ );
604
694
  }
605
695
  }
606
696
  throw new PrismicError(void 0, url, json);