@prismicio/client 6.5.0 → 6.6.1-alpha.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/dist/index.d.ts CHANGED
@@ -53,6 +53,35 @@ declare const isRepositoryName: (input: string) => boolean;
53
53
  */
54
54
  declare const isRepositoryEndpoint: (input: string) => boolean;
55
55
 
56
+ /**
57
+ * Functions like TypeScript's native `Extract` utility type with added support
58
+ * to fall back to a value if the resulting union is `never`.
59
+ *
60
+ * @example
61
+ *
62
+ * ```ts
63
+ * ExtractOrFallback<"a" | "b", "a"> // => "a"
64
+ * ExtractOrFallback<"a" | "b", "c"> // => "a" | "b"
65
+ * ExtractOrFallback<"a" | "b", "c", "foo"> // => "foo"
66
+ * ```
67
+ *
68
+ * @typeParam T - The union from which values will be extracted.
69
+ * @typeParam U - The extraction condition.
70
+ * @typeParam Fallback - The value to return if the resulting union is `never`.
71
+ * Defaults to `T`.
72
+ */
73
+ declare type ExtractOrFallback<T, U, Fallback = T> = Extract<T, U> extends never ? Fallback : Extract<T, U>;
74
+ /**
75
+ * Extracts one or more Prismic document types that match a given Prismic
76
+ * document type. If no matches are found, no extraction is performed and the
77
+ * union of all provided Prismic document types are returned.
78
+ *
79
+ * @typeParam TDocuments - Prismic document types from which to extract.
80
+ * @typeParam TDocumentType - Type(s) to match `TDocuments` against.
81
+ */
82
+ declare type ExtractDocumentType<TDocuments extends prismicT.PrismicDocument, TDocumentType extends TDocuments["type"]> = ExtractOrFallback<TDocuments, {
83
+ type: TDocumentType;
84
+ }>;
56
85
  /**
57
86
  * A universal API to make network requests. A subset of the `fetch()` API.
58
87
  *
@@ -101,7 +130,6 @@ declare type HttpRequestLike = /**
101
130
  get(name: string): string | null;
102
131
  };
103
132
  url?: string;
104
- query?: never;
105
133
  }
106
134
  /**
107
135
  * Express-style Request
@@ -111,7 +139,6 @@ declare type HttpRequestLike = /**
111
139
  cookie?: string;
112
140
  };
113
141
  query?: Record<string, unknown>;
114
- url?: never;
115
142
  };
116
143
  /**
117
144
  * An `orderings` parameter that orders the results by the specified field.
@@ -345,6 +372,13 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
345
372
  */
346
373
  documentID?: string;
347
374
  };
375
+ /**
376
+ * Type definitions for the `createClient()` function. May be augmented by
377
+ * third-party libraries.
378
+ */
379
+ interface CreateClient {
380
+ <TDocuments extends prismicT.PrismicDocument>(...args: ConstructorParameters<typeof Client>): Client<TDocuments>;
381
+ }
348
382
  /**
349
383
  * Creates a Prismic client that can be used to query a repository.
350
384
  *
@@ -358,6 +392,8 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
358
392
  * createClient("https://qwerty.cdn.prismic.io/api/v2");
359
393
  * ```
360
394
  *
395
+ * @typeParam TDocuments - A map of Prismic document type IDs mapped to their
396
+ * TypeScript type.
361
397
  * @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
362
398
  * API V2 endpoint for the repository.
363
399
  * @param options - Configuration that determines how content will be queried
@@ -365,15 +401,18 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
365
401
  *
366
402
  * @returns A client that can query content from the repository.
367
403
  */
368
- declare const createClient: (repositoryNameOrEndpoint: string, options?: ClientConfig | undefined) => Client;
404
+ declare const createClient: CreateClient;
369
405
  /**
370
406
  * A client that allows querying content from a Prismic repository.
371
407
  *
372
408
  * If used in an environment where a global `fetch` function is unavailable,
373
409
  * such as Node.js, the `fetch` option must be provided as part of the `options`
374
410
  * parameter.
411
+ *
412
+ * @typeParam TDocuments - Document types that are registered for the Prismic
413
+ * repository. Query methods will automatically be typed based on this type.
375
414
  */
376
- declare class Client {
415
+ declare class Client<TDocuments extends prismicT.PrismicDocument = prismicT.PrismicDocument> {
377
416
  /**
378
417
  * The Prismic REST API V2 endpoint for the repository (use
379
418
  * `prismic.getRepositoryEndpoint` for the default endpoint).
@@ -495,7 +534,7 @@ declare class Client {
495
534
  *
496
535
  * @returns A paginated response containing the result of the query.
497
536
  */
498
- query<TDocument extends prismicT.PrismicDocument>(predicates: NonNullable<BuildQueryURLArgs["predicates"]>, params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams): Promise<prismicT.Query<TDocument>>;
537
+ query<TDocument extends TDocuments>(predicates: NonNullable<BuildQueryURLArgs["predicates"]>, params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams): Promise<prismicT.Query<TDocument>>;
499
538
  /**
500
539
  * Queries content from the Prismic repository.
501
540
  *
@@ -510,7 +549,7 @@ declare class Client {
510
549
  *
511
550
  * @returns A paginated response containing the result of the query.
512
551
  */
513
- get<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
552
+ get<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
514
553
  /**
515
554
  * Queries content from the Prismic repository and returns only the first
516
555
  * result, if any.
@@ -525,7 +564,7 @@ declare class Client {
525
564
  * @param params - Parameters to filter, sort, and paginate results. @returns
526
565
  * The first result of the query, if any.
527
566
  */
528
- getFirst<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
567
+ getFirst<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
529
568
  /**
530
569
  * **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and
531
570
  * require more resources than other methods. Prefer using other methods that
@@ -547,7 +586,7 @@ declare class Client {
547
586
  *
548
587
  * @returns A list of documents matching the query.
549
588
  */
550
- dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
589
+ dangerouslyGetAll<TDocument extends TDocuments>(params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
551
590
  /**
552
591
  * Queries a document from the Prismic repository with a specific ID.
553
592
  *
@@ -568,7 +607,7 @@ declare class Client {
568
607
  * @returns The document with an ID matching the `id` parameter, if a matching
569
608
  * document exists.
570
609
  */
571
- getByID<TDocument extends prismicT.PrismicDocument>(id: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
610
+ getByID<TDocument extends TDocuments>(id: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
572
611
  /**
573
612
  * Queries documents from the Prismic repository with specific IDs.
574
613
  *
@@ -592,7 +631,7 @@ declare class Client {
592
631
  * @returns A paginated response containing documents with IDs matching the
593
632
  * `ids` parameter.
594
633
  */
595
- getByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
634
+ getByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
596
635
  /**
597
636
  * Queries all documents from the Prismic repository with specific IDs.
598
637
  *
@@ -617,7 +656,7 @@ declare class Client {
617
656
  *
618
657
  * @returns A list of documents with IDs matching the `ids` parameter.
619
658
  */
620
- getAllByIDs<TDocument extends prismicT.PrismicDocument>(ids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<TDocument[]>;
659
+ getAllByIDs<TDocument extends TDocuments>(ids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<TDocument[]>;
621
660
  /**
622
661
  * Queries a document from the Prismic repository with a specific UID and Custom Type.
623
662
  *
@@ -639,7 +678,7 @@ declare class Client {
639
678
  * @returns The document with a UID matching the `uid` parameter, if a
640
679
  * matching document exists.
641
680
  */
642
- getByUID<TDocument extends prismicT.PrismicDocument>(documentType: string, uid: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
681
+ getByUID<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uid: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>;
643
682
  /**
644
683
  * Queries document from the Prismic repository with specific UIDs and Custom Type.
645
684
  *
@@ -664,7 +703,7 @@ declare class Client {
664
703
  * @returns A paginated response containing documents with UIDs matching the
665
704
  * `uids` parameter.
666
705
  */
667
- getByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
706
+ getByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<ExtractDocumentType<TDocument, TDocumentType>>>;
668
707
  /**
669
708
  * Queries all documents from the Prismic repository with specific UIDs and Custom Type.
670
709
  *
@@ -690,7 +729,7 @@ declare class Client {
690
729
  *
691
730
  * @returns A list of documents with UIDs matching the `uids` parameter.
692
731
  */
693
- getAllByUIDs<TDocument extends prismicT.PrismicDocument>(documentType: string, uids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<TDocument[]>;
732
+ getAllByUIDs<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, uids: string[], params?: Partial<BuildQueryURLArgs> & GetAllParams & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>;
694
733
  /**
695
734
  * Queries a singleton document from the Prismic repository for a specific Custom Type.
696
735
  *
@@ -711,7 +750,7 @@ declare class Client {
711
750
  *
712
751
  * @returns The singleton document for the Custom Type, if a matching document exists.
713
752
  */
714
- getSingle<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
753
+ getSingle<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>>;
715
754
  /**
716
755
  * Queries documents from the Prismic repository for a specific Custom Type.
717
756
  *
@@ -730,7 +769,7 @@ declare class Client {
730
769
  *
731
770
  * @returns A paginated response containing documents of the Custom Type.
732
771
  */
733
- getByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
772
+ getByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<ExtractDocumentType<TDocument, TDocumentType>>>;
734
773
  /**
735
774
  * Queries all documents from the Prismic repository for a specific Custom Type.
736
775
  *
@@ -748,7 +787,7 @@ declare class Client {
748
787
  *
749
788
  * @returns A list of all documents of the Custom Type.
750
789
  */
751
- getAllByType<TDocument extends prismicT.PrismicDocument>(documentType: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
790
+ getAllByType<TDocument extends TDocuments, TDocumentType extends TDocument["type"] = TDocument["type"]>(documentType: TDocumentType, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<ExtractDocumentType<TDocument, TDocumentType>[]>;
752
791
  /**
753
792
  * Queries documents from the Prismic repository with a specific tag.
754
793
  *
@@ -766,7 +805,7 @@ declare class Client {
766
805
  *
767
806
  * @returns A paginated response containing documents with the tag.
768
807
  */
769
- getByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
808
+ getByTag<TDocument extends TDocuments>(tag: string, params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
770
809
  /**
771
810
  * Queries all documents from the Prismic repository with a specific tag.
772
811
  *
@@ -784,7 +823,7 @@ declare class Client {
784
823
  *
785
824
  * @returns A list of all documents with the tag.
786
825
  */
787
- getAllByTag<TDocument extends prismicT.PrismicDocument>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
826
+ getAllByTag<TDocument extends TDocuments>(tag: string, params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
788
827
  /**
789
828
  * Queries documents from the Prismic repository with specific tags. A
790
829
  * document must be tagged with all of the queried tags to be included.
@@ -801,7 +840,7 @@ declare class Client {
801
840
  *
802
841
  * @returns A paginated response containing documents with the tags.
803
842
  */
804
- getByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
843
+ getByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
805
844
  /**
806
845
  * Queries documents from the Prismic repository with specific tags. A
807
846
  * document must be tagged with all of the queried tags to be included.
@@ -820,7 +859,7 @@ declare class Client {
820
859
  *
821
860
  * @returns A list of all documents with the tags.
822
861
  */
823
- getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
862
+ getAllByEveryTag<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
824
863
  /**
825
864
  * Queries documents from the Prismic repository with specific tags. A
826
865
  * document must be tagged with at least one of the queried tags to be included.
@@ -837,7 +876,7 @@ declare class Client {
837
876
  *
838
877
  * @returns A paginated response containing documents with at least one of the tags.
839
878
  */
840
- getBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
879
+ getBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<prismicT.Query<TDocument>>;
841
880
  /**
842
881
  * Queries documents from the Prismic repository with specific tags. A
843
882
  * document must be tagged with at least one of the queried tags to be included.
@@ -856,7 +895,7 @@ declare class Client {
856
895
  *
857
896
  * @returns A list of all documents with at least one of the tags.
858
897
  */
859
- getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
898
+ getAllBySomeTags<TDocument extends TDocuments>(tags: string[], params?: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams & FetchParams): Promise<TDocument[]>;
860
899
  /**
861
900
  * Returns metadata about the Prismic repository, such as its refs, releases,
862
901
  * and custom types.
@@ -1424,4 +1463,4 @@ declare const Predicates: {
1424
1463
  dateHourBefore: (path: string, hour: number) => string;
1425
1464
  };
1426
1465
 
1427
- export { BuildQueryURLArgs, Client, ClientConfig, FetchLike, ForbiddenError, HttpRequestLike, NotFoundError, Ordering, ParsingError, Predicates, PrismicError, QueryParams, RequestInitLike, ResponseLike, Route, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
1466
+ export { BuildQueryURLArgs, Client, ClientConfig, CreateClient, FetchLike, ForbiddenError, HttpRequestLike, NotFoundError, Ordering, ParsingError, Predicates, PrismicError, QueryParams, RequestInitLike, ResponseLike, Route, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
package/dist/index.js CHANGED
@@ -224,7 +224,7 @@ const GET_ALL_QUERY_DELAY = 500;
224
224
  const typePredicate = (documentType) => predicate.at("document.type", documentType);
225
225
  const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags));
226
226
  const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
227
- const createClient = (...args) => new Client(...args);
227
+ const createClient = (repositoryNameOrEndpoint, options) => new Client(repositoryNameOrEndpoint, options);
228
228
  class Client {
229
229
  constructor(repositoryNameOrEndpoint, options = {}) {
230
230
  this.refState = {
@@ -397,7 +397,11 @@ class Client {
397
397
  async getTags(params) {
398
398
  try {
399
399
  const tagsForm = await this.getCachedRepositoryForm("tags", params);
400
- return await this.fetch(tagsForm.action);
400
+ const url = new URL(tagsForm.action);
401
+ if (this.accessToken) {
402
+ url.searchParams.set("access_token", this.accessToken);
403
+ }
404
+ return await this.fetch(url.toString(), params);
401
405
  } catch (e) {
402
406
  const repository = await this.getRepository(params);
403
407
  return repository.tags;
@@ -427,13 +431,13 @@ class Client {
427
431
  documentID = documentID || searchParams.get("documentId");
428
432
  previewToken = previewToken || searchParams.get("token");
429
433
  } else if (this.refState.httpRequest) {
430
- if (this.refState.httpRequest.url) {
434
+ if ("query" in this.refState.httpRequest) {
435
+ documentID = documentID || ((_a = this.refState.httpRequest.query) == null ? void 0 : _a.documentId);
436
+ previewToken = previewToken || ((_b = this.refState.httpRequest.query) == null ? void 0 : _b.token);
437
+ } else if ("url" in this.refState.httpRequest && this.refState.httpRequest.url) {
431
438
  const searchParams = new URL(this.refState.httpRequest.url).searchParams;
432
439
  documentID = documentID || searchParams.get("documentId");
433
440
  previewToken = previewToken || searchParams.get("token");
434
- } else {
435
- documentID = documentID || ((_a = this.refState.httpRequest.query) == null ? void 0 : _a.documentId);
436
- previewToken = previewToken || ((_b = this.refState.httpRequest.query) == null ? void 0 : _b.token);
437
441
  }
438
442
  }
439
443
  if (documentID != null && previewToken != null) {