@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.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,11 +313,12 @@ class Client {
|
|
|
278
313
|
return await this.fetch(url, params);
|
|
279
314
|
}
|
|
280
315
|
async getFirst(params) {
|
|
281
|
-
var _a;
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
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);
|
|
286
322
|
const result = await this.fetch(url, params);
|
|
287
323
|
const firstResult = result.results[0];
|
|
288
324
|
if (firstResult) {
|
|
@@ -295,7 +331,10 @@ class Client {
|
|
|
295
331
|
const { limit = Infinity, ...actualParams } = params;
|
|
296
332
|
const resolvedParams = {
|
|
297
333
|
...actualParams,
|
|
298
|
-
pageSize: Math.min(
|
|
334
|
+
pageSize: Math.min(
|
|
335
|
+
limit,
|
|
336
|
+
actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
|
|
337
|
+
)
|
|
299
338
|
};
|
|
300
339
|
const documents = [];
|
|
301
340
|
let latestResult;
|
|
@@ -310,58 +349,86 @@ class Client {
|
|
|
310
349
|
return documents.slice(0, limit);
|
|
311
350
|
}
|
|
312
351
|
async getByID(id, params) {
|
|
313
|
-
return await this.getFirst(
|
|
352
|
+
return await this.getFirst(
|
|
353
|
+
appendPredicates(params, predicate.at("document.id", id))
|
|
354
|
+
);
|
|
314
355
|
}
|
|
315
356
|
async getByIDs(ids, params) {
|
|
316
|
-
return await this.get(
|
|
357
|
+
return await this.get(
|
|
358
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
359
|
+
);
|
|
317
360
|
}
|
|
318
361
|
async getAllByIDs(ids, params) {
|
|
319
|
-
return await this.dangerouslyGetAll(
|
|
362
|
+
return await this.dangerouslyGetAll(
|
|
363
|
+
appendPredicates(params, predicate.in("document.id", ids))
|
|
364
|
+
);
|
|
320
365
|
}
|
|
321
366
|
async getByUID(documentType, uid, params) {
|
|
322
|
-
return await this.getFirst(
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
367
|
+
return await this.getFirst(
|
|
368
|
+
appendPredicates(params, [
|
|
369
|
+
typePredicate(documentType),
|
|
370
|
+
predicate.at(`my.${documentType}.uid`, uid)
|
|
371
|
+
])
|
|
372
|
+
);
|
|
326
373
|
}
|
|
327
374
|
async getByUIDs(documentType, uids, params) {
|
|
328
|
-
return await this.get(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
375
|
+
return await this.get(
|
|
376
|
+
appendPredicates(params, [
|
|
377
|
+
typePredicate(documentType),
|
|
378
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
379
|
+
])
|
|
380
|
+
);
|
|
332
381
|
}
|
|
333
382
|
async getAllByUIDs(documentType, uids, params) {
|
|
334
|
-
return await this.dangerouslyGetAll(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
383
|
+
return await this.dangerouslyGetAll(
|
|
384
|
+
appendPredicates(params, [
|
|
385
|
+
typePredicate(documentType),
|
|
386
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
387
|
+
])
|
|
388
|
+
);
|
|
338
389
|
}
|
|
339
390
|
async getSingle(documentType, params) {
|
|
340
|
-
return await this.getFirst(
|
|
391
|
+
return await this.getFirst(
|
|
392
|
+
appendPredicates(params, typePredicate(documentType))
|
|
393
|
+
);
|
|
341
394
|
}
|
|
342
395
|
async getByType(documentType, params) {
|
|
343
|
-
return await this.get(
|
|
396
|
+
return await this.get(
|
|
397
|
+
appendPredicates(params, typePredicate(documentType))
|
|
398
|
+
);
|
|
344
399
|
}
|
|
345
400
|
async getAllByType(documentType, params) {
|
|
346
401
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
347
402
|
}
|
|
348
403
|
async getByTag(tag, params) {
|
|
349
|
-
return await this.get(
|
|
404
|
+
return await this.get(
|
|
405
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
406
|
+
);
|
|
350
407
|
}
|
|
351
408
|
async getAllByTag(tag, params) {
|
|
352
|
-
return await this.dangerouslyGetAll(
|
|
409
|
+
return await this.dangerouslyGetAll(
|
|
410
|
+
appendPredicates(params, someTagsPredicate(tag))
|
|
411
|
+
);
|
|
353
412
|
}
|
|
354
413
|
async getByEveryTag(tags, params) {
|
|
355
|
-
return await this.get(
|
|
414
|
+
return await this.get(
|
|
415
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
416
|
+
);
|
|
356
417
|
}
|
|
357
418
|
async getAllByEveryTag(tags, params) {
|
|
358
|
-
return await this.dangerouslyGetAll(
|
|
419
|
+
return await this.dangerouslyGetAll(
|
|
420
|
+
appendPredicates(params, everyTagPredicate(tags))
|
|
421
|
+
);
|
|
359
422
|
}
|
|
360
423
|
async getBySomeTags(tags, params) {
|
|
361
|
-
return await this.get(
|
|
424
|
+
return await this.get(
|
|
425
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
426
|
+
);
|
|
362
427
|
}
|
|
363
428
|
async getAllBySomeTags(tags, params) {
|
|
364
|
-
return await this.dangerouslyGetAll(
|
|
429
|
+
return await this.dangerouslyGetAll(
|
|
430
|
+
appendPredicates(params, someTagsPredicate(tags))
|
|
431
|
+
);
|
|
365
432
|
}
|
|
366
433
|
async getRepository(params) {
|
|
367
434
|
const url = new URL(this.endpoint);
|
|
@@ -486,21 +553,28 @@ class Client {
|
|
|
486
553
|
const ref = await this.getResolvedRefString();
|
|
487
554
|
const unsanitizedHeaders = {
|
|
488
555
|
"Prismic-ref": ref,
|
|
489
|
-
"Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
|
|
490
556
|
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
491
557
|
...init ? init.headers : {}
|
|
492
558
|
};
|
|
559
|
+
if (cachedRepository.integrationFieldsRef) {
|
|
560
|
+
unsanitizedHeaders["Prismic-integration-field-ref"] = cachedRepository.integrationFieldsRef;
|
|
561
|
+
}
|
|
493
562
|
const headers = {};
|
|
494
563
|
for (const key in unsanitizedHeaders) {
|
|
495
564
|
if (unsanitizedHeaders[key]) {
|
|
496
565
|
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
497
566
|
}
|
|
498
567
|
}
|
|
499
|
-
const url = new URL(
|
|
568
|
+
const url = new URL(
|
|
569
|
+
input
|
|
570
|
+
);
|
|
500
571
|
url.searchParams.set("ref", ref);
|
|
501
572
|
const query = url.searchParams.get("query");
|
|
502
573
|
if (query) {
|
|
503
|
-
url.searchParams.set(
|
|
574
|
+
url.searchParams.set(
|
|
575
|
+
"query",
|
|
576
|
+
minifyGraphQLQuery(query)
|
|
577
|
+
);
|
|
504
578
|
}
|
|
505
579
|
return await this.fetchFn(url.toString(), {
|
|
506
580
|
...init,
|
|
@@ -518,7 +592,11 @@ class Client {
|
|
|
518
592
|
const cachedRepository = await this.getCachedRepository(params);
|
|
519
593
|
const form = cachedRepository.forms[name];
|
|
520
594
|
if (!form) {
|
|
521
|
-
throw new PrismicError(
|
|
595
|
+
throw new PrismicError(
|
|
596
|
+
`Form with name "${name}" could not be found`,
|
|
597
|
+
void 0,
|
|
598
|
+
void 0
|
|
599
|
+
);
|
|
522
600
|
}
|
|
523
601
|
return form;
|
|
524
602
|
}
|
|
@@ -527,17 +605,17 @@ class Client {
|
|
|
527
605
|
if (this.refState.autoPreviewsEnabled) {
|
|
528
606
|
let previewRef;
|
|
529
607
|
let cookieJar;
|
|
530
|
-
if ((_a =
|
|
531
|
-
cookieJar = globalThis.document.cookie;
|
|
532
|
-
} else if ((_b = this.refState.httpRequest) == null ? void 0 : _b.headers) {
|
|
608
|
+
if ((_a = this.refState.httpRequest) == null ? void 0 : _a.headers) {
|
|
533
609
|
if ("get" in this.refState.httpRequest.headers && typeof this.refState.httpRequest.headers.get === "function") {
|
|
534
610
|
cookieJar = this.refState.httpRequest.headers.get("cookie");
|
|
535
611
|
} else if ("cookie" in this.refState.httpRequest.headers) {
|
|
536
612
|
cookieJar = this.refState.httpRequest.headers.cookie;
|
|
537
613
|
}
|
|
614
|
+
} else if ((_b = globalThis.document) == null ? void 0 : _b.cookie) {
|
|
615
|
+
cookieJar = globalThis.document.cookie;
|
|
538
616
|
}
|
|
539
617
|
if (cookieJar) {
|
|
540
|
-
previewRef =
|
|
618
|
+
previewRef = getPreviewCookie(cookieJar);
|
|
541
619
|
}
|
|
542
620
|
if (previewRef) {
|
|
543
621
|
return previewRef;
|
|
@@ -566,7 +644,11 @@ class Client {
|
|
|
566
644
|
json = await res.json();
|
|
567
645
|
} catch (e) {
|
|
568
646
|
if (res.status === 404) {
|
|
569
|
-
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
|
+
);
|
|
570
652
|
} else {
|
|
571
653
|
throw new PrismicError(void 0, url, void 0);
|
|
572
654
|
}
|
|
@@ -580,7 +662,11 @@ class Client {
|
|
|
580
662
|
}
|
|
581
663
|
case 401:
|
|
582
664
|
case 403: {
|
|
583
|
-
throw new ForbiddenError(
|
|
665
|
+
throw new ForbiddenError(
|
|
666
|
+
"error" in json ? json.error : json.message,
|
|
667
|
+
url,
|
|
668
|
+
json
|
|
669
|
+
);
|
|
584
670
|
}
|
|
585
671
|
}
|
|
586
672
|
throw new PrismicError(void 0, url, json);
|