@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 +1 -1
- package/dist/index.cjs +92 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +98 -31
- package/dist/index.js +92 -60
- package/dist/index.js.map +1 -1
- package/package.json +20 -16
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +177 -59
- package/src/getGraphQLEndpoint.ts +12 -0
- package/src/index.ts +1 -0
- package/src/lib/minifyGraphQLQuery.ts +13 -0
- package/src/types.ts +42 -0
package/src/client.ts
CHANGED
|
@@ -2,20 +2,22 @@ import * as prismicT from "@prismicio/types";
|
|
|
2
2
|
import * as prismicH from "@prismicio/helpers";
|
|
3
3
|
|
|
4
4
|
import { appendPredicates } from "./lib/appendPredicates";
|
|
5
|
+
import { castArray } from "./lib/castArray";
|
|
5
6
|
import { castThunk } from "./lib/castThunk";
|
|
6
7
|
import { findMasterRef } from "./lib/findMasterRef";
|
|
7
8
|
import { findRefByID } from "./lib/findRefByID";
|
|
8
9
|
import { findRefByLabel } from "./lib/findRefByLabel";
|
|
9
10
|
import { getCookie } from "./lib/getCookie";
|
|
11
|
+
import { minifyGraphQLQuery } from "./lib/minifyGraphQLQuery";
|
|
10
12
|
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
+
import * as cookie from "./cookie";
|
|
14
|
+
import { AbortSignalLike, FetchLike, HttpRequestLike } from "./types";
|
|
13
15
|
import { ForbiddenError } from "./ForbiddenError";
|
|
16
|
+
import { NotFoundError } from "./NotFoundError";
|
|
14
17
|
import { ParsingError } from "./ParsingError";
|
|
15
18
|
import { PrismicError } from "./PrismicError";
|
|
19
|
+
import { buildQueryURL, BuildQueryURLArgs } from "./buildQueryURL";
|
|
16
20
|
import { predicate } from "./predicate";
|
|
17
|
-
import * as cookie from "./cookie";
|
|
18
|
-
import { NotFoundError } from "./NotFoundError";
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* The largest page size allowed by the Prismic REST API V2. This value is used
|
|
@@ -148,6 +150,20 @@ export type ClientConfig = {
|
|
|
148
150
|
fetch?: FetchLike;
|
|
149
151
|
};
|
|
150
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Parameters for any client method that use `fetch()`. Only a subset of
|
|
155
|
+
* `fetch()` parameters are exposed.
|
|
156
|
+
*/
|
|
157
|
+
type FetchParams = {
|
|
158
|
+
/**
|
|
159
|
+
* An `AbortSignal` provided by an `AbortController`. This allows the network
|
|
160
|
+
* request to be cancelled if necessary.
|
|
161
|
+
*
|
|
162
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
163
|
+
*/
|
|
164
|
+
signal?: AbortSignalLike;
|
|
165
|
+
};
|
|
166
|
+
|
|
151
167
|
/**
|
|
152
168
|
* Parameters specific to client methods that fetch all documents. These methods
|
|
153
169
|
* start with `getAll` (for example, `getAllByType`).
|
|
@@ -205,7 +221,7 @@ const typePredicate = (documentType: string): string =>
|
|
|
205
221
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
206
222
|
*/
|
|
207
223
|
const everyTagPredicate = (tags: string | string[]): string =>
|
|
208
|
-
predicate.at("document.tags", tags);
|
|
224
|
+
predicate.at("document.tags", castArray(tags));
|
|
209
225
|
|
|
210
226
|
/**
|
|
211
227
|
* Creates a predicate to filter content by document tags. At least one matching
|
|
@@ -216,7 +232,7 @@ const everyTagPredicate = (tags: string | string[]): string =>
|
|
|
216
232
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
217
233
|
*/
|
|
218
234
|
const someTagsPredicate = (tags: string | string[]): string =>
|
|
219
|
-
predicate.any("document.tags", tags);
|
|
235
|
+
predicate.any("document.tags", castArray(tags));
|
|
220
236
|
|
|
221
237
|
/**
|
|
222
238
|
* Creates a Prismic client that can be used to query a repository.
|
|
@@ -347,6 +363,8 @@ export class Client {
|
|
|
347
363
|
if (this.fetchFn === globalThis.fetch) {
|
|
348
364
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
349
365
|
}
|
|
366
|
+
|
|
367
|
+
this.graphqlFetch = this.graphqlFetch.bind(this);
|
|
350
368
|
}
|
|
351
369
|
|
|
352
370
|
/**
|
|
@@ -425,7 +443,7 @@ export class Client {
|
|
|
425
443
|
*/
|
|
426
444
|
async query<TDocument extends prismicT.PrismicDocument>(
|
|
427
445
|
predicates: NonNullable<BuildQueryURLArgs["predicates"]>,
|
|
428
|
-
params?: Partial<Omit<BuildQueryURLArgs, "predicates"
|
|
446
|
+
params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams,
|
|
429
447
|
): Promise<prismicT.Query<TDocument>> {
|
|
430
448
|
const url = await this.buildQueryURL({ ...params, predicates });
|
|
431
449
|
|
|
@@ -447,7 +465,7 @@ export class Client {
|
|
|
447
465
|
* @returns A paginated response containing the result of the query.
|
|
448
466
|
*/
|
|
449
467
|
async get<TDocument extends prismicT.PrismicDocument>(
|
|
450
|
-
params?: Partial<BuildQueryURLArgs
|
|
468
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
451
469
|
): Promise<prismicT.Query<TDocument>> {
|
|
452
470
|
const url = await this.buildQueryURL(params);
|
|
453
471
|
|
|
@@ -469,7 +487,7 @@ export class Client {
|
|
|
469
487
|
* The first result of the query, if any.
|
|
470
488
|
*/
|
|
471
489
|
async getFirst<TDocument extends prismicT.PrismicDocument>(
|
|
472
|
-
params?: Partial<BuildQueryURLArgs
|
|
490
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
473
491
|
): Promise<TDocument> {
|
|
474
492
|
const url = await this.buildQueryURL(params);
|
|
475
493
|
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
@@ -505,7 +523,9 @@ export class Client {
|
|
|
505
523
|
* @returns A list of documents matching the query.
|
|
506
524
|
*/
|
|
507
525
|
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(
|
|
508
|
-
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
526
|
+
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
527
|
+
GetAllParams &
|
|
528
|
+
FetchParams = {},
|
|
509
529
|
): Promise<TDocument[]> {
|
|
510
530
|
const { limit = Infinity, ...actualParams } = params;
|
|
511
531
|
const resolvedParams = {
|
|
@@ -555,7 +575,7 @@ export class Client {
|
|
|
555
575
|
*/
|
|
556
576
|
async getByID<TDocument extends prismicT.PrismicDocument>(
|
|
557
577
|
id: string,
|
|
558
|
-
params?: Partial<BuildQueryURLArgs
|
|
578
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
559
579
|
): Promise<TDocument> {
|
|
560
580
|
return await this.getFirst<TDocument>(
|
|
561
581
|
appendPredicates(params, predicate.at("document.id", id)),
|
|
@@ -587,7 +607,7 @@ export class Client {
|
|
|
587
607
|
*/
|
|
588
608
|
async getByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
589
609
|
ids: string[],
|
|
590
|
-
params?: Partial<BuildQueryURLArgs
|
|
610
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
591
611
|
): Promise<prismicT.Query<TDocument>> {
|
|
592
612
|
return await this.get<TDocument>(
|
|
593
613
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -620,7 +640,7 @@ export class Client {
|
|
|
620
640
|
*/
|
|
621
641
|
async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
622
642
|
ids: string[],
|
|
623
|
-
params?: Partial<BuildQueryURLArgs
|
|
643
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
624
644
|
): Promise<TDocument[]> {
|
|
625
645
|
return await this.dangerouslyGetAll<TDocument>(
|
|
626
646
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -651,7 +671,7 @@ export class Client {
|
|
|
651
671
|
async getByUID<TDocument extends prismicT.PrismicDocument>(
|
|
652
672
|
documentType: string,
|
|
653
673
|
uid: string,
|
|
654
|
-
params?: Partial<BuildQueryURLArgs
|
|
674
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
655
675
|
): Promise<TDocument> {
|
|
656
676
|
return await this.getFirst<TDocument>(
|
|
657
677
|
appendPredicates(params, [
|
|
@@ -688,7 +708,7 @@ export class Client {
|
|
|
688
708
|
async getByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
689
709
|
documentType: string,
|
|
690
710
|
uids: string[],
|
|
691
|
-
params?: Partial<BuildQueryURLArgs
|
|
711
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
692
712
|
): Promise<prismicT.Query<TDocument>> {
|
|
693
713
|
return await this.get<TDocument>(
|
|
694
714
|
appendPredicates(params, [
|
|
@@ -726,7 +746,7 @@ export class Client {
|
|
|
726
746
|
async getAllByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
727
747
|
documentType: string,
|
|
728
748
|
uids: string[],
|
|
729
|
-
params?: Partial<BuildQueryURLArgs
|
|
749
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
730
750
|
): Promise<TDocument[]> {
|
|
731
751
|
return await this.dangerouslyGetAll<TDocument>(
|
|
732
752
|
appendPredicates(params, [
|
|
@@ -758,7 +778,7 @@ export class Client {
|
|
|
758
778
|
*/
|
|
759
779
|
async getSingle<TDocument extends prismicT.PrismicDocument>(
|
|
760
780
|
documentType: string,
|
|
761
|
-
params?: Partial<BuildQueryURLArgs
|
|
781
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
762
782
|
): Promise<TDocument> {
|
|
763
783
|
return await this.getFirst<TDocument>(
|
|
764
784
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -785,7 +805,7 @@ export class Client {
|
|
|
785
805
|
*/
|
|
786
806
|
async getByType<TDocument extends prismicT.PrismicDocument>(
|
|
787
807
|
documentType: string,
|
|
788
|
-
params?: Partial<BuildQueryURLArgs
|
|
808
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
789
809
|
): Promise<prismicT.Query<TDocument>> {
|
|
790
810
|
return await this.get<TDocument>(
|
|
791
811
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -811,7 +831,7 @@ export class Client {
|
|
|
811
831
|
*/
|
|
812
832
|
async getAllByType<TDocument extends prismicT.PrismicDocument>(
|
|
813
833
|
documentType: string,
|
|
814
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
834
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
815
835
|
): Promise<TDocument[]> {
|
|
816
836
|
return await this.dangerouslyGetAll<TDocument>(
|
|
817
837
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -837,10 +857,10 @@ export class Client {
|
|
|
837
857
|
*/
|
|
838
858
|
async getByTag<TDocument extends prismicT.PrismicDocument>(
|
|
839
859
|
tag: string,
|
|
840
|
-
params?: Partial<BuildQueryURLArgs
|
|
860
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
841
861
|
): Promise<prismicT.Query<TDocument>> {
|
|
842
862
|
return await this.get<TDocument>(
|
|
843
|
-
appendPredicates(params,
|
|
863
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
844
864
|
);
|
|
845
865
|
}
|
|
846
866
|
|
|
@@ -863,10 +883,10 @@ export class Client {
|
|
|
863
883
|
*/
|
|
864
884
|
async getAllByTag<TDocument extends prismicT.PrismicDocument>(
|
|
865
885
|
tag: string,
|
|
866
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
886
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
867
887
|
): Promise<TDocument[]> {
|
|
868
888
|
return await this.dangerouslyGetAll<TDocument>(
|
|
869
|
-
appendPredicates(params,
|
|
889
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
870
890
|
);
|
|
871
891
|
}
|
|
872
892
|
|
|
@@ -888,7 +908,7 @@ export class Client {
|
|
|
888
908
|
*/
|
|
889
909
|
async getByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
890
910
|
tags: string[],
|
|
891
|
-
params?: Partial<BuildQueryURLArgs
|
|
911
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
892
912
|
): Promise<prismicT.Query<TDocument>> {
|
|
893
913
|
return await this.get<TDocument>(
|
|
894
914
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -915,7 +935,7 @@ export class Client {
|
|
|
915
935
|
*/
|
|
916
936
|
async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
917
937
|
tags: string[],
|
|
918
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
938
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
919
939
|
): Promise<TDocument[]> {
|
|
920
940
|
return await this.dangerouslyGetAll<TDocument>(
|
|
921
941
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -940,7 +960,7 @@ export class Client {
|
|
|
940
960
|
*/
|
|
941
961
|
async getBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
942
962
|
tags: string[],
|
|
943
|
-
params?: Partial<BuildQueryURLArgs
|
|
963
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
944
964
|
): Promise<prismicT.Query<TDocument>> {
|
|
945
965
|
return await this.get<TDocument>(
|
|
946
966
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -967,7 +987,7 @@ export class Client {
|
|
|
967
987
|
*/
|
|
968
988
|
async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
969
989
|
tags: string[],
|
|
970
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
990
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
971
991
|
): Promise<TDocument[]> {
|
|
972
992
|
return await this.dangerouslyGetAll<TDocument>(
|
|
973
993
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -980,7 +1000,7 @@ export class Client {
|
|
|
980
1000
|
*
|
|
981
1001
|
* @returns Repository metadata.
|
|
982
1002
|
*/
|
|
983
|
-
async getRepository(): Promise<prismicT.Repository> {
|
|
1003
|
+
async getRepository(params?: FetchParams): Promise<prismicT.Repository> {
|
|
984
1004
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
985
1005
|
// return await this.fetch<prismicT.Repository>(this.endpoint);
|
|
986
1006
|
|
|
@@ -990,7 +1010,7 @@ export class Client {
|
|
|
990
1010
|
url.searchParams.set("access_token", this.accessToken);
|
|
991
1011
|
}
|
|
992
1012
|
|
|
993
|
-
return await this.fetch<prismicT.Repository>(url.toString());
|
|
1013
|
+
return await this.fetch<prismicT.Repository>(url.toString(), params);
|
|
994
1014
|
}
|
|
995
1015
|
|
|
996
1016
|
/**
|
|
@@ -1002,8 +1022,8 @@ export class Client {
|
|
|
1002
1022
|
*
|
|
1003
1023
|
* @returns A list of all refs for the Prismic repository.
|
|
1004
1024
|
*/
|
|
1005
|
-
async getRefs(): Promise<prismicT.Ref[]> {
|
|
1006
|
-
const repository = await this.getRepository();
|
|
1025
|
+
async getRefs(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1026
|
+
const repository = await this.getRepository(params);
|
|
1007
1027
|
|
|
1008
1028
|
return repository.refs;
|
|
1009
1029
|
}
|
|
@@ -1015,8 +1035,8 @@ export class Client {
|
|
|
1015
1035
|
*
|
|
1016
1036
|
* @returns The ref with a matching ID, if it exists.
|
|
1017
1037
|
*/
|
|
1018
|
-
async getRefByID(id: string): Promise<prismicT.Ref> {
|
|
1019
|
-
const refs = await this.getRefs();
|
|
1038
|
+
async getRefByID(id: string, params?: FetchParams): Promise<prismicT.Ref> {
|
|
1039
|
+
const refs = await this.getRefs(params);
|
|
1020
1040
|
|
|
1021
1041
|
return findRefByID(refs, id);
|
|
1022
1042
|
}
|
|
@@ -1028,8 +1048,11 @@ export class Client {
|
|
|
1028
1048
|
*
|
|
1029
1049
|
* @returns The ref with a matching label, if it exists.
|
|
1030
1050
|
*/
|
|
1031
|
-
async getRefByLabel(
|
|
1032
|
-
|
|
1051
|
+
async getRefByLabel(
|
|
1052
|
+
label: string,
|
|
1053
|
+
params?: FetchParams,
|
|
1054
|
+
): Promise<prismicT.Ref> {
|
|
1055
|
+
const refs = await this.getRefs(params);
|
|
1033
1056
|
|
|
1034
1057
|
return findRefByLabel(refs, label);
|
|
1035
1058
|
}
|
|
@@ -1040,8 +1063,8 @@ export class Client {
|
|
|
1040
1063
|
*
|
|
1041
1064
|
* @returns The repository's master ref.
|
|
1042
1065
|
*/
|
|
1043
|
-
async getMasterRef(): Promise<prismicT.Ref> {
|
|
1044
|
-
const refs = await this.getRefs();
|
|
1066
|
+
async getMasterRef(params?: FetchParams): Promise<prismicT.Ref> {
|
|
1067
|
+
const refs = await this.getRefs(params);
|
|
1045
1068
|
|
|
1046
1069
|
return findMasterRef(refs);
|
|
1047
1070
|
}
|
|
@@ -1052,8 +1075,8 @@ export class Client {
|
|
|
1052
1075
|
*
|
|
1053
1076
|
* @returns A list of all Releases for the Prismic repository.
|
|
1054
1077
|
*/
|
|
1055
|
-
async getReleases(): Promise<prismicT.Ref[]> {
|
|
1056
|
-
const refs = await this.getRefs();
|
|
1078
|
+
async getReleases(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1079
|
+
const refs = await this.getRefs(params);
|
|
1057
1080
|
|
|
1058
1081
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
1059
1082
|
}
|
|
@@ -1065,8 +1088,11 @@ export class Client {
|
|
|
1065
1088
|
*
|
|
1066
1089
|
* @returns The Release with a matching ID, if it exists.
|
|
1067
1090
|
*/
|
|
1068
|
-
async getReleaseByID(
|
|
1069
|
-
|
|
1091
|
+
async getReleaseByID(
|
|
1092
|
+
id: string,
|
|
1093
|
+
params?: FetchParams,
|
|
1094
|
+
): Promise<prismicT.Ref> {
|
|
1095
|
+
const releases = await this.getReleases(params);
|
|
1070
1096
|
|
|
1071
1097
|
return findRefByID(releases, id);
|
|
1072
1098
|
}
|
|
@@ -1078,8 +1104,11 @@ export class Client {
|
|
|
1078
1104
|
*
|
|
1079
1105
|
* @returns The ref with a matching label, if it exists.
|
|
1080
1106
|
*/
|
|
1081
|
-
async getReleaseByLabel(
|
|
1082
|
-
|
|
1107
|
+
async getReleaseByLabel(
|
|
1108
|
+
label: string,
|
|
1109
|
+
params?: FetchParams,
|
|
1110
|
+
): Promise<prismicT.Ref> {
|
|
1111
|
+
const releases = await this.getReleases(params);
|
|
1083
1112
|
|
|
1084
1113
|
return findRefByLabel(releases, label);
|
|
1085
1114
|
}
|
|
@@ -1089,13 +1118,13 @@ export class Client {
|
|
|
1089
1118
|
*
|
|
1090
1119
|
* @returns A list of all tags used in the repository.
|
|
1091
1120
|
*/
|
|
1092
|
-
async getTags(): Promise<string[]> {
|
|
1121
|
+
async getTags(params?: FetchParams): Promise<string[]> {
|
|
1093
1122
|
try {
|
|
1094
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
1123
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
1095
1124
|
|
|
1096
1125
|
return await this.fetch<string[]>(tagsForm.action);
|
|
1097
1126
|
} catch {
|
|
1098
|
-
const repository = await this.getRepository();
|
|
1127
|
+
const repository = await this.getRepository(params);
|
|
1099
1128
|
|
|
1100
1129
|
return repository.tags;
|
|
1101
1130
|
}
|
|
@@ -1108,13 +1137,14 @@ export class Client {
|
|
|
1108
1137
|
*
|
|
1109
1138
|
* @returns A URL string that can be requested to query content.
|
|
1110
1139
|
*/
|
|
1111
|
-
async buildQueryURL(
|
|
1112
|
-
|
|
1113
|
-
|
|
1140
|
+
async buildQueryURL({
|
|
1141
|
+
signal,
|
|
1142
|
+
...params
|
|
1143
|
+
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
|
|
1114
1144
|
const ref = params.ref || (await this.getResolvedRefString());
|
|
1115
1145
|
const integrationFieldsRef =
|
|
1116
1146
|
params.integrationFieldsRef ||
|
|
1117
|
-
(await this.getCachedRepository()).integrationFieldsRef ||
|
|
1147
|
+
(await this.getCachedRepository({ signal })).integrationFieldsRef ||
|
|
1118
1148
|
undefined;
|
|
1119
1149
|
|
|
1120
1150
|
return buildQueryURL(this.endpoint, {
|
|
@@ -1146,7 +1176,9 @@ export class Client {
|
|
|
1146
1176
|
* @returns The URL for the previewed document during an active preview
|
|
1147
1177
|
* session. The user should be redirected to this URL.
|
|
1148
1178
|
*/
|
|
1149
|
-
async resolvePreviewURL(
|
|
1179
|
+
async resolvePreviewURL(
|
|
1180
|
+
args: ResolvePreviewArgs & FetchParams,
|
|
1181
|
+
): Promise<string> {
|
|
1150
1182
|
let documentID = args.documentID;
|
|
1151
1183
|
let previewToken = args.previewToken;
|
|
1152
1184
|
|
|
@@ -1164,7 +1196,9 @@ export class Client {
|
|
|
1164
1196
|
|
|
1165
1197
|
if (documentID != null) {
|
|
1166
1198
|
const document = await this.getByID(documentID, {
|
|
1199
|
+
signal: args.signal,
|
|
1167
1200
|
ref: previewToken,
|
|
1201
|
+
lang: "*",
|
|
1168
1202
|
});
|
|
1169
1203
|
|
|
1170
1204
|
// We know we have a valid field to resolve since we are using prismicH.documentToLinkField
|
|
@@ -1261,18 +1295,96 @@ export class Client {
|
|
|
1261
1295
|
};
|
|
1262
1296
|
}
|
|
1263
1297
|
|
|
1298
|
+
/**
|
|
1299
|
+
* A `fetch()` function to be used with GraphQL clients configured for
|
|
1300
|
+
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
|
|
1301
|
+
* and Authorization headers. Queries will automatically be minified by
|
|
1302
|
+
* removing whitespace where possible.
|
|
1303
|
+
*
|
|
1304
|
+
* @example
|
|
1305
|
+
*
|
|
1306
|
+
* ```ts
|
|
1307
|
+
* const graphqlClient = new ApolloClient({
|
|
1308
|
+
* link: new HttpLink({
|
|
1309
|
+
* uri: prismic.getGraphQLEndpoint(repositoryName),
|
|
1310
|
+
* // Provide `client.graphqlFetch` as the fetch implementation.
|
|
1311
|
+
* fetch: client.graphqlFetch,
|
|
1312
|
+
* // Using GET is required.
|
|
1313
|
+
* useGETForQueries: true,
|
|
1314
|
+
* }),
|
|
1315
|
+
* cache: new InMemoryCache(),
|
|
1316
|
+
* });
|
|
1317
|
+
* ```
|
|
1318
|
+
*
|
|
1319
|
+
* @param input - The `fetch()` `input` parameter. Only strings are supported.
|
|
1320
|
+
* @param init - The `fetch()` `init` parameter. Only plain objects are supported.
|
|
1321
|
+
*
|
|
1322
|
+
* @returns The `fetch()` Response for the request.
|
|
1323
|
+
* @experimental
|
|
1324
|
+
*/
|
|
1325
|
+
async graphqlFetch(
|
|
1326
|
+
input: RequestInfo,
|
|
1327
|
+
init?: RequestInit,
|
|
1328
|
+
): Promise<Response> {
|
|
1329
|
+
const cachedRepository = await this.getCachedRepository();
|
|
1330
|
+
const ref = await this.getResolvedRefString();
|
|
1331
|
+
|
|
1332
|
+
const unsanitizedHeaders: Record<string, string> = {
|
|
1333
|
+
"Prismic-ref": ref,
|
|
1334
|
+
"Prismic-integration-field-ref":
|
|
1335
|
+
cachedRepository.integrationFieldsRef || "",
|
|
1336
|
+
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
1337
|
+
// Asserting `init.headers` is a Record since popular GraphQL
|
|
1338
|
+
// libraries pass this as a Record. Header objects as input
|
|
1339
|
+
// are unsupported.
|
|
1340
|
+
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1341
|
+
};
|
|
1342
|
+
|
|
1343
|
+
// Normalize header keys to lowercase. This prevents header
|
|
1344
|
+
// conflicts between the Prismic client and the GraphQL
|
|
1345
|
+
// client.
|
|
1346
|
+
const headers: Record<string, string> = {};
|
|
1347
|
+
for (const key in unsanitizedHeaders) {
|
|
1348
|
+
if (unsanitizedHeaders[key]) {
|
|
1349
|
+
headers[key.toLowerCase()] =
|
|
1350
|
+
unsanitizedHeaders[key as keyof typeof unsanitizedHeaders];
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
// Compress the GraphQL query (if it exists) by removing
|
|
1355
|
+
// whitespace. This is done to optimize the query size and avoid hitting the
|
|
1356
|
+
// upper limit of GET requests (2048 characters).
|
|
1357
|
+
const url = new URL(
|
|
1358
|
+
// Asserting `input` is a string since popular GraphQL
|
|
1359
|
+
// libraries pass this as a string. Request objects as
|
|
1360
|
+
// input are unsupported.
|
|
1361
|
+
input as string,
|
|
1362
|
+
);
|
|
1363
|
+
const query = url.searchParams.get("query");
|
|
1364
|
+
if (query) {
|
|
1365
|
+
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
return (await this.fetchFn(url.toString(), {
|
|
1369
|
+
...init,
|
|
1370
|
+
headers,
|
|
1371
|
+
})) as Response;
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1264
1374
|
/**
|
|
1265
1375
|
* Returns a cached version of `getRepository` with a TTL.
|
|
1266
1376
|
*
|
|
1267
1377
|
* @returns Cached repository metadata.
|
|
1268
1378
|
*/
|
|
1269
|
-
private async getCachedRepository(
|
|
1379
|
+
private async getCachedRepository(
|
|
1380
|
+
params?: FetchParams,
|
|
1381
|
+
): Promise<prismicT.Repository> {
|
|
1270
1382
|
if (
|
|
1271
1383
|
!this.cachedRepository ||
|
|
1272
1384
|
Date.now() >= this.cachedRepositoryExpiration
|
|
1273
1385
|
) {
|
|
1274
1386
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
1275
|
-
this.cachedRepository = await this.getRepository();
|
|
1387
|
+
this.cachedRepository = await this.getRepository(params);
|
|
1276
1388
|
}
|
|
1277
1389
|
|
|
1278
1390
|
return this.cachedRepository;
|
|
@@ -1287,8 +1399,11 @@ export class Client {
|
|
|
1287
1399
|
* @returns The repository form.
|
|
1288
1400
|
* @throws If a matching form cannot be found.
|
|
1289
1401
|
*/
|
|
1290
|
-
private async getCachedRepositoryForm(
|
|
1291
|
-
|
|
1402
|
+
private async getCachedRepositoryForm(
|
|
1403
|
+
name: string,
|
|
1404
|
+
params?: FetchParams,
|
|
1405
|
+
): Promise<prismicT.Form> {
|
|
1406
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1292
1407
|
const form = cachedRepository.forms[name];
|
|
1293
1408
|
|
|
1294
1409
|
if (!form) {
|
|
@@ -1323,7 +1438,7 @@ export class Client {
|
|
|
1323
1438
|
*
|
|
1324
1439
|
* @returns The ref to use during a query.
|
|
1325
1440
|
*/
|
|
1326
|
-
private async getResolvedRefString(): Promise<string> {
|
|
1441
|
+
private async getResolvedRefString(params?: FetchParams): Promise<string> {
|
|
1327
1442
|
if (this.refState.autoPreviewsEnabled) {
|
|
1328
1443
|
let previewRef: string | undefined = undefined;
|
|
1329
1444
|
|
|
@@ -1341,7 +1456,7 @@ export class Client {
|
|
|
1341
1456
|
}
|
|
1342
1457
|
}
|
|
1343
1458
|
|
|
1344
|
-
const cachedRepository = await this.getCachedRepository();
|
|
1459
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1345
1460
|
|
|
1346
1461
|
const refModeType = this.refState.mode;
|
|
1347
1462
|
if (refModeType === RefStateMode.ReleaseID) {
|
|
@@ -1374,7 +1489,8 @@ export class Client {
|
|
|
1374
1489
|
private async fetch<T = unknown>(
|
|
1375
1490
|
url: string,
|
|
1376
1491
|
// TODO: Change to `params` when Authorization header support works in browsers with CORS.
|
|
1377
|
-
_params?: Partial<BuildQueryURLArgs>,
|
|
1492
|
+
// _params?: Partial<BuildQueryURLArgs>,
|
|
1493
|
+
params: FetchParams = {},
|
|
1378
1494
|
): Promise<T> {
|
|
1379
1495
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1380
1496
|
// const accessToken = (params && params.accessToken) || this.accessToken;
|
|
@@ -1382,7 +1498,9 @@ export class Client {
|
|
|
1382
1498
|
// ? { headers: { Authorization: `Token ${accessToken}` } }
|
|
1383
1499
|
// : {};
|
|
1384
1500
|
|
|
1385
|
-
const res = await this.fetchFn(url
|
|
1501
|
+
const res = await this.fetchFn(url, {
|
|
1502
|
+
signal: params.signal,
|
|
1503
|
+
});
|
|
1386
1504
|
|
|
1387
1505
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1388
1506
|
let json: any;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a repository's Prismic GraphQL endpoint.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
5
|
+
* @param repositoryName - Name of the repository.
|
|
6
|
+
*
|
|
7
|
+
* @returns The repository's Prismic REST API V2 endpoint
|
|
8
|
+
*/
|
|
9
|
+
export const getGraphQLEndpoint = <RepositoryName extends string>(
|
|
10
|
+
repositoryName: RepositoryName,
|
|
11
|
+
): `https://${RepositoryName}.cdn.prismic.io/graphql` =>
|
|
12
|
+
`https://${repositoryName}.cdn.prismic.io/graphql` as const;
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minifies a GraphQL query by removing whitespace where possible.
|
|
3
|
+
*
|
|
4
|
+
* @param query - GraphQL query to minify.
|
|
5
|
+
*
|
|
6
|
+
* @returns A minified version of `query`.
|
|
7
|
+
*/
|
|
8
|
+
export const minifyGraphQLQuery = (query: string): string => {
|
|
9
|
+
return query.replace(
|
|
10
|
+
/(\n| )*( |{|})(\n| )*/gm,
|
|
11
|
+
(_chars, _spaces, brackets) => brackets,
|
|
12
|
+
);
|
|
13
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,16 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a union of the given object's values, and optionally specify which
|
|
3
|
+
* keys to get the values from.
|
|
4
|
+
*
|
|
5
|
+
* Taken from the `type-fest` package.
|
|
6
|
+
*
|
|
7
|
+
* See:
|
|
8
|
+
* https://github.com/sindresorhus/type-fest/blob/61c35052f09caa23de5eef96d95196375d8ed498/source/value-of.d.ts
|
|
9
|
+
*/
|
|
10
|
+
export type ValueOf<
|
|
11
|
+
ObjectType,
|
|
12
|
+
ValueType extends keyof ObjectType = keyof ObjectType,
|
|
13
|
+
> = ObjectType[ValueType];
|
|
14
|
+
|
|
1
15
|
/**
|
|
2
16
|
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
17
|
+
*
|
|
18
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
|
|
3
19
|
*/
|
|
4
20
|
export type FetchLike = (
|
|
5
21
|
input: string,
|
|
6
22
|
init?: RequestInitLike,
|
|
7
23
|
) => Promise<ResponseLike>;
|
|
8
24
|
|
|
25
|
+
/**
|
|
26
|
+
* An object that allows you to abort a `fetch()` request if needed via an
|
|
27
|
+
* `AbortController` object
|
|
28
|
+
*
|
|
29
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
30
|
+
*/
|
|
31
|
+
// `any` is used often here to ensure this type is universally valid amond
|
|
32
|
+
// different AbortSignal implementations. The types of each property are not
|
|
33
|
+
// important to validate since it is blindly passed to a given `fetch()`
|
|
34
|
+
// function.
|
|
35
|
+
export type AbortSignalLike = {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
aborted: any;
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
addEventListener: any;
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
removeEventListener: any;
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
dispatchEvent: any;
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
+
onabort: any;
|
|
46
|
+
};
|
|
47
|
+
|
|
9
48
|
/**
|
|
10
49
|
* The minimum required properties from RequestInit.
|
|
11
50
|
*/
|
|
12
51
|
export interface RequestInitLike {
|
|
13
52
|
headers?: Record<string, string>;
|
|
53
|
+
// `null` is included to match TypeScript's `fetch()` type in `lib.dom.d.ts`.
|
|
54
|
+
// See: https://github.com/microsoft/TypeScript/blob/3328feb7991f358e245088d48b64ad9da8f015e2/lib/lib.dom.d.ts#L1515-L1518
|
|
55
|
+
signal?: AbortSignalLike | null;
|
|
14
56
|
}
|
|
15
57
|
|
|
16
58
|
/**
|