@prismicio/client 6.4.1 → 6.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/client.ts CHANGED
@@ -218,7 +218,7 @@ const typePredicate = (documentType: string): string =>
218
218
  * Creates a predicate to filter content by document tags. All tags are required
219
219
  * on the document.
220
220
  *
221
- * @param documentType - Document tags to filter queried content.
221
+ * @param tags - Document tags to filter queried content.
222
222
  *
223
223
  * @returns A predicate that can be used in a Prismic REST API V2 request.
224
224
  */
@@ -229,7 +229,7 @@ const everyTagPredicate = (tags: string | string[]): string =>
229
229
  * Creates a predicate to filter content by document tags. At least one matching
230
230
  * tag is required on the document.
231
231
  *
232
- * @param documentType - Document tags to filter queried content.
232
+ * @param tags - Document tags to filter queried content.
233
233
  *
234
234
  * @returns A predicate that can be used in a Prismic REST API V2 request.
235
235
  */
@@ -547,8 +547,10 @@ export class Client {
547
547
  const { limit = Infinity, ...actualParams } = params;
548
548
  const resolvedParams = {
549
549
  ...actualParams,
550
- pageSize:
550
+ pageSize: Math.min(
551
+ limit,
551
552
  actualParams.pageSize || this.defaultParams?.pageSize || MAX_PAGE_SIZE,
553
+ ),
552
554
  };
553
555
 
554
556
  const documents: TDocument[] = [];
@@ -658,7 +660,7 @@ export class Client {
658
660
  */
659
661
  async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
660
662
  ids: string[],
661
- params?: Partial<BuildQueryURLArgs> & FetchParams,
663
+ params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
662
664
  ): Promise<TDocument[]> {
663
665
  return await this.dangerouslyGetAll<TDocument>(
664
666
  appendPredicates(params, predicate.in("document.id", ids)),
@@ -764,7 +766,7 @@ export class Client {
764
766
  async getAllByUIDs<TDocument extends prismicT.PrismicDocument>(
765
767
  documentType: string,
766
768
  uids: string[],
767
- params?: Partial<BuildQueryURLArgs> & FetchParams,
769
+ params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams,
768
770
  ): Promise<TDocument[]> {
769
771
  return await this.dangerouslyGetAll<TDocument>(
770
772
  appendPredicates(params, [
@@ -849,7 +851,9 @@ export class Client {
849
851
  */
850
852
  async getAllByType<TDocument extends prismicT.PrismicDocument>(
851
853
  documentType: string,
852
- params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
854
+ params?: Partial<Omit<BuildQueryURLArgs, "page">> &
855
+ GetAllParams &
856
+ FetchParams,
853
857
  ): Promise<TDocument[]> {
854
858
  return await this.dangerouslyGetAll<TDocument>(
855
859
  appendPredicates(params, typePredicate(documentType)),
@@ -901,7 +905,9 @@ export class Client {
901
905
  */
902
906
  async getAllByTag<TDocument extends prismicT.PrismicDocument>(
903
907
  tag: string,
904
- params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
908
+ params?: Partial<Omit<BuildQueryURLArgs, "page">> &
909
+ GetAllParams &
910
+ FetchParams,
905
911
  ): Promise<TDocument[]> {
906
912
  return await this.dangerouslyGetAll<TDocument>(
907
913
  appendPredicates(params, someTagsPredicate(tag)),
@@ -953,7 +959,9 @@ export class Client {
953
959
  */
954
960
  async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
955
961
  tags: string[],
956
- params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
962
+ params?: Partial<Omit<BuildQueryURLArgs, "page">> &
963
+ GetAllParams &
964
+ FetchParams,
957
965
  ): Promise<TDocument[]> {
958
966
  return await this.dangerouslyGetAll<TDocument>(
959
967
  appendPredicates(params, everyTagPredicate(tags)),
@@ -1005,7 +1013,9 @@ export class Client {
1005
1013
  */
1006
1014
  async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
1007
1015
  tags: string[],
1008
- params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
1016
+ params?: Partial<Omit<BuildQueryURLArgs, "page">> &
1017
+ GetAllParams &
1018
+ FetchParams,
1009
1019
  ): Promise<TDocument[]> {
1010
1020
  return await this.dangerouslyGetAll<TDocument>(
1011
1021
  appendPredicates(params, someTagsPredicate(tags)),
@@ -1197,19 +1207,27 @@ export class Client {
1197
1207
  async resolvePreviewURL<LinkResolverReturnType>(
1198
1208
  args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams,
1199
1209
  ): Promise<string> {
1200
- let documentID = args.documentID;
1201
- let previewToken = args.previewToken;
1210
+ let documentID: string | undefined | null = args.documentID;
1211
+ let previewToken: string | undefined | null = args.previewToken;
1202
1212
 
1203
1213
  if (typeof globalThis.location !== "undefined") {
1204
1214
  const searchParams = new URLSearchParams(globalThis.location.search);
1205
1215
 
1206
- documentID = documentID || searchParams.get("documentId") || undefined;
1207
- previewToken = previewToken || searchParams.get("token") || undefined;
1208
- } else if (this.refState.httpRequest?.query) {
1209
- documentID =
1210
- documentID || (this.refState.httpRequest.query.documentId as string);
1211
- previewToken =
1212
- previewToken || (this.refState.httpRequest.query.token as string);
1216
+ documentID = documentID || searchParams.get("documentId");
1217
+ previewToken = previewToken || searchParams.get("token");
1218
+ } else if (this.refState.httpRequest) {
1219
+ if (this.refState.httpRequest.url) {
1220
+ const searchParams = new URL(this.refState.httpRequest.url)
1221
+ .searchParams;
1222
+
1223
+ documentID = documentID || searchParams.get("documentId");
1224
+ previewToken = previewToken || searchParams.get("token");
1225
+ } else {
1226
+ documentID =
1227
+ documentID || (this.refState.httpRequest.query?.documentId as string);
1228
+ previewToken =
1229
+ previewToken || (this.refState.httpRequest.query?.token as string);
1230
+ }
1213
1231
  }
1214
1232
 
1215
1233
  if (documentID != null && previewToken != null) {
@@ -1258,7 +1276,7 @@ export class Client {
1258
1276
  * const document = await client.getByID("WW4bKScAAMAqmluX");
1259
1277
  * ```
1260
1278
  *
1261
- * @param id - The ID of the Release.
1279
+ * @param releaseID - The ID of the Release.
1262
1280
  */
1263
1281
  queryContentFromReleaseByID(releaseID: string): void {
1264
1282
  this.refState = {
@@ -1281,7 +1299,7 @@ export class Client {
1281
1299
  * const document = await client.getByID("WW4bKScAAMAqmluX");
1282
1300
  * ```
1283
1301
  *
1284
- * @param label - The label of the Release.
1302
+ * @param releaseLabel - The label of the Release.
1285
1303
  */
1286
1304
  queryContentFromReleaseByLabel(releaseLabel: string): void {
1287
1305
  this.refState = {
@@ -1482,15 +1500,27 @@ export class Client {
1482
1500
  */
1483
1501
  private async getResolvedRefString(params?: FetchParams): Promise<string> {
1484
1502
  if (this.refState.autoPreviewsEnabled) {
1485
- let previewRef: string | undefined = undefined;
1503
+ let previewRef: string | undefined;
1504
+
1505
+ let cookieJar: string | null | undefined;
1486
1506
 
1487
1507
  if (globalThis.document?.cookie) {
1488
- previewRef = getCookie(cookie.preview, globalThis.document.cookie);
1489
- } else if (this.refState.httpRequest?.headers?.cookie) {
1490
- previewRef = getCookie(
1491
- cookie.preview,
1492
- this.refState.httpRequest.headers.cookie,
1493
- );
1508
+ cookieJar = globalThis.document.cookie;
1509
+ } else if (this.refState.httpRequest?.headers) {
1510
+ if (
1511
+ "get" in this.refState.httpRequest.headers &&
1512
+ typeof this.refState.httpRequest.headers.get === "function"
1513
+ ) {
1514
+ // Web API Headers
1515
+ cookieJar = this.refState.httpRequest.headers.get("cookie");
1516
+ } else if ("cookie" in this.refState.httpRequest.headers) {
1517
+ // Express-style headers
1518
+ cookieJar = this.refState.httpRequest.headers.cookie;
1519
+ }
1520
+ }
1521
+
1522
+ if (cookieJar) {
1523
+ previewRef = getCookie(cookie.preview, cookieJar);
1494
1524
  }
1495
1525
 
1496
1526
  if (previewRef) {
@@ -47,12 +47,12 @@ const getAll = (cookieStore: string): { [name: string]: string } =>
47
47
  /**
48
48
  * Returns the value of a cookie from a given cookie store.
49
49
  *
50
- * @param Name - Of the cookie.
51
- * @param cookieStore - The stringified cookie store from which to read the cookie.
50
+ * @param name - Of the cookie.
51
+ * @param cookieJar - The stringified cookie store from which to read the cookie.
52
52
  *
53
53
  * @returns The value of the cookie, if it exists.
54
54
  */
55
55
  export const getCookie = (
56
56
  name: string,
57
- cookieStore: string,
58
- ): string | undefined => getAll(cookieStore)[name];
57
+ cookieJar: string,
58
+ ): string | undefined => getAll(cookieJar)[name];
package/src/predicate.ts CHANGED
@@ -91,11 +91,6 @@ const argsPredicate = <Args extends unknown[]>(name: string) => {
91
91
  return fn;
92
92
  };
93
93
 
94
- /**
95
- * The default arguments allowed by predicates.
96
- */
97
- type DefaultPredicateArgs = [value: string | number | (string | number)[]];
98
-
99
94
  export const predicate = {
100
95
  /**
101
96
  * The `at` predicate checks that the path matches the described value
@@ -103,15 +98,19 @@ export const predicate = {
103
98
  *
104
99
  * {@link https://prismic.io/docs/technologies/query-predicates-reference-rest-api#at}
105
100
  */
106
- at: pathWithArgsPredicate<DefaultPredicateArgs>("at"),
101
+ at: pathWithArgsPredicate<
102
+ [value: string | number | boolean | Date | string[]]
103
+ >("at"),
107
104
 
108
105
  /**
109
106
  * The `not` predicate checks that the path doesn't match the provided value
110
- * exactly. It takes a single value as the argument.
107
+ * exactly. It takes a single value for a field or an array (only for tags).
111
108
  *
112
109
  * {@link https://prismic.io/docs/technologies/query-predicates-reference-rest-api#not}
113
110
  */
114
- not: pathWithArgsPredicate<DefaultPredicateArgs>("not"),
111
+ not: pathWithArgsPredicate<
112
+ [value: string | number | boolean | Date | string[]]
113
+ >("not"),
115
114
 
116
115
  /**
117
116
  * The `any` predicate takes an array of values. It works exactly the same way
@@ -120,7 +119,9 @@ export const predicate = {
120
119
  *
121
120
  * {@link https://prismic.io/docs/technologies/query-predicates-reference-rest-api#any}
122
121
  */
123
- any: pathWithArgsPredicate<DefaultPredicateArgs>("any"),
122
+ any: pathWithArgsPredicate<[values: (string | number | boolean | Date)[]]>(
123
+ "any",
124
+ ),
124
125
 
125
126
  /**
126
127
  * The `in` predicate is used specifically to retrieve an array of documents
@@ -129,7 +130,7 @@ export const predicate = {
129
130
  *
130
131
  * {@link https://prismic.io/docs/technologies/query-predicates-reference-rest-api#in}
131
132
  */
132
- in: pathWithArgsPredicate<DefaultPredicateArgs>("in"),
133
+ in: pathWithArgsPredicate<[values: string[]]>("in"),
133
134
 
134
135
  /**
135
136
  * The `fulltext` predicate provides two capabilities:
@@ -141,7 +142,7 @@ export const predicate = {
141
142
  *
142
143
  * {@link https://prismic.io/docs/technologies/query-predicates-reference-rest-api#fulltext}
143
144
  */
144
- fulltext: pathWithArgsPredicate<DefaultPredicateArgs>("fulltext"),
145
+ fulltext: pathWithArgsPredicate<[searchTerms: string]>("fulltext"),
145
146
 
146
147
  /**
147
148
  * The `has` predicate checks whether a fragment has a value. It will return
package/src/types.ts CHANGED
@@ -43,6 +43,10 @@ export type AbortSignalLike = {
43
43
  dispatchEvent: any;
44
44
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
45
  onabort: any;
46
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
+ reason: any;
48
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
+ throwIfAborted: any;
46
50
  };
47
51
 
48
52
  /**
@@ -68,12 +72,30 @@ export interface ResponseLike {
68
72
  * The minimum required properties to treat as an HTTP Request for automatic
69
73
  * Prismic preview support.
70
74
  */
71
- export interface HttpRequestLike {
72
- headers?: {
73
- cookie?: string;
74
- };
75
- query?: Record<string, unknown>;
76
- }
75
+ export type HttpRequestLike =
76
+ | /**
77
+ * Web API Request
78
+ *
79
+ * @see http://developer.mozilla.org/en-US/docs/Web/API/Request
80
+ */
81
+ {
82
+ headers?: {
83
+ get(name: string): string | null;
84
+ };
85
+ url?: string;
86
+ query?: never;
87
+ }
88
+
89
+ /**
90
+ * Express-style Request
91
+ */
92
+ | {
93
+ headers?: {
94
+ cookie?: string;
95
+ };
96
+ query?: Record<string, unknown>;
97
+ url?: never;
98
+ };
77
99
 
78
100
  /**
79
101
  * An `orderings` parameter that orders the results by the specified field.