@prismicio/client 7.0.0-alpha.1 → 7.0.0-alpha.3

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.
@@ -49,6 +49,13 @@ export const REPOSITORY_CACHE_TTL = 5000;
49
49
  */
50
50
  export const GET_ALL_QUERY_DELAY = 500;
51
51
 
52
+ /**
53
+ * The URL search parameter name used for optimizing `/api/v2` requests. The
54
+ * value of the parameter effectively forcibly cache-busts `/api/v2` requests
55
+ * after REPOSITORY_CACHE_TTL milliseconds have elapsed.
56
+ */
57
+ const OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME = "x-valid-until";
58
+
52
59
  /**
53
60
  * Extracts one or more Prismic document types that match a given Prismic
54
61
  * document type. If no matches are found, no extraction is performed and the
@@ -257,7 +264,7 @@ export type ClientConfig = {
257
264
  * Node.js, this function must be provided.
258
265
  */
259
266
  fetch?: FetchLike;
260
- };
267
+ } & OptimizeParams;
261
268
 
262
269
  /**
263
270
  * Parameters for any client method that use `fetch()`. Only a subset of
@@ -273,6 +280,36 @@ type FetchParams = {
273
280
  signal?: AbortSignalLike;
274
281
  };
275
282
 
283
+ /**
284
+ * Parameters that determine if or how the client is optimized.
285
+ */
286
+ type OptimizeParams = {
287
+ /**
288
+ * Options that determine if or how the client is optimized.
289
+ */
290
+ optimize?: {
291
+ /**
292
+ * Determines if fetching repository metadata should be optimized. The
293
+ * optimizations result in faster queries, less network requests, and more
294
+ * accurate cache-busting.
295
+ *
296
+ * Only opt out of this optimization if you know what you are doing.
297
+ *
298
+ * @defaultValue `true`
299
+ */
300
+ repositoryRequests?: boolean;
301
+
302
+ /**
303
+ * Determines if concurrent requests to the same URL should be optimized. The optimizations result in less network requests and quicker responses.
304
+ *
305
+ * Only opt out of this optimization if you know what you are doing.
306
+ *
307
+ * @defaultValue `true`
308
+ */
309
+ concurrentRequests?: boolean;
310
+ };
311
+ };
312
+
276
313
  /**
277
314
  * Parameters specific to client methods that fetch all documents. These methods
278
315
  * start with `getAll` (for example, `getAllByType`).
@@ -416,6 +453,11 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
416
453
  "ref" | "integrationFieldsRef" | "accessToken" | "routes"
417
454
  >;
418
455
 
456
+ /**
457
+ * Options that determine if or how the client is optimized.
458
+ */
459
+ optimize: NonNullable<OptimizeParams["optimize"]>;
460
+
419
461
  /**
420
462
  * The client's ref mode state. This determines which ref is used during
421
463
  * queries.
@@ -479,6 +521,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
479
521
  this.routes = options.routes;
480
522
  this.brokenRoute = options.brokenRoute;
481
523
  this.defaultParams = options.defaultParams;
524
+ this.optimize = {
525
+ repositoryRequests: options.optimize?.repositoryRequests ?? true,
526
+ concurrentRequests: options.optimize?.concurrentRequests ?? true,
527
+ };
482
528
 
483
529
  if (options.ref) {
484
530
  this.queryContentFromRef(options.ref);
@@ -579,7 +625,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
579
625
  * @returns A paginated response containing the result of the query.
580
626
  */
581
627
  async get<TDocument extends TDocuments>(
582
- params?: Partial<BuildQueryURLArgs> & FetchParams,
628
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
583
629
  ): Promise<Query<TDocument>> {
584
630
  const url = await this.buildQueryURL(params);
585
631
 
@@ -601,12 +647,11 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
601
647
  * The first result of the query, if any.
602
648
  */
603
649
  async getFirst<TDocument extends TDocuments>(
604
- params?: Partial<BuildQueryURLArgs> & FetchParams,
650
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
605
651
  ): Promise<TDocument> {
606
652
  const actualParams = { ...params };
607
653
  if (!(params && params.page) && !params?.pageSize) {
608
- actualParams.pageSize =
609
- this.defaultParams?.pageSize == null ? 1 : this.defaultParams?.pageSize;
654
+ actualParams.pageSize = this.defaultParams?.pageSize ?? 1;
610
655
  }
611
656
  const url = await this.buildQueryURL(actualParams);
612
657
  const result = await this.fetch<Query<TDocument>>(url, params);
@@ -645,7 +690,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
645
690
  async dangerouslyGetAll<TDocument extends TDocuments>(
646
691
  params: Partial<Omit<BuildQueryURLArgs, "page">> &
647
692
  GetAllParams &
648
- FetchParams = {},
693
+ FetchParams &
694
+ OptimizeParams = {},
649
695
  ): Promise<TDocument[]> {
650
696
  const { limit = Infinity, ...actualParams } = params;
651
697
  const resolvedParams = {
@@ -699,7 +745,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
699
745
  */
700
746
  async getByID<TDocument extends TDocuments>(
701
747
  id: string,
702
- params?: Partial<BuildQueryURLArgs> & FetchParams,
748
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
703
749
  ): Promise<TDocument> {
704
750
  return await this.getFirst<TDocument>(
705
751
  appendFilters(params, filter.at("document.id", id)),
@@ -732,7 +778,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
732
778
  */
733
779
  async getByIDs<TDocument extends TDocuments>(
734
780
  ids: string[],
735
- params?: Partial<BuildQueryURLArgs> & FetchParams,
781
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
736
782
  ): Promise<Query<TDocument>> {
737
783
  return await this.get<TDocument>(
738
784
  appendFilters(params, filter.in("document.id", ids)),
@@ -767,7 +813,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
767
813
  */
768
814
  async getAllByIDs<TDocument extends TDocuments>(
769
815
  ids: string[],
770
- params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
816
+ params?: Partial<BuildQueryURLArgs> &
817
+ GetAllParams &
818
+ FetchParams &
819
+ OptimizeParams,
771
820
  ): Promise<TDocument[]> {
772
821
  return await this.dangerouslyGetAll<TDocument>(
773
822
  appendFilters(params, filter.in("document.id", ids)),
@@ -803,7 +852,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
803
852
  >(
804
853
  documentType: TDocumentType,
805
854
  uid: string,
806
- params?: Partial<BuildQueryURLArgs> & FetchParams,
855
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
807
856
  ): Promise<ExtractDocumentType<TDocument, TDocumentType>> {
808
857
  return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>(
809
858
  appendFilters(params, [
@@ -845,7 +894,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
845
894
  >(
846
895
  documentType: TDocumentType,
847
896
  uids: string[],
848
- params?: Partial<BuildQueryURLArgs> & FetchParams,
897
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
849
898
  ): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> {
850
899
  return await this.get<ExtractDocumentType<TDocument, TDocumentType>>(
851
900
  appendFilters(params, [
@@ -889,7 +938,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
889
938
  >(
890
939
  documentType: TDocumentType,
891
940
  uids: string[],
892
- params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
941
+ params?: Partial<BuildQueryURLArgs> &
942
+ GetAllParams &
943
+ FetchParams &
944
+ OptimizeParams,
893
945
  ): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> {
894
946
  return await this.dangerouslyGetAll<
895
947
  ExtractDocumentType<TDocument, TDocumentType>
@@ -928,7 +980,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
928
980
  TDocumentType extends TDocument["type"] = TDocument["type"],
929
981
  >(
930
982
  documentType: TDocumentType,
931
- params?: Partial<BuildQueryURLArgs> & FetchParams,
983
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
932
984
  ): Promise<ExtractDocumentType<TDocument, TDocumentType>> {
933
985
  return await this.getFirst<ExtractDocumentType<TDocument, TDocumentType>>(
934
986
  appendFilters(params, typeFilter(documentType)),
@@ -958,7 +1010,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
958
1010
  TDocumentType extends TDocument["type"] = TDocument["type"],
959
1011
  >(
960
1012
  documentType: TDocumentType,
961
- params?: Partial<BuildQueryURLArgs> & FetchParams,
1013
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
962
1014
  ): Promise<Query<ExtractDocumentType<TDocument, TDocumentType>>> {
963
1015
  return await this.get<ExtractDocumentType<TDocument, TDocumentType>>(
964
1016
  appendFilters(params, typeFilter(documentType)),
@@ -991,7 +1043,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
991
1043
  documentType: TDocumentType,
992
1044
  params?: Partial<Omit<BuildQueryURLArgs, "page">> &
993
1045
  GetAllParams &
994
- FetchParams,
1046
+ FetchParams &
1047
+ OptimizeParams,
995
1048
  ): Promise<ExtractDocumentType<TDocument, TDocumentType>[]> {
996
1049
  return await this.dangerouslyGetAll<
997
1050
  ExtractDocumentType<TDocument, TDocumentType>
@@ -1018,7 +1071,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1018
1071
  */
1019
1072
  async getByTag<TDocument extends TDocuments>(
1020
1073
  tag: string,
1021
- params?: Partial<BuildQueryURLArgs> & FetchParams,
1074
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
1022
1075
  ): Promise<Query<TDocument>> {
1023
1076
  return await this.get<TDocument>(
1024
1077
  appendFilters(params, someTagsFilter(tag)),
@@ -1047,7 +1100,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1047
1100
  tag: string,
1048
1101
  params?: Partial<Omit<BuildQueryURLArgs, "page">> &
1049
1102
  GetAllParams &
1050
- FetchParams,
1103
+ FetchParams &
1104
+ OptimizeParams,
1051
1105
  ): Promise<TDocument[]> {
1052
1106
  return await this.dangerouslyGetAll<TDocument>(
1053
1107
  appendFilters(params, someTagsFilter(tag)),
@@ -1072,7 +1126,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1072
1126
  */
1073
1127
  async getByEveryTag<TDocument extends TDocuments>(
1074
1128
  tags: string[],
1075
- params?: Partial<BuildQueryURLArgs> & FetchParams,
1129
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
1076
1130
  ): Promise<Query<TDocument>> {
1077
1131
  return await this.get<TDocument>(
1078
1132
  appendFilters(params, everyTagFilter(tags)),
@@ -1102,7 +1156,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1102
1156
  tags: string[],
1103
1157
  params?: Partial<Omit<BuildQueryURLArgs, "page">> &
1104
1158
  GetAllParams &
1105
- FetchParams,
1159
+ FetchParams &
1160
+ OptimizeParams,
1106
1161
  ): Promise<TDocument[]> {
1107
1162
  return await this.dangerouslyGetAll<TDocument>(
1108
1163
  appendFilters(params, everyTagFilter(tags)),
@@ -1129,7 +1184,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1129
1184
  */
1130
1185
  async getBySomeTags<TDocument extends TDocuments>(
1131
1186
  tags: string[],
1132
- params?: Partial<BuildQueryURLArgs> & FetchParams,
1187
+ params?: Partial<BuildQueryURLArgs> & FetchParams & OptimizeParams,
1133
1188
  ): Promise<Query<TDocument>> {
1134
1189
  return await this.get<TDocument>(
1135
1190
  appendFilters(params, someTagsFilter(tags)),
@@ -1160,7 +1215,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1160
1215
  tags: string[],
1161
1216
  params?: Partial<Omit<BuildQueryURLArgs, "page">> &
1162
1217
  GetAllParams &
1163
- FetchParams,
1218
+ FetchParams &
1219
+ OptimizeParams,
1164
1220
  ): Promise<TDocument[]> {
1165
1221
  return await this.dangerouslyGetAll<TDocument>(
1166
1222
  appendFilters(params, someTagsFilter(tags)),
@@ -1173,7 +1229,9 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1173
1229
  *
1174
1230
  * @returns Repository metadata.
1175
1231
  */
1176
- async getRepository(params?: FetchParams): Promise<Repository> {
1232
+ async getRepository(
1233
+ params?: FetchParams & OptimizeParams,
1234
+ ): Promise<Repository> {
1177
1235
  // TODO: Restore when Authorization header support works in browsers with CORS.
1178
1236
  // return await this.fetch<Repository>(this.endpoint);
1179
1237
 
@@ -1183,6 +1241,28 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1183
1241
  url.searchParams.set("access_token", this.accessToken);
1184
1242
  }
1185
1243
 
1244
+ if (
1245
+ params?.optimize?.repositoryRequests ??
1246
+ this.optimize.repositoryRequests
1247
+ ) {
1248
+ // Add a URL parameter to fingerprint the URL. The URL
1249
+ // should be valid for the duration of
1250
+ // REPOSITORY_CACHE_TTL.
1251
+ //
1252
+ // This fingerprint works as a second "cache" that
1253
+ // works outside `getRepositoryCache()`. This is useful
1254
+ // when multiple client instances exist with an
1255
+ // external cache; all clients can share the same URL
1256
+ // cache key for the duration of REPOSITORY_CACHE_TTL.
1257
+ url.searchParams.set(
1258
+ OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME,
1259
+ (
1260
+ Math.floor(Date.now() / REPOSITORY_CACHE_TTL) * REPOSITORY_CACHE_TTL +
1261
+ REPOSITORY_CACHE_TTL
1262
+ ).toString(),
1263
+ );
1264
+ }
1265
+
1186
1266
  return await this.fetch<Repository>(url.toString(), params);
1187
1267
  }
1188
1268
 
@@ -1195,7 +1275,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1195
1275
  *
1196
1276
  * @returns A list of all refs for the Prismic repository.
1197
1277
  */
1198
- async getRefs(params?: FetchParams): Promise<Ref[]> {
1278
+ async getRefs(params?: FetchParams & OptimizeParams): Promise<Ref[]> {
1199
1279
  const repository = await this.getRepository(params);
1200
1280
 
1201
1281
  return repository.refs;
@@ -1208,7 +1288,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1208
1288
  *
1209
1289
  * @returns The ref with a matching ID, if it exists.
1210
1290
  */
1211
- async getRefByID(id: string, params?: FetchParams): Promise<Ref> {
1291
+ async getRefByID(
1292
+ id: string,
1293
+ params?: FetchParams & OptimizeParams,
1294
+ ): Promise<Ref> {
1212
1295
  const refs = await this.getRefs(params);
1213
1296
 
1214
1297
  return findRefByID(refs, id);
@@ -1221,7 +1304,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1221
1304
  *
1222
1305
  * @returns The ref with a matching label, if it exists.
1223
1306
  */
1224
- async getRefByLabel(label: string, params?: FetchParams): Promise<Ref> {
1307
+ async getRefByLabel(
1308
+ label: string,
1309
+ params?: FetchParams & OptimizeParams,
1310
+ ): Promise<Ref> {
1225
1311
  const refs = await this.getRefs(params);
1226
1312
 
1227
1313
  return findRefByLabel(refs, label);
@@ -1233,7 +1319,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1233
1319
  *
1234
1320
  * @returns The repository's master ref.
1235
1321
  */
1236
- async getMasterRef(params?: FetchParams): Promise<Ref> {
1322
+ async getMasterRef(params?: FetchParams & OptimizeParams): Promise<Ref> {
1237
1323
  const refs = await this.getRefs(params);
1238
1324
 
1239
1325
  return findMasterRef(refs);
@@ -1245,7 +1331,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1245
1331
  *
1246
1332
  * @returns A list of all Releases for the Prismic repository.
1247
1333
  */
1248
- async getReleases(params?: FetchParams): Promise<Ref[]> {
1334
+ async getReleases(params?: FetchParams & OptimizeParams): Promise<Ref[]> {
1249
1335
  const refs = await this.getRefs(params);
1250
1336
 
1251
1337
  return refs.filter((ref) => !ref.isMasterRef);
@@ -1258,7 +1344,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1258
1344
  *
1259
1345
  * @returns The Release with a matching ID, if it exists.
1260
1346
  */
1261
- async getReleaseByID(id: string, params?: FetchParams): Promise<Ref> {
1347
+ async getReleaseByID(
1348
+ id: string,
1349
+ params?: FetchParams & OptimizeParams,
1350
+ ): Promise<Ref> {
1262
1351
  const releases = await this.getReleases(params);
1263
1352
 
1264
1353
  return findRefByID(releases, id);
@@ -1271,7 +1360,10 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1271
1360
  *
1272
1361
  * @returns The ref with a matching label, if it exists.
1273
1362
  */
1274
- async getReleaseByLabel(label: string, params?: FetchParams): Promise<Ref> {
1363
+ async getReleaseByLabel(
1364
+ label: string,
1365
+ params?: FetchParams & OptimizeParams,
1366
+ ): Promise<Ref> {
1275
1367
  const releases = await this.getReleases(params);
1276
1368
 
1277
1369
  return findRefByLabel(releases, label);
@@ -1282,7 +1374,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1282
1374
  *
1283
1375
  * @returns A list of all tags used in the repository.
1284
1376
  */
1285
- async getTags(params?: FetchParams): Promise<string[]> {
1377
+ async getTags(params?: FetchParams & OptimizeParams): Promise<string[]> {
1286
1378
  try {
1287
1379
  const tagsForm = await this.getCachedRepositoryForm("tags", params);
1288
1380
 
@@ -1309,12 +1401,17 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1309
1401
  */
1310
1402
  async buildQueryURL({
1311
1403
  signal,
1404
+ optimize,
1312
1405
  ...params
1313
- }: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
1314
- const ref = params.ref || (await this.getResolvedRefString());
1406
+ }: Partial<BuildQueryURLArgs> &
1407
+ FetchParams &
1408
+ OptimizeParams = {}): Promise<string> {
1409
+ const ref =
1410
+ params.ref || (await this.getResolvedRefString({ signal, optimize }));
1315
1411
  const integrationFieldsRef =
1316
1412
  params.integrationFieldsRef ||
1317
- (await this.getCachedRepository({ signal })).integrationFieldsRef ||
1413
+ (await this.getCachedRepository({ signal, optimize }))
1414
+ .integrationFieldsRef ||
1318
1415
  undefined;
1319
1416
 
1320
1417
  return buildQueryURL(this.endpoint, {
@@ -1348,7 +1445,9 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1348
1445
  * session. The user should be redirected to this URL.
1349
1446
  */
1350
1447
  async resolvePreviewURL<LinkResolverReturnType>(
1351
- args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams,
1448
+ args: ResolvePreviewArgs<LinkResolverReturnType> &
1449
+ FetchParams &
1450
+ OptimizeParams,
1352
1451
  ): Promise<string> {
1353
1452
  let documentID: string | undefined | null = args.documentID;
1354
1453
  let previewToken: string | undefined | null = args.previewToken;
@@ -1378,6 +1477,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1378
1477
 
1379
1478
  if (documentID != null && previewToken != null) {
1380
1479
  const document = await this.getByID(documentID, {
1480
+ optimize: args.optimize,
1381
1481
  signal: args.signal,
1382
1482
  ref: previewToken,
1383
1483
  lang: "*",
@@ -1585,16 +1685,25 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1585
1685
  *
1586
1686
  * @returns Cached repository metadata.
1587
1687
  */
1588
- private async getCachedRepository(params?: FetchParams): Promise<Repository> {
1688
+ private async getCachedRepository(
1689
+ params?: FetchParams & OptimizeParams,
1690
+ ): Promise<Repository> {
1589
1691
  if (
1590
- !this.cachedRepository ||
1591
- Date.now() >= this.cachedRepositoryExpiration
1692
+ params?.optimize?.repositoryRequests ??
1693
+ this.optimize.repositoryRequests
1592
1694
  ) {
1593
- this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
1594
- this.cachedRepository = await this.getRepository(params);
1595
- }
1695
+ if (
1696
+ !this.cachedRepository ||
1697
+ Date.now() >= this.cachedRepositoryExpiration
1698
+ ) {
1699
+ this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
1700
+ this.cachedRepository = await this.getRepository(params);
1701
+ }
1596
1702
 
1597
- return this.cachedRepository;
1703
+ return this.cachedRepository;
1704
+ } else {
1705
+ return await this.getRepository(params);
1706
+ }
1598
1707
  }
1599
1708
 
1600
1709
  /**
@@ -1608,7 +1717,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1608
1717
  */
1609
1718
  private async getCachedRepositoryForm(
1610
1719
  name: string,
1611
- params?: FetchParams,
1720
+ params?: FetchParams & OptimizeParams,
1612
1721
  ): Promise<Form> {
1613
1722
  const cachedRepository = await this.getCachedRepository(params);
1614
1723
  const form = cachedRepository.forms[name];
@@ -1647,7 +1756,9 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1647
1756
  *
1648
1757
  * @returns The ref to use during a query.
1649
1758
  */
1650
- private async getResolvedRefString(params?: FetchParams): Promise<string> {
1759
+ private async getResolvedRefString(
1760
+ params?: FetchParams & OptimizeParams,
1761
+ ): Promise<string> {
1651
1762
  if (this.refState.autoPreviewsEnabled) {
1652
1763
  let previewRef: string | undefined;
1653
1764
 
@@ -1709,56 +1820,94 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
1709
1820
  */
1710
1821
  private async fetch<T = unknown>(
1711
1822
  url: string,
1712
- params: FetchParams = {},
1823
+ params: FetchParams & OptimizeParams = {},
1713
1824
  ): Promise<T> {
1714
- let job: Promise<FetchJobResult>;
1825
+ let res: FetchJobResult;
1715
1826
 
1716
- // `fetchJobs` is keyed twice: first by the URL and again by is
1717
- // signal, if one exists.
1718
- //
1719
- // Using two keys allows us to reuse fetch requests for
1720
- // equivalent URLs, but eject when we detect unique signals.
1721
- if (this.fetchJobs[url] && this.fetchJobs[url].has(params.signal)) {
1722
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1723
- job = this.fetchJobs[url].get(params.signal)!;
1724
- } else {
1725
- this.fetchJobs[url] = this.fetchJobs[url] || new Map();
1827
+ if (
1828
+ params.optimize?.concurrentRequests ??
1829
+ this.optimize.concurrentRequests
1830
+ ) {
1831
+ let job: Promise<FetchJobResult>;
1726
1832
 
1727
- job = this.fetchFn(url, {
1728
- signal: params.signal,
1729
- })
1730
- .then(async (res) => {
1731
- // We can assume Prismic REST API responses
1732
- // will have a `application/json`
1733
- // Content Type. If not, this will
1734
- // throw, signaling an invalid
1735
- // response.
1736
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1737
- let json: any = undefined;
1738
- try {
1739
- json = await res.json();
1740
- } catch {
1741
- // noop
1742
- }
1743
-
1744
- return {
1745
- status: res.status,
1746
- json,
1747
- };
1833
+ const fetchJobKeyInstance = new URL(url);
1834
+ fetchJobKeyInstance.searchParams.delete(
1835
+ OPTIMIZE_REPOSITORY_REQUESTS_VALID_UNTIL_PARAMETER_NAME,
1836
+ );
1837
+ const fetchJobKey = fetchJobKeyInstance.toString();
1838
+
1839
+ // `fetchJobs` is keyed twice: first by the URL and again by is
1840
+ // signal, if one exists.
1841
+ //
1842
+ // Using two keys allows us to reuse fetch requests for
1843
+ // equivalent URLs, but eject when we detect unique signals.
1844
+ if (
1845
+ this.fetchJobs[fetchJobKey] &&
1846
+ this.fetchJobs[fetchJobKey].has(params.signal)
1847
+ ) {
1848
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1849
+ job = this.fetchJobs[fetchJobKey].get(params.signal)!;
1850
+ } else {
1851
+ this.fetchJobs[fetchJobKey] = this.fetchJobs[fetchJobKey] || new Map();
1852
+
1853
+ job = this.fetchFn(url, {
1854
+ signal: params.signal,
1748
1855
  })
1749
- .finally(() => {
1750
- this.fetchJobs[url].delete(params.signal);
1856
+ .then(async (res) => {
1857
+ // We can assume Prismic REST API responses
1858
+ // will have a `application/json`
1859
+ // Content Type. If not, this will
1860
+ // throw, signaling an invalid
1861
+ // response.
1862
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1863
+ let json: any = undefined;
1864
+ try {
1865
+ json = await res.json();
1866
+ } catch {
1867
+ // noop
1868
+ }
1869
+
1870
+ return {
1871
+ status: res.status,
1872
+ json,
1873
+ };
1874
+ })
1875
+ .finally(() => {
1876
+ this.fetchJobs[fetchJobKey].delete(params.signal);
1877
+
1878
+ if (this.fetchJobs[fetchJobKey].size === 0) {
1879
+ delete this.fetchJobs[fetchJobKey];
1880
+ }
1881
+ });
1882
+
1883
+ this.fetchJobs[fetchJobKey].set(params.signal, job);
1884
+ }
1885
+
1886
+ res = await job;
1887
+ } else {
1888
+ const job = await this.fetchFn(url, {
1889
+ signal: params.signal,
1890
+ });
1751
1891
 
1752
- if (this.fetchJobs[url].size === 0) {
1753
- delete this.fetchJobs[url];
1754
- }
1755
- });
1892
+ // We can assume Prismic REST API responses
1893
+ // will have a `application/json`
1894
+ // Content Type. If not, this will
1895
+ // throw, signaling an invalid
1896
+ // response.
1897
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1898
+ let json: any = undefined;
1899
+ try {
1900
+ json = await job.json();
1901
+ } catch {
1902
+ // noop
1903
+ }
1756
1904
 
1757
- this.fetchJobs[url].set(params.signal, job);
1905
+ res = {
1906
+ status: job.status,
1907
+ json,
1908
+ };
1758
1909
  }
1759
1910
 
1760
- const res = await job;
1761
-
1762
1911
  if (res.status !== 404 && res.json == null) {
1763
1912
  throw new PrismicError(undefined, url, res.json);
1764
1913
  }
@@ -23,7 +23,7 @@ type AsLinkAttrsConfigRelArgs<
23
23
  target?: string;
24
24
  };
25
25
 
26
- type AsLinkAttrsConfig<
26
+ export type AsLinkAttrsConfig<
27
27
  LinkResolverFunctionReturnType = ReturnType<LinkResolverFunction>,
28
28
  Field extends LinkField | PrismicDocument | null | undefined =
29
29
  | LinkField
package/src/index.ts CHANGED
@@ -74,6 +74,7 @@ export { documentToLinkField } from "./helpers/documentToLinkField";
74
74
  export { Element } from "@prismicio/richtext";
75
75
 
76
76
  export type { LinkResolverFunction } from "./helpers/asLink";
77
+ export type { AsLinkAttrsConfig } from "./helpers/asLinkAttrs";
77
78
 
78
79
  /**
79
80
  * @deprecated Renamed to `HTMLRichTextMapSerializer`