@prismicio/client 6.0.0-beta.5 → 6.2.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 CHANGED
@@ -72,7 +72,7 @@ For more clarity on this project and its structure you can also check out the de
72
72
  [prismic]: https://prismic.io
73
73
  [prismic-predicates]: https://prismic.io/docs/technologies/rest-api-technical-reference
74
74
  [prismic-previews]: https://prismic.io/docs/core-concepts/previews
75
- [prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client?version=v6
75
+ [prismic-docs]: https://prismic.io/docs/technical-reference/prismicio-client
76
76
  [changelog]: ./CHANGELOG.md
77
77
  [contributing]: ./CONTRIBUTING.md
78
78
  [forum-question]: https://community.prismic.io/c/kits-and-dev-languages/javascript/14
package/dist/index.cjs CHANGED
@@ -26,6 +26,8 @@ const prismicH__namespace = /*#__PURE__*/_interopNamespace(prismicH);
26
26
 
27
27
  const getEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/api/v2`;
28
28
 
29
+ const getGraphQLEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/graphql`;
30
+
29
31
  const castArray = (a) => Array.isArray(a) ? a : [a];
30
32
 
31
33
  const RENAMED_PARAMS = {
@@ -122,9 +124,23 @@ const parse = (cookieString) => {
122
124
  const getAll = (cookieStore) => parse(cookieStore);
123
125
  const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
124
126
 
127
+ const minifyGraphQLQuery = (query) => {
128
+ return query.replace(/(\n| )*( |{|})(\n| )*/gm, (_chars, _spaces, brackets) => brackets);
129
+ };
130
+
131
+ const preview = "io.prismic.preview";
132
+
133
+ const cookie = /*#__PURE__*/Object.freeze({
134
+ __proto__: null,
135
+ preview: preview
136
+ });
137
+
125
138
  class ForbiddenError extends PrismicError {
126
139
  }
127
140
 
141
+ class NotFoundError extends PrismicError {
142
+ }
143
+
128
144
  class ParsingError extends PrismicError {
129
145
  }
130
146
 
@@ -193,34 +209,17 @@ const predicate = {
193
209
  dateHourBefore: pathWithArgsPredicate("date.hour-before")
194
210
  };
195
211
 
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
212
  const MAX_PAGE_SIZE = 100;
207
213
  const REPOSITORY_CACHE_TTL = 5e3;
208
214
  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
215
  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);
216
+ const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags));
217
+ const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
219
218
  const createClient = (...args) => new Client(...args);
220
219
  class Client {
221
220
  constructor(endpoint, options = {}) {
222
221
  this.refState = {
223
- mode: RefStateMode.Master,
222
+ mode: "Master" /* Master */,
224
223
  autoPreviewsEnabled: true
225
224
  };
226
225
  this.cachedRepositoryExpiration = 0;
@@ -244,6 +243,7 @@ class Client {
244
243
  if (this.fetchFn === globalThis.fetch) {
245
244
  this.fetchFn = this.fetchFn.bind(globalThis);
246
245
  }
246
+ this.graphqlFetch = this.graphqlFetch.bind(this);
247
247
  }
248
248
  enableAutoPreviews() {
249
249
  this.refState.autoPreviewsEnabled = true;
@@ -327,10 +327,10 @@ class Client {
327
327
  return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
328
328
  }
329
329
  async getByTag(tag, params) {
330
- return await this.get(appendPredicates(params, everyTagPredicate(tag)));
330
+ return await this.get(appendPredicates(params, someTagsPredicate(tag)));
331
331
  }
332
332
  async getAllByTag(tag, params) {
333
- return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tag)));
333
+ return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
334
334
  }
335
335
  async getByEveryTag(tags, params) {
336
336
  return await this.get(appendPredicates(params, everyTagPredicate(tags)));
@@ -344,53 +344,56 @@ class Client {
344
344
  async getAllBySomeTags(tags, params) {
345
345
  return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
346
346
  }
347
- async getRepository() {
347
+ async getRepository(params) {
348
348
  const url = new URL(this.endpoint);
349
349
  if (this.accessToken) {
350
350
  url.searchParams.set("access_token", this.accessToken);
351
351
  }
352
- return await this.fetch(url.toString());
352
+ return await this.fetch(url.toString(), params);
353
353
  }
354
- async getRefs() {
355
- const repository = await this.getRepository();
354
+ async getRefs(params) {
355
+ const repository = await this.getRepository(params);
356
356
  return repository.refs;
357
357
  }
358
- async getRefByID(id) {
359
- const refs = await this.getRefs();
358
+ async getRefByID(id, params) {
359
+ const refs = await this.getRefs(params);
360
360
  return findRefByID(refs, id);
361
361
  }
362
- async getRefByLabel(label) {
363
- const refs = await this.getRefs();
362
+ async getRefByLabel(label, params) {
363
+ const refs = await this.getRefs(params);
364
364
  return findRefByLabel(refs, label);
365
365
  }
366
- async getMasterRef() {
367
- const refs = await this.getRefs();
366
+ async getMasterRef(params) {
367
+ const refs = await this.getRefs(params);
368
368
  return findMasterRef(refs);
369
369
  }
370
- async getReleases() {
371
- const refs = await this.getRefs();
370
+ async getReleases(params) {
371
+ const refs = await this.getRefs(params);
372
372
  return refs.filter((ref) => !ref.isMasterRef);
373
373
  }
374
- async getReleaseByID(id) {
375
- const releases = await this.getReleases();
374
+ async getReleaseByID(id, params) {
375
+ const releases = await this.getReleases(params);
376
376
  return findRefByID(releases, id);
377
377
  }
378
- async getReleaseByLabel(label) {
379
- const releases = await this.getReleases();
378
+ async getReleaseByLabel(label, params) {
379
+ const releases = await this.getReleases(params);
380
380
  return findRefByLabel(releases, label);
381
381
  }
382
- async getTags() {
382
+ async getTags(params) {
383
383
  try {
384
- const tagsForm = await this.getCachedRepositoryForm("tags");
384
+ const tagsForm = await this.getCachedRepositoryForm("tags", params);
385
385
  return await this.fetch(tagsForm.action);
386
386
  } catch (e) {
387
- const repository = await this.getRepository();
387
+ const repository = await this.getRepository(params);
388
388
  return repository.tags;
389
389
  }
390
390
  }
391
- async buildQueryURL(params = {}) {
391
+ async buildQueryURL({
392
+ signal,
393
+ ...params
394
+ } = {}) {
392
395
  const ref = params.ref || await this.getResolvedRefString();
393
- const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
396
+ const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0;
394
397
  return buildQueryURL(this.endpoint, {
395
398
  ...this.defaultParams,
396
399
  ...params,
@@ -414,7 +417,9 @@ class Client {
414
417
  }
415
418
  if (documentID != null) {
416
419
  const document = await this.getByID(documentID, {
417
- ref: previewToken
420
+ signal: args.signal,
421
+ ref: previewToken,
422
+ lang: "*"
418
423
  });
419
424
  return prismicH__namespace.asLink(document, args.linkResolver);
420
425
  } else {
@@ -422,45 +427,70 @@ class Client {
422
427
  }
423
428
  }
424
429
  queryLatestContent() {
425
- this.refState.mode = RefStateMode.Master;
430
+ this.refState.mode = "Master" /* Master */;
426
431
  }
427
432
  queryContentFromReleaseByID(releaseID) {
428
433
  this.refState = {
429
434
  ...this.refState,
430
- mode: RefStateMode.ReleaseID,
435
+ mode: "ReleaseID" /* ReleaseID */,
431
436
  releaseID
432
437
  };
433
438
  }
434
439
  queryContentFromReleaseByLabel(releaseLabel) {
435
440
  this.refState = {
436
441
  ...this.refState,
437
- mode: RefStateMode.ReleaseLabel,
442
+ mode: "ReleaseLabel" /* ReleaseLabel */,
438
443
  releaseLabel
439
444
  };
440
445
  }
441
446
  queryContentFromRef(ref) {
442
447
  this.refState = {
443
448
  ...this.refState,
444
- mode: RefStateMode.Manual,
449
+ mode: "Manual" /* Manual */,
445
450
  ref
446
451
  };
447
452
  }
448
- async getCachedRepository() {
453
+ async graphqlFetch(input, init) {
454
+ const cachedRepository = await this.getCachedRepository();
455
+ const ref = await this.getResolvedRefString();
456
+ const unsanitizedHeaders = {
457
+ "Prismic-ref": ref,
458
+ "Prismic-integration-field-ref": cachedRepository.integrationFieldsRef || "",
459
+ Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
460
+ ...init ? init.headers : {}
461
+ };
462
+ const headers = {};
463
+ for (const key in unsanitizedHeaders) {
464
+ if (unsanitizedHeaders[key]) {
465
+ headers[key.toLowerCase()] = unsanitizedHeaders[key];
466
+ }
467
+ }
468
+ const url = new URL(input);
469
+ const query = url.searchParams.get("query");
470
+ if (query) {
471
+ url.searchParams.set("query", minifyGraphQLQuery(query));
472
+ }
473
+ return await this.fetchFn(url.toString(), {
474
+ ...init,
475
+ headers
476
+ });
477
+ }
478
+ async getCachedRepository(params) {
449
479
  if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
450
480
  this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
451
- this.cachedRepository = await this.getRepository();
481
+ this.cachedRepository = await this.getRepository(params);
452
482
  }
453
483
  return this.cachedRepository;
454
484
  }
455
- async getCachedRepositoryForm(name) {
456
- const cachedRepository = await this.getCachedRepository();
485
+ async getCachedRepositoryForm(name, params) {
486
+ const cachedRepository = await this.getCachedRepository(params);
457
487
  const form = cachedRepository.forms[name];
458
488
  if (!form) {
459
489
  throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
460
490
  }
461
491
  return form;
462
492
  }
463
- async getResolvedRefString() {
493
+ async getResolvedRefString(params) {
464
494
  var _a, _b, _c;
465
495
  if (this.refState.autoPreviewsEnabled) {
466
496
  let previewRef = void 0;
@@ -473,13 +503,13 @@ class Client {
473
503
  return previewRef;
474
504
  }
475
505
  }
476
- const cachedRepository = await this.getCachedRepository();
506
+ const cachedRepository = await this.getCachedRepository(params);
477
507
  const refModeType = this.refState.mode;
478
- if (refModeType === RefStateMode.ReleaseID) {
508
+ if (refModeType === "ReleaseID" /* ReleaseID */) {
479
509
  return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
480
- } else if (refModeType === RefStateMode.ReleaseLabel) {
510
+ } else if (refModeType === "ReleaseLabel" /* ReleaseLabel */) {
481
511
  return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel).ref;
482
- } else if (refModeType === RefStateMode.Manual) {
512
+ } else if (refModeType === "Manual" /* Manual */) {
483
513
  const res = await castThunk(this.refState.ref)();
484
514
  if (typeof res === "string") {
485
515
  return res;
@@ -487,8 +517,10 @@ class Client {
487
517
  }
488
518
  return findMasterRef(cachedRepository.refs).ref;
489
519
  }
490
- async fetch(url, _params) {
491
- const res = await this.fetchFn(url);
520
+ async fetch(url, params = {}) {
521
+ const res = await this.fetchFn(url, {
522
+ signal: params.signal
523
+ });
492
524
  let json;
493
525
  try {
494
526
  json = await res.json();
@@ -528,6 +560,7 @@ exports.buildQueryURL = buildQueryURL;
528
560
  exports.cookie = cookie;
529
561
  exports.createClient = createClient;
530
562
  exports.getEndpoint = getEndpoint;
563
+ exports.getGraphQLEndpoint = getGraphQLEndpoint;
531
564
  exports.predicate = predicate;
532
565
  exports.predicates = predicates;
533
566
  //# sourceMappingURL=index.cjs.map