@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 +1 -1
- package/dist/index.cjs +59 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +61 -31
- package/dist/index.js +59 -44
- package/dist/index.js.map +1 -1
- package/package.json +14 -14
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +117 -66
- 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.
|
|
@@ -427,7 +443,7 @@ export class Client {
|
|
|
427
443
|
*/
|
|
428
444
|
async query<TDocument extends prismicT.PrismicDocument>(
|
|
429
445
|
predicates: NonNullable<BuildQueryURLArgs["predicates"]>,
|
|
430
|
-
params?: Partial<Omit<BuildQueryURLArgs, "predicates"
|
|
446
|
+
params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams,
|
|
431
447
|
): Promise<prismicT.Query<TDocument>> {
|
|
432
448
|
const url = await this.buildQueryURL({ ...params, predicates });
|
|
433
449
|
|
|
@@ -449,7 +465,7 @@ export class Client {
|
|
|
449
465
|
* @returns A paginated response containing the result of the query.
|
|
450
466
|
*/
|
|
451
467
|
async get<TDocument extends prismicT.PrismicDocument>(
|
|
452
|
-
params?: Partial<BuildQueryURLArgs
|
|
468
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
453
469
|
): Promise<prismicT.Query<TDocument>> {
|
|
454
470
|
const url = await this.buildQueryURL(params);
|
|
455
471
|
|
|
@@ -471,7 +487,7 @@ export class Client {
|
|
|
471
487
|
* The first result of the query, if any.
|
|
472
488
|
*/
|
|
473
489
|
async getFirst<TDocument extends prismicT.PrismicDocument>(
|
|
474
|
-
params?: Partial<BuildQueryURLArgs
|
|
490
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
475
491
|
): Promise<TDocument> {
|
|
476
492
|
const url = await this.buildQueryURL(params);
|
|
477
493
|
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
@@ -507,7 +523,9 @@ export class Client {
|
|
|
507
523
|
* @returns A list of documents matching the query.
|
|
508
524
|
*/
|
|
509
525
|
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(
|
|
510
|
-
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
526
|
+
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
527
|
+
GetAllParams &
|
|
528
|
+
FetchParams = {},
|
|
511
529
|
): Promise<TDocument[]> {
|
|
512
530
|
const { limit = Infinity, ...actualParams } = params;
|
|
513
531
|
const resolvedParams = {
|
|
@@ -557,7 +575,7 @@ export class Client {
|
|
|
557
575
|
*/
|
|
558
576
|
async getByID<TDocument extends prismicT.PrismicDocument>(
|
|
559
577
|
id: string,
|
|
560
|
-
params?: Partial<BuildQueryURLArgs
|
|
578
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
561
579
|
): Promise<TDocument> {
|
|
562
580
|
return await this.getFirst<TDocument>(
|
|
563
581
|
appendPredicates(params, predicate.at("document.id", id)),
|
|
@@ -589,7 +607,7 @@ export class Client {
|
|
|
589
607
|
*/
|
|
590
608
|
async getByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
591
609
|
ids: string[],
|
|
592
|
-
params?: Partial<BuildQueryURLArgs
|
|
610
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
593
611
|
): Promise<prismicT.Query<TDocument>> {
|
|
594
612
|
return await this.get<TDocument>(
|
|
595
613
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -622,7 +640,7 @@ export class Client {
|
|
|
622
640
|
*/
|
|
623
641
|
async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
624
642
|
ids: string[],
|
|
625
|
-
params?: Partial<BuildQueryURLArgs
|
|
643
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
626
644
|
): Promise<TDocument[]> {
|
|
627
645
|
return await this.dangerouslyGetAll<TDocument>(
|
|
628
646
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -653,7 +671,7 @@ export class Client {
|
|
|
653
671
|
async getByUID<TDocument extends prismicT.PrismicDocument>(
|
|
654
672
|
documentType: string,
|
|
655
673
|
uid: string,
|
|
656
|
-
params?: Partial<BuildQueryURLArgs
|
|
674
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
657
675
|
): Promise<TDocument> {
|
|
658
676
|
return await this.getFirst<TDocument>(
|
|
659
677
|
appendPredicates(params, [
|
|
@@ -690,7 +708,7 @@ export class Client {
|
|
|
690
708
|
async getByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
691
709
|
documentType: string,
|
|
692
710
|
uids: string[],
|
|
693
|
-
params?: Partial<BuildQueryURLArgs
|
|
711
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
694
712
|
): Promise<prismicT.Query<TDocument>> {
|
|
695
713
|
return await this.get<TDocument>(
|
|
696
714
|
appendPredicates(params, [
|
|
@@ -728,7 +746,7 @@ export class Client {
|
|
|
728
746
|
async getAllByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
729
747
|
documentType: string,
|
|
730
748
|
uids: string[],
|
|
731
|
-
params?: Partial<BuildQueryURLArgs
|
|
749
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
732
750
|
): Promise<TDocument[]> {
|
|
733
751
|
return await this.dangerouslyGetAll<TDocument>(
|
|
734
752
|
appendPredicates(params, [
|
|
@@ -760,7 +778,7 @@ export class Client {
|
|
|
760
778
|
*/
|
|
761
779
|
async getSingle<TDocument extends prismicT.PrismicDocument>(
|
|
762
780
|
documentType: string,
|
|
763
|
-
params?: Partial<BuildQueryURLArgs
|
|
781
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
764
782
|
): Promise<TDocument> {
|
|
765
783
|
return await this.getFirst<TDocument>(
|
|
766
784
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -787,7 +805,7 @@ export class Client {
|
|
|
787
805
|
*/
|
|
788
806
|
async getByType<TDocument extends prismicT.PrismicDocument>(
|
|
789
807
|
documentType: string,
|
|
790
|
-
params?: Partial<BuildQueryURLArgs
|
|
808
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
791
809
|
): Promise<prismicT.Query<TDocument>> {
|
|
792
810
|
return await this.get<TDocument>(
|
|
793
811
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -813,7 +831,7 @@ export class Client {
|
|
|
813
831
|
*/
|
|
814
832
|
async getAllByType<TDocument extends prismicT.PrismicDocument>(
|
|
815
833
|
documentType: string,
|
|
816
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
834
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
817
835
|
): Promise<TDocument[]> {
|
|
818
836
|
return await this.dangerouslyGetAll<TDocument>(
|
|
819
837
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -839,10 +857,10 @@ export class Client {
|
|
|
839
857
|
*/
|
|
840
858
|
async getByTag<TDocument extends prismicT.PrismicDocument>(
|
|
841
859
|
tag: string,
|
|
842
|
-
params?: Partial<BuildQueryURLArgs
|
|
860
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
843
861
|
): Promise<prismicT.Query<TDocument>> {
|
|
844
862
|
return await this.get<TDocument>(
|
|
845
|
-
appendPredicates(params,
|
|
863
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
846
864
|
);
|
|
847
865
|
}
|
|
848
866
|
|
|
@@ -865,10 +883,10 @@ export class Client {
|
|
|
865
883
|
*/
|
|
866
884
|
async getAllByTag<TDocument extends prismicT.PrismicDocument>(
|
|
867
885
|
tag: string,
|
|
868
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
886
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
869
887
|
): Promise<TDocument[]> {
|
|
870
888
|
return await this.dangerouslyGetAll<TDocument>(
|
|
871
|
-
appendPredicates(params,
|
|
889
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
872
890
|
);
|
|
873
891
|
}
|
|
874
892
|
|
|
@@ -890,7 +908,7 @@ export class Client {
|
|
|
890
908
|
*/
|
|
891
909
|
async getByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
892
910
|
tags: string[],
|
|
893
|
-
params?: Partial<BuildQueryURLArgs
|
|
911
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
894
912
|
): Promise<prismicT.Query<TDocument>> {
|
|
895
913
|
return await this.get<TDocument>(
|
|
896
914
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -917,7 +935,7 @@ export class Client {
|
|
|
917
935
|
*/
|
|
918
936
|
async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
919
937
|
tags: string[],
|
|
920
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
938
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
921
939
|
): Promise<TDocument[]> {
|
|
922
940
|
return await this.dangerouslyGetAll<TDocument>(
|
|
923
941
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -942,7 +960,7 @@ export class Client {
|
|
|
942
960
|
*/
|
|
943
961
|
async getBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
944
962
|
tags: string[],
|
|
945
|
-
params?: Partial<BuildQueryURLArgs
|
|
963
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
946
964
|
): Promise<prismicT.Query<TDocument>> {
|
|
947
965
|
return await this.get<TDocument>(
|
|
948
966
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -969,7 +987,7 @@ export class Client {
|
|
|
969
987
|
*/
|
|
970
988
|
async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
971
989
|
tags: string[],
|
|
972
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
990
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
973
991
|
): Promise<TDocument[]> {
|
|
974
992
|
return await this.dangerouslyGetAll<TDocument>(
|
|
975
993
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -982,7 +1000,7 @@ export class Client {
|
|
|
982
1000
|
*
|
|
983
1001
|
* @returns Repository metadata.
|
|
984
1002
|
*/
|
|
985
|
-
async getRepository(): Promise<prismicT.Repository> {
|
|
1003
|
+
async getRepository(params?: FetchParams): Promise<prismicT.Repository> {
|
|
986
1004
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
987
1005
|
// return await this.fetch<prismicT.Repository>(this.endpoint);
|
|
988
1006
|
|
|
@@ -992,7 +1010,7 @@ export class Client {
|
|
|
992
1010
|
url.searchParams.set("access_token", this.accessToken);
|
|
993
1011
|
}
|
|
994
1012
|
|
|
995
|
-
return await this.fetch<prismicT.Repository>(url.toString());
|
|
1013
|
+
return await this.fetch<prismicT.Repository>(url.toString(), params);
|
|
996
1014
|
}
|
|
997
1015
|
|
|
998
1016
|
/**
|
|
@@ -1004,8 +1022,8 @@ export class Client {
|
|
|
1004
1022
|
*
|
|
1005
1023
|
* @returns A list of all refs for the Prismic repository.
|
|
1006
1024
|
*/
|
|
1007
|
-
async getRefs(): Promise<prismicT.Ref[]> {
|
|
1008
|
-
const repository = await this.getRepository();
|
|
1025
|
+
async getRefs(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1026
|
+
const repository = await this.getRepository(params);
|
|
1009
1027
|
|
|
1010
1028
|
return repository.refs;
|
|
1011
1029
|
}
|
|
@@ -1017,8 +1035,8 @@ export class Client {
|
|
|
1017
1035
|
*
|
|
1018
1036
|
* @returns The ref with a matching ID, if it exists.
|
|
1019
1037
|
*/
|
|
1020
|
-
async getRefByID(id: string): Promise<prismicT.Ref> {
|
|
1021
|
-
const refs = await this.getRefs();
|
|
1038
|
+
async getRefByID(id: string, params?: FetchParams): Promise<prismicT.Ref> {
|
|
1039
|
+
const refs = await this.getRefs(params);
|
|
1022
1040
|
|
|
1023
1041
|
return findRefByID(refs, id);
|
|
1024
1042
|
}
|
|
@@ -1030,8 +1048,11 @@ export class Client {
|
|
|
1030
1048
|
*
|
|
1031
1049
|
* @returns The ref with a matching label, if it exists.
|
|
1032
1050
|
*/
|
|
1033
|
-
async getRefByLabel(
|
|
1034
|
-
|
|
1051
|
+
async getRefByLabel(
|
|
1052
|
+
label: string,
|
|
1053
|
+
params?: FetchParams,
|
|
1054
|
+
): Promise<prismicT.Ref> {
|
|
1055
|
+
const refs = await this.getRefs(params);
|
|
1035
1056
|
|
|
1036
1057
|
return findRefByLabel(refs, label);
|
|
1037
1058
|
}
|
|
@@ -1042,8 +1063,8 @@ export class Client {
|
|
|
1042
1063
|
*
|
|
1043
1064
|
* @returns The repository's master ref.
|
|
1044
1065
|
*/
|
|
1045
|
-
async getMasterRef(): Promise<prismicT.Ref> {
|
|
1046
|
-
const refs = await this.getRefs();
|
|
1066
|
+
async getMasterRef(params?: FetchParams): Promise<prismicT.Ref> {
|
|
1067
|
+
const refs = await this.getRefs(params);
|
|
1047
1068
|
|
|
1048
1069
|
return findMasterRef(refs);
|
|
1049
1070
|
}
|
|
@@ -1054,8 +1075,8 @@ export class Client {
|
|
|
1054
1075
|
*
|
|
1055
1076
|
* @returns A list of all Releases for the Prismic repository.
|
|
1056
1077
|
*/
|
|
1057
|
-
async getReleases(): Promise<prismicT.Ref[]> {
|
|
1058
|
-
const refs = await this.getRefs();
|
|
1078
|
+
async getReleases(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1079
|
+
const refs = await this.getRefs(params);
|
|
1059
1080
|
|
|
1060
1081
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
1061
1082
|
}
|
|
@@ -1067,8 +1088,11 @@ export class Client {
|
|
|
1067
1088
|
*
|
|
1068
1089
|
* @returns The Release with a matching ID, if it exists.
|
|
1069
1090
|
*/
|
|
1070
|
-
async getReleaseByID(
|
|
1071
|
-
|
|
1091
|
+
async getReleaseByID(
|
|
1092
|
+
id: string,
|
|
1093
|
+
params?: FetchParams,
|
|
1094
|
+
): Promise<prismicT.Ref> {
|
|
1095
|
+
const releases = await this.getReleases(params);
|
|
1072
1096
|
|
|
1073
1097
|
return findRefByID(releases, id);
|
|
1074
1098
|
}
|
|
@@ -1080,8 +1104,11 @@ export class Client {
|
|
|
1080
1104
|
*
|
|
1081
1105
|
* @returns The ref with a matching label, if it exists.
|
|
1082
1106
|
*/
|
|
1083
|
-
async getReleaseByLabel(
|
|
1084
|
-
|
|
1107
|
+
async getReleaseByLabel(
|
|
1108
|
+
label: string,
|
|
1109
|
+
params?: FetchParams,
|
|
1110
|
+
): Promise<prismicT.Ref> {
|
|
1111
|
+
const releases = await this.getReleases(params);
|
|
1085
1112
|
|
|
1086
1113
|
return findRefByLabel(releases, label);
|
|
1087
1114
|
}
|
|
@@ -1091,13 +1118,13 @@ export class Client {
|
|
|
1091
1118
|
*
|
|
1092
1119
|
* @returns A list of all tags used in the repository.
|
|
1093
1120
|
*/
|
|
1094
|
-
async getTags(): Promise<string[]> {
|
|
1121
|
+
async getTags(params?: FetchParams): Promise<string[]> {
|
|
1095
1122
|
try {
|
|
1096
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
1123
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
1097
1124
|
|
|
1098
1125
|
return await this.fetch<string[]>(tagsForm.action);
|
|
1099
1126
|
} catch {
|
|
1100
|
-
const repository = await this.getRepository();
|
|
1127
|
+
const repository = await this.getRepository(params);
|
|
1101
1128
|
|
|
1102
1129
|
return repository.tags;
|
|
1103
1130
|
}
|
|
@@ -1110,13 +1137,14 @@ export class Client {
|
|
|
1110
1137
|
*
|
|
1111
1138
|
* @returns A URL string that can be requested to query content.
|
|
1112
1139
|
*/
|
|
1113
|
-
async buildQueryURL(
|
|
1114
|
-
|
|
1115
|
-
|
|
1140
|
+
async buildQueryURL({
|
|
1141
|
+
signal,
|
|
1142
|
+
...params
|
|
1143
|
+
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
|
|
1116
1144
|
const ref = params.ref || (await this.getResolvedRefString());
|
|
1117
1145
|
const integrationFieldsRef =
|
|
1118
1146
|
params.integrationFieldsRef ||
|
|
1119
|
-
(await this.getCachedRepository()).integrationFieldsRef ||
|
|
1147
|
+
(await this.getCachedRepository({ signal })).integrationFieldsRef ||
|
|
1120
1148
|
undefined;
|
|
1121
1149
|
|
|
1122
1150
|
return buildQueryURL(this.endpoint, {
|
|
@@ -1148,7 +1176,9 @@ export class Client {
|
|
|
1148
1176
|
* @returns The URL for the previewed document during an active preview
|
|
1149
1177
|
* session. The user should be redirected to this URL.
|
|
1150
1178
|
*/
|
|
1151
|
-
async resolvePreviewURL(
|
|
1179
|
+
async resolvePreviewURL(
|
|
1180
|
+
args: ResolvePreviewArgs & FetchParams,
|
|
1181
|
+
): Promise<string> {
|
|
1152
1182
|
let documentID = args.documentID;
|
|
1153
1183
|
let previewToken = args.previewToken;
|
|
1154
1184
|
|
|
@@ -1166,6 +1196,7 @@ export class Client {
|
|
|
1166
1196
|
|
|
1167
1197
|
if (documentID != null) {
|
|
1168
1198
|
const document = await this.getByID(documentID, {
|
|
1199
|
+
signal: args.signal,
|
|
1169
1200
|
ref: previewToken,
|
|
1170
1201
|
lang: "*",
|
|
1171
1202
|
});
|
|
@@ -1267,7 +1298,8 @@ export class Client {
|
|
|
1267
1298
|
/**
|
|
1268
1299
|
* A `fetch()` function to be used with GraphQL clients configured for
|
|
1269
1300
|
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
|
|
1270
|
-
* and Authorization headers.
|
|
1301
|
+
* and Authorization headers. Queries will automatically be minified by
|
|
1302
|
+
* removing whitespace where possible.
|
|
1271
1303
|
*
|
|
1272
1304
|
* @example
|
|
1273
1305
|
*
|
|
@@ -1308,6 +1340,9 @@ export class Client {
|
|
|
1308
1340
|
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1309
1341
|
};
|
|
1310
1342
|
|
|
1343
|
+
// Normalize header keys to lowercase. This prevents header
|
|
1344
|
+
// conflicts between the Prismic client and the GraphQL
|
|
1345
|
+
// client.
|
|
1311
1346
|
const headers: Record<string, string> = {};
|
|
1312
1347
|
for (const key in unsanitizedHeaders) {
|
|
1313
1348
|
if (unsanitizedHeaders[key]) {
|
|
@@ -1316,16 +1351,24 @@ export class Client {
|
|
|
1316
1351
|
}
|
|
1317
1352
|
}
|
|
1318
1353
|
|
|
1319
|
-
|
|
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(
|
|
1320
1358
|
// Asserting `input` is a string since popular GraphQL
|
|
1321
1359
|
// libraries pass this as a string. Request objects as
|
|
1322
1360
|
// input are unsupported.
|
|
1323
1361
|
input as string,
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
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;
|
|
1329
1372
|
}
|
|
1330
1373
|
|
|
1331
1374
|
/**
|
|
@@ -1333,13 +1376,15 @@ export class Client {
|
|
|
1333
1376
|
*
|
|
1334
1377
|
* @returns Cached repository metadata.
|
|
1335
1378
|
*/
|
|
1336
|
-
private async getCachedRepository(
|
|
1379
|
+
private async getCachedRepository(
|
|
1380
|
+
params?: FetchParams,
|
|
1381
|
+
): Promise<prismicT.Repository> {
|
|
1337
1382
|
if (
|
|
1338
1383
|
!this.cachedRepository ||
|
|
1339
1384
|
Date.now() >= this.cachedRepositoryExpiration
|
|
1340
1385
|
) {
|
|
1341
1386
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
1342
|
-
this.cachedRepository = await this.getRepository();
|
|
1387
|
+
this.cachedRepository = await this.getRepository(params);
|
|
1343
1388
|
}
|
|
1344
1389
|
|
|
1345
1390
|
return this.cachedRepository;
|
|
@@ -1354,8 +1399,11 @@ export class Client {
|
|
|
1354
1399
|
* @returns The repository form.
|
|
1355
1400
|
* @throws If a matching form cannot be found.
|
|
1356
1401
|
*/
|
|
1357
|
-
private async getCachedRepositoryForm(
|
|
1358
|
-
|
|
1402
|
+
private async getCachedRepositoryForm(
|
|
1403
|
+
name: string,
|
|
1404
|
+
params?: FetchParams,
|
|
1405
|
+
): Promise<prismicT.Form> {
|
|
1406
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1359
1407
|
const form = cachedRepository.forms[name];
|
|
1360
1408
|
|
|
1361
1409
|
if (!form) {
|
|
@@ -1390,7 +1438,7 @@ export class Client {
|
|
|
1390
1438
|
*
|
|
1391
1439
|
* @returns The ref to use during a query.
|
|
1392
1440
|
*/
|
|
1393
|
-
private async getResolvedRefString(): Promise<string> {
|
|
1441
|
+
private async getResolvedRefString(params?: FetchParams): Promise<string> {
|
|
1394
1442
|
if (this.refState.autoPreviewsEnabled) {
|
|
1395
1443
|
let previewRef: string | undefined = undefined;
|
|
1396
1444
|
|
|
@@ -1408,7 +1456,7 @@ export class Client {
|
|
|
1408
1456
|
}
|
|
1409
1457
|
}
|
|
1410
1458
|
|
|
1411
|
-
const cachedRepository = await this.getCachedRepository();
|
|
1459
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1412
1460
|
|
|
1413
1461
|
const refModeType = this.refState.mode;
|
|
1414
1462
|
if (refModeType === RefStateMode.ReleaseID) {
|
|
@@ -1441,7 +1489,8 @@ export class Client {
|
|
|
1441
1489
|
private async fetch<T = unknown>(
|
|
1442
1490
|
url: string,
|
|
1443
1491
|
// TODO: Change to `params` when Authorization header support works in browsers with CORS.
|
|
1444
|
-
_params?: Partial<BuildQueryURLArgs>,
|
|
1492
|
+
// _params?: Partial<BuildQueryURLArgs>,
|
|
1493
|
+
params: FetchParams = {},
|
|
1445
1494
|
): Promise<T> {
|
|
1446
1495
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1447
1496
|
// const accessToken = (params && params.accessToken) || this.accessToken;
|
|
@@ -1449,7 +1498,9 @@ export class Client {
|
|
|
1449
1498
|
// ? { headers: { Authorization: `Token ${accessToken}` } }
|
|
1450
1499
|
// : {};
|
|
1451
1500
|
|
|
1452
|
-
const res = await this.fetchFn(url
|
|
1501
|
+
const res = await this.fetchFn(url, {
|
|
1502
|
+
signal: params.signal,
|
|
1503
|
+
});
|
|
1453
1504
|
|
|
1454
1505
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1455
1506
|
let json: any;
|
|
@@ -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
|
/**
|