@prismicio/client 6.1.1 → 6.4.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 +126 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +126 -45
- package/dist/index.js +123 -66
- package/dist/index.js.map +1 -1
- package/package.json +16 -17
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +170 -93
- package/src/getGraphQLEndpoint.ts +14 -2
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -3
- 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,9 +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
|
+
};
|
|
28
54
|
|
|
29
|
-
const getGraphQLEndpoint = (repositoryName) =>
|
|
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
|
+
};
|
|
30
71
|
|
|
31
72
|
const castArray = (a) => Array.isArray(a) ? a : [a];
|
|
32
73
|
|
|
@@ -79,14 +120,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
|
|
|
79
120
|
|
|
80
121
|
const castThunk = (a) => typeof a === "function" ? a : () => a;
|
|
81
122
|
|
|
82
|
-
class PrismicError extends Error {
|
|
83
|
-
constructor(message = "An invalid API response was returned", url, response) {
|
|
84
|
-
super(message);
|
|
85
|
-
this.url = url;
|
|
86
|
-
this.response = response;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
123
|
const findRef = (refs, predicate) => {
|
|
91
124
|
const ref = refs.find((ref2) => predicate(ref2));
|
|
92
125
|
if (!ref) {
|
|
@@ -124,9 +157,23 @@ const parse = (cookieString) => {
|
|
|
124
157
|
const getAll = (cookieStore) => parse(cookieStore);
|
|
125
158
|
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
126
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
|
+
|
|
127
171
|
class ForbiddenError extends PrismicError {
|
|
128
172
|
}
|
|
129
173
|
|
|
174
|
+
class NotFoundError extends PrismicError {
|
|
175
|
+
}
|
|
176
|
+
|
|
130
177
|
class ParsingError extends PrismicError {
|
|
131
178
|
}
|
|
132
179
|
|
|
@@ -195,34 +242,29 @@ const predicate = {
|
|
|
195
242
|
dateHourBefore: pathWithArgsPredicate("date.hour-before")
|
|
196
243
|
};
|
|
197
244
|
|
|
198
|
-
const preview = "io.prismic.preview";
|
|
199
|
-
|
|
200
|
-
const cookie = /*#__PURE__*/Object.freeze({
|
|
201
|
-
__proto__: null,
|
|
202
|
-
preview: preview
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
class NotFoundError extends PrismicError {
|
|
206
|
-
}
|
|
207
|
-
|
|
208
245
|
const MAX_PAGE_SIZE = 100;
|
|
209
246
|
const REPOSITORY_CACHE_TTL = 5e3;
|
|
210
247
|
const GET_ALL_QUERY_DELAY = 500;
|
|
211
248
|
const typePredicate = (documentType) => predicate.at("document.type", documentType);
|
|
212
|
-
const everyTagPredicate = (tags) => predicate.at("document.tags", tags);
|
|
213
|
-
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));
|
|
214
251
|
const createClient = (...args) => new Client(...args);
|
|
215
252
|
class Client {
|
|
216
|
-
constructor(
|
|
253
|
+
constructor(repositoryNameOrEndpoint, options = {}) {
|
|
217
254
|
this.refState = {
|
|
218
255
|
mode: "Master" /* Master */,
|
|
219
256
|
autoPreviewsEnabled: true
|
|
220
257
|
};
|
|
221
258
|
this.cachedRepositoryExpiration = 0;
|
|
222
|
-
|
|
223
|
-
|
|
259
|
+
this.graphqlFetch = this.graphQLFetch;
|
|
260
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
261
|
+
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
262
|
+
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);
|
|
263
|
+
}
|
|
264
|
+
this.endpoint = repositoryNameOrEndpoint;
|
|
265
|
+
} else {
|
|
266
|
+
this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
|
|
224
267
|
}
|
|
225
|
-
this.endpoint = endpoint;
|
|
226
268
|
this.accessToken = options.accessToken;
|
|
227
269
|
this.routes = options.routes;
|
|
228
270
|
this.defaultParams = options.defaultParams;
|
|
@@ -239,7 +281,7 @@ class Client {
|
|
|
239
281
|
if (this.fetchFn === globalThis.fetch) {
|
|
240
282
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
241
283
|
}
|
|
242
|
-
this.
|
|
284
|
+
this.graphQLFetch = this.graphQLFetch.bind(this);
|
|
243
285
|
}
|
|
244
286
|
enableAutoPreviews() {
|
|
245
287
|
this.refState.autoPreviewsEnabled = true;
|
|
@@ -269,10 +311,11 @@ class Client {
|
|
|
269
311
|
throw new PrismicError("No documents were returned", url, void 0);
|
|
270
312
|
}
|
|
271
313
|
async dangerouslyGetAll(params = {}) {
|
|
314
|
+
var _a;
|
|
272
315
|
const { limit = Infinity, ...actualParams } = params;
|
|
273
316
|
const resolvedParams = {
|
|
274
317
|
...actualParams,
|
|
275
|
-
pageSize: actualParams.pageSize || MAX_PAGE_SIZE
|
|
318
|
+
pageSize: actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
|
|
276
319
|
};
|
|
277
320
|
const documents = [];
|
|
278
321
|
let latestResult;
|
|
@@ -323,10 +366,10 @@ class Client {
|
|
|
323
366
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
324
367
|
}
|
|
325
368
|
async getByTag(tag, params) {
|
|
326
|
-
return await this.get(appendPredicates(params,
|
|
369
|
+
return await this.get(appendPredicates(params, someTagsPredicate(tag)));
|
|
327
370
|
}
|
|
328
371
|
async getAllByTag(tag, params) {
|
|
329
|
-
return await this.dangerouslyGetAll(appendPredicates(params,
|
|
372
|
+
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
|
|
330
373
|
}
|
|
331
374
|
async getByEveryTag(tags, params) {
|
|
332
375
|
return await this.get(appendPredicates(params, everyTagPredicate(tags)));
|
|
@@ -340,53 +383,56 @@ class Client {
|
|
|
340
383
|
async getAllBySomeTags(tags, params) {
|
|
341
384
|
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
|
|
342
385
|
}
|
|
343
|
-
async getRepository() {
|
|
386
|
+
async getRepository(params) {
|
|
344
387
|
const url = new URL(this.endpoint);
|
|
345
388
|
if (this.accessToken) {
|
|
346
389
|
url.searchParams.set("access_token", this.accessToken);
|
|
347
390
|
}
|
|
348
|
-
return await this.fetch(url.toString());
|
|
391
|
+
return await this.fetch(url.toString(), params);
|
|
349
392
|
}
|
|
350
|
-
async getRefs() {
|
|
351
|
-
const repository = await this.getRepository();
|
|
393
|
+
async getRefs(params) {
|
|
394
|
+
const repository = await this.getRepository(params);
|
|
352
395
|
return repository.refs;
|
|
353
396
|
}
|
|
354
|
-
async getRefByID(id) {
|
|
355
|
-
const refs = await this.getRefs();
|
|
397
|
+
async getRefByID(id, params) {
|
|
398
|
+
const refs = await this.getRefs(params);
|
|
356
399
|
return findRefByID(refs, id);
|
|
357
400
|
}
|
|
358
|
-
async getRefByLabel(label) {
|
|
359
|
-
const refs = await this.getRefs();
|
|
401
|
+
async getRefByLabel(label, params) {
|
|
402
|
+
const refs = await this.getRefs(params);
|
|
360
403
|
return findRefByLabel(refs, label);
|
|
361
404
|
}
|
|
362
|
-
async getMasterRef() {
|
|
363
|
-
const refs = await this.getRefs();
|
|
405
|
+
async getMasterRef(params) {
|
|
406
|
+
const refs = await this.getRefs(params);
|
|
364
407
|
return findMasterRef(refs);
|
|
365
408
|
}
|
|
366
|
-
async getReleases() {
|
|
367
|
-
const refs = await this.getRefs();
|
|
409
|
+
async getReleases(params) {
|
|
410
|
+
const refs = await this.getRefs(params);
|
|
368
411
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
369
412
|
}
|
|
370
|
-
async getReleaseByID(id) {
|
|
371
|
-
const releases = await this.getReleases();
|
|
413
|
+
async getReleaseByID(id, params) {
|
|
414
|
+
const releases = await this.getReleases(params);
|
|
372
415
|
return findRefByID(releases, id);
|
|
373
416
|
}
|
|
374
|
-
async getReleaseByLabel(label) {
|
|
375
|
-
const releases = await this.getReleases();
|
|
417
|
+
async getReleaseByLabel(label, params) {
|
|
418
|
+
const releases = await this.getReleases(params);
|
|
376
419
|
return findRefByLabel(releases, label);
|
|
377
420
|
}
|
|
378
|
-
async getTags() {
|
|
421
|
+
async getTags(params) {
|
|
379
422
|
try {
|
|
380
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
423
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
381
424
|
return await this.fetch(tagsForm.action);
|
|
382
425
|
} catch (e) {
|
|
383
|
-
const repository = await this.getRepository();
|
|
426
|
+
const repository = await this.getRepository(params);
|
|
384
427
|
return repository.tags;
|
|
385
428
|
}
|
|
386
429
|
}
|
|
387
|
-
async buildQueryURL(
|
|
430
|
+
async buildQueryURL({
|
|
431
|
+
signal,
|
|
432
|
+
...params
|
|
433
|
+
} = {}) {
|
|
388
434
|
const ref = params.ref || await this.getResolvedRefString();
|
|
389
|
-
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
|
|
435
|
+
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0;
|
|
390
436
|
return buildQueryURL(this.endpoint, {
|
|
391
437
|
...this.defaultParams,
|
|
392
438
|
...params,
|
|
@@ -408,15 +454,18 @@ class Client {
|
|
|
408
454
|
documentID = documentID || this.refState.httpRequest.query.documentId;
|
|
409
455
|
previewToken = previewToken || this.refState.httpRequest.query.token;
|
|
410
456
|
}
|
|
411
|
-
if (documentID != null) {
|
|
457
|
+
if (documentID != null && previewToken != null) {
|
|
412
458
|
const document = await this.getByID(documentID, {
|
|
459
|
+
signal: args.signal,
|
|
413
460
|
ref: previewToken,
|
|
414
461
|
lang: "*"
|
|
415
462
|
});
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
463
|
+
const url = prismicH__namespace.asLink(document, args.linkResolver);
|
|
464
|
+
if (typeof url === "string") {
|
|
465
|
+
return url;
|
|
466
|
+
}
|
|
419
467
|
}
|
|
468
|
+
return args.defaultURL;
|
|
420
469
|
}
|
|
421
470
|
queryLatestContent() {
|
|
422
471
|
this.refState.mode = "Master" /* Master */;
|
|
@@ -442,7 +491,7 @@ class Client {
|
|
|
442
491
|
ref
|
|
443
492
|
};
|
|
444
493
|
}
|
|
445
|
-
async
|
|
494
|
+
async graphQLFetch(input, init) {
|
|
446
495
|
const cachedRepository = await this.getCachedRepository();
|
|
447
496
|
const ref = await this.getResolvedRefString();
|
|
448
497
|
const unsanitizedHeaders = {
|
|
@@ -457,27 +506,32 @@ class Client {
|
|
|
457
506
|
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
458
507
|
}
|
|
459
508
|
}
|
|
460
|
-
|
|
509
|
+
const url = new URL(input);
|
|
510
|
+
const query = url.searchParams.get("query");
|
|
511
|
+
if (query) {
|
|
512
|
+
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
513
|
+
}
|
|
514
|
+
return await this.fetchFn(url.toString(), {
|
|
461
515
|
...init,
|
|
462
516
|
headers
|
|
463
517
|
});
|
|
464
518
|
}
|
|
465
|
-
async getCachedRepository() {
|
|
519
|
+
async getCachedRepository(params) {
|
|
466
520
|
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
|
|
467
521
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
468
|
-
this.cachedRepository = await this.getRepository();
|
|
522
|
+
this.cachedRepository = await this.getRepository(params);
|
|
469
523
|
}
|
|
470
524
|
return this.cachedRepository;
|
|
471
525
|
}
|
|
472
|
-
async getCachedRepositoryForm(name) {
|
|
473
|
-
const cachedRepository = await this.getCachedRepository();
|
|
526
|
+
async getCachedRepositoryForm(name, params) {
|
|
527
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
474
528
|
const form = cachedRepository.forms[name];
|
|
475
529
|
if (!form) {
|
|
476
530
|
throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
|
|
477
531
|
}
|
|
478
532
|
return form;
|
|
479
533
|
}
|
|
480
|
-
async getResolvedRefString() {
|
|
534
|
+
async getResolvedRefString(params) {
|
|
481
535
|
var _a, _b, _c;
|
|
482
536
|
if (this.refState.autoPreviewsEnabled) {
|
|
483
537
|
let previewRef = void 0;
|
|
@@ -490,7 +544,7 @@ class Client {
|
|
|
490
544
|
return previewRef;
|
|
491
545
|
}
|
|
492
546
|
}
|
|
493
|
-
const cachedRepository = await this.getCachedRepository();
|
|
547
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
494
548
|
const refModeType = this.refState.mode;
|
|
495
549
|
if (refModeType === "ReleaseID" /* ReleaseID */) {
|
|
496
550
|
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
|
|
@@ -504,8 +558,10 @@ class Client {
|
|
|
504
558
|
}
|
|
505
559
|
return findMasterRef(cachedRepository.refs).ref;
|
|
506
560
|
}
|
|
507
|
-
async fetch(url,
|
|
508
|
-
const res = await this.fetchFn(url
|
|
561
|
+
async fetch(url, params = {}) {
|
|
562
|
+
const res = await this.fetchFn(url, {
|
|
563
|
+
signal: params.signal
|
|
564
|
+
});
|
|
509
565
|
let json;
|
|
510
566
|
try {
|
|
511
567
|
json = await res.json();
|
|
@@ -532,6 +588,7 @@ class Client {
|
|
|
532
588
|
}
|
|
533
589
|
}
|
|
534
590
|
|
|
591
|
+
const getEndpoint = getRepositoryEndpoint;
|
|
535
592
|
const predicates = predicate;
|
|
536
593
|
const Predicates = predicate;
|
|
537
594
|
|
|
@@ -546,6 +603,10 @@ exports.cookie = cookie;
|
|
|
546
603
|
exports.createClient = createClient;
|
|
547
604
|
exports.getEndpoint = getEndpoint;
|
|
548
605
|
exports.getGraphQLEndpoint = getGraphQLEndpoint;
|
|
606
|
+
exports.getRepositoryEndpoint = getRepositoryEndpoint;
|
|
607
|
+
exports.getRepositoryName = getRepositoryName;
|
|
608
|
+
exports.isRepositoryEndpoint = isRepositoryEndpoint;
|
|
609
|
+
exports.isRepositoryName = isRepositoryName;
|
|
549
610
|
exports.predicate = predicate;
|
|
550
611
|
exports.predicates = predicates;
|
|
551
612
|
//# sourceMappingURL=index.cjs.map
|