@prismicio/client 6.6.3 → 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 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +152 -66
- package/dist/index.js.map +1 -1
- package/package.json +15 -14
- package/src/client.ts +17 -16
- package/src/index.ts +1 -0
- package/src/lib/getPreviewCookie.ts +30 -0
- package/src/types.ts +1 -1
- package/src/lib/getCookie.ts +0 -58
package/dist/index.cjs
CHANGED
|
@@ -40,7 +40,11 @@ const getRepositoryEndpoint = (repositoryName) => {
|
|
|
40
40
|
if (isRepositoryName(repositoryName)) {
|
|
41
41
|
return `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
42
42
|
} else {
|
|
43
|
-
throw new PrismicError(
|
|
43
|
+
throw new PrismicError(
|
|
44
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
45
|
+
void 0,
|
|
46
|
+
void 0
|
|
47
|
+
);
|
|
44
48
|
}
|
|
45
49
|
};
|
|
46
50
|
|
|
@@ -48,7 +52,11 @@ const getRepositoryName = (repositoryEndpoint) => {
|
|
|
48
52
|
try {
|
|
49
53
|
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
50
54
|
} catch (e) {
|
|
51
|
-
throw new PrismicError(
|
|
55
|
+
throw new PrismicError(
|
|
56
|
+
`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`,
|
|
57
|
+
void 0,
|
|
58
|
+
void 0
|
|
59
|
+
);
|
|
52
60
|
}
|
|
53
61
|
};
|
|
54
62
|
|
|
@@ -56,7 +64,11 @@ const getGraphQLEndpoint = (repositoryName) => {
|
|
|
56
64
|
if (isRepositoryName(repositoryName)) {
|
|
57
65
|
return `https://${repositoryName}.cdn.prismic.io/graphql`;
|
|
58
66
|
} else {
|
|
59
|
-
throw new PrismicError(
|
|
67
|
+
throw new PrismicError(
|
|
68
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
69
|
+
void 0,
|
|
70
|
+
void 0
|
|
71
|
+
);
|
|
60
72
|
}
|
|
61
73
|
};
|
|
62
74
|
|
|
@@ -140,34 +152,37 @@ const findRefByLabel = (refs, label) => {
|
|
|
140
152
|
return findRef(refs, (ref) => ref.label === label);
|
|
141
153
|
};
|
|
142
154
|
|
|
155
|
+
const preview = "io.prismic.preview";
|
|
156
|
+
|
|
157
|
+
const cookie = /*#__PURE__*/Object.freeze({
|
|
158
|
+
__proto__: null,
|
|
159
|
+
preview: preview
|
|
160
|
+
});
|
|
161
|
+
|
|
143
162
|
const readValue = (value) => {
|
|
144
163
|
return value.replace(/%3B/g, ";");
|
|
145
164
|
};
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
165
|
+
const getPreviewCookie = (cookieJar) => {
|
|
166
|
+
const cookies = cookieJar.split("; ");
|
|
167
|
+
let value;
|
|
149
168
|
for (const cookie of cookies) {
|
|
150
169
|
const parts = cookie.split("=");
|
|
151
|
-
const value = parts.slice(1).join("=");
|
|
152
170
|
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
153
|
-
|
|
171
|
+
if (name === preview) {
|
|
172
|
+
value = readValue(parts.slice(1).join("="));
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
154
175
|
}
|
|
155
|
-
return
|
|
176
|
+
return value;
|
|
156
177
|
};
|
|
157
|
-
const getAll = (cookieStore) => parse(cookieStore);
|
|
158
|
-
const getCookie = (name, cookieJar) => getAll(cookieJar)[name];
|
|
159
178
|
|
|
160
179
|
const minifyGraphQLQuery = (query) => {
|
|
161
|
-
return query.replace(
|
|
180
|
+
return query.replace(
|
|
181
|
+
/(\n| )*( |{|})(\n| )*/gm,
|
|
182
|
+
(_chars, _spaces, brackets) => brackets
|
|
183
|
+
);
|
|
162
184
|
};
|
|
163
185
|
|
|
164
|
-
const preview = "io.prismic.preview";
|
|
165
|
-
|
|
166
|
-
const cookie = /*#__PURE__*/Object.freeze({
|
|
167
|
-
__proto__: null,
|
|
168
|
-
preview: preview
|
|
169
|
-
});
|
|
170
|
-
|
|
171
186
|
class ForbiddenError extends PrismicError {
|
|
172
187
|
}
|
|
173
188
|
|
|
@@ -214,7 +229,9 @@ const argsPredicate = (name) => {
|
|
|
214
229
|
const predicate = {
|
|
215
230
|
at: pathWithArgsPredicate("at"),
|
|
216
231
|
not: pathWithArgsPredicate("not"),
|
|
217
|
-
any: pathWithArgsPredicate(
|
|
232
|
+
any: pathWithArgsPredicate(
|
|
233
|
+
"any"
|
|
234
|
+
),
|
|
218
235
|
in: pathWithArgsPredicate("in"),
|
|
219
236
|
fulltext: pathWithArgsPredicate("fulltext"),
|
|
220
237
|
has: pathPredicate("has"),
|
|
@@ -223,16 +240,26 @@ const predicate = {
|
|
|
223
240
|
geopointNear: pathWithArgsPredicate("geopoint.near"),
|
|
224
241
|
numberLessThan: pathWithArgsPredicate("number.lt"),
|
|
225
242
|
numberGreaterThan: pathWithArgsPredicate("number.gt"),
|
|
226
|
-
numberInRange: pathWithArgsPredicate(
|
|
243
|
+
numberInRange: pathWithArgsPredicate(
|
|
244
|
+
"number.inRange"
|
|
245
|
+
),
|
|
227
246
|
dateAfter: pathWithArgsPredicate("date.after"),
|
|
228
247
|
dateBefore: pathWithArgsPredicate("date.before"),
|
|
229
248
|
dateBetween: pathWithArgsPredicate("date.between"),
|
|
230
249
|
dateDayOfMonth: pathWithArgsPredicate("date.day-of-month"),
|
|
231
|
-
dateDayOfMonthAfter: pathWithArgsPredicate(
|
|
232
|
-
|
|
250
|
+
dateDayOfMonthAfter: pathWithArgsPredicate(
|
|
251
|
+
"date.day-of-month-after"
|
|
252
|
+
),
|
|
253
|
+
dateDayOfMonthBefore: pathWithArgsPredicate(
|
|
254
|
+
"date.day-of-month-before"
|
|
255
|
+
),
|
|
233
256
|
dateDayOfWeek: pathWithArgsPredicate("date.day-of-week"),
|
|
234
|
-
dateDayOfWeekAfter: pathWithArgsPredicate(
|
|
235
|
-
|
|
257
|
+
dateDayOfWeekAfter: pathWithArgsPredicate(
|
|
258
|
+
"date.day-of-week-after"
|
|
259
|
+
),
|
|
260
|
+
dateDayOfWeekBefore: pathWithArgsPredicate(
|
|
261
|
+
"date.day-of-week-before"
|
|
262
|
+
),
|
|
236
263
|
dateMonth: pathWithArgsPredicate("date.month"),
|
|
237
264
|
dateMonthAfter: pathWithArgsPredicate("date.month-after"),
|
|
238
265
|
dateMonthBefore: pathWithArgsPredicate("date.month-before"),
|
|
@@ -259,7 +286,11 @@ class Client {
|
|
|
259
286
|
this.graphqlFetch = this.graphQLFetch.bind(this);
|
|
260
287
|
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
261
288
|
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
262
|
-
throw new PrismicError(
|
|
289
|
+
throw new PrismicError(
|
|
290
|
+
"@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.",
|
|
291
|
+
void 0,
|
|
292
|
+
void 0
|
|
293
|
+
);
|
|
263
294
|
}
|
|
264
295
|
this.endpoint = repositoryNameOrEndpoint;
|
|
265
296
|
} else {
|
|
@@ -276,7 +307,11 @@ class Client {
|
|
|
276
307
|
} else if (typeof globalThis.fetch === "function") {
|
|
277
308
|
this.fetchFn = globalThis.fetch;
|
|
278
309
|
} else {
|
|
279
|
-
throw new PrismicError(
|
|
310
|
+
throw new PrismicError(
|
|
311
|
+
"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.",
|
|
312
|
+
void 0,
|
|
313
|
+
void 0
|
|
314
|
+
);
|
|
280
315
|
}
|
|
281
316
|
if (this.fetchFn === globalThis.fetch) {
|
|
282
317
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
@@ -302,11 +337,12 @@ class Client {
|
|
|
302
337
|
return await this.fetch(url, params);
|
|
303
338
|
}
|
|
304
339
|
async getFirst(params) {
|
|
305
|
-
var _a;
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
340
|
+
var _a, _b;
|
|
341
|
+
const actualParams = { ...params };
|
|
342
|
+
if (!(params && params.page) && !(params == null ? void 0 : params.pageSize)) {
|
|
343
|
+
actualParams.pageSize = (_b = (_a = this.defaultParams) == null ? void 0 : _a.pageSize) != null ? _b : 1;
|
|
344
|
+
}
|
|
345
|
+
const url = await this.buildQueryURL(actualParams);
|
|
310
346
|
const result = await this.fetch(url, params);
|
|
311
347
|
const firstResult = result.results[0];
|
|
312
348
|
if (firstResult) {
|
|
@@ -319,7 +355,10 @@ class Client {
|
|
|
319
355
|
const { limit = Infinity, ...actualParams } = params;
|
|
320
356
|
const resolvedParams = {
|
|
321
357
|
...actualParams,
|
|
322
|
-
pageSize: Math.min(
|
|
358
|
+
pageSize: Math.min(
|
|
359
|
+
limit,
|
|
360
|
+
actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
|
|
361
|
+
)
|
|
323
362
|
};
|
|
324
363
|
const documents = [];
|
|
325
364
|
let latestResult;
|
|
@@ -334,58 +373,86 @@ class Client {
|
|
|
334
373
|
return documents.slice(0, limit);
|
|
335
374
|
}
|
|
336
375
|
async getByID(id, params) {
|
|
337
|
-
return await this.getFirst(
|
|
376
|
+
return await this.getFirst(
|
|
377
|
+
appendPredicates(params, predicate.at("document.id", id))
|
|
378
|
+
);
|
|
338
379
|
}
|
|
339
380
|
async getByIDs(ids, params) {
|
|
340
|
-
return await this.get(
|
|
381
|
+
return await this.get(
|
|
382
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
383
|
+
);
|
|
341
384
|
}
|
|
342
385
|
async getAllByIDs(ids, params) {
|
|
343
|
-
return await this.dangerouslyGetAll(
|
|
386
|
+
return await this.dangerouslyGetAll(
|
|
387
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
388
|
+
);
|
|
344
389
|
}
|
|
345
390
|
async getByUID(documentType, uid, params) {
|
|
346
|
-
return await this.getFirst(
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
391
|
+
return await this.getFirst(
|
|
392
|
+
appendPredicates(params, [
|
|
393
|
+
typePredicate(documentType),
|
|
394
|
+
predicate.at(`my.${documentType}.uid`, uid)
|
|
395
|
+
])
|
|
396
|
+
);
|
|
350
397
|
}
|
|
351
398
|
async getByUIDs(documentType, uids, params) {
|
|
352
|
-
return await this.get(
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
399
|
+
return await this.get(
|
|
400
|
+
appendPredicates(params, [
|
|
401
|
+
typePredicate(documentType),
|
|
402
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
403
|
+
])
|
|
404
|
+
);
|
|
356
405
|
}
|
|
357
406
|
async getAllByUIDs(documentType, uids, params) {
|
|
358
|
-
return await this.dangerouslyGetAll(
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
407
|
+
return await this.dangerouslyGetAll(
|
|
408
|
+
appendPredicates(params, [
|
|
409
|
+
typePredicate(documentType),
|
|
410
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
411
|
+
])
|
|
412
|
+
);
|
|
362
413
|
}
|
|
363
414
|
async getSingle(documentType, params) {
|
|
364
|
-
return await this.getFirst(
|
|
415
|
+
return await this.getFirst(
|
|
416
|
+
appendPredicates(params, typePredicate(documentType))
|
|
417
|
+
);
|
|
365
418
|
}
|
|
366
419
|
async getByType(documentType, params) {
|
|
367
|
-
return await this.get(
|
|
420
|
+
return await this.get(
|
|
421
|
+
appendPredicates(params, typePredicate(documentType))
|
|
422
|
+
);
|
|
368
423
|
}
|
|
369
424
|
async getAllByType(documentType, params) {
|
|
370
425
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
371
426
|
}
|
|
372
427
|
async getByTag(tag, params) {
|
|
373
|
-
return await this.get(
|
|
428
|
+
return await this.get(
|
|
429
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
430
|
+
);
|
|
374
431
|
}
|
|
375
432
|
async getAllByTag(tag, params) {
|
|
376
|
-
return await this.dangerouslyGetAll(
|
|
433
|
+
return await this.dangerouslyGetAll(
|
|
434
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
435
|
+
);
|
|
377
436
|
}
|
|
378
437
|
async getByEveryTag(tags, params) {
|
|
379
|
-
return await this.get(
|
|
438
|
+
return await this.get(
|
|
439
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
440
|
+
);
|
|
380
441
|
}
|
|
381
442
|
async getAllByEveryTag(tags, params) {
|
|
382
|
-
return await this.dangerouslyGetAll(
|
|
443
|
+
return await this.dangerouslyGetAll(
|
|
444
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
445
|
+
);
|
|
383
446
|
}
|
|
384
447
|
async getBySomeTags(tags, params) {
|
|
385
|
-
return await this.get(
|
|
448
|
+
return await this.get(
|
|
449
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
450
|
+
);
|
|
386
451
|
}
|
|
387
452
|
async getAllBySomeTags(tags, params) {
|
|
388
|
-
return await this.dangerouslyGetAll(
|
|
453
|
+
return await this.dangerouslyGetAll(
|
|
454
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
455
|
+
);
|
|
389
456
|
}
|
|
390
457
|
async getRepository(params) {
|
|
391
458
|
const url = new URL(this.endpoint);
|
|
@@ -510,21 +577,28 @@ class Client {
|
|
|
510
577
|
const ref = await this.getResolvedRefString();
|
|
511
578
|
const unsanitizedHeaders = {
|
|
512
579
|
"Prismic-ref": ref,
|
|
513
|
-
"Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
|
|
514
580
|
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
515
581
|
...init ? init.headers : {}
|
|
516
582
|
};
|
|
583
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
584
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
|
|
585
|
+
}
|
|
517
586
|
const headers = {};
|
|
518
587
|
for (const key in unsanitizedHeaders) {
|
|
519
588
|
if (unsanitizedHeaders[key]) {
|
|
520
589
|
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
521
590
|
}
|
|
522
591
|
}
|
|
523
|
-
const url = new URL(
|
|
592
|
+
const url = new URL(
|
|
593
|
+
input
|
|
594
|
+
);
|
|
524
595
|
url.searchParams.set("ref", ref);
|
|
525
596
|
const query = url.searchParams.get("query");
|
|
526
597
|
if (query) {
|
|
527
|
-
url.searchParams.set(
|
|
598
|
+
url.searchParams.set(
|
|
599
|
+
"query",
|
|
600
|
+
minifyGraphQLQuery(query)
|
|
601
|
+
);
|
|
528
602
|
}
|
|
529
603
|
return await this.fetchFn(url.toString(), {
|
|
530
604
|
...init,
|
|
@@ -542,7 +616,11 @@ class Client {
|
|
|
542
616
|
const cachedRepository = await this.getCachedRepository(params);
|
|
543
617
|
const form = cachedRepository.forms[name];
|
|
544
618
|
if (!form) {
|
|
545
|
-
throw new PrismicError(
|
|
619
|
+
throw new PrismicError(
|
|
620
|
+
`Form with name "${name}" could not be found`,
|
|
621
|
+
void 0,
|
|
622
|
+
void 0
|
|
623
|
+
);
|
|
546
624
|
}
|
|
547
625
|
return form;
|
|
548
626
|
}
|
|
@@ -551,17 +629,17 @@ class Client {
|
|
|
551
629
|
if (this.refState.autoPreviewsEnabled) {
|
|
552
630
|
let previewRef;
|
|
553
631
|
let cookieJar;
|
|
554
|
-
if ((_a =
|
|
555
|
-
cookieJar = globalThis.document.cookie;
|
|
556
|
-
} else if ((_b = this.refState.httpRequest) == null ? void 0 : _b.headers) {
|
|
632
|
+
if ((_a = this.refState.httpRequest) == null ? void 0 : _a.headers) {
|
|
557
633
|
if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") {
|
|
558
634
|
cookieJar = this.refState.httpRequest.headers.get("cookie");
|
|
559
635
|
} else if ("cookie" in this.refState.httpRequest.headers) {
|
|
560
636
|
cookieJar = this.refState.httpRequest.headers.cookie;
|
|
561
637
|
}
|
|
638
|
+
} else if ((_b = globalThis.document) == null ? void 0 : _b.cookie) {
|
|
639
|
+
cookieJar = globalThis.document.cookie;
|
|
562
640
|
}
|
|
563
641
|
if (cookieJar) {
|
|
564
|
-
previewRef =
|
|
642
|
+
previewRef = getPreviewCookie(cookieJar);
|
|
565
643
|
}
|
|
566
644
|
if (previewRef) {
|
|
567
645
|
return previewRef;
|
|
@@ -590,7 +668,11 @@ class Client {
|
|
|
590
668
|
json = await res.json();
|
|
591
669
|
} catch (e) {
|
|
592
670
|
if (res.status === 404) {
|
|
593
|
-
throw new NotFoundError(
|
|
671
|
+
throw new NotFoundError(
|
|
672
|
+
`Prismic repository not found. Check that "${this.endpoint}" is pointing to the correct repository.`,
|
|
673
|
+
url,
|
|
674
|
+
void 0
|
|
675
|
+
);
|
|
594
676
|
} else {
|
|
595
677
|
throw new PrismicError(void 0, url, void 0);
|
|
596
678
|
}
|
|
@@ -604,7 +686,11 @@ class Client {
|
|
|
604
686
|
}
|
|
605
687
|
case 401:
|
|
606
688
|
case 403: {
|
|
607
|
-
throw new ForbiddenError(
|
|
689
|
+
throw new ForbiddenError(
|
|
690
|
+
"error" in json ? json.error : json.message,
|
|
691
|
+
url,
|
|
692
|
+
json
|
|
693
|
+
);
|
|
608
694
|
}
|
|
609
695
|
}
|
|
610
696
|
throw new PrismicError(void 0, url, json);
|