@prismicio/client 6.0.0 → 6.3.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 +3 -4
- package/dist/index.cjs +147 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +154 -40
- package/dist/index.js +143 -73
- package/dist/index.js.map +1 -1
- package/package.json +19 -16
- package/src/buildQueryURL.ts +1 -3
- package/src/client.ts +210 -75
- package/src/getGraphQLEndpoint.ts +24 -0
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -2
- package/src/isRepositoryEndpoint.ts +17 -0
- package/src/isRepositoryName.ts +10 -0
- package/src/lib/minifyGraphQLQuery.ts +13 -0
- package/src/types.ts +42 -0
- package/src/getEndpoint.ts +0 -12
package/src/client.ts
CHANGED
|
@@ -2,20 +2,24 @@ 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";
|
|
20
|
+
import { getRepositoryEndpoint } from "./getRepositoryEndpoint";
|
|
21
|
+
import { isRepositoryEndpoint } from "./isRepositoryEndpoint";
|
|
16
22
|
import { predicate } from "./predicate";
|
|
17
|
-
import * as cookie from "./cookie";
|
|
18
|
-
import { NotFoundError } from "./NotFoundError";
|
|
19
23
|
|
|
20
24
|
/**
|
|
21
25
|
* The largest page size allowed by the Prismic REST API V2. This value is used
|
|
@@ -148,6 +152,20 @@ export type ClientConfig = {
|
|
|
148
152
|
fetch?: FetchLike;
|
|
149
153
|
};
|
|
150
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Parameters for any client method that use `fetch()`. Only a subset of
|
|
157
|
+
* `fetch()` parameters are exposed.
|
|
158
|
+
*/
|
|
159
|
+
type FetchParams = {
|
|
160
|
+
/**
|
|
161
|
+
* An `AbortSignal` provided by an `AbortController`. This allows the network
|
|
162
|
+
* request to be cancelled if necessary.
|
|
163
|
+
*
|
|
164
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
165
|
+
*/
|
|
166
|
+
signal?: AbortSignalLike;
|
|
167
|
+
};
|
|
168
|
+
|
|
151
169
|
/**
|
|
152
170
|
* Parameters specific to client methods that fetch all documents. These methods
|
|
153
171
|
* start with `getAll` (for example, `getAllByType`).
|
|
@@ -205,7 +223,7 @@ const typePredicate = (documentType: string): string =>
|
|
|
205
223
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
206
224
|
*/
|
|
207
225
|
const everyTagPredicate = (tags: string | string[]): string =>
|
|
208
|
-
predicate.at("document.tags", tags);
|
|
226
|
+
predicate.at("document.tags", castArray(tags));
|
|
209
227
|
|
|
210
228
|
/**
|
|
211
229
|
* Creates a predicate to filter content by document tags. At least one matching
|
|
@@ -216,13 +234,23 @@ const everyTagPredicate = (tags: string | string[]): string =>
|
|
|
216
234
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
217
235
|
*/
|
|
218
236
|
const someTagsPredicate = (tags: string | string[]): string =>
|
|
219
|
-
predicate.any("document.tags", tags);
|
|
237
|
+
predicate.any("document.tags", castArray(tags));
|
|
220
238
|
|
|
221
239
|
/**
|
|
222
240
|
* Creates a Prismic client that can be used to query a repository.
|
|
223
241
|
*
|
|
224
|
-
* @
|
|
225
|
-
*
|
|
242
|
+
* @example
|
|
243
|
+
*
|
|
244
|
+
* ```ts
|
|
245
|
+
* // With a repository name.
|
|
246
|
+
* createClient("qwerty");
|
|
247
|
+
*
|
|
248
|
+
* // Or with a full Prismic Rest API V2 endpoint.
|
|
249
|
+
* createClient("https://qwerty.cdn.prismic.io/api/v2");
|
|
250
|
+
* ```
|
|
251
|
+
*
|
|
252
|
+
* @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
|
|
253
|
+
* API V2 endpoint for the repository.
|
|
226
254
|
* @param options - Configuration that determines how content will be queried
|
|
227
255
|
* from the Prismic repository.
|
|
228
256
|
*
|
|
@@ -242,7 +270,7 @@ export const createClient = (
|
|
|
242
270
|
export class Client {
|
|
243
271
|
/**
|
|
244
272
|
* The Prismic REST API V2 endpoint for the repository (use
|
|
245
|
-
* `prismic.
|
|
273
|
+
* `prismic.getRepositoryEndpoint` for the default endpoint).
|
|
246
274
|
*/
|
|
247
275
|
endpoint: string;
|
|
248
276
|
|
|
@@ -303,26 +331,31 @@ export class Client {
|
|
|
303
331
|
* such as Node.js, the `fetch` option must be provided as part of the
|
|
304
332
|
* `options` parameter.
|
|
305
333
|
*
|
|
306
|
-
* @param
|
|
307
|
-
*
|
|
334
|
+
* @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
|
|
335
|
+
* API V2 endpoint for the repository.
|
|
308
336
|
* @param options - Configuration that determines how content will be queried
|
|
309
337
|
* from the Prismic repository.
|
|
310
338
|
*
|
|
311
339
|
* @returns A client that can query content from the repository.
|
|
312
340
|
*/
|
|
313
|
-
constructor(
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
341
|
+
constructor(repositoryNameOrEndpoint: string, options: ClientConfig = {}) {
|
|
342
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
343
|
+
if (
|
|
344
|
+
process.env.NODE_ENV === "development" &&
|
|
345
|
+
/\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)
|
|
346
|
+
) {
|
|
347
|
+
throw new PrismicError(
|
|
348
|
+
"@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.",
|
|
349
|
+
undefined,
|
|
350
|
+
undefined,
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
this.endpoint = repositoryNameOrEndpoint;
|
|
355
|
+
} else {
|
|
356
|
+
this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
|
|
323
357
|
}
|
|
324
358
|
|
|
325
|
-
this.endpoint = endpoint;
|
|
326
359
|
this.accessToken = options.accessToken;
|
|
327
360
|
this.routes = options.routes;
|
|
328
361
|
this.defaultParams = options.defaultParams;
|
|
@@ -347,6 +380,8 @@ export class Client {
|
|
|
347
380
|
if (this.fetchFn === globalThis.fetch) {
|
|
348
381
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
349
382
|
}
|
|
383
|
+
|
|
384
|
+
this.graphqlFetch = this.graphqlFetch.bind(this);
|
|
350
385
|
}
|
|
351
386
|
|
|
352
387
|
/**
|
|
@@ -425,7 +460,7 @@ export class Client {
|
|
|
425
460
|
*/
|
|
426
461
|
async query<TDocument extends prismicT.PrismicDocument>(
|
|
427
462
|
predicates: NonNullable<BuildQueryURLArgs["predicates"]>,
|
|
428
|
-
params?: Partial<Omit<BuildQueryURLArgs, "predicates"
|
|
463
|
+
params?: Partial<Omit<BuildQueryURLArgs, "predicates">> & FetchParams,
|
|
429
464
|
): Promise<prismicT.Query<TDocument>> {
|
|
430
465
|
const url = await this.buildQueryURL({ ...params, predicates });
|
|
431
466
|
|
|
@@ -447,7 +482,7 @@ export class Client {
|
|
|
447
482
|
* @returns A paginated response containing the result of the query.
|
|
448
483
|
*/
|
|
449
484
|
async get<TDocument extends prismicT.PrismicDocument>(
|
|
450
|
-
params?: Partial<BuildQueryURLArgs
|
|
485
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
451
486
|
): Promise<prismicT.Query<TDocument>> {
|
|
452
487
|
const url = await this.buildQueryURL(params);
|
|
453
488
|
|
|
@@ -469,7 +504,7 @@ export class Client {
|
|
|
469
504
|
* The first result of the query, if any.
|
|
470
505
|
*/
|
|
471
506
|
async getFirst<TDocument extends prismicT.PrismicDocument>(
|
|
472
|
-
params?: Partial<BuildQueryURLArgs
|
|
507
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
473
508
|
): Promise<TDocument> {
|
|
474
509
|
const url = await this.buildQueryURL(params);
|
|
475
510
|
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
@@ -505,7 +540,9 @@ export class Client {
|
|
|
505
540
|
* @returns A list of documents matching the query.
|
|
506
541
|
*/
|
|
507
542
|
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(
|
|
508
|
-
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
543
|
+
params: Partial<Omit<BuildQueryURLArgs, "page">> &
|
|
544
|
+
GetAllParams &
|
|
545
|
+
FetchParams = {},
|
|
509
546
|
): Promise<TDocument[]> {
|
|
510
547
|
const { limit = Infinity, ...actualParams } = params;
|
|
511
548
|
const resolvedParams = {
|
|
@@ -555,7 +592,7 @@ export class Client {
|
|
|
555
592
|
*/
|
|
556
593
|
async getByID<TDocument extends prismicT.PrismicDocument>(
|
|
557
594
|
id: string,
|
|
558
|
-
params?: Partial<BuildQueryURLArgs
|
|
595
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
559
596
|
): Promise<TDocument> {
|
|
560
597
|
return await this.getFirst<TDocument>(
|
|
561
598
|
appendPredicates(params, predicate.at("document.id", id)),
|
|
@@ -587,7 +624,7 @@ export class Client {
|
|
|
587
624
|
*/
|
|
588
625
|
async getByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
589
626
|
ids: string[],
|
|
590
|
-
params?: Partial<BuildQueryURLArgs
|
|
627
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
591
628
|
): Promise<prismicT.Query<TDocument>> {
|
|
592
629
|
return await this.get<TDocument>(
|
|
593
630
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -620,7 +657,7 @@ export class Client {
|
|
|
620
657
|
*/
|
|
621
658
|
async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
622
659
|
ids: string[],
|
|
623
|
-
params?: Partial<BuildQueryURLArgs
|
|
660
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
624
661
|
): Promise<TDocument[]> {
|
|
625
662
|
return await this.dangerouslyGetAll<TDocument>(
|
|
626
663
|
appendPredicates(params, predicate.in("document.id", ids)),
|
|
@@ -651,7 +688,7 @@ export class Client {
|
|
|
651
688
|
async getByUID<TDocument extends prismicT.PrismicDocument>(
|
|
652
689
|
documentType: string,
|
|
653
690
|
uid: string,
|
|
654
|
-
params?: Partial<BuildQueryURLArgs
|
|
691
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
655
692
|
): Promise<TDocument> {
|
|
656
693
|
return await this.getFirst<TDocument>(
|
|
657
694
|
appendPredicates(params, [
|
|
@@ -688,7 +725,7 @@ export class Client {
|
|
|
688
725
|
async getByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
689
726
|
documentType: string,
|
|
690
727
|
uids: string[],
|
|
691
|
-
params?: Partial<BuildQueryURLArgs
|
|
728
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
692
729
|
): Promise<prismicT.Query<TDocument>> {
|
|
693
730
|
return await this.get<TDocument>(
|
|
694
731
|
appendPredicates(params, [
|
|
@@ -726,7 +763,7 @@ export class Client {
|
|
|
726
763
|
async getAllByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
727
764
|
documentType: string,
|
|
728
765
|
uids: string[],
|
|
729
|
-
params?: Partial<BuildQueryURLArgs
|
|
766
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
730
767
|
): Promise<TDocument[]> {
|
|
731
768
|
return await this.dangerouslyGetAll<TDocument>(
|
|
732
769
|
appendPredicates(params, [
|
|
@@ -758,7 +795,7 @@ export class Client {
|
|
|
758
795
|
*/
|
|
759
796
|
async getSingle<TDocument extends prismicT.PrismicDocument>(
|
|
760
797
|
documentType: string,
|
|
761
|
-
params?: Partial<BuildQueryURLArgs
|
|
798
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
762
799
|
): Promise<TDocument> {
|
|
763
800
|
return await this.getFirst<TDocument>(
|
|
764
801
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -785,7 +822,7 @@ export class Client {
|
|
|
785
822
|
*/
|
|
786
823
|
async getByType<TDocument extends prismicT.PrismicDocument>(
|
|
787
824
|
documentType: string,
|
|
788
|
-
params?: Partial<BuildQueryURLArgs
|
|
825
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
789
826
|
): Promise<prismicT.Query<TDocument>> {
|
|
790
827
|
return await this.get<TDocument>(
|
|
791
828
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -811,7 +848,7 @@ export class Client {
|
|
|
811
848
|
*/
|
|
812
849
|
async getAllByType<TDocument extends prismicT.PrismicDocument>(
|
|
813
850
|
documentType: string,
|
|
814
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
851
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
815
852
|
): Promise<TDocument[]> {
|
|
816
853
|
return await this.dangerouslyGetAll<TDocument>(
|
|
817
854
|
appendPredicates(params, typePredicate(documentType)),
|
|
@@ -837,10 +874,10 @@ export class Client {
|
|
|
837
874
|
*/
|
|
838
875
|
async getByTag<TDocument extends prismicT.PrismicDocument>(
|
|
839
876
|
tag: string,
|
|
840
|
-
params?: Partial<BuildQueryURLArgs
|
|
877
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
841
878
|
): Promise<prismicT.Query<TDocument>> {
|
|
842
879
|
return await this.get<TDocument>(
|
|
843
|
-
appendPredicates(params,
|
|
880
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
844
881
|
);
|
|
845
882
|
}
|
|
846
883
|
|
|
@@ -863,10 +900,10 @@ export class Client {
|
|
|
863
900
|
*/
|
|
864
901
|
async getAllByTag<TDocument extends prismicT.PrismicDocument>(
|
|
865
902
|
tag: string,
|
|
866
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
903
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
867
904
|
): Promise<TDocument[]> {
|
|
868
905
|
return await this.dangerouslyGetAll<TDocument>(
|
|
869
|
-
appendPredicates(params,
|
|
906
|
+
appendPredicates(params, someTagsPredicate(tag)),
|
|
870
907
|
);
|
|
871
908
|
}
|
|
872
909
|
|
|
@@ -888,7 +925,7 @@ export class Client {
|
|
|
888
925
|
*/
|
|
889
926
|
async getByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
890
927
|
tags: string[],
|
|
891
|
-
params?: Partial<BuildQueryURLArgs
|
|
928
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
892
929
|
): Promise<prismicT.Query<TDocument>> {
|
|
893
930
|
return await this.get<TDocument>(
|
|
894
931
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -915,7 +952,7 @@ export class Client {
|
|
|
915
952
|
*/
|
|
916
953
|
async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
917
954
|
tags: string[],
|
|
918
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
955
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
919
956
|
): Promise<TDocument[]> {
|
|
920
957
|
return await this.dangerouslyGetAll<TDocument>(
|
|
921
958
|
appendPredicates(params, everyTagPredicate(tags)),
|
|
@@ -940,7 +977,7 @@ export class Client {
|
|
|
940
977
|
*/
|
|
941
978
|
async getBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
942
979
|
tags: string[],
|
|
943
|
-
params?: Partial<BuildQueryURLArgs
|
|
980
|
+
params?: Partial<BuildQueryURLArgs> & FetchParams,
|
|
944
981
|
): Promise<prismicT.Query<TDocument>> {
|
|
945
982
|
return await this.get<TDocument>(
|
|
946
983
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -967,7 +1004,7 @@ export class Client {
|
|
|
967
1004
|
*/
|
|
968
1005
|
async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
969
1006
|
tags: string[],
|
|
970
|
-
params?: Partial<Omit<BuildQueryURLArgs, "page"
|
|
1007
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">> & FetchParams,
|
|
971
1008
|
): Promise<TDocument[]> {
|
|
972
1009
|
return await this.dangerouslyGetAll<TDocument>(
|
|
973
1010
|
appendPredicates(params, someTagsPredicate(tags)),
|
|
@@ -980,7 +1017,7 @@ export class Client {
|
|
|
980
1017
|
*
|
|
981
1018
|
* @returns Repository metadata.
|
|
982
1019
|
*/
|
|
983
|
-
async getRepository(): Promise<prismicT.Repository> {
|
|
1020
|
+
async getRepository(params?: FetchParams): Promise<prismicT.Repository> {
|
|
984
1021
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
985
1022
|
// return await this.fetch<prismicT.Repository>(this.endpoint);
|
|
986
1023
|
|
|
@@ -990,7 +1027,7 @@ export class Client {
|
|
|
990
1027
|
url.searchParams.set("access_token", this.accessToken);
|
|
991
1028
|
}
|
|
992
1029
|
|
|
993
|
-
return await this.fetch<prismicT.Repository>(url.toString());
|
|
1030
|
+
return await this.fetch<prismicT.Repository>(url.toString(), params);
|
|
994
1031
|
}
|
|
995
1032
|
|
|
996
1033
|
/**
|
|
@@ -1002,8 +1039,8 @@ export class Client {
|
|
|
1002
1039
|
*
|
|
1003
1040
|
* @returns A list of all refs for the Prismic repository.
|
|
1004
1041
|
*/
|
|
1005
|
-
async getRefs(): Promise<prismicT.Ref[]> {
|
|
1006
|
-
const repository = await this.getRepository();
|
|
1042
|
+
async getRefs(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1043
|
+
const repository = await this.getRepository(params);
|
|
1007
1044
|
|
|
1008
1045
|
return repository.refs;
|
|
1009
1046
|
}
|
|
@@ -1015,8 +1052,8 @@ export class Client {
|
|
|
1015
1052
|
*
|
|
1016
1053
|
* @returns The ref with a matching ID, if it exists.
|
|
1017
1054
|
*/
|
|
1018
|
-
async getRefByID(id: string): Promise<prismicT.Ref> {
|
|
1019
|
-
const refs = await this.getRefs();
|
|
1055
|
+
async getRefByID(id: string, params?: FetchParams): Promise<prismicT.Ref> {
|
|
1056
|
+
const refs = await this.getRefs(params);
|
|
1020
1057
|
|
|
1021
1058
|
return findRefByID(refs, id);
|
|
1022
1059
|
}
|
|
@@ -1028,8 +1065,11 @@ export class Client {
|
|
|
1028
1065
|
*
|
|
1029
1066
|
* @returns The ref with a matching label, if it exists.
|
|
1030
1067
|
*/
|
|
1031
|
-
async getRefByLabel(
|
|
1032
|
-
|
|
1068
|
+
async getRefByLabel(
|
|
1069
|
+
label: string,
|
|
1070
|
+
params?: FetchParams,
|
|
1071
|
+
): Promise<prismicT.Ref> {
|
|
1072
|
+
const refs = await this.getRefs(params);
|
|
1033
1073
|
|
|
1034
1074
|
return findRefByLabel(refs, label);
|
|
1035
1075
|
}
|
|
@@ -1040,8 +1080,8 @@ export class Client {
|
|
|
1040
1080
|
*
|
|
1041
1081
|
* @returns The repository's master ref.
|
|
1042
1082
|
*/
|
|
1043
|
-
async getMasterRef(): Promise<prismicT.Ref> {
|
|
1044
|
-
const refs = await this.getRefs();
|
|
1083
|
+
async getMasterRef(params?: FetchParams): Promise<prismicT.Ref> {
|
|
1084
|
+
const refs = await this.getRefs(params);
|
|
1045
1085
|
|
|
1046
1086
|
return findMasterRef(refs);
|
|
1047
1087
|
}
|
|
@@ -1052,8 +1092,8 @@ export class Client {
|
|
|
1052
1092
|
*
|
|
1053
1093
|
* @returns A list of all Releases for the Prismic repository.
|
|
1054
1094
|
*/
|
|
1055
|
-
async getReleases(): Promise<prismicT.Ref[]> {
|
|
1056
|
-
const refs = await this.getRefs();
|
|
1095
|
+
async getReleases(params?: FetchParams): Promise<prismicT.Ref[]> {
|
|
1096
|
+
const refs = await this.getRefs(params);
|
|
1057
1097
|
|
|
1058
1098
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
1059
1099
|
}
|
|
@@ -1065,8 +1105,11 @@ export class Client {
|
|
|
1065
1105
|
*
|
|
1066
1106
|
* @returns The Release with a matching ID, if it exists.
|
|
1067
1107
|
*/
|
|
1068
|
-
async getReleaseByID(
|
|
1069
|
-
|
|
1108
|
+
async getReleaseByID(
|
|
1109
|
+
id: string,
|
|
1110
|
+
params?: FetchParams,
|
|
1111
|
+
): Promise<prismicT.Ref> {
|
|
1112
|
+
const releases = await this.getReleases(params);
|
|
1070
1113
|
|
|
1071
1114
|
return findRefByID(releases, id);
|
|
1072
1115
|
}
|
|
@@ -1078,8 +1121,11 @@ export class Client {
|
|
|
1078
1121
|
*
|
|
1079
1122
|
* @returns The ref with a matching label, if it exists.
|
|
1080
1123
|
*/
|
|
1081
|
-
async getReleaseByLabel(
|
|
1082
|
-
|
|
1124
|
+
async getReleaseByLabel(
|
|
1125
|
+
label: string,
|
|
1126
|
+
params?: FetchParams,
|
|
1127
|
+
): Promise<prismicT.Ref> {
|
|
1128
|
+
const releases = await this.getReleases(params);
|
|
1083
1129
|
|
|
1084
1130
|
return findRefByLabel(releases, label);
|
|
1085
1131
|
}
|
|
@@ -1089,13 +1135,13 @@ export class Client {
|
|
|
1089
1135
|
*
|
|
1090
1136
|
* @returns A list of all tags used in the repository.
|
|
1091
1137
|
*/
|
|
1092
|
-
async getTags(): Promise<string[]> {
|
|
1138
|
+
async getTags(params?: FetchParams): Promise<string[]> {
|
|
1093
1139
|
try {
|
|
1094
|
-
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
1140
|
+
const tagsForm = await this.getCachedRepositoryForm("tags", params);
|
|
1095
1141
|
|
|
1096
1142
|
return await this.fetch<string[]>(tagsForm.action);
|
|
1097
1143
|
} catch {
|
|
1098
|
-
const repository = await this.getRepository();
|
|
1144
|
+
const repository = await this.getRepository(params);
|
|
1099
1145
|
|
|
1100
1146
|
return repository.tags;
|
|
1101
1147
|
}
|
|
@@ -1108,13 +1154,14 @@ export class Client {
|
|
|
1108
1154
|
*
|
|
1109
1155
|
* @returns A URL string that can be requested to query content.
|
|
1110
1156
|
*/
|
|
1111
|
-
async buildQueryURL(
|
|
1112
|
-
|
|
1113
|
-
|
|
1157
|
+
async buildQueryURL({
|
|
1158
|
+
signal,
|
|
1159
|
+
...params
|
|
1160
|
+
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
|
|
1114
1161
|
const ref = params.ref || (await this.getResolvedRefString());
|
|
1115
1162
|
const integrationFieldsRef =
|
|
1116
1163
|
params.integrationFieldsRef ||
|
|
1117
|
-
(await this.getCachedRepository()).integrationFieldsRef ||
|
|
1164
|
+
(await this.getCachedRepository({ signal })).integrationFieldsRef ||
|
|
1118
1165
|
undefined;
|
|
1119
1166
|
|
|
1120
1167
|
return buildQueryURL(this.endpoint, {
|
|
@@ -1146,7 +1193,9 @@ export class Client {
|
|
|
1146
1193
|
* @returns The URL for the previewed document during an active preview
|
|
1147
1194
|
* session. The user should be redirected to this URL.
|
|
1148
1195
|
*/
|
|
1149
|
-
async resolvePreviewURL(
|
|
1196
|
+
async resolvePreviewURL(
|
|
1197
|
+
args: ResolvePreviewArgs & FetchParams,
|
|
1198
|
+
): Promise<string> {
|
|
1150
1199
|
let documentID = args.documentID;
|
|
1151
1200
|
let previewToken = args.previewToken;
|
|
1152
1201
|
|
|
@@ -1164,7 +1213,9 @@ export class Client {
|
|
|
1164
1213
|
|
|
1165
1214
|
if (documentID != null) {
|
|
1166
1215
|
const document = await this.getByID(documentID, {
|
|
1216
|
+
signal: args.signal,
|
|
1167
1217
|
ref: previewToken,
|
|
1218
|
+
lang: "*",
|
|
1168
1219
|
});
|
|
1169
1220
|
|
|
1170
1221
|
// We know we have a valid field to resolve since we are using prismicH.documentToLinkField
|
|
@@ -1261,18 +1312,96 @@ export class Client {
|
|
|
1261
1312
|
};
|
|
1262
1313
|
}
|
|
1263
1314
|
|
|
1315
|
+
/**
|
|
1316
|
+
* A `fetch()` function to be used with GraphQL clients configured for
|
|
1317
|
+
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
|
|
1318
|
+
* and Authorization headers. Queries will automatically be minified by
|
|
1319
|
+
* removing whitespace where possible.
|
|
1320
|
+
*
|
|
1321
|
+
* @example
|
|
1322
|
+
*
|
|
1323
|
+
* ```ts
|
|
1324
|
+
* const graphqlClient = new ApolloClient({
|
|
1325
|
+
* link: new HttpLink({
|
|
1326
|
+
* uri: prismic.getGraphQLEndpoint(repositoryName),
|
|
1327
|
+
* // Provide `client.graphqlFetch` as the fetch implementation.
|
|
1328
|
+
* fetch: client.graphqlFetch,
|
|
1329
|
+
* // Using GET is required.
|
|
1330
|
+
* useGETForQueries: true,
|
|
1331
|
+
* }),
|
|
1332
|
+
* cache: new InMemoryCache(),
|
|
1333
|
+
* });
|
|
1334
|
+
* ```
|
|
1335
|
+
*
|
|
1336
|
+
* @param input - The `fetch()` `input` parameter. Only strings are supported.
|
|
1337
|
+
* @param init - The `fetch()` `init` parameter. Only plain objects are supported.
|
|
1338
|
+
*
|
|
1339
|
+
* @returns The `fetch()` Response for the request.
|
|
1340
|
+
* @experimental
|
|
1341
|
+
*/
|
|
1342
|
+
async graphqlFetch(
|
|
1343
|
+
input: RequestInfo,
|
|
1344
|
+
init?: RequestInit,
|
|
1345
|
+
): Promise<Response> {
|
|
1346
|
+
const cachedRepository = await this.getCachedRepository();
|
|
1347
|
+
const ref = await this.getResolvedRefString();
|
|
1348
|
+
|
|
1349
|
+
const unsanitizedHeaders: Record<string, string> = {
|
|
1350
|
+
"Prismic-ref": ref,
|
|
1351
|
+
"Prismic-integration-field-ref":
|
|
1352
|
+
cachedRepository.integrationFieldsRef || "",
|
|
1353
|
+
Authorization: this.accessToken ? `Token ${this.accessToken}` : "",
|
|
1354
|
+
// Asserting `init.headers` is a Record since popular GraphQL
|
|
1355
|
+
// libraries pass this as a Record. Header objects as input
|
|
1356
|
+
// are unsupported.
|
|
1357
|
+
...(init ? (init.headers as Record<string, string>) : {}),
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
// Normalize header keys to lowercase. This prevents header
|
|
1361
|
+
// conflicts between the Prismic client and the GraphQL
|
|
1362
|
+
// client.
|
|
1363
|
+
const headers: Record<string, string> = {};
|
|
1364
|
+
for (const key in unsanitizedHeaders) {
|
|
1365
|
+
if (unsanitizedHeaders[key]) {
|
|
1366
|
+
headers[key.toLowerCase()] =
|
|
1367
|
+
unsanitizedHeaders[key as keyof typeof unsanitizedHeaders];
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// Compress the GraphQL query (if it exists) by removing
|
|
1372
|
+
// whitespace. This is done to optimize the query size and avoid hitting the
|
|
1373
|
+
// upper limit of GET requests (2048 characters).
|
|
1374
|
+
const url = new URL(
|
|
1375
|
+
// Asserting `input` is a string since popular GraphQL
|
|
1376
|
+
// libraries pass this as a string. Request objects as
|
|
1377
|
+
// input are unsupported.
|
|
1378
|
+
input as string,
|
|
1379
|
+
);
|
|
1380
|
+
const query = url.searchParams.get("query");
|
|
1381
|
+
if (query) {
|
|
1382
|
+
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
return (await this.fetchFn(url.toString(), {
|
|
1386
|
+
...init,
|
|
1387
|
+
headers,
|
|
1388
|
+
})) as Response;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1264
1391
|
/**
|
|
1265
1392
|
* Returns a cached version of `getRepository` with a TTL.
|
|
1266
1393
|
*
|
|
1267
1394
|
* @returns Cached repository metadata.
|
|
1268
1395
|
*/
|
|
1269
|
-
private async getCachedRepository(
|
|
1396
|
+
private async getCachedRepository(
|
|
1397
|
+
params?: FetchParams,
|
|
1398
|
+
): Promise<prismicT.Repository> {
|
|
1270
1399
|
if (
|
|
1271
1400
|
!this.cachedRepository ||
|
|
1272
1401
|
Date.now() >= this.cachedRepositoryExpiration
|
|
1273
1402
|
) {
|
|
1274
1403
|
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
1275
|
-
this.cachedRepository = await this.getRepository();
|
|
1404
|
+
this.cachedRepository = await this.getRepository(params);
|
|
1276
1405
|
}
|
|
1277
1406
|
|
|
1278
1407
|
return this.cachedRepository;
|
|
@@ -1287,8 +1416,11 @@ export class Client {
|
|
|
1287
1416
|
* @returns The repository form.
|
|
1288
1417
|
* @throws If a matching form cannot be found.
|
|
1289
1418
|
*/
|
|
1290
|
-
private async getCachedRepositoryForm(
|
|
1291
|
-
|
|
1419
|
+
private async getCachedRepositoryForm(
|
|
1420
|
+
name: string,
|
|
1421
|
+
params?: FetchParams,
|
|
1422
|
+
): Promise<prismicT.Form> {
|
|
1423
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1292
1424
|
const form = cachedRepository.forms[name];
|
|
1293
1425
|
|
|
1294
1426
|
if (!form) {
|
|
@@ -1323,7 +1455,7 @@ export class Client {
|
|
|
1323
1455
|
*
|
|
1324
1456
|
* @returns The ref to use during a query.
|
|
1325
1457
|
*/
|
|
1326
|
-
private async getResolvedRefString(): Promise<string> {
|
|
1458
|
+
private async getResolvedRefString(params?: FetchParams): Promise<string> {
|
|
1327
1459
|
if (this.refState.autoPreviewsEnabled) {
|
|
1328
1460
|
let previewRef: string | undefined = undefined;
|
|
1329
1461
|
|
|
@@ -1341,7 +1473,7 @@ export class Client {
|
|
|
1341
1473
|
}
|
|
1342
1474
|
}
|
|
1343
1475
|
|
|
1344
|
-
const cachedRepository = await this.getCachedRepository();
|
|
1476
|
+
const cachedRepository = await this.getCachedRepository(params);
|
|
1345
1477
|
|
|
1346
1478
|
const refModeType = this.refState.mode;
|
|
1347
1479
|
if (refModeType === RefStateMode.ReleaseID) {
|
|
@@ -1374,7 +1506,8 @@ export class Client {
|
|
|
1374
1506
|
private async fetch<T = unknown>(
|
|
1375
1507
|
url: string,
|
|
1376
1508
|
// TODO: Change to `params` when Authorization header support works in browsers with CORS.
|
|
1377
|
-
_params?: Partial<BuildQueryURLArgs>,
|
|
1509
|
+
// _params?: Partial<BuildQueryURLArgs>,
|
|
1510
|
+
params: FetchParams = {},
|
|
1378
1511
|
): Promise<T> {
|
|
1379
1512
|
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1380
1513
|
// const accessToken = (params && params.accessToken) || this.accessToken;
|
|
@@ -1382,7 +1515,9 @@ export class Client {
|
|
|
1382
1515
|
// ? { headers: { Authorization: `Token ${accessToken}` } }
|
|
1383
1516
|
// : {};
|
|
1384
1517
|
|
|
1385
|
-
const res = await this.fetchFn(url
|
|
1518
|
+
const res = await this.fetchFn(url, {
|
|
1519
|
+
signal: params.signal,
|
|
1520
|
+
});
|
|
1386
1521
|
|
|
1387
1522
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1388
1523
|
let json: any;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isRepositoryName } from "./isRepositoryName";
|
|
2
|
+
import { PrismicError } from "./PrismicError";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get a repository's Prismic GraphQL endpoint.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
8
|
+
* @param repositoryName - Name of the repository.
|
|
9
|
+
*
|
|
10
|
+
* @returns The repository's Prismic REST API V2 endpoint
|
|
11
|
+
*/
|
|
12
|
+
export const getGraphQLEndpoint = <RepositoryName extends string>(
|
|
13
|
+
repositoryName: RepositoryName,
|
|
14
|
+
): `https://${RepositoryName}.cdn.prismic.io/graphql` => {
|
|
15
|
+
if (isRepositoryName(repositoryName)) {
|
|
16
|
+
return `https://${repositoryName}.cdn.prismic.io/graphql` as const;
|
|
17
|
+
} else {
|
|
18
|
+
throw new PrismicError(
|
|
19
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
20
|
+
undefined,
|
|
21
|
+
undefined,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isRepositoryName } from "./isRepositoryName";
|
|
2
|
+
import { PrismicError } from "./PrismicError";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get a repository's Prismic Rest API V2 endpoint.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
8
|
+
* @param repositoryName - Name of the repository.
|
|
9
|
+
*
|
|
10
|
+
* @returns The repository's Prismic Rest API V2 endpoint
|
|
11
|
+
* @throws {@link Error} Thrown if an invalid repository name is provided.
|
|
12
|
+
*/
|
|
13
|
+
export const getRepositoryEndpoint = <RepositoryName extends string>(
|
|
14
|
+
repositoryName: RepositoryName,
|
|
15
|
+
): `https://${RepositoryName}.cdn.prismic.io/api/v2` => {
|
|
16
|
+
if (isRepositoryName(repositoryName)) {
|
|
17
|
+
return `https://${repositoryName}.cdn.prismic.io/api/v2` as const;
|
|
18
|
+
} else {
|
|
19
|
+
throw new PrismicError(
|
|
20
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
21
|
+
undefined,
|
|
22
|
+
undefined,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
};
|