@prismicio/client 6.3.0 → 6.4.1-alpha.1

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/src/client.ts CHANGED
@@ -181,11 +181,11 @@ type GetAllParams = {
181
181
  /**
182
182
  * Arguments to determine how the URL for a preview session is resolved.
183
183
  */
184
- type ResolvePreviewArgs = {
184
+ type ResolvePreviewArgs<LinkResolverReturnType> = {
185
185
  /**
186
186
  * A function that maps a Prismic document to a URL within your app.
187
187
  */
188
- linkResolver?: prismicH.LinkResolverFunction;
188
+ linkResolver?: prismicH.LinkResolverFunction<LinkResolverReturnType>;
189
189
 
190
190
  /**
191
191
  * A fallback URL if the Link Resolver does not return a value.
@@ -381,7 +381,7 @@ export class Client {
381
381
  this.fetchFn = this.fetchFn.bind(globalThis);
382
382
  }
383
383
 
384
- this.graphqlFetch = this.graphqlFetch.bind(this);
384
+ this.graphQLFetch = this.graphQLFetch.bind(this);
385
385
  }
386
386
 
387
387
  /**
@@ -547,7 +547,8 @@ export class Client {
547
547
  const { limit = Infinity, ...actualParams } = params;
548
548
  const resolvedParams = {
549
549
  ...actualParams,
550
- pageSize: actualParams.pageSize || MAX_PAGE_SIZE,
550
+ pageSize:
551
+ actualParams.pageSize || this.defaultParams?.pageSize || MAX_PAGE_SIZE,
551
552
  };
552
553
 
553
554
  const documents: TDocument[] = [];
@@ -1193,8 +1194,8 @@ export class Client {
1193
1194
  * @returns The URL for the previewed document during an active preview
1194
1195
  * session. The user should be redirected to this URL.
1195
1196
  */
1196
- async resolvePreviewURL(
1197
- args: ResolvePreviewArgs & FetchParams,
1197
+ async resolvePreviewURL<LinkResolverReturnType>(
1198
+ args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams,
1198
1199
  ): Promise<string> {
1199
1200
  let documentID = args.documentID;
1200
1201
  let previewToken = args.previewToken;
@@ -1211,19 +1212,21 @@ export class Client {
1211
1212
  previewToken || (this.refState.httpRequest.query.token as string);
1212
1213
  }
1213
1214
 
1214
- if (documentID != null) {
1215
+ if (documentID != null && previewToken != null) {
1215
1216
  const document = await this.getByID(documentID, {
1216
1217
  signal: args.signal,
1217
1218
  ref: previewToken,
1218
1219
  lang: "*",
1219
1220
  });
1220
1221
 
1221
- // We know we have a valid field to resolve since we are using prismicH.documentToLinkField
1222
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1223
- return prismicH.asLink(document, args.linkResolver)!;
1224
- } else {
1225
- return args.defaultURL;
1222
+ const url = prismicH.asLink(document, args.linkResolver);
1223
+
1224
+ if (typeof url === "string") {
1225
+ return url;
1226
+ }
1226
1227
  }
1228
+
1229
+ return args.defaultURL;
1227
1230
  }
1228
1231
 
1229
1232
  /**
@@ -1312,6 +1315,12 @@ export class Client {
1312
1315
  };
1313
1316
  }
1314
1317
 
1318
+ /**
1319
+ * @deprecated Renamed to `graphQLFetch()` (note the capitalization of "QL").
1320
+ */
1321
+ // TODO: Remove in v3
1322
+ graphqlFetch = this.graphQLFetch.bind(this);
1323
+
1315
1324
  /**
1316
1325
  * A `fetch()` function to be used with GraphQL clients configured for
1317
1326
  * Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
@@ -1339,7 +1348,7 @@ export class Client {
1339
1348
  * @returns The `fetch()` Response for the request.
1340
1349
  * @experimental
1341
1350
  */
1342
- async graphqlFetch(
1351
+ async graphQLFetch(
1343
1352
  input: RequestInfo,
1344
1353
  init?: RequestInit,
1345
1354
  ): Promise<Response> {
@@ -1368,18 +1377,34 @@ export class Client {
1368
1377
  }
1369
1378
  }
1370
1379
 
1371
- // Compress the GraphQL query (if it exists) by removing
1372
- // whitespace. This is done to optimize the query size and avoid hitting the
1373
- // upper limit of GET requests (2048 characters).
1374
1380
  const url = new URL(
1375
1381
  // Asserting `input` is a string since popular GraphQL
1376
1382
  // libraries pass this as a string. Request objects as
1377
1383
  // input are unsupported.
1378
1384
  input as string,
1379
1385
  );
1386
+
1387
+ // This prevents the request from being cached unnecessarily.
1388
+ // Without adding this `ref` param, re-running a query
1389
+ // could return a locally cached response, even if the
1390
+ // `ref` changed. This happens because the URL is
1391
+ // identical when the `ref` is not included. Caches may ignore
1392
+ // headers.
1393
+ //
1394
+ // The Prismic GraphQL API ignores the `ref` param.
1395
+ url.searchParams.set("ref", ref);
1396
+
1380
1397
  const query = url.searchParams.get("query");
1381
1398
  if (query) {
1382
- url.searchParams.set("query", minifyGraphQLQuery(query));
1399
+ url.searchParams.set(
1400
+ "query",
1401
+ // Compress the GraphQL query (if it exists) by
1402
+ // removing whitespace. This is done to
1403
+ // optimize the query size and avoid
1404
+ // hitting the upper limit of GET requests
1405
+ // (2048 characters).
1406
+ minifyGraphQLQuery(query),
1407
+ );
1383
1408
  }
1384
1409
 
1385
1410
  return (await this.fetchFn(url.toString(), {