@prismicio/client 6.2.0 → 6.4.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -3
- package/dist/index.cjs +68 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +66 -15
- package/dist/index.js +65 -22
- package/dist/index.js.map +1 -1
- package/package.json +10 -11
- package/src/client.ts +75 -33
- package/src/getGraphQLEndpoint.ts +14 -2
- package/src/getRepositoryEndpoint.ts +25 -0
- package/src/getRepositoryName.ts +23 -0
- package/src/index.ts +13 -3
- package/src/isRepositoryEndpoint.ts +17 -0
- package/src/isRepositoryName.ts +10 -0
- package/src/getEndpoint.ts +0 -12
package/dist/index.d.ts
CHANGED
|
@@ -2,14 +2,27 @@ import * as prismicT from '@prismicio/types';
|
|
|
2
2
|
import * as prismicH from '@prismicio/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Get a repository's Prismic
|
|
5
|
+
* Get a repository's Prismic Rest API V2 endpoint.
|
|
6
6
|
*
|
|
7
7
|
* @typeParam RepositoryName - Name of the Prismic repository.
|
|
8
8
|
* @param repositoryName - Name of the repository.
|
|
9
9
|
*
|
|
10
|
-
* @returns The repository's Prismic
|
|
10
|
+
* @returns The repository's Prismic Rest API V2 endpoint
|
|
11
|
+
* @throws {@link Error} Thrown if an invalid repository name is provided.
|
|
11
12
|
*/
|
|
12
|
-
declare const
|
|
13
|
+
declare const getRepositoryEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/api/v2`;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get a Prismic repository's name from its standard Prismic Rest API V2 or
|
|
17
|
+
* GraphQL endpoint.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam RepositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
|
|
20
|
+
* @param repositoryEndpoint - Prismic Rest API V2 endpoint for the repository.
|
|
21
|
+
*
|
|
22
|
+
* @returns The Prismic repository's name.
|
|
23
|
+
* @throws {@link Error} Thrown if an invalid Prismic Rest API V2 endpoint is provided.
|
|
24
|
+
*/
|
|
25
|
+
declare const getRepositoryName: (repositoryEndpoint: string) => string;
|
|
13
26
|
|
|
14
27
|
/**
|
|
15
28
|
* Get a repository's Prismic GraphQL endpoint.
|
|
@@ -21,6 +34,25 @@ declare const getEndpoint: <RepositoryName extends string>(repositoryName: Repos
|
|
|
21
34
|
*/
|
|
22
35
|
declare const getGraphQLEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/graphql`;
|
|
23
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Determines if an input is a valid Prismic repository name.
|
|
39
|
+
*
|
|
40
|
+
* @param input - Input to test.
|
|
41
|
+
*
|
|
42
|
+
* @returns `true` if `input` is a valid Prismic repository name, `false` otherwise.
|
|
43
|
+
*/
|
|
44
|
+
declare const isRepositoryName: (input: string) => boolean;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid
|
|
48
|
+
* URL is a valid endpoint to support network proxies.
|
|
49
|
+
*
|
|
50
|
+
* @param input - Input to test.
|
|
51
|
+
*
|
|
52
|
+
* @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false` otherwise.
|
|
53
|
+
*/
|
|
54
|
+
declare const isRepositoryEndpoint: (input: string) => boolean;
|
|
55
|
+
|
|
24
56
|
/**
|
|
25
57
|
* A universal API to make network requests. A subset of the `fetch()` API.
|
|
26
58
|
*
|
|
@@ -277,11 +309,11 @@ declare type GetAllParams = {
|
|
|
277
309
|
/**
|
|
278
310
|
* Arguments to determine how the URL for a preview session is resolved.
|
|
279
311
|
*/
|
|
280
|
-
declare type ResolvePreviewArgs = {
|
|
312
|
+
declare type ResolvePreviewArgs<LinkResolverReturnType> = {
|
|
281
313
|
/**
|
|
282
314
|
* A function that maps a Prismic document to a URL within your app.
|
|
283
315
|
*/
|
|
284
|
-
linkResolver?: prismicH.LinkResolverFunction
|
|
316
|
+
linkResolver?: prismicH.LinkResolverFunction<LinkResolverReturnType>;
|
|
285
317
|
/**
|
|
286
318
|
* A fallback URL if the Link Resolver does not return a value.
|
|
287
319
|
*/
|
|
@@ -299,14 +331,24 @@ declare type ResolvePreviewArgs = {
|
|
|
299
331
|
/**
|
|
300
332
|
* Creates a Prismic client that can be used to query a repository.
|
|
301
333
|
*
|
|
302
|
-
* @
|
|
303
|
-
*
|
|
334
|
+
* @example
|
|
335
|
+
*
|
|
336
|
+
* ```ts
|
|
337
|
+
* // With a repository name.
|
|
338
|
+
* createClient("qwerty");
|
|
339
|
+
*
|
|
340
|
+
* // Or with a full Prismic Rest API V2 endpoint.
|
|
341
|
+
* createClient("https://qwerty.cdn.prismic.io/api/v2");
|
|
342
|
+
* ```
|
|
343
|
+
*
|
|
344
|
+
* @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
|
|
345
|
+
* API V2 endpoint for the repository.
|
|
304
346
|
* @param options - Configuration that determines how content will be queried
|
|
305
347
|
* from the Prismic repository.
|
|
306
348
|
*
|
|
307
349
|
* @returns A client that can query content from the repository.
|
|
308
350
|
*/
|
|
309
|
-
declare const createClient: (
|
|
351
|
+
declare const createClient: (repositoryNameOrEndpoint: string, options?: ClientConfig | undefined) => Client;
|
|
310
352
|
/**
|
|
311
353
|
* A client that allows querying content from a Prismic repository.
|
|
312
354
|
*
|
|
@@ -317,7 +359,7 @@ declare const createClient: (endpoint: string, options?: ClientConfig | undefine
|
|
|
317
359
|
declare class Client {
|
|
318
360
|
/**
|
|
319
361
|
* The Prismic REST API V2 endpoint for the repository (use
|
|
320
|
-
* `prismic.
|
|
362
|
+
* `prismic.getRepositoryEndpoint` for the default endpoint).
|
|
321
363
|
*/
|
|
322
364
|
endpoint: string;
|
|
323
365
|
/**
|
|
@@ -364,14 +406,14 @@ declare class Client {
|
|
|
364
406
|
* such as Node.js, the `fetch` option must be provided as part of the
|
|
365
407
|
* `options` parameter.
|
|
366
408
|
*
|
|
367
|
-
* @param
|
|
368
|
-
*
|
|
409
|
+
* @param repositoryNameOrEndpoint - The Prismic repository name or full Rest
|
|
410
|
+
* API V2 endpoint for the repository.
|
|
369
411
|
* @param options - Configuration that determines how content will be queried
|
|
370
412
|
* from the Prismic repository.
|
|
371
413
|
*
|
|
372
414
|
* @returns A client that can query content from the repository.
|
|
373
415
|
*/
|
|
374
|
-
constructor(
|
|
416
|
+
constructor(repositoryNameOrEndpoint: string, options?: ClientConfig);
|
|
375
417
|
/**
|
|
376
418
|
* Enables the client to automatically query content from a preview session if
|
|
377
419
|
* one is active in browser environments. This is enabled by default in the browser.
|
|
@@ -894,7 +936,7 @@ declare class Client {
|
|
|
894
936
|
* @returns The URL for the previewed document during an active preview
|
|
895
937
|
* session. The user should be redirected to this URL.
|
|
896
938
|
*/
|
|
897
|
-
resolvePreviewURL(args: ResolvePreviewArgs & FetchParams): Promise<string>;
|
|
939
|
+
resolvePreviewURL<LinkResolverReturnType>(args: ResolvePreviewArgs<LinkResolverReturnType> & FetchParams): Promise<string>;
|
|
898
940
|
/**
|
|
899
941
|
* Configures the client to query the latest published content for all future queries.
|
|
900
942
|
*
|
|
@@ -957,6 +999,10 @@ declare class Client {
|
|
|
957
999
|
* @param ref - The ref or a function that returns the ref from which to query content.
|
|
958
1000
|
*/
|
|
959
1001
|
queryContentFromRef(ref: RefStringOrThunk): void;
|
|
1002
|
+
/**
|
|
1003
|
+
* @deprecated Renamed to `graphQLFetch()` (note the capitalization of "QL").
|
|
1004
|
+
*/
|
|
1005
|
+
graphqlFetch: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;
|
|
960
1006
|
/**
|
|
961
1007
|
* A `fetch()` function to be used with GraphQL clients configured for
|
|
962
1008
|
* Prismic's GraphQL API. It automatically applies the necessary `prismic-ref`
|
|
@@ -984,7 +1030,7 @@ declare class Client {
|
|
|
984
1030
|
* @returns The `fetch()` Response for the request.
|
|
985
1031
|
* @experimental
|
|
986
1032
|
*/
|
|
987
|
-
|
|
1033
|
+
graphQLFetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
|
|
988
1034
|
/**
|
|
989
1035
|
* Returns a cached version of `getRepository` with a TTL.
|
|
990
1036
|
*
|
|
@@ -1289,6 +1335,11 @@ declare namespace cookie {
|
|
|
1289
1335
|
};
|
|
1290
1336
|
}
|
|
1291
1337
|
|
|
1338
|
+
/**
|
|
1339
|
+
* @deprecated Renamed to `getRepositoryEndpoint`.
|
|
1340
|
+
*/
|
|
1341
|
+
declare const getEndpoint: <RepositoryName extends string>(repositoryName: RepositoryName) => `https://${RepositoryName}.cdn.prismic.io/api/v2`;
|
|
1342
|
+
|
|
1292
1343
|
/**
|
|
1293
1344
|
* @deprecated Renamed to `predicate` (without an "s").
|
|
1294
1345
|
*/
|
|
@@ -1356,4 +1407,4 @@ declare const Predicates: {
|
|
|
1356
1407
|
dateHourBefore: (path: string, hour: number) => string;
|
|
1357
1408
|
};
|
|
1358
1409
|
|
|
1359
|
-
export { BuildQueryURLArgs, Client, ClientConfig, FetchLike, ForbiddenError, HttpRequestLike, NotFoundError, Ordering, ParsingError, Predicates, PrismicError, QueryParams, RequestInitLike, ResponseLike, Route, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, predicate, predicates };
|
|
1410
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,49 @@
|
|
|
1
1
|
import * as prismicH from '@prismicio/helpers';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const isRepositoryName = (input) => {
|
|
4
|
+
return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
class PrismicError extends Error {
|
|
8
|
+
constructor(message = "An invalid API response was returned", url, response) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.url = url;
|
|
11
|
+
this.response = response;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
4
14
|
|
|
5
|
-
const
|
|
15
|
+
const getRepositoryEndpoint = (repositoryName) => {
|
|
16
|
+
if (isRepositoryName(repositoryName)) {
|
|
17
|
+
return `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
18
|
+
} else {
|
|
19
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getRepositoryName = (repositoryEndpoint) => {
|
|
24
|
+
try {
|
|
25
|
+
return new URL(repositoryEndpoint).hostname.split(".")[0];
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw new PrismicError(`An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, void 0, void 0);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const getGraphQLEndpoint = (repositoryName) => {
|
|
32
|
+
if (isRepositoryName(repositoryName)) {
|
|
33
|
+
return `https://${repositoryName}.cdn.prismic.io/graphql`;
|
|
34
|
+
} else {
|
|
35
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const isRepositoryEndpoint = (input) => {
|
|
40
|
+
try {
|
|
41
|
+
new URL(input);
|
|
42
|
+
return true;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
6
47
|
|
|
7
48
|
const castArray = (a) => Array.isArray(a) ? a : [a];
|
|
8
49
|
|
|
@@ -55,14 +96,6 @@ const appendPredicates = (objWithPredicates = {}, predicates) => {
|
|
|
55
96
|
|
|
56
97
|
const castThunk = (a) => typeof a === "function" ? a : () => a;
|
|
57
98
|
|
|
58
|
-
class PrismicError extends Error {
|
|
59
|
-
constructor(message = "An invalid API response was returned", url, response) {
|
|
60
|
-
super(message);
|
|
61
|
-
this.url = url;
|
|
62
|
-
this.response = response;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
99
|
const findRef = (refs, predicate) => {
|
|
67
100
|
const ref = refs.find((ref2) => predicate(ref2));
|
|
68
101
|
if (!ref) {
|
|
@@ -193,16 +226,21 @@ const everyTagPredicate = (tags) => predicate.at("document.tags", castArray(tags
|
|
|
193
226
|
const someTagsPredicate = (tags) => predicate.any("document.tags", castArray(tags));
|
|
194
227
|
const createClient = (...args) => new Client(...args);
|
|
195
228
|
class Client {
|
|
196
|
-
constructor(
|
|
229
|
+
constructor(repositoryNameOrEndpoint, options = {}) {
|
|
197
230
|
this.refState = {
|
|
198
231
|
mode: "Master" /* Master */,
|
|
199
232
|
autoPreviewsEnabled: true
|
|
200
233
|
};
|
|
201
234
|
this.cachedRepositoryExpiration = 0;
|
|
202
|
-
|
|
203
|
-
|
|
235
|
+
this.graphqlFetch = this.graphQLFetch;
|
|
236
|
+
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
237
|
+
if (process.env.NODE_ENV === "development" && /\.prismic\.io\/(?!api\/v2\/?)/.test(repositoryNameOrEndpoint)) {
|
|
238
|
+
throw new PrismicError("@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.", void 0, void 0);
|
|
239
|
+
}
|
|
240
|
+
this.endpoint = repositoryNameOrEndpoint;
|
|
241
|
+
} else {
|
|
242
|
+
this.endpoint = getRepositoryEndpoint(repositoryNameOrEndpoint);
|
|
204
243
|
}
|
|
205
|
-
this.endpoint = endpoint;
|
|
206
244
|
this.accessToken = options.accessToken;
|
|
207
245
|
this.routes = options.routes;
|
|
208
246
|
this.defaultParams = options.defaultParams;
|
|
@@ -219,7 +257,7 @@ class Client {
|
|
|
219
257
|
if (this.fetchFn === globalThis.fetch) {
|
|
220
258
|
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
221
259
|
}
|
|
222
|
-
this.
|
|
260
|
+
this.graphQLFetch = this.graphQLFetch.bind(this);
|
|
223
261
|
}
|
|
224
262
|
enableAutoPreviews() {
|
|
225
263
|
this.refState.autoPreviewsEnabled = true;
|
|
@@ -249,10 +287,11 @@ class Client {
|
|
|
249
287
|
throw new PrismicError("No documents were returned", url, void 0);
|
|
250
288
|
}
|
|
251
289
|
async dangerouslyGetAll(params = {}) {
|
|
290
|
+
var _a;
|
|
252
291
|
const { limit = Infinity, ...actualParams } = params;
|
|
253
292
|
const resolvedParams = {
|
|
254
293
|
...actualParams,
|
|
255
|
-
pageSize: actualParams.pageSize || MAX_PAGE_SIZE
|
|
294
|
+
pageSize: actualParams.pageSize || ((_a = this.defaultParams) == null ? void 0 : _a.pageSize) || MAX_PAGE_SIZE
|
|
256
295
|
};
|
|
257
296
|
const documents = [];
|
|
258
297
|
let latestResult;
|
|
@@ -391,16 +430,18 @@ class Client {
|
|
|
391
430
|
documentID = documentID || this.refState.httpRequest.query.documentId;
|
|
392
431
|
previewToken = previewToken || this.refState.httpRequest.query.token;
|
|
393
432
|
}
|
|
394
|
-
if (documentID != null) {
|
|
433
|
+
if (documentID != null && previewToken != null) {
|
|
395
434
|
const document = await this.getByID(documentID, {
|
|
396
435
|
signal: args.signal,
|
|
397
436
|
ref: previewToken,
|
|
398
437
|
lang: "*"
|
|
399
438
|
});
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
439
|
+
const url = prismicH.asLink(document, args.linkResolver);
|
|
440
|
+
if (typeof url === "string") {
|
|
441
|
+
return url;
|
|
442
|
+
}
|
|
403
443
|
}
|
|
444
|
+
return args.defaultURL;
|
|
404
445
|
}
|
|
405
446
|
queryLatestContent() {
|
|
406
447
|
this.refState.mode = "Master" /* Master */;
|
|
@@ -426,7 +467,7 @@ class Client {
|
|
|
426
467
|
ref
|
|
427
468
|
};
|
|
428
469
|
}
|
|
429
|
-
async
|
|
470
|
+
async graphQLFetch(input, init) {
|
|
430
471
|
const cachedRepository = await this.getCachedRepository();
|
|
431
472
|
const ref = await this.getResolvedRefString();
|
|
432
473
|
const unsanitizedHeaders = {
|
|
@@ -442,6 +483,7 @@ class Client {
|
|
|
442
483
|
}
|
|
443
484
|
}
|
|
444
485
|
const url = new URL(input);
|
|
486
|
+
url.searchParams.set("ref", ref);
|
|
445
487
|
const query = url.searchParams.get("query");
|
|
446
488
|
if (query) {
|
|
447
489
|
url.searchParams.set("query", minifyGraphQLQuery(query));
|
|
@@ -523,8 +565,9 @@ class Client {
|
|
|
523
565
|
}
|
|
524
566
|
}
|
|
525
567
|
|
|
568
|
+
const getEndpoint = getRepositoryEndpoint;
|
|
526
569
|
const predicates = predicate;
|
|
527
570
|
const Predicates = predicate;
|
|
528
571
|
|
|
529
|
-
export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, predicate, predicates };
|
|
572
|
+
export { Client, ForbiddenError, NotFoundError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, getGraphQLEndpoint, getRepositoryEndpoint, getRepositoryName, isRepositoryEndpoint, isRepositoryName, predicate, predicates };
|
|
530
573
|
//# sourceMappingURL=index.js.map
|