@prismicio/client 6.1.1 → 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
@@ -124,9 +124,23 @@ const parse = (cookieString) => {
124
124
  const getAll = (cookieStore) => parse(cookieStore);
125
125
  const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
126
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
+
127
138
  class ForbiddenError extends PrismicError {
128
139
  }
129
140
 
141
+ class NotFoundError extends PrismicError {
142
+ }
143
+
130
144
  class ParsingError extends PrismicError {
131
145
  }
132
146
 
@@ -195,22 +209,12 @@ const predicate = {
195
209
  dateHourBefore: pathWithArgsPredicate("date.hour-before")
196
210
  };
197
211
 
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
212
  const MAX_PAGE_SIZE = 100;
209
213
  const REPOSITORY_CACHE_TTL = 5e3;
210
214
  const GET_ALL_QUERY_DELAY = 500;
211
215
  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);
216
+ const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags));
217
+ const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
214
218
  const createClient = (...args) => new Client(...args);
215
219
  class Client {
216
220
  constructor(endpoint, options = {}) {
@@ -323,10 +327,10 @@ class Client {
323
327
  return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
324
328
  }
325
329
  async getByTag(tag, params) {
326
- return await this.get(appendPredicates(params, everyTagPredicate(tag)));
330
+ return await this.get(appendPredicates(params, someTagsPredicate(tag)));
327
331
  }
328
332
  async getAllByTag(tag, params) {
329
- return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tag)));
333
+ return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tag)));
330
334
  }
331
335
  async getByEveryTag(tags, params) {
332
336
  return await this.get(appendPredicates(params, everyTagPredicate(tags)));
@@ -340,53 +344,56 @@ class Client {
340
344
  async getAllBySomeTags(tags, params) {
341
345
  return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
342
346
  }
343
- async getRepository() {
347
+ async getRepository(params) {
344
348
  const url = new URL(this.endpoint);
345
349
  if (this.accessToken) {
346
350
  url.searchParams.set("access_token", this.accessToken);
347
351
  }
348
- return await this.fetch(url.toString());
352
+ return await this.fetch(url.toString(), params);
349
353
  }
350
- async getRefs() {
351
- const repository = await this.getRepository();
354
+ async getRefs(params) {
355
+ const repository = await this.getRepository(params);
352
356
  return repository.refs;
353
357
  }
354
- async getRefByID(id) {
355
- const refs = await this.getRefs();
358
+ async getRefByID(id, params) {
359
+ const refs = await this.getRefs(params);
356
360
  return findRefByID(refs, id);
357
361
  }
358
- async getRefByLabel(label) {
359
- const refs = await this.getRefs();
362
+ async getRefByLabel(label, params) {
363
+ const refs = await this.getRefs(params);
360
364
  return findRefByLabel(refs, label);
361
365
  }
362
- async getMasterRef() {
363
- const refs = await this.getRefs();
366
+ async getMasterRef(params) {
367
+ const refs = await this.getRefs(params);
364
368
  return findMasterRef(refs);
365
369
  }
366
- async getReleases() {
367
- const refs = await this.getRefs();
370
+ async getReleases(params) {
371
+ const refs = await this.getRefs(params);
368
372
  return refs.filter((ref) => !ref.isMasterRef);
369
373
  }
370
- async getReleaseByID(id) {
371
- const releases = await this.getReleases();
374
+ async getReleaseByID(id, params) {
375
+ const releases = await this.getReleases(params);
372
376
  return findRefByID(releases, id);
373
377
  }
374
- async getReleaseByLabel(label) {
375
- const releases = await this.getReleases();
378
+ async getReleaseByLabel(label, params) {
379
+ const releases = await this.getReleases(params);
376
380
  return findRefByLabel(releases, label);
377
381
  }
378
- async getTags() {
382
+ async getTags(params) {
379
383
  try {
380
- const tagsForm = await this.getCachedRepositoryForm("tags");
384
+ const tagsForm = await this.getCachedRepositoryForm("tags", params);
381
385
  return await this.fetch(tagsForm.action);
382
386
  } catch (e) {
383
- const repository = await this.getRepository();
387
+ const repository = await this.getRepository(params);
384
388
  return repository.tags;
385
389
  }
386
390
  }
387
- async buildQueryURL(params = {}) {
391
+ async buildQueryURL({
392
+ signal,
393
+ ...params
394
+ } = {}) {
388
395
  const ref = params.ref || await this.getResolvedRefString();
389
- const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
396
+ const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository({ signal })).integrationFieldsRef || void 0;
390
397
  return buildQueryURL(this.endpoint, {
391
398
  ...this.defaultParams,
392
399
  ...params,
@@ -410,6 +417,7 @@ class Client {
410
417
  }
411
418
  if (documentID != null) {
412
419
  const document = await this.getByID(documentID, {
420
+ signal: args.signal,
413
421
  ref: previewToken,
414
422
  lang: "*"
415
423
  });
@@ -457,27 +465,32 @@ class Client {
457
465
  headers[key.toLowerCase()] = unsanitizedHeaders[key];
458
466
  }
459
467
  }
460
- return await this.fetchFn(input, {
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(), {
461
474
  ...init,
462
475
  headers
463
476
  });
464
477
  }
465
- async getCachedRepository() {
478
+ async getCachedRepository(params) {
466
479
  if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
467
480
  this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
468
- this.cachedRepository = await this.getRepository();
481
+ this.cachedRepository = await this.getRepository(params);
469
482
  }
470
483
  return this.cachedRepository;
471
484
  }
472
- async getCachedRepositoryForm(name) {
473
- const cachedRepository = await this.getCachedRepository();
485
+ async getCachedRepositoryForm(name, params) {
486
+ const cachedRepository = await this.getCachedRepository(params);
474
487
  const form = cachedRepository.forms[name];
475
488
  if (!form) {
476
489
  throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
477
490
  }
478
491
  return form;
479
492
  }
480
- async getResolvedRefString() {
493
+ async getResolvedRefString(params) {
481
494
  var _a, _b, _c;
482
495
  if (this.refState.autoPreviewsEnabled) {
483
496
  let previewRef = void 0;
@@ -490,7 +503,7 @@ class Client {
490
503
  return previewRef;
491
504
  }
492
505
  }
493
- const cachedRepository = await this.getCachedRepository();
506
+ const cachedRepository = await this.getCachedRepository(params);
494
507
  const refModeType = this.refState.mode;
495
508
  if (refModeType === "ReleaseID" /* ReleaseID */) {
496
509
  return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
@@ -504,8 +517,10 @@ class Client {
504
517
  }
505
518
  return findMasterRef(cachedRepository.refs).ref;
506
519
  }
507
- async fetch(url, _params) {
508
- const res = await this.fetchFn(url);
520
+ async fetch(url, params = {}) {
521
+ const res = await this.fetchFn(url, {
522
+ signal: params.signal
523
+ });
509
524
  let json;
510
525
  try {
511
526
  json = await res.json();