@prismicio/client 6.0.0 → 6.3.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/README.md +3 -4
- package/dist/index.cjs +147 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +154 -40
- package/dist/index.js +143 -73
- package/dist/index.js.map +1 -1
- package/package.json +19 -16
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +210 -75
- package/src/getGraphQLEndpoint.ts +24 -0
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -2
- package/src/isRepositoryEndpoint.ts +17 -0
- package/src/isRepositoryName.ts +10 -0
- package/src/lib/minifyGraphQLQuery.ts +13 -0
- package/src/types.ts +42 -0
- package/src/getEndpoint.ts +0 -12
package/README.md
CHANGED
|
@@ -18,8 +18,7 @@ The official JavaScript + TypeScript client library for [Prismic][prismic].
|
|
|
18
18
|
import * as prismic from "@prismicio/client";
|
|
19
19
|
|
|
20
20
|
// Create a client
|
|
21
|
-
const
|
|
22
|
-
const client = prismic.createClient(endpoint);
|
|
21
|
+
const client = prismic.createClient("my-repository");
|
|
23
22
|
|
|
24
23
|
// Then query for your content
|
|
25
24
|
const blogPosts = await client.getAllByType("blog_post");
|
|
@@ -52,7 +51,7 @@ For more clarity on this project and its structure you can also check out the de
|
|
|
52
51
|
## License
|
|
53
52
|
|
|
54
53
|
```
|
|
55
|
-
Copyright 2013-
|
|
54
|
+
Copyright 2013-2022 Prismic <contact@prismic.io> (https://prismic.io)
|
|
56
55
|
|
|
57
56
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
58
57
|
you may not use this file except in compliance with the License.
|
|
@@ -72,7 +71,7 @@ For more clarity on this project and its structure you can also check out the de
|
|
|
72
71
|
[prismic]: https://prismic.io
|
|
73
72
|
[prismic-predicates]: https://prismic.io/docs/technologies/rest-api-technical-reference
|
|
74
73
|
[prismic-previews]: https://prismic.io/docs/core-concepts/previews
|
|
75
|
-
[prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client
|
|
74
|
+
[prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client
|
|
76
75
|
[changelog]: ./CHANGELOG.md
|
|
77
76
|
[contributing]: ./CONTRIBUTING.md
|
|
78
77
|
[forum-question]: https://community.prismic.io/c/kits-and-dev-languages/javascript/14
|
package/dist/index.cjs
CHANGED
|
@@ -24,7 +24,50 @@ function _interopNamespace(e) {
|
|
|
24
24
|
|
|
25
25
|
const prismicH__namespace = /*#__PURE__*/_interopNamespace(prismicH);
|
|
26
26
|
|
|
27
|
-
const
|
|
27
|
+
const isRepositoryName = (input) => {
|
|
28
|
+
return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
class PrismicError extends Error {
|
|
32
|
+
constructor(message = "An invalid API response was returned", url, response) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.url = url;
|
|
35
|
+
this.response = response;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const getRepositoryEndpoint = (repositoryName) => {
|
|
40
|
+
if (isRepositoryName(repositoryName)) {
|
|
41
|
+
return `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
42
|
+
} else {
|
|
43
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const getRepositoryName = (repositoryEndpoint) => {
|
|
48
|
+
try {
|
|
49
|
+
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
50
|
+
} catch (e) {
|
|
51
|
+
throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const getGraphQLEndpoint = (repositoryName) => {
|
|
56
|
+
if (isRepositoryName(repositoryName)) {
|
|
57
|
+
return `https://${repositoryName}.cdn.prismic.io/graphql`;
|
|
58
|
+
} else {
|
|
59
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const isRepositoryEndpoint = (input) => {
|
|
64
|
+
try {
|
|
65
|
+
new URL(input);
|
|
66
|
+
return true;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
28
71
|
|
|
29
72
|
const castArray = (a) => Array.isArray(a) ? a : [a];
|
|
30
73
|
|
|
@@ -77,14 +120,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
|
|
|
77
120
|
|
|
78
121
|
const castThunk = (a) => typeof a === "function" ? a : () => a;
|
|
79
122
|
|
|
80
|
-
class PrismicError extends Error {
|
|
81
|
-
constructor(message = "An invalid API response was returned", url, response) {
|
|
82
|
-
super(message);
|
|
83
|
-
this.url = url;
|
|
84
|
-
this.response = response;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
123
|
const findRef = (refs, predicate) => {
|
|
89
124
|
const ref = refs.find((ref2) => predicate(ref2));
|
|
90
125
|
if (!ref) {
|
|
@@ -122,9 +157,23 @@ const parse = (cookieString) => {
|
|
|
122
157
|
const getAll = (cookieStore) => parse(cookieStore);
|
|
123
158
|
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
124
159
|
|
|
160
|
+
const minifyGraphQLQuery = (query) => {
|
|
161
|
+
return query.replace(/(\n| )*( |{|})(\n| )*/gm, (_chars, _spaces, brackets) => brackets);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const preview = "io.prismic.preview";
|
|
165
|
+
|
|
166
|
+
const cookie = /*#__PURE__*/Object.freeze({
|
|
167
|
+
__proto__: null,
|
|
168
|
+
preview: preview
|
|
169
|
+
});
|
|
170
|
+
|
|
125
171
|
class ForbiddenError extends PrismicError {
|
|
126
172
|
}
|
|
127
173
|
|
|
174
|
+
class NotFoundError extends PrismicError {
|
|
175
|
+
}
|
|
176
|
+
|
|
128
177
|
class ParsingError extends PrismicError {
|
|
129
178
|
}
|
|
130
179
|
|
|
@@ -193,41 +242,28 @@ const predicate = {
|
|
|
193
242
|
dateHourBefore: pathWithArgsPredicate("date.hour-before")
|
|
194
243
|
};
|
|
195
244
|
|
|
196
|
-
const preview = "io.prismic.preview";
|
|
197
|
-
|
|
198
|
-
const cookie = /*#__PURE__*/Object.freeze({
|
|
199
|
-
__proto__: null,
|
|
200
|
-
preview: preview
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
class NotFoundError extends PrismicError {
|
|
204
|
-
}
|
|
205
|
-
|
|
206
245
|
const MAX_PAGE_SIZE = 100;
|
|
207
246
|
const REPOSITORY_CACHE_TTL = 5e3;
|
|
208
247
|
const GET_ALL_QUERY_DELAY = 500;
|
|
209
|
-
var RefStateMode;
|
|
210
|
-
(function(RefStateMode2) {
|
|
211
|
-
RefStateMode2["Master"] = "Master";
|
|
212
|
-
RefStateMode2["ReleaseID"] = "ReleaseID";
|
|
213
|
-
RefStateMode2["ReleaseLabel"] = "ReleaseLabel";
|
|
214
|
-
RefStateMode2["Manual"] = "Manual";
|
|
215
|
-
})(RefStateMode || (RefStateMode = {}));
|
|
216
248
|
const typePredicate = (documentType) => predicate.at("document.type", documentType);
|
|
217
|
-
const everyTagPredicate = (tags) => predicate.at("document.tags", tags);
|
|
218
|
-
const someTagsPredicate = (tags) => predicate.any("document.tags", tags);
|
|
249
|
+
const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags));
|
|
250
|
+
const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
|
|
219
251
|
const createClient = (...args) => new Client(...args);
|
|
220
252
|
class Client {
|
|
221
|
-
constructor(
|
|
253
|
+
constructor(repositoryNameOrEndpoint, options = {}) {
|
|
222
254
|
this.refState = {
|
|
223
|
-
mode:
|
|
255
|
+
mode: "Master" /* Master */,
|
|
224
256
|
autoPreviewsEnabled: true
|
|
225
257
|
};
|
|
226
258
|
this.cachedRepositoryExpiration = 0;
|
|
227
|
-
if (
|
|
228
|
-
|
|
259
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
260
|
+
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
261
|
+
throw new PrismicError("@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.", void 0, void 0);
|
|
262
|
+
}
|
|
263
|
+
this.endpoint = repositoryNameOrEndpoint;
|
|
264
|
+
} else {
|
|
265
|
+
this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
|
|
229
266
|
}
|
|
230
|
-
this.endpoint = endpoint;
|
|
231
267
|
this.accessToken = options.accessToken;
|
|
232
268
|
this.routes = options.routes;
|
|
233
269
|
this.defaultParams = options.defaultParams;
|
|
@@ -244,6 +280,7 @@ class Client {
|
|
|
244
280
|
if (this.fetchFn === globalThis.fetch) {
|
|
245
281
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
246
282
|
}
|
|
283
|
+
this.graphqlFetch = this.graphqlFetch.bind(this);
|
|
247
284
|
}
|
|
248
285
|
enableAutoPreviews() {
|
|
249
286
|
this.refState.autoPreviewsEnabled = true;
|
|
@@ -327,10 +364,10 @@ class Client {
|
|
|
327
364
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
328
365
|
}
|
|
329
366
|
async getByTag(tag, params) {
|
|
330
|
-
return await this.get(appendPredicates(params,
|
|
367
|
+
return await this.get(appendPredicates(params, someTagsPredicate(tag)));
|
|
331
368
|
}
|
|
332
369
|
async getAllByTag(tag, params) {
|
|
333
|
-
return await this.dangerouslyGetAll(appendPredicates(params,
|
|
370
|
+
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
|
|
334
371
|
}
|
|
335
372
|
async getByEveryTag(tags, params) {
|
|
336
373
|
return await this.get(appendPredicates(params, everyTagPredicate(tags)));
|
|
@@ -344,53 +381,56 @@ class Client {
|
|
|
344
381
|
async getAllBySomeTags(tags, params) {
|
|
345
382
|
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
|
|
346
383
|
}
|
|
347
|
-
async getRepository() {
|
|
384
|
+
async getRepository(params) {
|
|
348
385
|
const url = new URL(this.endpoint);
|
|
349
386
|
if (this.accessToken) {
|
|
350
387
|
url.searchParams.set("access_token", this.accessToken);
|
|
351
388
|
}
|
|
352
|
-
return await this.fetch(url.toString());
|
|
389
|
+
return await this.fetch(url.toString(), params);
|
|
353
390
|
}
|
|
354
|
-
async getRefs() {
|
|
355
|
-
const repository = await this.getRepository();
|
|
391
|
+
async getRefs(params) {
|
|
392
|
+
const repository = await this.getRepository(params);
|
|
356
393
|
return repository.refs;
|
|
357
394
|
}
|
|
358
|
-
async getRefByID(id) {
|
|
359
|
-
const refs = await this.getRefs();
|
|
395
|
+
async getRefByID(id, params) {
|
|
396
|
+
const refs = await this.getRefs(params);
|
|
360
397
|
return findRefByID(refs, id);
|
|
361
398
|
}
|
|
362
|
-
async getRefByLabel(label) {
|
|
363
|
-
const refs = await this.getRefs();
|
|
399
|
+
async getRefByLabel(label, params) {
|
|
400
|
+
const refs = await this.getRefs(params);
|
|
364
401
|
return findRefByLabel(refs, label);
|
|
365
402
|
}
|
|
366
|
-
async getMasterRef() {
|
|
367
|
-
const refs = await this.getRefs();
|
|
403
|
+
async getMasterRef(params) {
|
|
404
|
+
const refs = await this.getRefs(params);
|
|
368
405
|
return findMasterRef(refs);
|
|
369
406
|
}
|
|
370
|
-
async getReleases() {
|
|
371
|
-
const refs = await this.getRefs();
|
|
407
|
+
async getReleases(params) {
|
|
408
|
+
const refs = await this.getRefs(params);
|
|
372
409
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
373
410
|
}
|
|
374
|
-
async getReleaseByID(id) {
|
|
375
|
-
const releases = await this.getReleases();
|
|
411
|
+
async getReleaseByID(id, params) {
|
|
412
|
+
const releases = await this.getReleases(params);
|
|
376
413
|
return findRefByID(releases, id);
|
|
377
414
|
}
|
|
378
|
-
async getReleaseByLabel(label) {
|
|
379
|
-
const releases = await this.getReleases();
|
|
415
|
+
async getReleaseByLabel(label, params) {
|
|
416
|
+
const releases = await this.getReleases(params);
|
|
380
417
|
return findRefByLabel(releases, label);
|
|
381
418
|
}
|
|
382
|
-
async getTags() {
|
|
419
|
+
async getTags(params) {
|
|
383
420
|
try {
|
|
384
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
421
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
385
422
|
return await this.fetch(tagsForm.action);
|
|
386
423
|
} catch (e) {
|
|
387
|
-
const repository = await this.getRepository();
|
|
424
|
+
const repository = await this.getRepository(params);
|
|
388
425
|
return repository.tags;
|
|
389
426
|
}
|
|
390
427
|
}
|
|
391
|
-
async buildQueryURL(
|
|
428
|
+
async buildQueryURL({
|
|
429
|
+
signal,
|
|
430
|
+
...params
|
|
431
|
+
} = {}) {
|
|
392
432
|
const ref = params.ref || await this.getResolvedRefString();
|
|
393
|
-
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
|
|
433
|
+
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0;
|
|
394
434
|
return buildQueryURL(this.endpoint, {
|
|
395
435
|
...this.defaultParams,
|
|
396
436
|
...params,
|
|
@@ -414,7 +454,9 @@ class Client {
|
|
|
414
454
|
}
|
|
415
455
|
if (documentID != null) {
|
|
416
456
|
const document = await this.getByID(documentID, {
|
|
417
|
-
|
|
457
|
+
signal: args.signal,
|
|
458
|
+
ref: previewToken,
|
|
459
|
+
lang: "*"
|
|
418
460
|
});
|
|
419
461
|
return prismicH__namespace.asLink(document, args.linkResolver);
|
|
420
462
|
} else {
|
|
@@ -422,45 +464,70 @@ class Client {
|
|
|
422
464
|
}
|
|
423
465
|
}
|
|
424
466
|
queryLatestContent() {
|
|
425
|
-
this.refState.mode =
|
|
467
|
+
this.refState.mode = "Master" /* Master */;
|
|
426
468
|
}
|
|
427
469
|
queryContentFromReleaseByID(releaseID) {
|
|
428
470
|
this.refState = {
|
|
429
471
|
...this.refState,
|
|
430
|
-
mode:
|
|
472
|
+
mode: "ReleaseID" /* ReleaseID */,
|
|
431
473
|
releaseID
|
|
432
474
|
};
|
|
433
475
|
}
|
|
434
476
|
queryContentFromReleaseByLabel(releaseLabel) {
|
|
435
477
|
this.refState = {
|
|
436
478
|
...this.refState,
|
|
437
|
-
mode:
|
|
479
|
+
mode: "ReleaseLabel" /* ReleaseLabel */,
|
|
438
480
|
releaseLabel
|
|
439
481
|
};
|
|
440
482
|
}
|
|
441
483
|
queryContentFromRef(ref) {
|
|
442
484
|
this.refState = {
|
|
443
485
|
...this.refState,
|
|
444
|
-
mode:
|
|
486
|
+
mode: "Manual" /* Manual */,
|
|
445
487
|
ref
|
|
446
488
|
};
|
|
447
489
|
}
|
|
448
|
-
async
|
|
490
|
+
async graphqlFetch(input, init) {
|
|
491
|
+
const cachedRepository = await this.getCachedRepository();
|
|
492
|
+
const ref = await this.getResolvedRefString();
|
|
493
|
+
const unsanitizedHeaders = {
|
|
494
|
+
"Prismic-ref": ref,
|
|
495
|
+
"Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
|
|
496
|
+
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
497
|
+
...init ? init.headers : {}
|
|
498
|
+
};
|
|
499
|
+
const headers = {};
|
|
500
|
+
for (const key in unsanitizedHeaders) {
|
|
501
|
+
if (unsanitizedHeaders[key]) {
|
|
502
|
+
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
const url = new URL(input);
|
|
506
|
+
const query = url.searchParams.get("query");
|
|
507
|
+
if (query) {
|
|
508
|
+
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
509
|
+
}
|
|
510
|
+
return await this.fetchFn(url.toString(), {
|
|
511
|
+
...init,
|
|
512
|
+
headers
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
async getCachedRepository(params) {
|
|
449
516
|
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
|
|
450
517
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
451
|
-
this.cachedRepository = await this.getRepository();
|
|
518
|
+
this.cachedRepository = await this.getRepository(params);
|
|
452
519
|
}
|
|
453
520
|
return this.cachedRepository;
|
|
454
521
|
}
|
|
455
|
-
async getCachedRepositoryForm(name) {
|
|
456
|
-
const cachedRepository = await this.getCachedRepository();
|
|
522
|
+
async getCachedRepositoryForm(name, params) {
|
|
523
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
457
524
|
const form = cachedRepository.forms[name];
|
|
458
525
|
if (!form) {
|
|
459
526
|
throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
|
|
460
527
|
}
|
|
461
528
|
return form;
|
|
462
529
|
}
|
|
463
|
-
async getResolvedRefString() {
|
|
530
|
+
async getResolvedRefString(params) {
|
|
464
531
|
var _a, _b, _c;
|
|
465
532
|
if (this.refState.autoPreviewsEnabled) {
|
|
466
533
|
let previewRef = void 0;
|
|
@@ -473,13 +540,13 @@ class Client {
|
|
|
473
540
|
return previewRef;
|
|
474
541
|
}
|
|
475
542
|
}
|
|
476
|
-
const cachedRepository = await this.getCachedRepository();
|
|
543
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
477
544
|
const refModeType = this.refState.mode;
|
|
478
|
-
if (refModeType ===
|
|
545
|
+
if (refModeType === "ReleaseID" /* ReleaseID */) {
|
|
479
546
|
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
|
|
480
|
-
} else if (refModeType ===
|
|
547
|
+
} else if (refModeType === "ReleaseLabel" /* ReleaseLabel */) {
|
|
481
548
|
return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel).ref;
|
|
482
|
-
} else if (refModeType ===
|
|
549
|
+
} else if (refModeType === "Manual" /* Manual */) {
|
|
483
550
|
const res = await castThunk(this.refState.ref)();
|
|
484
551
|
if (typeof res === "string") {
|
|
485
552
|
return res;
|
|
@@ -487,8 +554,10 @@ class Client {
|
|
|
487
554
|
}
|
|
488
555
|
return findMasterRef(cachedRepository.refs).ref;
|
|
489
556
|
}
|
|
490
|
-
async fetch(url,
|
|
491
|
-
const res = await this.fetchFn(url
|
|
557
|
+
async fetch(url, params = {}) {
|
|
558
|
+
const res = await this.fetchFn(url, {
|
|
559
|
+
signal: params.signal
|
|
560
|
+
});
|
|
492
561
|
let json;
|
|
493
562
|
try {
|
|
494
563
|
json = await res.json();
|
|
@@ -515,6 +584,7 @@ class Client {
|
|
|
515
584
|
}
|
|
516
585
|
}
|
|
517
586
|
|
|
587
|
+
const getEndpoint = getRepositoryEndpoint;
|
|
518
588
|
const predicates = predicate;
|
|
519
589
|
const Predicates = predicate;
|
|
520
590
|
|
|
@@ -528,6 +598,11 @@ exports.buildQueryURL = buildQueryURL;
|
|
|
528
598
|
exports.cookie = cookie;
|
|
529
599
|
exports.createClient = createClient;
|
|
530
600
|
exports.getEndpoint = getEndpoint;
|
|
601
|
+
exports.getGraphQLEndpoint = getGraphQLEndpoint;
|
|
602
|
+
exports.getRepositoryEndpoint = getRepositoryEndpoint;
|
|
603
|
+
exports.getRepositoryName = getRepositoryName;
|
|
604
|
+
exports.isRepositoryEndpoint = isRepositoryEndpoint;
|
|
605
|
+
exports.isRepositoryName = isRepositoryName;
|
|
531
606
|
exports.predicate = predicate;
|
|
532
607
|
exports.predicates = predicates;
|
|
533
608
|
//# sourceMappingURL=index.cjs.map
|