@prismicio/client 7.21.0-pr.418.8bcf835 → 7.21.0-pr.419.1e13842

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/Client.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  const require_filter = require('./filter.cjs');
2
2
  const require_appendFilters = require('./lib/appendFilters.cjs');
3
+ const require_castThunk = require('./lib/castThunk.cjs');
3
4
  const require_devMsg = require('./lib/devMsg.cjs');
4
5
  const require_everyTagFilter = require('./lib/everyTagFilter.cjs');
5
6
  const require_PrismicError = require('./errors/PrismicError.cjs');
@@ -53,6 +54,28 @@ const GET_ALL_QUERY_DELAY = 500;
53
54
  */
54
55
  const MAX_INVALID_REF_RETRY_ATTEMPTS = 3;
55
56
  /**
57
+ * Modes for client ref management.
58
+ */
59
+ var RefStateMode = /* @__PURE__ */ function(RefStateMode$1) {
60
+ /**
61
+ * Use the repository's master ref.
62
+ */
63
+ RefStateMode$1["Master"] = "Master";
64
+ /**
65
+ * Use a given Release identified by its ID.
66
+ */
67
+ RefStateMode$1["ReleaseID"] = "ReleaseID";
68
+ /**
69
+ * Use a given Release identified by its label.
70
+ */
71
+ RefStateMode$1["ReleaseLabel"] = "ReleaseLabel";
72
+ /**
73
+ * Use a given ref.
74
+ */
75
+ RefStateMode$1["Manual"] = "Manual";
76
+ return RefStateMode$1;
77
+ }(RefStateMode || {});
78
+ /**
56
79
  * A client that allows querying content from a Prismic repository.
57
80
  *
58
81
  * If used in an environment where a global `fetch` function is unavailable,
@@ -135,9 +158,14 @@ var Client = class {
135
158
  * be overridden on each query if needed.
136
159
  */
137
160
  defaultParams;
138
- #getRef;
139
- #autoPreviews = true;
140
- #autoPreviewsRequest;
161
+ /**
162
+ * The client's ref mode state. This determines which ref is used during
163
+ * queries.
164
+ */
165
+ refState = {
166
+ mode: RefStateMode.Master,
167
+ autoPreviewsEnabled: true
168
+ };
141
169
  #cachedRepository;
142
170
  #cachedRepositoryExpiration = 0;
143
171
  /**
@@ -186,11 +214,7 @@ var Client = class {
186
214
  this.graphQLFetch = this.graphQLFetch.bind(this);
187
215
  }
188
216
  /**
189
- * Enables the client to automatically query content from a preview session if
190
- * one is active in browser environments. This is enabled by default in the
191
- * browser.
192
- *
193
- * For server environments, use `enableAutoPreviewsFromReq`.
217
+ * Enables the client to automatically query content from a preview session.
194
218
  *
195
219
  * @example
196
220
  *
@@ -198,126 +222,95 @@ var Client = class {
198
222
  * client.enableAutoPreviews()
199
223
  * ```
200
224
  *
201
- * @see enableAutoPreviewsFromReq
225
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#enableautopreviews}
202
226
  */
203
227
  enableAutoPreviews() {
204
- this.#autoPreviews = true;
228
+ this.refState.autoPreviewsEnabled = true;
205
229
  }
206
230
  /**
207
- * Enables the client to automatically query content from a preview session if
208
- * one is active in server environments. This is disabled by default on the
209
- * server.
210
- *
211
- * For browser environments, use `enableAutoPreviews`.
231
+ * Enables the client to automatically query content from a preview session
232
+ * using an HTTP request object.
212
233
  *
213
234
  * @example
214
235
  *
215
236
  * ```ts
216
- * // In an express app
217
- * app.get("/", function (req, res) {
218
- * client.enableAutoPreviewsFromReq(req)
219
- * })
237
+ * client.enableAutoPreviewsFromReq(req)
220
238
  * ```
221
239
  *
222
- * @param req - An HTTP server request object containing the request's
223
- * cookies.
240
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#enableautopreviewsfromreq}
224
241
  */
225
- enableAutoPreviewsFromReq(request$1) {
226
- this.enableAutoPreviews();
227
- this.#autoPreviewsRequest = request$1;
242
+ enableAutoPreviewsFromReq(req) {
243
+ this.refState.httpRequest = req;
244
+ this.refState.autoPreviewsEnabled = true;
228
245
  }
229
246
  /**
230
247
  * Disables the client from automatically querying content from a preview
231
- * session if one is active.
232
- *
233
- * Automatic preview content querying is enabled by default unless this method
234
- * is called.
248
+ * session.
235
249
  *
236
250
  * @example
237
251
  *
238
252
  * ```ts
239
253
  * client.disableAutoPreviews()
240
254
  * ```
255
+ *
256
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#disableautopreviews}
241
257
  */
242
258
  disableAutoPreviews() {
243
- this.#autoPreviews = false;
244
- this.#autoPreviewsRequest = void 0;
259
+ this.refState.autoPreviewsEnabled = false;
245
260
  }
246
261
  /**
247
- * Queries content from the Prismic repository.
262
+ * Fetches pages based on the `params` argument. Results are paginated.
248
263
  *
249
264
  * @example
250
265
  *
251
266
  * ```ts
252
- * const response = await client.get()
267
+ * const response = await client.get({ pageSize: 10 })
253
268
  * ```
254
269
  *
255
- * @typeParam TDocument - Type of Prismic documents returned.
256
- *
257
- * @param params - Parameters to filter, sort, and paginate results.
258
- *
259
- * @returns A paginated response containing the result of the query.
270
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#get}
260
271
  */
261
272
  async get(params) {
262
- const { data } = await this._get(params);
263
- return data;
273
+ return await (await this.#internalGet(params)).json();
264
274
  }
265
275
  /**
266
- * Queries content from the Prismic repository and returns only the first
267
- * result, if any.
276
+ * Fetches the first page returned based on the `params` argument.
268
277
  *
269
278
  * @example
270
279
  *
271
280
  * ```ts
272
- * const document = await client.getFirst()
281
+ * const page = await client.getFirst()
273
282
  * ```
274
283
  *
275
- * @typeParam TDocument - Type of the Prismic document returned.
276
- *
277
- * @param params - Parameters to filter, sort, and paginate results.
278
- *
279
- * @returns The first result of the query, if any.
284
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getfirst}
280
285
  */
281
286
  async getFirst(params) {
282
- const actualParams = { ...params };
283
- if (!(params && params.page) && !(params === null || params === void 0 ? void 0 : params.pageSize)) {
284
- var _this$defaultParams;
285
- actualParams.pageSize = ((_this$defaultParams = this.defaultParams) === null || _this$defaultParams === void 0 ? void 0 : _this$defaultParams.pageSize) ?? 1;
286
- }
287
- const { data, url } = await this._get(actualParams);
288
- const firstResult = data.results[0];
289
- if (firstResult) return firstResult;
290
- throw new require_NotFoundError.NotFoundError("No documents were returned", url, void 0);
287
+ const actualParams = (params === null || params === void 0 ? void 0 : params.page) || (params === null || params === void 0 ? void 0 : params.pageSize) ? params : {
288
+ ...params,
289
+ pageSize: 1
290
+ };
291
+ const response = await this.#internalGet(actualParams);
292
+ const { results } = await response.clone().json();
293
+ if (results[0]) return results[0];
294
+ throw new require_NotFoundError.NotFoundError("No documents were returned", response.url, void 0);
291
295
  }
292
296
  /**
293
- * **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and
294
- * require more resources than other methods. Prefer using other methods that
295
- * filter by filters such as `getAllByType`.
296
- *
297
- * Queries content from the Prismic repository and returns all matching
298
- * content. If no filters are provided, all documents will be fetched.
299
- *
300
- * This method may make multiple network requests to query all matching
301
- * content.
297
+ * Fetches all pages based on the `params` argument. This method may make
298
+ * multiple network requests to fetch all matching pages.
302
299
  *
303
300
  * @example
304
301
  *
305
302
  * ```ts
306
- * const response = await client.dangerouslyGetAll()
303
+ * const pages = await client.dangerouslyGetAll()
307
304
  * ```
308
305
  *
309
- * @typeParam TDocument - Type of Prismic documents returned.
310
- *
311
- * @param params - Parameters to filter, sort, and paginate results.
312
- *
313
- * @returns A list of documents matching the query.
306
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#dangerouslygetall}
314
307
  */
315
308
  async dangerouslyGetAll(params = {}) {
316
- var _this$defaultParams2;
309
+ var _this$defaultParams;
317
310
  const { limit = Infinity,...actualParams } = params;
318
311
  const resolvedParams = {
319
312
  ...actualParams,
320
- pageSize: Math.min(limit, actualParams.pageSize || ((_this$defaultParams2 = this.defaultParams) === null || _this$defaultParams2 === void 0 ? void 0 : _this$defaultParams2.pageSize) || MAX_PAGE_SIZE)
313
+ pageSize: Math.min(limit, actualParams.pageSize || ((_this$defaultParams = this.defaultParams) === null || _this$defaultParams === void 0 ? void 0 : _this$defaultParams.pageSize) || MAX_PAGE_SIZE)
321
314
  };
322
315
  const documents = [];
323
316
  let latestResult;
@@ -333,39 +326,21 @@ var Client = class {
333
326
  return documents.slice(0, limit);
334
327
  }
335
328
  /**
336
- * Queries a document from the Prismic repository with a specific ID.
337
- *
338
- * @remarks
339
- * A document's UID is different from its ID. An ID is automatically generated
340
- * for all documents and is made available on its `id` property. A UID is
341
- * provided in the Prismic editor and is unique among all documents of its
342
- * custom type.
329
+ * Fetches a page with a specific ID.
343
330
  *
344
331
  * @example
345
332
  *
346
333
  * ```ts
347
- * const document = await client.getByID("WW4bKScAAMAqmluX")
334
+ * const page = await client.getByID("WW4bKScAAMAqmluX")
348
335
  * ```
349
336
  *
350
- * @typeParam TDocument- Type of the Prismic document returned.
351
- *
352
- * @param id - ID of the document.
353
- * @param params - Parameters to filter, sort, and paginate the results.
354
- *
355
- * @returns The document with an ID matching the `id` parameter, if a matching
356
- * document exists.
337
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbyid}
357
338
  */
358
339
  async getByID(id, params) {
359
340
  return await this.getFirst(require_appendFilters.appendFilters(params, require_filter.filter.at("document.id", id)));
360
341
  }
361
342
  /**
362
- * Queries documents from the Prismic repository with specific IDs.
363
- *
364
- * @remarks
365
- * A document's UID is different from its ID. An ID is automatically generated
366
- * for all documents and is made available on its `id` property. A UID is
367
- * provided in the Prismic editor and is unique among all documents of its
368
- * custom type.
343
+ * Fetches pages with specific IDs. Results are paginated.
369
344
  *
370
345
  * @example
371
346
  *
@@ -376,172 +351,95 @@ var Client = class {
376
351
  * ])
377
352
  * ```
378
353
  *
379
- * @typeParam TDocument - Type of Prismic documents returned.
380
- *
381
- * @param ids - A list of document IDs.
382
- * @param params - Parameters to filter, sort, and paginate the results.
383
- *
384
- * @returns A paginated response containing documents with IDs matching the
385
- * `ids` parameter.
354
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbyids}
386
355
  */
387
356
  async getByIDs(ids, params) {
388
357
  return await this.get(require_appendFilters.appendFilters(params, require_filter.filter.in("document.id", ids)));
389
358
  }
390
359
  /**
391
- * Queries all documents from the Prismic repository with specific IDs.
392
- *
393
- * This method may make multiple network requests to query all matching
394
- * content.
395
- *
396
- * @remarks
397
- * A document's UID is different from its ID. An ID is automatically generated
398
- * for all documents and is made available on its `id` property. A UID is
399
- * provided in the Prismic editor and is unique among all documents of its
400
- * custom type.
360
+ * Fetches pages with specific IDs. This method may make multiple network
361
+ * requests to fetch all matching pages.
401
362
  *
402
363
  * @example
403
364
  *
404
365
  * ```ts
405
- * const response = await client.getAllByIDs([
366
+ * const pages = await client.getAllByIDs([
406
367
  * "WW4bKScAAMAqmluX",
407
368
  * "U1kTRgEAAC8A5ldS",
408
369
  * ])
409
370
  * ```
410
371
  *
411
- * @typeParam TDocument - Type of Prismic documents returned.
412
- *
413
- * @param ids - A list of document IDs.
414
- * @param params - Parameters to filter, sort, and paginate the results.
415
- *
416
- * @returns A list of documents with IDs matching the `ids` parameter.
372
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbyids}
417
373
  */
418
374
  async getAllByIDs(ids, params) {
419
375
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, require_filter.filter.in("document.id", ids)));
420
376
  }
421
377
  /**
422
- * Queries a document from the Prismic repository with a specific UID and
423
- * custom type.
424
- *
425
- * @remarks
426
- * A document's UID is different from its ID. An ID is automatically generated
427
- * for all documents and is made available on its `id` property. A UID is
428
- * provided in the Prismic editor and is unique among all documents of its
429
- * custom type.
378
+ * Fetches a page with a specific UID and type.
430
379
  *
431
380
  * @example
432
381
  *
433
382
  * ```ts
434
- * const document = await client.getByUID("blog_post", "my-first-post")
383
+ * const page = await client.getByUID("blog_post", "my-first-post")
435
384
  * ```
436
385
  *
437
- * @typeParam TDocument - Type of the Prismic document returned.
438
- *
439
- * @param documentType - The API ID of the document's custom type.
440
- * @param uid - UID of the document.
441
- * @param params - Parameters to filter, sort, and paginate the results.
442
- *
443
- * @returns The document with a UID matching the `uid` parameter, if a
444
- * matching document exists.
386
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbyuid}
445
387
  */
446
388
  async getByUID(documentType, uid, params) {
447
389
  return await this.getFirst(require_appendFilters.appendFilters(params, [require_typeFilter.typeFilter(documentType), require_filter.filter.at(`my.${documentType}.uid`, uid)]));
448
390
  }
449
391
  /**
450
- * Queries document from the Prismic repository with specific UIDs and Custom
451
- * Type.
452
- *
453
- * @remarks
454
- * A document's UID is different from its ID. An ID is automatically generated
455
- * for all documents and is made available on its `id` property. A UID is
456
- * provided in the Prismic editor and is unique among all documents of its
457
- * custom type.
392
+ * Fetches pages with specific UIDs and a specific type. Results are
393
+ * paginated.
458
394
  *
459
395
  * @example
460
396
  *
461
397
  * ```ts
462
- * const document = await client.getByUIDs("blog_post", [
398
+ * const response = await client.getByUIDs("blog_post", [
463
399
  * "my-first-post",
464
400
  * "my-second-post",
465
401
  * ])
466
402
  * ```
467
403
  *
468
- * @typeParam TDocument - Type of the Prismic document returned.
469
- *
470
- * @param documentType - The API ID of the document's custom type.
471
- * @param uids - A list of document UIDs.
472
- * @param params - Parameters to filter, sort, and paginate the results.
473
- *
474
- * @returns A paginated response containing documents with UIDs matching the
475
- * `uids` parameter.
404
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbyuids}
476
405
  */
477
406
  async getByUIDs(documentType, uids, params) {
478
407
  return await this.get(require_appendFilters.appendFilters(params, [require_typeFilter.typeFilter(documentType), require_filter.filter.in(`my.${documentType}.uid`, uids)]));
479
408
  }
480
409
  /**
481
- * Queries all documents from the Prismic repository with specific UIDs and
482
- * custom type.
483
- *
484
- * This method may make multiple network requests to query all matching
485
- * content.
486
- *
487
- * @remarks
488
- * A document's UID is different from its ID. An ID is automatically generated
489
- * for all documents and is made available on its `id` property. A UID is
490
- * provided in the Prismic editor and is unique among all documents of its
491
- * custom type.
410
+ * Fetches pages with specific UIDs and a specific type. This method may make
411
+ * multiple network requests to fetch all matching pages.
492
412
  *
493
413
  * @example
494
414
  *
495
415
  * ```ts
496
- * const response = await client.getAllByUIDs([
416
+ * const pages = await client.getAllByUIDs("blog_post", [
497
417
  * "my-first-post",
498
418
  * "my-second-post",
499
419
  * ])
500
420
  * ```
501
421
  *
502
- * @typeParam TDocument - Type of Prismic documents returned.
503
- *
504
- * @param documentType - The API ID of the document's custom type.
505
- * @param uids - A list of document UIDs.
506
- * @param params - Parameters to filter, sort, and paginate the results.
507
- *
508
- * @returns A list of documents with UIDs matching the `uids` parameter.
422
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbyuids}
509
423
  */
510
424
  async getAllByUIDs(documentType, uids, params) {
511
425
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, [require_typeFilter.typeFilter(documentType), require_filter.filter.in(`my.${documentType}.uid`, uids)]));
512
426
  }
513
427
  /**
514
- * Queries a singleton document from the Prismic repository for a specific
515
- * custom type.
516
- *
517
- * @remarks
518
- * A singleton document is one that is configured in Prismic to only allow one
519
- * instance. For example, a repository may be configured to contain just one
520
- * Settings document. This is in contrast to a repeatable custom type which
521
- * allows multiple instances of itself.
428
+ * Fetches a specific single type page.
522
429
  *
523
430
  * @example
524
431
  *
525
432
  * ```ts
526
- * const document = await client.getSingle("settings")
433
+ * const page = await client.getSingle("settings")
527
434
  * ```
528
435
  *
529
- * @typeParam TDocument - Type of the Prismic document returned.
530
- *
531
- * @param documentType - The API ID of the singleton custom type.
532
- * @param params - Parameters to filter, sort, and paginate the results.
533
- *
534
- * @returns The singleton document for the custom type, if a matching document
535
- * exists.
436
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getsingle}
536
437
  */
537
438
  async getSingle(documentType, params) {
538
439
  return await this.getFirst(require_appendFilters.appendFilters(params, require_typeFilter.typeFilter(documentType)));
539
440
  }
540
441
  /**
541
- * Queries documents from the Prismic repository for a specific custom type.
542
- *
543
- * Use `getAllByType` instead if you need to query all documents for a
544
- * specific custom type.
442
+ * Fetches pages with a specific type. Results are paginated.
545
443
  *
546
444
  * @example
547
445
  *
@@ -549,177 +447,124 @@ var Client = class {
549
447
  * const response = await client.getByType("blog_post")
550
448
  * ```
551
449
  *
552
- * @typeParam TDocument - Type of Prismic documents returned.
553
- *
554
- * @param documentType - The API ID of the custom type.
555
- * @param params - Parameters to filter, sort, and paginate the results.
556
- *
557
- * @returns A paginated response containing documents of the custom type.
450
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbytype}
558
451
  */
559
452
  async getByType(documentType, params) {
560
453
  return await this.get(require_appendFilters.appendFilters(params, require_typeFilter.typeFilter(documentType)));
561
454
  }
562
455
  /**
563
- * Queries all documents from the Prismic repository for a specific Custom
564
- * Type.
565
- *
566
- * This method may make multiple network requests to query all matching
567
- * content.
456
+ * Fetches pages with a specific type. This method may make multiple network
457
+ * requests to fetch all matching documents.
568
458
  *
569
459
  * @example
570
460
  *
571
461
  * ```ts
572
- * const response = await client.getByType("blog_post")
462
+ * const pages = await client.getAllByType("blog_post")
573
463
  * ```
574
464
  *
575
- * @typeParam TDocument - Type of Prismic documents returned.
576
- *
577
- * @param documentType - The API ID of the custom type.
578
- * @param params - Parameters to filter, sort, and paginate the results.
579
- *
580
- * @returns A list of all documents of the custom type.
465
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbytype}
581
466
  */
582
467
  async getAllByType(documentType, params) {
583
468
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, require_typeFilter.typeFilter(documentType)));
584
469
  }
585
470
  /**
586
- * Queries documents from the Prismic repository with a specific tag.
587
- *
588
- * Use `getAllByTag` instead if you need to query all documents with a
589
- * specific tag.
471
+ * Fetches pages with a specific tag. Results are paginated.
590
472
  *
591
473
  * @example
592
474
  *
593
475
  * ```ts
594
- * const response = await client.getByTag("food")
476
+ * const response = await client.getByTag("featured")
595
477
  * ```
596
478
  *
597
- * @typeParam TDocument - Type of Prismic documents returned.
598
- *
599
- * @param tag - The tag that must be included on a document.
600
- * @param params - Parameters to filter, sort, and paginate the results.
601
- *
602
- * @returns A paginated response containing documents with the tag.
479
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbytag}
603
480
  */
604
481
  async getByTag(tag, params) {
605
482
  return await this.get(require_appendFilters.appendFilters(params, require_someTagsFilter.someTagsFilter(tag)));
606
483
  }
607
484
  /**
608
- * Queries all documents from the Prismic repository with a specific tag.
609
- *
610
- * This method may make multiple network requests to query all matching
611
- * content.
485
+ * Fetches pages with a specific tag. This method may make multiple network
486
+ * requests to fetch all matching documents.
612
487
  *
613
488
  * @example
614
489
  *
615
490
  * ```ts
616
- * const response = await client.getAllByTag("food")
491
+ * const pages = await client.getAllByTag("featured")
617
492
  * ```
618
493
  *
619
- * @typeParam TDocument - Type of Prismic documents returned.
620
- *
621
- * @param tag - The tag that must be included on a document.
622
- * @param params - Parameters to filter, sort, and paginate the results.
623
- *
624
- * @returns A list of all documents with the tag.
494
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbytag}
625
495
  */
626
496
  async getAllByTag(tag, params) {
627
497
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, require_someTagsFilter.someTagsFilter(tag)));
628
498
  }
629
499
  /**
630
- * Queries documents from the Prismic repository with specific tags. A
631
- * document must be tagged with all of the queried tags to be included.
500
+ * Fetches pages with every tag from a list of tags. Results are paginated.
632
501
  *
633
502
  * @example
634
503
  *
635
504
  * ```ts
636
- * const response = await client.getByEveryTag(["food", "fruit"])
505
+ * const response = await client.getByEveryTag(["featured", "homepage"])
637
506
  * ```
638
507
  *
639
- * @typeParam TDocument - Type of Prismic documents returned.
640
- *
641
- * @param tags - A list of tags that must be included on a document.
642
- * @param params - Parameters to filter, sort, and paginate the results.
643
- *
644
- * @returns A paginated response containing documents with the tags.
508
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbyeverytag}
645
509
  */
646
510
  async getByEveryTag(tags, params) {
647
511
  return await this.get(require_appendFilters.appendFilters(params, require_everyTagFilter.everyTagFilter(tags)));
648
512
  }
649
513
  /**
650
- * Queries documents from the Prismic repository with specific tags. A
651
- * document must be tagged with all of the queried tags to be included.
652
- *
653
- * This method may make multiple network requests to query all matching
654
- * content.
514
+ * Fetches pages with every tag from a list of tags. This method may make
515
+ * multiple network requests to fetch all matching pages.
655
516
  *
656
517
  * @example
657
518
  *
658
519
  * ```ts
659
- * const response = await client.getAllByEveryTag(["food", "fruit"])
520
+ * const pages = await client.getAllByEveryTag(["featured", "homepage"])
660
521
  * ```
661
522
  *
662
- * @typeParam TDocument - Type of Prismic documents returned.
663
- *
664
- * @param tags - A list of tags that must be included on a document.
665
- * @param params - Parameters to filter, sort, and paginate the results.
666
- *
667
- * @returns A list of all documents with the tags.
523
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbyeverytag}
668
524
  */
669
525
  async getAllByEveryTag(tags, params) {
670
526
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, require_everyTagFilter.everyTagFilter(tags)));
671
527
  }
672
528
  /**
673
- * Queries documents from the Prismic repository with specific tags. A
674
- * document must be tagged with at least one of the queried tags to be
675
- * included.
529
+ * Fetches pages with at least one tag from a list of tags. Results are
530
+ * paginated.
676
531
  *
677
532
  * @example
678
533
  *
679
534
  * ```ts
680
- * const response = await client.getByEveryTag(["food", "fruit"])
535
+ * const response = await client.getBySomeTags(["featured", "homepage"])
681
536
  * ```
682
537
  *
683
- * @typeParam TDocument - Type of Prismic documents returned.
684
- *
685
- * @param tags - A list of tags that must be included on a document.
686
- * @param params - Parameters to filter, sort, and paginate the results.
687
- *
688
- * @returns A paginated response containing documents with at least one of the
689
- * tags.
538
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getbysometags}
690
539
  */
691
540
  async getBySomeTags(tags, params) {
692
541
  return await this.get(require_appendFilters.appendFilters(params, require_someTagsFilter.someTagsFilter(tags)));
693
542
  }
694
543
  /**
695
- * Queries documents from the Prismic repository with specific tags. A
696
- * document must be tagged with at least one of the queried tags to be
697
- * included.
698
- *
699
- * This method may make multiple network requests to query all matching
700
- * content.
544
+ * Fetches pages with at least one tag from a list of tags. This method may
545
+ * make multiple network requests to fetch all matching documents.
701
546
  *
702
547
  * @example
703
548
  *
704
549
  * ```ts
705
- * const response = await client.getAllBySomeTags(["food", "fruit"])
550
+ * const pages = await client.getAllBySomeTags(["featured", "homepage"])
706
551
  * ```
707
552
  *
708
- * @typeParam TDocument - Type of Prismic documents returned.
709
- *
710
- * @param tags - A list of tags that must be included on a document.
711
- * @param params - Parameters to filter, sort, and paginate the results.
712
- *
713
- * @returns A list of all documents with at least one of the tags.
553
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getallbysometags}
714
554
  */
715
555
  async getAllBySomeTags(tags, params) {
716
556
  return await this.dangerouslyGetAll(require_appendFilters.appendFilters(params, require_someTagsFilter.someTagsFilter(tags)));
717
557
  }
718
558
  /**
719
- * Returns metadata about the Prismic repository, such as its refs, releases,
720
- * and custom types.
559
+ * Fetches metadata about the client's Prismic repository.
560
+ *
561
+ * @example
562
+ *
563
+ * ```ts
564
+ * const repository = await client.getRepository()
565
+ * ```
721
566
  *
722
- * @returns Repository metadata.
567
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getrepository}
723
568
  */
724
569
  async getRepository(params) {
725
570
  if (this.#cachedRepository && this.#cachedRepositoryExpiration > Date.now()) return this.#cachedRepository;
@@ -727,93 +572,124 @@ var Client = class {
727
572
  const accessToken = (params === null || params === void 0 ? void 0 : params.accessToken) || this.accessToken;
728
573
  if (accessToken) url.searchParams.set("access_token", accessToken);
729
574
  const response = await this.#request(url, params);
730
- switch (response.status) {
731
- case 200:
732
- this.#cachedRepository = await response.json();
733
- this.#cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
734
- return this.#cachedRepository;
735
- case 401: {
736
- const json = await response.json();
737
- throw new require_ForbiddenError.ForbiddenError(json.error, url.toString(), json);
738
- }
739
- case 404: throw new require_RepositoryNotFoundError.RepositoryNotFoundError(`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`, url.toString(), void 0);
740
- default: throw new require_PrismicError.PrismicError(void 0, url.toString(), await response.text());
575
+ if (response.ok) {
576
+ this.#cachedRepository = await response.json();
577
+ this.#cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
578
+ return this.#cachedRepository;
741
579
  }
580
+ if (response.status === 404) throw new require_RepositoryNotFoundError.RepositoryNotFoundError(`Prismic repository not found. Check that "${this.documentAPIEndpoint}" is pointing to the correct repository.`, url.toString(), void 0);
581
+ return await this.#throwContentAPIError(response, url.toString());
742
582
  }
743
583
  /**
744
- * Returns a list of all refs for the Prismic repository.
584
+ * Fetches the repository's active refs.
585
+ *
586
+ * @example
745
587
  *
746
- * Refs are used to identify which version of the repository's content should
747
- * be queried. All repositories will have at least one ref pointing to the
748
- * latest published content called the "master ref".
588
+ * ```ts
589
+ * const refs = await client.getRefs()
590
+ * ```
749
591
  *
750
- * @returns A list of all refs for the Prismic repository.
592
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getrefs}
751
593
  */
752
594
  async getRefs(params) {
753
595
  return (await this.getRepository(params)).refs;
754
596
  }
755
597
  /**
756
- * Returns a ref for the Prismic repository with a matching ID.
598
+ * Fetches a ref by its ID.
757
599
  *
758
- * @param id - ID of the ref.
600
+ * @example
601
+ *
602
+ * ```ts
603
+ * const ref = await client.getRefByID("YhE3YhEAACIA4321")
604
+ * ```
759
605
  *
760
- * @returns The ref with a matching ID, if it exists.
606
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getrefbyid}
761
607
  */
762
608
  async getRefByID(id, params) {
763
609
  return require_findRefByID.findRefByID(await this.getRefs(params), id);
764
610
  }
765
611
  /**
766
- * Returns a ref for the Prismic repository with a matching label.
612
+ * Fetches a ref by its label. A release ref's label is its name shown in the
613
+ * Page Builder.
614
+ *
615
+ * @example
767
616
  *
768
- * @param label - Label of the ref.
617
+ * ```ts
618
+ * const ref = await client.getRefByLabel("My Release")
619
+ * ```
769
620
  *
770
- * @returns The ref with a matching label, if it exists.
621
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getrefbylabel}
771
622
  */
772
623
  async getRefByLabel(label, params) {
773
624
  return require_findRefByLabel.findRefByLabel(await this.getRefs(params), label);
774
625
  }
775
626
  /**
776
- * Returns the master ref for the Prismic repository. The master ref points to
777
- * the repository's latest published content.
627
+ * Fetches the repository's master ref.
628
+ *
629
+ * @example
630
+ *
631
+ * ```ts
632
+ * const masterRef = await client.getMasterRef()
633
+ * ```
778
634
  *
779
- * @returns The repository's master ref.
635
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getmasterref}
780
636
  */
781
637
  async getMasterRef(params) {
782
638
  return require_findMasterRef.findMasterRef(await this.getRefs(params));
783
639
  }
784
640
  /**
785
- * Returns a list of all Releases for the Prismic repository. Releases are
786
- * used to group content changes before publishing.
641
+ * Fetches the repository's active releases.
787
642
  *
788
- * @returns A list of all Releases for the Prismic repository.
643
+ * @example
644
+ *
645
+ * ```ts
646
+ * const releases = await client.getReleases()
647
+ * ```
648
+ *
649
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getreleases}
789
650
  */
790
651
  async getReleases(params) {
791
652
  return (await this.getRefs(params)).filter((ref) => !ref.isMasterRef);
792
653
  }
793
654
  /**
794
- * Returns a Release for the Prismic repository with a matching ID.
655
+ * Fetches a release with a specific ID.
795
656
  *
796
- * @param id - ID of the Release.
657
+ * @example
797
658
  *
798
- * @returns The Release with a matching ID, if it exists.
659
+ * ```ts
660
+ * const release = await client.getReleaseByID("YhE3YhEAACIA4321")
661
+ * ```
662
+ *
663
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getreleasebyid}
799
664
  */
800
665
  async getReleaseByID(id, params) {
801
666
  return require_findRefByID.findRefByID(await this.getReleases(params), id);
802
667
  }
803
668
  /**
804
- * Returns a Release for the Prismic repository with a matching label.
669
+ * Fetches a release by its label. A release ref's label is its name shown in
670
+ * the Page Builder.
671
+ *
672
+ * @example
805
673
  *
806
- * @param label - Label of the ref.
674
+ * ```ts
675
+ * const release = await client.getReleaseByLabel("My Release")
676
+ * ```
807
677
  *
808
- * @returns The ref with a matching label, if it exists.
678
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#getreleasebylabel}
809
679
  */
810
680
  async getReleaseByLabel(label, params) {
811
681
  return require_findRefByLabel.findRefByLabel(await this.getReleases(params), label);
812
682
  }
813
683
  /**
814
- * Returns a list of all tags used in the Prismic repository.
684
+ * Fetches the repository's page tags.
685
+ *
686
+ * @example
815
687
  *
816
- * @returns A list of all tags used in the repository.
688
+ * ```ts
689
+ * const tags = await client.getTags()
690
+ * ```
691
+ *
692
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#gettags}
817
693
  */
818
694
  async getTags(params) {
819
695
  const repository = await this.getRepository(params);
@@ -827,14 +703,20 @@ var Client = class {
827
703
  return repository.tags;
828
704
  }
829
705
  /**
830
- * Builds a URL used to query content from the Prismic repository.
706
+ * Builds a Content API query URL with a set of parameters.
831
707
  *
832
- * @param params - Parameters to filter, sort, and paginate the results.
708
+ * @example
833
709
  *
834
- * @returns A URL string that can be requested to query content.
710
+ * ```ts
711
+ * const url = await client.buildQueryURL({
712
+ * filters: [filter.at("document.type", "blog_post")],
713
+ * })
714
+ * ```
715
+ *
716
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#buildqueryurl}
835
717
  */
836
718
  async buildQueryURL({ signal, fetchOptions,...params } = {}) {
837
- const ref = params.ref || await this.#getResolvedRef({
719
+ const ref = params.ref || await this.getResolvedRefString({
838
720
  accessToken: params.accessToken,
839
721
  signal,
840
722
  fetchOptions
@@ -855,23 +737,18 @@ var Client = class {
855
737
  });
856
738
  }
857
739
  /**
858
- * Determines the URL for a previewed document during an active preview
859
- * session. The result of this method should be used to redirect the user to
860
- * the document's URL.
740
+ * Fetches a previewed page's URL using a preview token and page ID.
861
741
  *
862
742
  * @example
863
743
  *
864
744
  * ```ts
865
- * const url = client.resolvePreviewURL({
866
- * linkResolver: (document) => `/${document.uid}`
867
- * defaultURL: '/'
868
- * })
745
+ * const url = await client.resolvePreviewURL({
746
+ * linkResolver,
747
+ * defaultURL: "/",
748
+ * })
869
749
  * ```
870
750
  *
871
- * @param args - Arguments to configure the URL resolving.
872
- *
873
- * @returns The URL for the previewed document during an active preview
874
- * session. The user should be redirected to this URL.
751
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#resolvepreviewurl}
875
752
  */
876
753
  async resolvePreviewURL(args) {
877
754
  let documentID = args.documentID;
@@ -880,13 +757,13 @@ var Client = class {
880
757
  const searchParams = new URLSearchParams(globalThis.location.search);
881
758
  documentID = documentID || searchParams.get("documentId");
882
759
  previewToken = previewToken || searchParams.get("token");
883
- } else if (this.#autoPreviewsRequest) {
884
- if ("query" in this.#autoPreviewsRequest) {
885
- var _this$autoPreviewsReq, _this$autoPreviewsReq2;
886
- documentID = documentID || ((_this$autoPreviewsReq = this.#autoPreviewsRequest.query) === null || _this$autoPreviewsReq === void 0 ? void 0 : _this$autoPreviewsReq.documentId);
887
- previewToken = previewToken || ((_this$autoPreviewsReq2 = this.#autoPreviewsRequest.query) === null || _this$autoPreviewsReq2 === void 0 ? void 0 : _this$autoPreviewsReq2.token);
888
- } else if ("url" in this.#autoPreviewsRequest && this.#autoPreviewsRequest.url) {
889
- const searchParams = new URL(this.#autoPreviewsRequest.url, "missing-host://").searchParams;
760
+ } else if (this.refState.httpRequest) {
761
+ if ("query" in this.refState.httpRequest) {
762
+ var _this$refState$httpRe, _this$refState$httpRe2;
763
+ documentID = documentID || ((_this$refState$httpRe = this.refState.httpRequest.query) === null || _this$refState$httpRe === void 0 ? void 0 : _this$refState$httpRe.documentId);
764
+ previewToken = previewToken || ((_this$refState$httpRe2 = this.refState.httpRequest.query) === null || _this$refState$httpRe2 === void 0 ? void 0 : _this$refState$httpRe2.token);
765
+ } else if ("url" in this.refState.httpRequest && this.refState.httpRequest.url) {
766
+ const searchParams = new URL(this.refState.httpRequest.url, "missing-host://").searchParams;
890
767
  documentID = documentID || searchParams.get("documentId");
891
768
  previewToken = previewToken || searchParams.get("token");
892
769
  }
@@ -903,96 +780,88 @@ var Client = class {
903
780
  return args.defaultURL;
904
781
  }
905
782
  /**
906
- * Configures the client to query the latest published content for all future
907
- * queries.
908
- *
909
- * If the `ref` parameter is provided during a query, it takes priority for
910
- * that query.
783
+ * Configures the client to query the latest published content. This is the
784
+ * client's default mode.
911
785
  *
912
786
  * @example
913
787
  *
914
788
  * ```ts
915
- * await client.queryLatestContent()
916
- * const document = await client.getByID("WW4bKScAAMAqmluX")
789
+ * client.queryLatestContent()
917
790
  * ```
791
+ *
792
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#querylatestcontent}
918
793
  */
919
794
  queryLatestContent() {
920
- this.#getRef = void 0;
795
+ this.refState.mode = RefStateMode.Master;
921
796
  }
922
797
  /**
923
- * Configures the client to query content from a specific Release identified
924
- * by its ID for all future queries.
925
- *
926
- * If the `ref` parameter is provided during a query, it takes priority for
927
- * that query.
798
+ * Configures the client to query content from a release with a specific ID.
928
799
  *
929
800
  * @example
930
801
  *
931
802
  * ```ts
932
- * await client.queryContentFromReleaseByID("YLB7OBAAACMA7Cpa")
933
- * const document = await client.getByID("WW4bKScAAMAqmluX")
803
+ * client.queryContentFromReleaseByID("YhE3YhEAACIA4321")
934
804
  * ```
935
805
  *
936
- * @param releaseID - The ID of the Release.
806
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#querycontentfromreleasebyid}
937
807
  */
938
- queryContentFromReleaseByID(id) {
939
- this.#getRef = async (params) => {
940
- return (await this.getReleaseByID(id, params)).ref;
808
+ queryContentFromReleaseByID(releaseID) {
809
+ this.refState = {
810
+ ...this.refState,
811
+ mode: RefStateMode.ReleaseID,
812
+ releaseID
941
813
  };
942
814
  }
943
815
  /**
944
- * Configures the client to query content from a specific Release identified
945
- * by its label for all future queries.
946
- *
947
- * If the `ref` parameter is provided during a query, it takes priority for
948
- * that query.
816
+ * Configures the client to query content from a release with a specific
817
+ * label.
949
818
  *
950
819
  * @example
951
820
  *
952
821
  * ```ts
953
- * await client.queryContentFromReleaseByLabel("My Release")
954
- * const document = await client.getByID("WW4bKScAAMAqmluX")
822
+ * client.queryContentFromReleaseByLabel("My Release")
955
823
  * ```
956
824
  *
957
- * @param releaseLabel - The label of the Release.
825
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#querycontentfromreleasebylabel}
958
826
  */
959
- queryContentFromReleaseByLabel(label) {
960
- this.#getRef = async (params) => {
961
- return (await this.getReleaseByLabel(label, params)).ref;
827
+ queryContentFromReleaseByLabel(releaseLabel) {
828
+ this.refState = {
829
+ ...this.refState,
830
+ mode: RefStateMode.ReleaseLabel,
831
+ releaseLabel
962
832
  };
963
833
  }
964
834
  /**
965
- * Configures the client to query content from a specific ref. The ref can be
966
- * provided as a string or a function.
967
- *
968
- * If a function is provided, the ref is fetched lazily before each query. The
969
- * function may also be asynchronous.
835
+ * Configures the client to query content from a specific ref.
970
836
  *
971
837
  * @example
972
838
  *
973
839
  * ```ts
974
- * await client.queryContentFromRef("my-ref")
975
- * const document = await client.getByID("WW4bKScAAMAqmluX")
840
+ * client.queryContentFromRef("my-ref")
976
841
  * ```
977
842
  *
978
- * @param ref - The ref or a function that returns the ref from which to query
979
- * content.
843
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#querycontentfromref}
980
844
  */
981
845
  queryContentFromRef(ref) {
982
- this.#getRef = typeof ref === "string" ? () => ref : ref;
846
+ this.refState = {
847
+ ...this.refState,
848
+ mode: RefStateMode.Manual,
849
+ ref
850
+ };
983
851
  }
984
852
  /**
985
- * A `fetch()` function to be used with GraphQL clients configured for
986
- * Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
987
- * and Authorization headers. Queries will automatically be minified by
988
- * removing whitespace where possible.
853
+ * A preconfigured `fetch()` function for Prismic's GraphQL API that can be
854
+ * provided to GraphQL clients.
989
855
  *
990
856
  * @example
991
857
  *
992
858
  * ```ts
859
+ * import { createClient, getGraphQLEndpoint } from "@prismicio/client"
860
+ *
861
+ * const client = createClient("example-prismic-repo")
993
862
  * const graphQLClient = new ApolloClient({
994
863
  * link: new HttpLink({
995
- * uri: prismic.getGraphQLEndpoint(repositoryName),
864
+ * uri: getGraphQLEndpoint(client.repositoryName),
996
865
  * // Provide `client.graphQLFetch` as the fetch implementation.
997
866
  * fetch: client.graphQLFetch,
998
867
  * // Using GET is required.
@@ -1002,17 +871,11 @@ var Client = class {
1002
871
  * })
1003
872
  * ```
1004
873
  *
1005
- * @param input - The `fetch()` `input` parameter. Only strings are supported.
1006
- * @param init - The `fetch()` `init` parameter. Only plain objects are
1007
- * supported.
1008
- *
1009
- * @returns The `fetch()` Response for the request.
1010
- *
1011
- * @experimental
874
+ * @see {@link https://prismic.io/docs/technical-reference/prismicio-client/v7#graphqlfetch}
1012
875
  */
1013
876
  async graphQLFetch(input, init) {
1014
877
  const repository = await this.getRepository();
1015
- const ref = await this.#getResolvedRef();
878
+ const ref = await this.getResolvedRefString();
1016
879
  const unsanitizedHeaders = {
1017
880
  "Prismic-ref": ref,
1018
881
  Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
@@ -1053,79 +916,72 @@ var Client = class {
1053
916
  *
1054
917
  * @returns The ref to use during a query.
1055
918
  */
1056
- async #getResolvedRef(params) {
1057
- var _this$getRef;
1058
- if (this.#autoPreviews) {
1059
- var _this$autoPreviewsReq3, _globalThis$document;
1060
- const previewRef = require_getPreviewCookie.getPreviewCookie((((_this$autoPreviewsReq3 = this.#autoPreviewsRequest) === null || _this$autoPreviewsReq3 === void 0 ? void 0 : _this$autoPreviewsReq3.headers) ? "get" in this.#autoPreviewsRequest.headers ? this.#autoPreviewsRequest.headers.get("cookie") : this.#autoPreviewsRequest.headers.cookie : (_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.cookie) ?? "");
919
+ async getResolvedRefString(params) {
920
+ if (this.refState.autoPreviewsEnabled) {
921
+ var _this$refState$httpRe3, _globalThis$document;
922
+ let previewRef;
923
+ let cookieJar;
924
+ if ((_this$refState$httpRe3 = this.refState.httpRequest) === null || _this$refState$httpRe3 === void 0 ? void 0 : _this$refState$httpRe3.headers) {
925
+ if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") cookieJar = this.refState.httpRequest.headers.get("cookie");
926
+ else if ("cookie" in this.refState.httpRequest.headers) cookieJar = this.refState.httpRequest.headers.cookie;
927
+ } else if ((_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.cookie) cookieJar = globalThis.document.cookie;
928
+ if (cookieJar) previewRef = require_getPreviewCookie.getPreviewCookie(cookieJar);
1061
929
  if (previewRef) return previewRef;
1062
930
  }
1063
- const ref = await ((_this$getRef = this.#getRef) === null || _this$getRef === void 0 ? void 0 : _this$getRef.call(this, params));
1064
- if (ref) return ref;
1065
- return (await this.getMasterRef(params)).ref;
931
+ const repository = await this.getRepository(params);
932
+ const refModeType = this.refState.mode;
933
+ if (refModeType === RefStateMode.ReleaseID) return require_findRefByID.findRefByID(repository.refs, this.refState.releaseID).ref;
934
+ else if (refModeType === RefStateMode.ReleaseLabel) return require_findRefByLabel.findRefByLabel(repository.refs, this.refState.releaseLabel).ref;
935
+ else if (refModeType === RefStateMode.Manual) {
936
+ const res = await require_castThunk.castThunk(this.refState.ref)();
937
+ if (typeof res === "string") return res;
938
+ }
939
+ return require_findMasterRef.findMasterRef(repository.refs).ref;
1066
940
  }
1067
- /**
1068
- * The private implementation of `this.get`. It returns the API response and
1069
- * the URL used to make the request. The URL is sometimes used in the public
1070
- * method to include in thrown errors.
1071
- *
1072
- * This method retries requests that throw `RefNotFoundError` or
1073
- * `RefExpiredError`. It contains special logic to retry with the latest
1074
- * master ref, provided in the API's error message.
1075
- *
1076
- * @typeParam TDocument - Type of Prismic documents returned.
1077
- *
1078
- * @param params - Parameters to filter, sort, and paginate results.
1079
- *
1080
- * @returns An object containing the paginated response containing the result
1081
- * of the query and the URL used to make the API request.
1082
- */
1083
- async _get(params, attemptCount = 0) {
941
+ async #internalGet(params, attempt = 1) {
1084
942
  const url = await this.buildQueryURL(params);
943
+ const response = await this.#request(new URL(url), params);
944
+ if (response.ok) return response;
1085
945
  try {
1086
- const response = await this.#request(new URL(url), params);
1087
- switch (response.status) {
1088
- case 200: try {
1089
- return {
1090
- data: await response.clone().json(),
1091
- url
1092
- };
1093
- } catch {
1094
- throw new require_PrismicError.PrismicError(void 0, url, await response.text());
1095
- }
1096
- case 400: {
1097
- const json = await response.json();
1098
- throw new require_ParsingError.ParsingError(json.message, url, json);
1099
- }
1100
- case 401: {
1101
- const json = await response.json();
1102
- throw new require_ForbiddenError.ForbiddenError(json.message, url, json);
1103
- }
1104
- case 404: {
1105
- const json = await response.json();
1106
- switch (json.type) {
1107
- case "api_notfound_error": throw new require_RefNotFoundError.RefNotFoundError(json.message, url, json);
1108
- case "api_security_error": if (/preview token.*expired/i.test(json.message)) throw new require_PreviewTokenExpired.PreviewTokenExpiredError(json.message, url, json);
1109
- default: throw new require_NotFoundError.NotFoundError(json.message, url, json);
1110
- }
1111
- }
1112
- case 410: {
1113
- const json = await response.json();
1114
- throw new require_RefExpiredError.RefExpiredError(json.message, url, json);
946
+ return await this.#throwContentAPIError(response, url);
947
+ } catch (error) {
948
+ if ((error instanceof require_RefNotFoundError.RefNotFoundError || error instanceof require_RefExpiredError.RefExpiredError) && attempt < MAX_INVALID_REF_RETRY_ATTEMPTS) {
949
+ var _error$message$match;
950
+ if (!(params === null || params === void 0 ? void 0 : params.ref)) this.#cachedRepository = void 0;
951
+ const masterRef = (_error$message$match = error.message.match(/master ref is: (?<ref>.*)$/i)) === null || _error$message$match === void 0 || (_error$message$match = _error$message$match.groups) === null || _error$message$match === void 0 ? void 0 : _error$message$match.ref;
952
+ if (!masterRef) throw error;
953
+ require_throttledLog.throttledLog(`[@prismicio/client] The ref (${new URL(url).searchParams.get("ref")}) was ${error instanceof require_RefNotFoundError.RefNotFoundError ? "invalid" : "expired"}. Now retrying with the latest master ref (${masterRef}). If you were previewing content, the response will not include draft content.`, { level: "warn" });
954
+ return await this.#internalGet({
955
+ ...params,
956
+ ref: masterRef
957
+ }, attempt + 1);
958
+ }
959
+ throw error;
960
+ }
961
+ }
962
+ async #throwContentAPIError(response, url) {
963
+ switch (response.status) {
964
+ case 400: {
965
+ const json = await response.clone().json();
966
+ throw new require_ParsingError.ParsingError(json.message, url, json);
967
+ }
968
+ case 401: {
969
+ const json = await response.clone().json();
970
+ throw new require_ForbiddenError.ForbiddenError(json.message, url, json);
971
+ }
972
+ case 404: {
973
+ const json = await response.clone().json();
974
+ switch (json.type) {
975
+ case "api_notfound_error": throw new require_RefNotFoundError.RefNotFoundError(json.message, url, json);
976
+ case "api_security_error": if (/preview token.*expired/i.test(json.message)) throw new require_PreviewTokenExpired.PreviewTokenExpiredError(json.message, url, json);
977
+ default: throw new require_NotFoundError.NotFoundError(json.message, url, json);
1115
978
  }
1116
- default: throw new require_PrismicError.PrismicError(void 0, url, await response.text());
1117
979
  }
1118
- } catch (error) {
1119
- var _error$message$match;
1120
- if (!(error instanceof require_RefNotFoundError.RefNotFoundError || error instanceof require_RefExpiredError.RefExpiredError) || attemptCount >= MAX_INVALID_REF_RETRY_ATTEMPTS - 1) throw error;
1121
- if (!(params === null || params === void 0 ? void 0 : params.ref)) this.#cachedRepository = void 0;
1122
- const masterRef = (_error$message$match = error.message.match(/master ref is: (?<ref>.*)$/i)) === null || _error$message$match === void 0 || (_error$message$match = _error$message$match.groups) === null || _error$message$match === void 0 ? void 0 : _error$message$match.ref;
1123
- if (!masterRef) throw error;
1124
- require_throttledLog.throttledLog(`[@prismicio/client] The ref (${new URL(url).searchParams.get("ref")}) was ${error instanceof require_RefNotFoundError.RefNotFoundError ? "invalid" : "expired"}. Now retrying with the latest master ref (${masterRef}). If you were previewing content, the response will not include draft content.`, { level: "warn" });
1125
- return await this._get({
1126
- ...params,
1127
- ref: masterRef
1128
- }, attemptCount + 1);
980
+ case 410: {
981
+ const json = await response.clone().json();
982
+ throw new require_RefExpiredError.RefExpiredError(json.message, url, json);
983
+ }
984
+ default: throw new require_PrismicError.PrismicError(void 0, url, await response.text());
1129
985
  }
1130
986
  }
1131
987
  /**
@@ -1138,11 +994,8 @@ var Client = class {
1138
994
  * @returns The response from the fetch request.
1139
995
  */
1140
996
  async #request(url, params) {
1141
- return await require_request.request(url, this._buildRequestInit(params), this.fetchFn);
1142
- }
1143
- _buildRequestInit(params) {
1144
997
  var _this$fetchOptions, _params$fetchOptions, _params$fetchOptions2, _this$fetchOptions2;
1145
- return {
998
+ return await require_request.request(url, {
1146
999
  ...this.fetchOptions,
1147
1000
  ...params === null || params === void 0 ? void 0 : params.fetchOptions,
1148
1001
  headers: {
@@ -1150,7 +1003,7 @@ var Client = class {
1150
1003
  ...params === null || params === void 0 || (_params$fetchOptions = params.fetchOptions) === null || _params$fetchOptions === void 0 ? void 0 : _params$fetchOptions.headers
1151
1004
  },
1152
1005
  signal: (params === null || params === void 0 || (_params$fetchOptions2 = params.fetchOptions) === null || _params$fetchOptions2 === void 0 ? void 0 : _params$fetchOptions2.signal) || (params === null || params === void 0 ? void 0 : params.signal) || ((_this$fetchOptions2 = this.fetchOptions) === null || _this$fetchOptions2 === void 0 ? void 0 : _this$fetchOptions2.signal)
1153
- };
1006
+ }, this.fetchFn);
1154
1007
  }
1155
1008
  };
1156
1009