@prismicio/client 6.6.1 → 6.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +152 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +152 -62
- package/dist/index.js.map +1 -1
- package/package.json +18 -17
- package/src/client.ts +18 -12
- package/src/index.ts +1 -0
- package/src/lib/getPreviewCookie.ts +30 -0
- package/src/types.ts +1 -5
- package/src/lib/getCookie.ts +0 -58
package/dist/index.js
CHANGED
|
@@ -16,7 +16,11 @@ const getRepositoryEndpoint = (repositoryName) => {
|
|
|
16
16
|
if (isRepositoryName(repositoryName)) {
|
|
17
17
|
return `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
18
18
|
} else {
|
|
19
|
-
throw new PrismicError(
|
|
19
|
+
throw new PrismicError(
|
|
20
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
21
|
+
void 0,
|
|
22
|
+
void 0
|
|
23
|
+
);
|
|
20
24
|
}
|
|
21
25
|
};
|
|
22
26
|
|
|
@@ -24,7 +28,11 @@ const getRepositoryName = (repositoryEndpoint) => {
|
|
|
24
28
|
try {
|
|
25
29
|
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
26
30
|
} catch (e) {
|
|
27
|
-
throw new PrismicError(
|
|
31
|
+
throw new PrismicError(
|
|
32
|
+
`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`,
|
|
33
|
+
void 0,
|
|
34
|
+
void 0
|
|
35
|
+
);
|
|
28
36
|
}
|
|
29
37
|
};
|
|
30
38
|
|
|
@@ -32,7 +40,11 @@ const getGraphQLEndpoint = (repositoryName) => {
|
|
|
32
40
|
if (isRepositoryName(repositoryName)) {
|
|
33
41
|
return `https://${repositoryName}.cdn.prismic.io/graphql`;
|
|
34
42
|
} else {
|
|
35
|
-
throw new PrismicError(
|
|
43
|
+
throw new PrismicError(
|
|
44
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
45
|
+
void 0,
|
|
46
|
+
void 0
|
|
47
|
+
);
|
|
36
48
|
}
|
|
37
49
|
};
|
|
38
50
|
|
|
@@ -116,34 +128,37 @@ const findRefByLabel = (refs, label) => {
|
|
|
116
128
|
return findRef(refs, (ref) => ref.label === label);
|
|
117
129
|
};
|
|
118
130
|
|
|
131
|
+
const preview = "io.prismic.preview";
|
|
132
|
+
|
|
133
|
+
var cookie = /*#__PURE__*/Object.freeze({
|
|
134
|
+
__proto__: null,
|
|
135
|
+
preview: preview
|
|
136
|
+
});
|
|
137
|
+
|
|
119
138
|
const readValue = (value) => {
|
|
120
139
|
return value.replace(/%3B/g, ";");
|
|
121
140
|
};
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
|
|
141
|
+
const getPreviewCookie = (cookieJar) => {
|
|
142
|
+
const cookies = cookieJar.split("; ");
|
|
143
|
+
let value;
|
|
125
144
|
for (const cookie of cookies) {
|
|
126
145
|
const parts = cookie.split("=");
|
|
127
|
-
const value = parts.slice(1).join("=");
|
|
128
146
|
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
129
|
-
|
|
147
|
+
if (name === preview) {
|
|
148
|
+
value = readValue(parts.slice(1).join("="));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
130
151
|
}
|
|
131
|
-
return
|
|
152
|
+
return value;
|
|
132
153
|
};
|
|
133
|
-
const getAll = (cookieStore) => parse(cookieStore);
|
|
134
|
-
const getCookie = (name, cookieJar) => getAll(cookieJar)[name];
|
|
135
154
|
|
|
136
155
|
const minifyGraphQLQuery = (query) => {
|
|
137
|
-
return query.replace(
|
|
156
|
+
return query.replace(
|
|
157
|
+
/(\n| )*( |{|})(\n| )*/gm,
|
|
158
|
+
(_chars, _spaces, brackets) => brackets
|
|
159
|
+
);
|
|
138
160
|
};
|
|
139
161
|
|
|
140
|
-
const preview = "io.prismic.preview";
|
|
141
|
-
|
|
142
|
-
var cookie = /*#__PURE__*/Object.freeze({
|
|
143
|
-
__proto__: null,
|
|
144
|
-
preview: preview
|
|
145
|
-
});
|
|
146
|
-
|
|
147
162
|
class ForbiddenError extends PrismicError {
|
|
148
163
|
}
|
|
149
164
|
|
|
@@ -190,7 +205,9 @@ const argsPredicate = (name) => {
|
|
|
190
205
|
const predicate = {
|
|
191
206
|
at: pathWithArgsPredicate("at"),
|
|
192
207
|
not: pathWithArgsPredicate("not"),
|
|
193
|
-
any: pathWithArgsPredicate(
|
|
208
|
+
any: pathWithArgsPredicate(
|
|
209
|
+
"any"
|
|
210
|
+
),
|
|
194
211
|
in: pathWithArgsPredicate("in"),
|
|
195
212
|
fulltext: pathWithArgsPredicate("fulltext"),
|
|
196
213
|
has: pathPredicate("has"),
|
|
@@ -199,16 +216,26 @@ const predicate = {
|
|
|
199
216
|
geopointNear: pathWithArgsPredicate("geopoint.near"),
|
|
200
217
|
numberLessThan: pathWithArgsPredicate("number.lt"),
|
|
201
218
|
numberGreaterThan: pathWithArgsPredicate("number.gt"),
|
|
202
|
-
numberInRange: pathWithArgsPredicate(
|
|
219
|
+
numberInRange: pathWithArgsPredicate(
|
|
220
|
+
"number.inRange"
|
|
221
|
+
),
|
|
203
222
|
dateAfter: pathWithArgsPredicate("date.after"),
|
|
204
223
|
dateBefore: pathWithArgsPredicate("date.before"),
|
|
205
224
|
dateBetween: pathWithArgsPredicate("date.between"),
|
|
206
225
|
dateDayOfMonth: pathWithArgsPredicate("date.day-of-month"),
|
|
207
|
-
dateDayOfMonthAfter: pathWithArgsPredicate(
|
|
208
|
-
|
|
226
|
+
dateDayOfMonthAfter: pathWithArgsPredicate(
|
|
227
|
+
"date.day-of-month-after"
|
|
228
|
+
),
|
|
229
|
+
dateDayOfMonthBefore: pathWithArgsPredicate(
|
|
230
|
+
"date.day-of-month-before"
|
|
231
|
+
),
|
|
209
232
|
dateDayOfWeek: pathWithArgsPredicate("date.day-of-week"),
|
|
210
|
-
dateDayOfWeekAfter: pathWithArgsPredicate(
|
|
211
|
-
|
|
233
|
+
dateDayOfWeekAfter: pathWithArgsPredicate(
|
|
234
|
+
"date.day-of-week-after"
|
|
235
|
+
),
|
|
236
|
+
dateDayOfWeekBefore: pathWithArgsPredicate(
|
|
237
|
+
"date.day-of-week-before"
|
|
238
|
+
),
|
|
212
239
|
dateMonth: pathWithArgsPredicate("date.month"),
|
|
213
240
|
dateMonthAfter: pathWithArgsPredicate("date.month-after"),
|
|
214
241
|
dateMonthBefore: pathWithArgsPredicate("date.month-before"),
|
|
@@ -235,7 +262,11 @@ class Client {
|
|
|
235
262
|
this.graphqlFetch = this.graphQLFetch.bind(this);
|
|
236
263
|
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
237
264
|
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
238
|
-
throw new PrismicError(
|
|
265
|
+
throw new PrismicError(
|
|
266
|
+
"@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.",
|
|
267
|
+
void 0,
|
|
268
|
+
void 0
|
|
269
|
+
);
|
|
239
270
|
}
|
|
240
271
|
this.endpoint = repositoryNameOrEndpoint;
|
|
241
272
|
} else {
|
|
@@ -252,7 +283,11 @@ class Client {
|
|
|
252
283
|
} else if (typeof globalThis.fetch === "function") {
|
|
253
284
|
this.fetchFn = globalThis.fetch;
|
|
254
285
|
} else {
|
|
255
|
-
throw new PrismicError(
|
|
286
|
+
throw new PrismicError(
|
|
287
|
+
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
|
|
288
|
+
void 0,
|
|
289
|
+
void 0
|
|
290
|
+
);
|
|
256
291
|
}
|
|
257
292
|
if (this.fetchFn === globalThis.fetch) {
|
|
258
293
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
@@ -278,7 +313,12 @@ class Client {
|
|
|
278
313
|
return await this.fetch(url, params);
|
|
279
314
|
}
|
|
280
315
|
async getFirst(params) {
|
|
281
|
-
|
|
316
|
+
var _a, _b;
|
|
317
|
+
const actualParams = { ...params };
|
|
318
|
+
if (!(params && params.page) && !(params == null ? void 0 : params.pageSize)) {
|
|
319
|
+
actualParams.pageSize = (_b = (_a = this.defaultParams) == null ? void 0 : _a.pageSize) != null ? _b : 1;
|
|
320
|
+
}
|
|
321
|
+
const url = await this.buildQueryURL(actualParams);
|
|
282
322
|
const result = await this.fetch(url, params);
|
|
283
323
|
const firstResult = result.results[0];
|
|
284
324
|
if (firstResult) {
|
|
@@ -291,7 +331,10 @@ class Client {
|
|
|
291
331
|
const { limit = Infinity, ...actualParams } = params;
|
|
292
332
|
const resolvedParams = {
|
|
293
333
|
...actualParams,
|
|
294
|
-
pageSize: Math.min(
|
|
334
|
+
pageSize: Math.min(
|
|
335
|
+
limit,
|
|
336
|
+
actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
|
|
337
|
+
)
|
|
295
338
|
};
|
|
296
339
|
const documents = [];
|
|
297
340
|
let latestResult;
|
|
@@ -306,58 +349,86 @@ class Client {
|
|
|
306
349
|
return documents.slice(0, limit);
|
|
307
350
|
}
|
|
308
351
|
async getByID(id, params) {
|
|
309
|
-
return await this.getFirst(
|
|
352
|
+
return await this.getFirst(
|
|
353
|
+
appendPredicates(params, predicate.at("document.id", id))
|
|
354
|
+
);
|
|
310
355
|
}
|
|
311
356
|
async getByIDs(ids, params) {
|
|
312
|
-
return await this.get(
|
|
357
|
+
return await this.get(
|
|
358
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
359
|
+
);
|
|
313
360
|
}
|
|
314
361
|
async getAllByIDs(ids, params) {
|
|
315
|
-
return await this.dangerouslyGetAll(
|
|
362
|
+
return await this.dangerouslyGetAll(
|
|
363
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
364
|
+
);
|
|
316
365
|
}
|
|
317
366
|
async getByUID(documentType, uid, params) {
|
|
318
|
-
return await this.getFirst(
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
367
|
+
return await this.getFirst(
|
|
368
|
+
appendPredicates(params, [
|
|
369
|
+
typePredicate(documentType),
|
|
370
|
+
predicate.at(`my.${documentType}.uid`, uid)
|
|
371
|
+
])
|
|
372
|
+
);
|
|
322
373
|
}
|
|
323
374
|
async getByUIDs(documentType, uids, params) {
|
|
324
|
-
return await this.get(
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
375
|
+
return await this.get(
|
|
376
|
+
appendPredicates(params, [
|
|
377
|
+
typePredicate(documentType),
|
|
378
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
379
|
+
])
|
|
380
|
+
);
|
|
328
381
|
}
|
|
329
382
|
async getAllByUIDs(documentType, uids, params) {
|
|
330
|
-
return await this.dangerouslyGetAll(
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
383
|
+
return await this.dangerouslyGetAll(
|
|
384
|
+
appendPredicates(params, [
|
|
385
|
+
typePredicate(documentType),
|
|
386
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
387
|
+
])
|
|
388
|
+
);
|
|
334
389
|
}
|
|
335
390
|
async getSingle(documentType, params) {
|
|
336
|
-
return await this.getFirst(
|
|
391
|
+
return await this.getFirst(
|
|
392
|
+
appendPredicates(params, typePredicate(documentType))
|
|
393
|
+
);
|
|
337
394
|
}
|
|
338
395
|
async getByType(documentType, params) {
|
|
339
|
-
return await this.get(
|
|
396
|
+
return await this.get(
|
|
397
|
+
appendPredicates(params, typePredicate(documentType))
|
|
398
|
+
);
|
|
340
399
|
}
|
|
341
400
|
async getAllByType(documentType, params) {
|
|
342
401
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
343
402
|
}
|
|
344
403
|
async getByTag(tag, params) {
|
|
345
|
-
return await this.get(
|
|
404
|
+
return await this.get(
|
|
405
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
406
|
+
);
|
|
346
407
|
}
|
|
347
408
|
async getAllByTag(tag, params) {
|
|
348
|
-
return await this.dangerouslyGetAll(
|
|
409
|
+
return await this.dangerouslyGetAll(
|
|
410
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
411
|
+
);
|
|
349
412
|
}
|
|
350
413
|
async getByEveryTag(tags, params) {
|
|
351
|
-
return await this.get(
|
|
414
|
+
return await this.get(
|
|
415
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
416
|
+
);
|
|
352
417
|
}
|
|
353
418
|
async getAllByEveryTag(tags, params) {
|
|
354
|
-
return await this.dangerouslyGetAll(
|
|
419
|
+
return await this.dangerouslyGetAll(
|
|
420
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
421
|
+
);
|
|
355
422
|
}
|
|
356
423
|
async getBySomeTags(tags, params) {
|
|
357
|
-
return await this.get(
|
|
424
|
+
return await this.get(
|
|
425
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
426
|
+
);
|
|
358
427
|
}
|
|
359
428
|
async getAllBySomeTags(tags, params) {
|
|
360
|
-
return await this.dangerouslyGetAll(
|
|
429
|
+
return await this.dangerouslyGetAll(
|
|
430
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
431
|
+
);
|
|
361
432
|
}
|
|
362
433
|
async getRepository(params) {
|
|
363
434
|
const url = new URL(this.endpoint);
|
|
@@ -482,21 +553,28 @@ class Client {
|
|
|
482
553
|
const ref = await this.getResolvedRefString();
|
|
483
554
|
const unsanitizedHeaders = {
|
|
484
555
|
"Prismic-ref": ref,
|
|
485
|
-
"Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
|
|
486
556
|
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
487
557
|
...init ? init.headers : {}
|
|
488
558
|
};
|
|
559
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
560
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
|
|
561
|
+
}
|
|
489
562
|
const headers = {};
|
|
490
563
|
for (const key in unsanitizedHeaders) {
|
|
491
564
|
if (unsanitizedHeaders[key]) {
|
|
492
565
|
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
493
566
|
}
|
|
494
567
|
}
|
|
495
|
-
const url = new URL(
|
|
568
|
+
const url = new URL(
|
|
569
|
+
input
|
|
570
|
+
);
|
|
496
571
|
url.searchParams.set("ref", ref);
|
|
497
572
|
const query = url.searchParams.get("query");
|
|
498
573
|
if (query) {
|
|
499
|
-
url.searchParams.set(
|
|
574
|
+
url.searchParams.set(
|
|
575
|
+
"query",
|
|
576
|
+
minifyGraphQLQuery(query)
|
|
577
|
+
);
|
|
500
578
|
}
|
|
501
579
|
return await this.fetchFn(url.toString(), {
|
|
502
580
|
...init,
|
|
@@ -514,7 +592,11 @@ class Client {
|
|
|
514
592
|
const cachedRepository = await this.getCachedRepository(params);
|
|
515
593
|
const form = cachedRepository.forms[name];
|
|
516
594
|
if (!form) {
|
|
517
|
-
throw new PrismicError(
|
|
595
|
+
throw new PrismicError(
|
|
596
|
+
`Form with name "${name}" could not be found`,
|
|
597
|
+
void 0,
|
|
598
|
+
void 0
|
|
599
|
+
);
|
|
518
600
|
}
|
|
519
601
|
return form;
|
|
520
602
|
}
|
|
@@ -523,17 +605,17 @@ class Client {
|
|
|
523
605
|
if (this.refState.autoPreviewsEnabled) {
|
|
524
606
|
let previewRef;
|
|
525
607
|
let cookieJar;
|
|
526
|
-
if ((_a =
|
|
527
|
-
cookieJar = globalThis.document.cookie;
|
|
528
|
-
} else if ((_b = this.refState.httpRequest) == null ? void 0 : _b.headers) {
|
|
608
|
+
if ((_a = this.refState.httpRequest) == null ? void 0 : _a.headers) {
|
|
529
609
|
if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") {
|
|
530
610
|
cookieJar = this.refState.httpRequest.headers.get("cookie");
|
|
531
611
|
} else if ("cookie" in this.refState.httpRequest.headers) {
|
|
532
612
|
cookieJar = this.refState.httpRequest.headers.cookie;
|
|
533
613
|
}
|
|
614
|
+
} else if ((_b = globalThis.document) == null ? void 0 : _b.cookie) {
|
|
615
|
+
cookieJar = globalThis.document.cookie;
|
|
534
616
|
}
|
|
535
617
|
if (cookieJar) {
|
|
536
|
-
previewRef =
|
|
618
|
+
previewRef = getPreviewCookie(cookieJar);
|
|
537
619
|
}
|
|
538
620
|
if (previewRef) {
|
|
539
621
|
return previewRef;
|
|
@@ -562,7 +644,11 @@ class Client {
|
|
|
562
644
|
json = await res.json();
|
|
563
645
|
} catch (e) {
|
|
564
646
|
if (res.status === 404) {
|
|
565
|
-
throw new NotFoundError(
|
|
647
|
+
throw new NotFoundError(
|
|
648
|
+
`Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
|
|
649
|
+
url,
|
|
650
|
+
void 0
|
|
651
|
+
);
|
|
566
652
|
} else {
|
|
567
653
|
throw new PrismicError(void 0, url, void 0);
|
|
568
654
|
}
|
|
@@ -576,7 +662,11 @@ class Client {
|
|
|
576
662
|
}
|
|
577
663
|
case 401:
|
|
578
664
|
case 403: {
|
|
579
|
-
throw new ForbiddenError(
|
|
665
|
+
throw new ForbiddenError(
|
|
666
|
+
"error" in json ? json.error : json.message,
|
|
667
|
+
url,
|
|
668
|
+
json
|
|
669
|
+
);
|
|
580
670
|
}
|
|
581
671
|
}
|
|
582
672
|
throw new PrismicError(void 0, url, json);
|