@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/dist/index.js
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
import * as prismicH from '@prismicio/helpers';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const isRepositoryName = (input) => {
|
|
4
|
+
return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
class PrismicError extends Error {
|
|
8
|
+
constructor(message = "An invalid API response was returned", url, response) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.url = url;
|
|
11
|
+
this.response = response;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const getRepositoryEndpoint = (repositoryName) => {
|
|
16
|
+
if (isRepositoryName(repositoryName)) {
|
|
17
|
+
return `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
18
|
+
} else {
|
|
19
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getRepositoryName = (repositoryEndpoint) => {
|
|
24
|
+
try {
|
|
25
|
+
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const getGraphQLEndpoint = (repositoryName) => {
|
|
32
|
+
if (isRepositoryName(repositoryName)) {
|
|
33
|
+
return `https://${repositoryName}.cdn.prismic.io/graphql`;
|
|
34
|
+
} else {
|
|
35
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const isRepositoryEndpoint = (input) => {
|
|
40
|
+
try {
|
|
41
|
+
new URL(input);
|
|
42
|
+
return true;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
4
47
|
|
|
5
48
|
const castArray = (a) => Array.isArray(a) ? a : [a];
|
|
6
49
|
|
|
@@ -53,14 +96,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
|
|
|
53
96
|
|
|
54
97
|
const castThunk = (a) => typeof a === "function" ? a : () => a;
|
|
55
98
|
|
|
56
|
-
class PrismicError extends Error {
|
|
57
|
-
constructor(message = "An invalid API response was returned", url, response) {
|
|
58
|
-
super(message);
|
|
59
|
-
this.url = url;
|
|
60
|
-
this.response = response;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
99
|
const findRef = (refs, predicate) => {
|
|
65
100
|
const ref = refs.find((ref2) => predicate(ref2));
|
|
66
101
|
if (!ref) {
|
|
@@ -98,9 +133,23 @@ const parse = (cookieString) => {
|
|
|
98
133
|
const getAll = (cookieStore) => parse(cookieStore);
|
|
99
134
|
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
100
135
|
|
|
136
|
+
const minifyGraphQLQuery = (query) => {
|
|
137
|
+
return query.replace(/(\n| )*( |{|})(\n| )*/gm, (_chars, _spaces, brackets) => brackets);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const preview = "io.prismic.preview";
|
|
141
|
+
|
|
142
|
+
var cookie = /*#__PURE__*/Object.freeze({
|
|
143
|
+
__proto__: null,
|
|
144
|
+
preview: preview
|
|
145
|
+
});
|
|
146
|
+
|
|
101
147
|
class ForbiddenError extends PrismicError {
|
|
102
148
|
}
|
|
103
149
|
|
|
150
|
+
class NotFoundError extends PrismicError {
|
|
151
|
+
}
|
|
152
|
+
|
|
104
153
|
class ParsingError extends PrismicError {
|
|
105
154
|
}
|
|
106
155
|
|
|
@@ -169,41 +218,28 @@ const predicate = {
|
|
|
169
218
|
dateHourBefore: pathWithArgsPredicate("date.hour-before")
|
|
170
219
|
};
|
|
171
220
|
|
|
172
|
-
const preview = "io.prismic.preview";
|
|
173
|
-
|
|
174
|
-
var cookie = /*#__PURE__*/Object.freeze({
|
|
175
|
-
__proto__: null,
|
|
176
|
-
preview: preview
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
class NotFoundError extends PrismicError {
|
|
180
|
-
}
|
|
181
|
-
|
|
182
221
|
const MAX_PAGE_SIZE = 100;
|
|
183
222
|
const REPOSITORY_CACHE_TTL = 5e3;
|
|
184
223
|
const GET_ALL_QUERY_DELAY = 500;
|
|
185
|
-
var RefStateMode;
|
|
186
|
-
(function(RefStateMode2) {
|
|
187
|
-
RefStateMode2["Master"] = "Master";
|
|
188
|
-
RefStateMode2["ReleaseID"] = "ReleaseID";
|
|
189
|
-
RefStateMode2["ReleaseLabel"] = "ReleaseLabel";
|
|
190
|
-
RefStateMode2["Manual"] = "Manual";
|
|
191
|
-
})(RefStateMode || (RefStateMode = {}));
|
|
192
224
|
const typePredicate = (documentType) => predicate.at("document.type", documentType);
|
|
193
|
-
const everyTagPredicate = (tags) => predicate.at("document.tags", tags);
|
|
194
|
-
const someTagsPredicate = (tags) => predicate.any("document.tags", tags);
|
|
225
|
+
const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags));
|
|
226
|
+
const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
|
|
195
227
|
const createClient = (...args) => new Client(...args);
|
|
196
228
|
class Client {
|
|
197
|
-
constructor(
|
|
229
|
+
constructor(repositoryNameOrEndpoint, options = {}) {
|
|
198
230
|
this.refState = {
|
|
199
|
-
mode:
|
|
231
|
+
mode: "Master" /* Master */,
|
|
200
232
|
autoPreviewsEnabled: true
|
|
201
233
|
};
|
|
202
234
|
this.cachedRepositoryExpiration = 0;
|
|
203
|
-
if (
|
|
204
|
-
|
|
235
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
236
|
+
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
237
|
+
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);
|
|
238
|
+
}
|
|
239
|
+
this.endpoint = repositoryNameOrEndpoint;
|
|
240
|
+
} else {
|
|
241
|
+
this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
|
|
205
242
|
}
|
|
206
|
-
this.endpoint = endpoint;
|
|
207
243
|
this.accessToken = options.accessToken;
|
|
208
244
|
this.routes = options.routes;
|
|
209
245
|
this.defaultParams = options.defaultParams;
|
|
@@ -220,6 +256,7 @@ class Client {
|
|
|
220
256
|
if (this.fetchFn === globalThis.fetch) {
|
|
221
257
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
222
258
|
}
|
|
259
|
+
this.graphqlFetch = this.graphqlFetch.bind(this);
|
|
223
260
|
}
|
|
224
261
|
enableAutoPreviews() {
|
|
225
262
|
this.refState.autoPreviewsEnabled = true;
|
|
@@ -303,10 +340,10 @@ class Client {
|
|
|
303
340
|
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
304
341
|
}
|
|
305
342
|
async getByTag(tag, params) {
|
|
306
|
-
return await this.get(appendPredicates(params,
|
|
343
|
+
return await this.get(appendPredicates(params, someTagsPredicate(tag)));
|
|
307
344
|
}
|
|
308
345
|
async getAllByTag(tag, params) {
|
|
309
|
-
return await this.dangerouslyGetAll(appendPredicates(params,
|
|
346
|
+
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
|
|
310
347
|
}
|
|
311
348
|
async getByEveryTag(tags, params) {
|
|
312
349
|
return await this.get(appendPredicates(params, everyTagPredicate(tags)));
|
|
@@ -320,53 +357,56 @@ class Client {
|
|
|
320
357
|
async getAllBySomeTags(tags, params) {
|
|
321
358
|
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
|
|
322
359
|
}
|
|
323
|
-
async getRepository() {
|
|
360
|
+
async getRepository(params) {
|
|
324
361
|
const url = new URL(this.endpoint);
|
|
325
362
|
if (this.accessToken) {
|
|
326
363
|
url.searchParams.set("access_token", this.accessToken);
|
|
327
364
|
}
|
|
328
|
-
return await this.fetch(url.toString());
|
|
365
|
+
return await this.fetch(url.toString(), params);
|
|
329
366
|
}
|
|
330
|
-
async getRefs() {
|
|
331
|
-
const repository = await this.getRepository();
|
|
367
|
+
async getRefs(params) {
|
|
368
|
+
const repository = await this.getRepository(params);
|
|
332
369
|
return repository.refs;
|
|
333
370
|
}
|
|
334
|
-
async getRefByID(id) {
|
|
335
|
-
const refs = await this.getRefs();
|
|
371
|
+
async getRefByID(id, params) {
|
|
372
|
+
const refs = await this.getRefs(params);
|
|
336
373
|
return findRefByID(refs, id);
|
|
337
374
|
}
|
|
338
|
-
async getRefByLabel(label) {
|
|
339
|
-
const refs = await this.getRefs();
|
|
375
|
+
async getRefByLabel(label, params) {
|
|
376
|
+
const refs = await this.getRefs(params);
|
|
340
377
|
return findRefByLabel(refs, label);
|
|
341
378
|
}
|
|
342
|
-
async getMasterRef() {
|
|
343
|
-
const refs = await this.getRefs();
|
|
379
|
+
async getMasterRef(params) {
|
|
380
|
+
const refs = await this.getRefs(params);
|
|
344
381
|
return findMasterRef(refs);
|
|
345
382
|
}
|
|
346
|
-
async getReleases() {
|
|
347
|
-
const refs = await this.getRefs();
|
|
383
|
+
async getReleases(params) {
|
|
384
|
+
const refs = await this.getRefs(params);
|
|
348
385
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
349
386
|
}
|
|
350
|
-
async getReleaseByID(id) {
|
|
351
|
-
const releases = await this.getReleases();
|
|
387
|
+
async getReleaseByID(id, params) {
|
|
388
|
+
const releases = await this.getReleases(params);
|
|
352
389
|
return findRefByID(releases, id);
|
|
353
390
|
}
|
|
354
|
-
async getReleaseByLabel(label) {
|
|
355
|
-
const releases = await this.getReleases();
|
|
391
|
+
async getReleaseByLabel(label, params) {
|
|
392
|
+
const releases = await this.getReleases(params);
|
|
356
393
|
return findRefByLabel(releases, label);
|
|
357
394
|
}
|
|
358
|
-
async getTags() {
|
|
395
|
+
async getTags(params) {
|
|
359
396
|
try {
|
|
360
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
397
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
361
398
|
return await this.fetch(tagsForm.action);
|
|
362
399
|
} catch (e) {
|
|
363
|
-
const repository = await this.getRepository();
|
|
400
|
+
const repository = await this.getRepository(params);
|
|
364
401
|
return repository.tags;
|
|
365
402
|
}
|
|
366
403
|
}
|
|
367
|
-
async buildQueryURL(
|
|
404
|
+
async buildQueryURL({
|
|
405
|
+
signal,
|
|
406
|
+
...params
|
|
407
|
+
} = {}) {
|
|
368
408
|
const ref = params.ref || await this.getResolvedRefString();
|
|
369
|
-
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
|
|
409
|
+
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0;
|
|
370
410
|
return buildQueryURL(this.endpoint, {
|
|
371
411
|
...this.defaultParams,
|
|
372
412
|
...params,
|
|
@@ -390,7 +430,9 @@ class Client {
|
|
|
390
430
|
}
|
|
391
431
|
if (documentID != null) {
|
|
392
432
|
const document = await this.getByID(documentID, {
|
|
393
|
-
|
|
433
|
+
signal: args.signal,
|
|
434
|
+
ref: previewToken,
|
|
435
|
+
lang: "*"
|
|
394
436
|
});
|
|
395
437
|
return prismicH.asLink(document, args.linkResolver);
|
|
396
438
|
} else {
|
|
@@ -398,45 +440,70 @@ class Client {
|
|
|
398
440
|
}
|
|
399
441
|
}
|
|
400
442
|
queryLatestContent() {
|
|
401
|
-
this.refState.mode =
|
|
443
|
+
this.refState.mode = "Master" /* Master */;
|
|
402
444
|
}
|
|
403
445
|
queryContentFromReleaseByID(releaseID) {
|
|
404
446
|
this.refState = {
|
|
405
447
|
...this.refState,
|
|
406
|
-
mode:
|
|
448
|
+
mode: "ReleaseID" /* ReleaseID */,
|
|
407
449
|
releaseID
|
|
408
450
|
};
|
|
409
451
|
}
|
|
410
452
|
queryContentFromReleaseByLabel(releaseLabel) {
|
|
411
453
|
this.refState = {
|
|
412
454
|
...this.refState,
|
|
413
|
-
mode:
|
|
455
|
+
mode: "ReleaseLabel" /* ReleaseLabel */,
|
|
414
456
|
releaseLabel
|
|
415
457
|
};
|
|
416
458
|
}
|
|
417
459
|
queryContentFromRef(ref) {
|
|
418
460
|
this.refState = {
|
|
419
461
|
...this.refState,
|
|
420
|
-
mode:
|
|
462
|
+
mode: "Manual" /* Manual */,
|
|
421
463
|
ref
|
|
422
464
|
};
|
|
423
465
|
}
|
|
424
|
-
async
|
|
466
|
+
async graphqlFetch(input, init) {
|
|
467
|
+
const cachedRepository = await this.getCachedRepository();
|
|
468
|
+
const ref = await this.getResolvedRefString();
|
|
469
|
+
const unsanitizedHeaders = {
|
|
470
|
+
"Prismic-ref": ref,
|
|
471
|
+
"Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
|
|
472
|
+
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
473
|
+
...init ? init.headers : {}
|
|
474
|
+
};
|
|
475
|
+
const headers = {};
|
|
476
|
+
for (const key in unsanitizedHeaders) {
|
|
477
|
+
if (unsanitizedHeaders[key]) {
|
|
478
|
+
headers[key.toLowerCase()] = unsanitizedHeaders[key];
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const url = new URL(input);
|
|
482
|
+
const query = url.searchParams.get("query");
|
|
483
|
+
if (query) {
|
|
484
|
+
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
485
|
+
}
|
|
486
|
+
return await this.fetchFn(url.toString(), {
|
|
487
|
+
...init,
|
|
488
|
+
headers
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
async getCachedRepository(params) {
|
|
425
492
|
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
|
|
426
493
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
427
|
-
this.cachedRepository = await this.getRepository();
|
|
494
|
+
this.cachedRepository = await this.getRepository(params);
|
|
428
495
|
}
|
|
429
496
|
return this.cachedRepository;
|
|
430
497
|
}
|
|
431
|
-
async getCachedRepositoryForm(name) {
|
|
432
|
-
const cachedRepository = await this.getCachedRepository();
|
|
498
|
+
async getCachedRepositoryForm(name, params) {
|
|
499
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
433
500
|
const form = cachedRepository.forms[name];
|
|
434
501
|
if (!form) {
|
|
435
502
|
throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
|
|
436
503
|
}
|
|
437
504
|
return form;
|
|
438
505
|
}
|
|
439
|
-
async getResolvedRefString() {
|
|
506
|
+
async getResolvedRefString(params) {
|
|
440
507
|
var _a, _b, _c;
|
|
441
508
|
if (this.refState.autoPreviewsEnabled) {
|
|
442
509
|
let previewRef = void 0;
|
|
@@ -449,13 +516,13 @@ class Client {
|
|
|
449
516
|
return previewRef;
|
|
450
517
|
}
|
|
451
518
|
}
|
|
452
|
-
const cachedRepository = await this.getCachedRepository();
|
|
519
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
453
520
|
const refModeType = this.refState.mode;
|
|
454
|
-
if (refModeType ===
|
|
521
|
+
if (refModeType === "ReleaseID" /* ReleaseID */) {
|
|
455
522
|
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
|
|
456
|
-
} else if (refModeType ===
|
|
523
|
+
} else if (refModeType === "ReleaseLabel" /* ReleaseLabel */) {
|
|
457
524
|
return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel).ref;
|
|
458
|
-
} else if (refModeType ===
|
|
525
|
+
} else if (refModeType === "Manual" /* Manual */) {
|
|
459
526
|
const res = await castThunk(this.refState.ref)();
|
|
460
527
|
if (typeof res === "string") {
|
|
461
528
|
return res;
|
|
@@ -463,8 +530,10 @@ class Client {
|
|
|
463
530
|
}
|
|
464
531
|
return findMasterRef(cachedRepository.refs).ref;
|
|
465
532
|
}
|
|
466
|
-
async fetch(url,
|
|
467
|
-
const res = await this.fetchFn(url
|
|
533
|
+
async fetch(url, params = {}) {
|
|
534
|
+
const res = await this.fetchFn(url, {
|
|
535
|
+
signal: params.signal
|
|
536
|
+
});
|
|
468
537
|
let json;
|
|
469
538
|
try {
|
|
470
539
|
json = await res.json();
|
|
@@ -491,8 +560,9 @@ class Client {
|
|
|
491
560
|
}
|
|
492
561
|
}
|
|
493
562
|
|
|
563
|
+
const getEndpoint = getRepositoryEndpoint;
|
|
494
564
|
const predicates = predicate;
|
|
495
565
|
const Predicates = predicate;
|
|
496
566
|
|
|
497
|
-
export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, predicate, predicates };
|
|
567
|
+
export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
|
|
498
568
|
//# sourceMappingURL=index.js.map
|