@prismicio/client 6.5.1 → 6.6.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
  *
@@ -343,6 +372,13 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
343
372
  */
344
373
  documentID?: string;
345
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
+ }
346
382
  /**
347
383
  * Creates a Prismic client that can be used to query a repository.
348
384
  *
@@ -356,6 +392,8 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
356
392
  * createClient("https://qwerty.cdn.prismic.io/api/v2");
357
393
  * ```
358
394
  *
395
+ * @typeParam TDocuments - A map of Prismic document type IDs mapped to their
396
+ * TypeScript type.
359
397
  * @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
360
398
  * API V2 endpoint for the repository.
361
399
  * @param options - Configuration that determines how content will be queried
@@ -363,15 +401,18 @@ declare type ResolvePreviewArgs<LinkResolverReturnType> = {
363
401
  *
364
402
  * @returns A client that can query content from the repository.
365
403
  */
366
- declare const createClient: (repositoryNameOrEndpoint: string, options?: ClientConfig | undefined) => Client;
404
+ declare const createClient: CreateClient;
367
405
  /**
368
406
  * A client that allows querying content from a Prismic repository.
369
407
  *
370
408
  * If used in an environment where a global `fetch` function is unavailable,
371
409
  * such as Node.js, the `fetch` option must be provided as part of the `options`
372
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.
373
414
  */
374
- declare class Client {
415
+ declare class Client<TDocuments extends prismicT.PrismicDocument = prismicT.PrismicDocument> {
375
416
  /**
376
417
  * The Prismic REST API V2 endpoint for the repository (use
377
418
  * `prismic.getRepositoryEndpoint` for the default endpoint).
@@ -493,7 +534,7 @@ declare class Client {
493
534
  *
494
535
  * @returns A paginated response containing the result of the query.
495
536
  */
496
- 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>>;
497
538
  /**
498
539
  * Queries content from the Prismic repository.
499
540
  *
@@ -508,7 +549,7 @@ declare class Client {
508
549
  *
509
550
  * @returns A paginated response containing the result of the query.
510
551
  */
511
- 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>>;
512
553
  /**
513
554
  * Queries content from the Prismic repository and returns only the first
514
555
  * result, if any.
@@ -523,7 +564,7 @@ declare class Client {
523
564
  * @param params - Parameters to filter, sort, and paginate results. @returns
524
565
  * The first result of the query, if any.
525
566
  */
526
- getFirst<TDocument extends prismicT.PrismicDocument>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
567
+ getFirst<TDocument extends TDocuments>(params?: Partial<BuildQueryURLArgs> & FetchParams): Promise<TDocument>;
527
568
  /**
528
569
  * **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and
529
570
  * require more resources than other methods. Prefer using other methods that
@@ -545,7 +586,7 @@ declare class Client {
545
586
  *
546
587
  * @returns A list of documents matching the query.
547
588
  */
548
- 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[]>;
549
590
  /**
550
591
  * Queries a document from the Prismic repository with a specific ID.
551
592
  *
@@ -566,7 +607,7 @@ declare class Client {
566
607
  * @returns The document with an ID matching the `id` parameter, if a matching
567
608
  * document exists.
568
609
  */
569
- 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>;
570
611
  /**
571
612
  * Queries documents from the Prismic repository with specific IDs.
572
613
  *
@@ -590,7 +631,7 @@ declare class Client {
590
631
  * @returns A paginated response containing documents with IDs matching the
591
632
  * `ids` parameter.
592
633
  */
593
- 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>>;
594
635
  /**
595
636
  * Queries all documents from the Prismic repository with specific IDs.
596
637
  *
@@ -615,7 +656,7 @@ declare class Client {
615
656
  *
616
657
  * @returns A list of documents with IDs matching the `ids` parameter.
617
658
  */
618
- 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[]>;
619
660
  /**
620
661
  * Queries a document from the Prismic repository with a specific UID and Custom Type.
621
662
  *
@@ -637,7 +678,7 @@ declare class Client {
637
678
  * @returns The document with a UID matching the `uid` parameter, if a
638
679
  * matching document exists.
639
680
  */
640
- 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<TDocuments, TDocumentType>>;
641
682
  /**
642
683
  * Queries document from the Prismic repository with specific UIDs and Custom Type.
643
684
  *
@@ -662,7 +703,7 @@ declare class Client {
662
703
  * @returns A paginated response containing documents with UIDs matching the
663
704
  * `uids` parameter.
664
705
  */
665
- 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<TDocuments, TDocumentType>>>;
666
707
  /**
667
708
  * Queries all documents from the Prismic repository with specific UIDs and Custom Type.
668
709
  *
@@ -688,7 +729,7 @@ declare class Client {
688
729
  *
689
730
  * @returns A list of documents with UIDs matching the `uids` parameter.
690
731
  */
691
- 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<TDocuments, TDocumentType>[]>;
692
733
  /**
693
734
  * Queries a singleton document from the Prismic repository for a specific Custom Type.
694
735
  *
@@ -709,7 +750,7 @@ declare class Client {
709
750
  *
710
751
  * @returns The singleton document for the Custom Type, if a matching document exists.
711
752
  */
712
- 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<TDocuments, TDocumentType>>;
713
754
  /**
714
755
  * Queries documents from the Prismic repository for a specific Custom Type.
715
756
  *
@@ -728,7 +769,7 @@ declare class Client {
728
769
  *
729
770
  * @returns A paginated response containing documents of the Custom Type.
730
771
  */
731
- 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<TDocuments, TDocumentType>>>;
732
773
  /**
733
774
  * Queries all documents from the Prismic repository for a specific Custom Type.
734
775
  *
@@ -746,7 +787,7 @@ declare class Client {
746
787
  *
747
788
  * @returns A list of all documents of the Custom Type.
748
789
  */
749
- 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<TDocuments, TDocumentType>[]>;
750
791
  /**
751
792
  * Queries documents from the Prismic repository with a specific tag.
752
793
  *
@@ -764,7 +805,7 @@ declare class Client {
764
805
  *
765
806
  * @returns A paginated response containing documents with the tag.
766
807
  */
767
- 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>>;
768
809
  /**
769
810
  * Queries all documents from the Prismic repository with a specific tag.
770
811
  *
@@ -782,7 +823,7 @@ declare class Client {
782
823
  *
783
824
  * @returns A list of all documents with the tag.
784
825
  */
785
- 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[]>;
786
827
  /**
787
828
  * Queries documents from the Prismic repository with specific tags. A
788
829
  * document must be tagged with all of the queried tags to be included.
@@ -799,7 +840,7 @@ declare class Client {
799
840
  *
800
841
  * @returns A paginated response containing documents with the tags.
801
842
  */
802
- 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>>;
803
844
  /**
804
845
  * Queries documents from the Prismic repository with specific tags. A
805
846
  * document must be tagged with all of the queried tags to be included.
@@ -818,7 +859,7 @@ declare class Client {
818
859
  *
819
860
  * @returns A list of all documents with the tags.
820
861
  */
821
- 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[]>;
822
863
  /**
823
864
  * Queries documents from the Prismic repository with specific tags. A
824
865
  * document must be tagged with at least one of the queried tags to be included.
@@ -835,7 +876,7 @@ declare class Client {
835
876
  *
836
877
  * @returns A paginated response containing documents with at least one of the tags.
837
878
  */
838
- 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>>;
839
880
  /**
840
881
  * Queries documents from the Prismic repository with specific tags. A
841
882
  * document must be tagged with at least one of the queried tags to be included.
@@ -854,7 +895,7 @@ declare class Client {
854
895
  *
855
896
  * @returns A list of all documents with at least one of the tags.
856
897
  */
857
- 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[]>;
858
899
  /**
859
900
  * Returns metadata about the Prismic repository, such as its refs, releases,
860
901
  * and custom types.
@@ -1422,4 +1463,4 @@ declare const Predicates: {
1422
1463
  dateHourBefore: (path: string, hour: number) => string;
1423
1464
  };
1424
1465
 
1425
- 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;