@prismicio/client 6.0.0-alpha.9 → 6.0.0-beta.3
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 +38 -34
- package/dist/index.cjs +226 -300
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +364 -433
- package/dist/index.mjs +220 -292
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -25
- package/src/ForbiddenError.ts +8 -41
- package/src/ParsingError.ts +4 -34
- package/src/PrismicError.ts +11 -12
- package/src/buildQueryURL.ts +1 -6
- package/src/client.ts +391 -413
- package/src/index.ts +1 -7
- package/src/lib/appendPredicates.ts +15 -15
- package/src/lib/findMasterRef.ts +15 -0
- package/src/lib/findRef.ts +27 -0
- package/src/lib/findRefByID.ts +16 -0
- package/src/lib/findRefByLabel.ts +19 -0
- package/src/predicate.ts +231 -237
- package/src/types.ts +0 -63
package/src/client.ts
CHANGED
|
@@ -3,82 +3,18 @@ import * as prismicH from "@prismicio/helpers";
|
|
|
3
3
|
|
|
4
4
|
import { appendPredicates } from "./lib/appendPredicates";
|
|
5
5
|
import { castThunk } from "./lib/castThunk";
|
|
6
|
+
import { findMasterRef } from "./lib/findMasterRef";
|
|
7
|
+
import { findRefByID } from "./lib/findRefByID";
|
|
8
|
+
import { findRefByLabel } from "./lib/findRefByLabel";
|
|
6
9
|
import { getCookie } from "./lib/getCookie";
|
|
7
10
|
|
|
8
|
-
import {
|
|
9
|
-
FetchLike,
|
|
10
|
-
Form,
|
|
11
|
-
HttpRequestLike,
|
|
12
|
-
Query,
|
|
13
|
-
Ref,
|
|
14
|
-
Repository,
|
|
15
|
-
// TODO: Uncomment when the Authorization header can be used
|
|
16
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
17
|
-
// RequestInitLike
|
|
18
|
-
} from "./types";
|
|
11
|
+
import { FetchLike, HttpRequestLike } from "./types";
|
|
19
12
|
import { buildQueryURL, BuildQueryURLArgs } from "./buildQueryURL";
|
|
20
|
-
import { ForbiddenError
|
|
21
|
-
import { ParsingError
|
|
13
|
+
import { ForbiddenError } from "./ForbiddenError";
|
|
14
|
+
import { ParsingError } from "./ParsingError";
|
|
22
15
|
import { PrismicError } from "./PrismicError";
|
|
16
|
+
import { predicate } from "./predicate";
|
|
23
17
|
import * as cookie from "./cookie";
|
|
24
|
-
import * as predicate from "./predicate";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A container for items in a SimpleTTLCache.
|
|
28
|
-
*/
|
|
29
|
-
type SimpleTTLCacheValueContainer<T = unknown> = {
|
|
30
|
-
expiresAt: number;
|
|
31
|
-
value: T;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* A simple time-to-live cache where items are only valid for a given number of
|
|
36
|
-
* milliseconds.
|
|
37
|
-
*/
|
|
38
|
-
interface SimpleTTLCache {
|
|
39
|
-
get<T>(key: string): T | undefined;
|
|
40
|
-
set<T>(key: string, value: T, ttl?: number): void;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Creates a simple cache where each element has a given time to live (TTL).
|
|
45
|
-
* After the TTL has ellapsed, the value is considered stale and will not be
|
|
46
|
-
* returned if requested.
|
|
47
|
-
*/
|
|
48
|
-
const createSimpleTTLCache = (): SimpleTTLCache => {
|
|
49
|
-
const cache = new Map<string, SimpleTTLCacheValueContainer>();
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
/**
|
|
53
|
-
* Get a value from the cache. If the item's TTL has ellapsed, `undefined`
|
|
54
|
-
* will be returned.
|
|
55
|
-
*
|
|
56
|
-
* @param key - Key for the item.
|
|
57
|
-
*
|
|
58
|
-
* @returns The value for the key, if a value exists.
|
|
59
|
-
*/
|
|
60
|
-
get<T>(key: string): T | undefined {
|
|
61
|
-
const cacheValue = cache.get(key);
|
|
62
|
-
|
|
63
|
-
if (cacheValue) {
|
|
64
|
-
if (new Date().getTime() < cacheValue.expiresAt) {
|
|
65
|
-
return cacheValue.value as T;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Set a value in the cache for a given key and TTL.
|
|
72
|
-
*
|
|
73
|
-
* @param key - Key to identify the item.
|
|
74
|
-
* @param value - Value for the key.
|
|
75
|
-
* @param ttl - Number of milliseconds to consider the value fresh.
|
|
76
|
-
*/
|
|
77
|
-
set<T>(key: string, value: T, ttl: number): void {
|
|
78
|
-
cache.set(key, { expiresAt: new Date().getTime() + ttl, value });
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
18
|
|
|
83
19
|
/**
|
|
84
20
|
* The largest page size allowed by the Prismic REST API V2. This value is used
|
|
@@ -95,37 +31,37 @@ const MAX_PAGE_SIZE = 100;
|
|
|
95
31
|
export const REPOSITORY_CACHE_TTL = 5000;
|
|
96
32
|
|
|
97
33
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
34
|
+
* The number of milliseconds in which a multi-page `getAll` (e.g. `getAll`,
|
|
35
|
+
* `getAllByType`, `getAllByTag`) will wait between individual page requests.
|
|
36
|
+
*
|
|
37
|
+
* This is done to ensure API performance is sustainable and reduces the chance
|
|
38
|
+
* of a failed API request due to overloading.
|
|
101
39
|
*/
|
|
102
|
-
|
|
103
|
-
| string
|
|
104
|
-
| (() => string | undefined | Promise<string | undefined>);
|
|
40
|
+
export const GET_ALL_QUERY_DELAY = 500;
|
|
105
41
|
|
|
106
42
|
/**
|
|
107
|
-
*
|
|
43
|
+
* Modes for client ref management.
|
|
108
44
|
*/
|
|
109
|
-
|
|
45
|
+
enum RefStateMode {
|
|
110
46
|
/**
|
|
111
47
|
* Use the repository's master ref.
|
|
112
48
|
*/
|
|
113
|
-
Master,
|
|
49
|
+
Master = "Master",
|
|
114
50
|
|
|
115
51
|
/**
|
|
116
52
|
* Use a given Release identified by its ID.
|
|
117
53
|
*/
|
|
118
|
-
|
|
54
|
+
ReleaseID = "ReleaseID",
|
|
119
55
|
|
|
120
56
|
/**
|
|
121
57
|
* Use a given Release identified by its label.
|
|
122
58
|
*/
|
|
123
|
-
|
|
59
|
+
ReleaseLabel = "ReleaseLabel",
|
|
124
60
|
|
|
125
61
|
/**
|
|
126
62
|
* Use a given ref.
|
|
127
63
|
*/
|
|
128
|
-
Manual,
|
|
64
|
+
Manual = "Manual",
|
|
129
65
|
}
|
|
130
66
|
|
|
131
67
|
/**
|
|
@@ -144,37 +80,31 @@ type RefState = {
|
|
|
144
80
|
httpRequest?: HttpRequestLike;
|
|
145
81
|
} & (
|
|
146
82
|
| {
|
|
147
|
-
|
|
83
|
+
mode: RefStateMode.Master;
|
|
148
84
|
}
|
|
149
85
|
| {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* The ID of the Release.
|
|
154
|
-
*/
|
|
155
|
-
releaseId: string;
|
|
156
|
-
};
|
|
86
|
+
mode: RefStateMode.ReleaseID;
|
|
87
|
+
releaseID: string;
|
|
157
88
|
}
|
|
158
89
|
| {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* The label of the Release.
|
|
163
|
-
*/
|
|
164
|
-
releaseLabel: string;
|
|
165
|
-
};
|
|
90
|
+
mode: RefStateMode.ReleaseLabel;
|
|
91
|
+
releaseLabel: string;
|
|
166
92
|
}
|
|
167
93
|
| {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* The user-provided ref or ref thunk.
|
|
172
|
-
*/
|
|
173
|
-
refStringOrFn: RefStringOrThunk;
|
|
174
|
-
};
|
|
94
|
+
mode: RefStateMode.Manual;
|
|
95
|
+
ref: RefStringOrThunk;
|
|
175
96
|
}
|
|
176
97
|
);
|
|
177
98
|
|
|
99
|
+
/**
|
|
100
|
+
* A ref or a function that returns a ref. If a static ref is known, one can be
|
|
101
|
+
* given. If the ref must be fetched on-demand, a function can be provided. This
|
|
102
|
+
* function can optionally be asynchronous.
|
|
103
|
+
*/
|
|
104
|
+
type RefStringOrThunk =
|
|
105
|
+
| string
|
|
106
|
+
| (() => string | undefined | Promise<string | undefined>);
|
|
107
|
+
|
|
178
108
|
/**
|
|
179
109
|
* Configuration for clients that determine how content is queried.
|
|
180
110
|
*/
|
|
@@ -193,16 +123,21 @@ export type ClientConfig = {
|
|
|
193
123
|
ref?: RefStringOrThunk;
|
|
194
124
|
|
|
195
125
|
/**
|
|
196
|
-
* A
|
|
197
|
-
*
|
|
126
|
+
* A list of Route Resolver objects that define how a document's `url` field
|
|
127
|
+
* is resolved.
|
|
128
|
+
*
|
|
129
|
+
* {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
|
|
198
130
|
*/
|
|
199
|
-
|
|
131
|
+
routes?: NonNullable<BuildQueryURLArgs["routes"]>;
|
|
200
132
|
|
|
201
133
|
/**
|
|
202
134
|
* Default parameters that will be sent with each query. These parameters can
|
|
203
135
|
* be overridden on each query if needed.
|
|
204
136
|
*/
|
|
205
|
-
defaultParams?: Omit<
|
|
137
|
+
defaultParams?: Omit<
|
|
138
|
+
BuildQueryURLArgs,
|
|
139
|
+
"ref" | "integrationFieldsRef" | "accessToken" | "routes"
|
|
140
|
+
>;
|
|
206
141
|
|
|
207
142
|
/**
|
|
208
143
|
* The function used to make network requests to the Prismic REST API. In
|
|
@@ -247,7 +182,7 @@ type ResolvePreviewArgs = {
|
|
|
247
182
|
/**
|
|
248
183
|
* The previewed document that will be used to determine the destination URL.
|
|
249
184
|
*/
|
|
250
|
-
|
|
185
|
+
documentID?: string;
|
|
251
186
|
};
|
|
252
187
|
|
|
253
188
|
/**
|
|
@@ -261,35 +196,26 @@ const typePredicate = (documentType: string): string =>
|
|
|
261
196
|
predicate.at("document.type", documentType);
|
|
262
197
|
|
|
263
198
|
/**
|
|
264
|
-
* Creates a predicate to filter content by document tags.
|
|
199
|
+
* Creates a predicate to filter content by document tags. All tags are required
|
|
200
|
+
* on the document.
|
|
265
201
|
*
|
|
266
202
|
* @param documentType - Document tags to filter queried content.
|
|
267
203
|
*
|
|
268
204
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
269
205
|
*/
|
|
270
|
-
const
|
|
206
|
+
const everyTagPredicate = (tags: string | string[]): string =>
|
|
271
207
|
predicate.at("document.tags", tags);
|
|
272
208
|
|
|
273
209
|
/**
|
|
274
|
-
*
|
|
275
|
-
*
|
|
210
|
+
* Creates a predicate to filter content by document tags. At least one matching
|
|
211
|
+
* tag is required on the document.
|
|
276
212
|
*
|
|
277
|
-
* @param
|
|
278
|
-
* @param predicate - A function that determines if a ref from the list matches
|
|
279
|
-
* the criteria.
|
|
213
|
+
* @param documentType - Document tags to filter queried content.
|
|
280
214
|
*
|
|
281
|
-
* @returns
|
|
282
|
-
* @throws If a matching ref cannot be found.
|
|
215
|
+
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
283
216
|
*/
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
if (!ref) {
|
|
288
|
-
throw new Error("Ref could not be found.");
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return ref;
|
|
292
|
-
};
|
|
217
|
+
const someTagsPredicate = (tags: string | string[]): string =>
|
|
218
|
+
predicate.any("document.tags", tags);
|
|
293
219
|
|
|
294
220
|
/**
|
|
295
221
|
* Creates a Prismic client that can be used to query a repository.
|
|
@@ -328,11 +254,12 @@ export class Client {
|
|
|
328
254
|
accessToken?: string;
|
|
329
255
|
|
|
330
256
|
/**
|
|
331
|
-
*
|
|
257
|
+
* A list of Route Resolver objects that define how a document's `url` field
|
|
258
|
+
* is resolved.
|
|
332
259
|
*
|
|
333
|
-
* {@link https://prismic.io/docs/core-concepts/
|
|
260
|
+
* {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver#route-resolver}
|
|
334
261
|
*/
|
|
335
|
-
|
|
262
|
+
routes?: NonNullable<BuildQueryURLArgs["routes"]>;
|
|
336
263
|
|
|
337
264
|
/**
|
|
338
265
|
* The function used to make network requests to the Prismic REST API. In
|
|
@@ -345,17 +272,28 @@ export class Client {
|
|
|
345
272
|
* Default parameters that will be sent with each query. These parameters can
|
|
346
273
|
* be overridden on each query if needed.
|
|
347
274
|
*/
|
|
348
|
-
defaultParams?: Omit<
|
|
275
|
+
defaultParams?: Omit<
|
|
276
|
+
BuildQueryURLArgs,
|
|
277
|
+
"ref" | "integrationFieldsRef" | "accessToken" | "routes"
|
|
278
|
+
>;
|
|
349
279
|
|
|
350
280
|
/**
|
|
351
281
|
* The client's ref mode state. This determines which ref is used during queries.
|
|
352
282
|
*/
|
|
353
|
-
private
|
|
283
|
+
private refState: RefState = {
|
|
284
|
+
mode: RefStateMode.Master,
|
|
285
|
+
autoPreviewsEnabled: true,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Cached repository value.
|
|
290
|
+
*/
|
|
291
|
+
private cachedRepository: prismicT.Repository | undefined;
|
|
354
292
|
|
|
355
293
|
/**
|
|
356
|
-
*
|
|
294
|
+
* Timestamp at which the cached repository data is considered stale.
|
|
357
295
|
*/
|
|
358
|
-
private
|
|
296
|
+
private cachedRepositoryExpiration = 0;
|
|
359
297
|
|
|
360
298
|
/**
|
|
361
299
|
* Creates a Prismic client that can be used to query a repository.
|
|
@@ -374,13 +312,8 @@ export class Client {
|
|
|
374
312
|
constructor(endpoint: string, options: ClientConfig = {}) {
|
|
375
313
|
this.endpoint = endpoint;
|
|
376
314
|
this.accessToken = options.accessToken;
|
|
377
|
-
this.
|
|
315
|
+
this.routes = options.routes;
|
|
378
316
|
this.defaultParams = options.defaultParams;
|
|
379
|
-
this.internalCache = createSimpleTTLCache();
|
|
380
|
-
this.refMode = {
|
|
381
|
-
type: RefStateType.Master,
|
|
382
|
-
autoPreviewsEnabled: true,
|
|
383
|
-
};
|
|
384
317
|
|
|
385
318
|
if (options.ref) {
|
|
386
319
|
this.queryContentFromRef(options.ref);
|
|
@@ -391,8 +324,10 @@ export class Client {
|
|
|
391
324
|
} else if (typeof globalThis.fetch === "function") {
|
|
392
325
|
this.fetchFn = globalThis.fetch;
|
|
393
326
|
} else {
|
|
394
|
-
throw new
|
|
327
|
+
throw new PrismicError(
|
|
395
328
|
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option.",
|
|
329
|
+
undefined,
|
|
330
|
+
undefined,
|
|
396
331
|
);
|
|
397
332
|
}
|
|
398
333
|
|
|
@@ -417,7 +352,7 @@ export class Client {
|
|
|
417
352
|
* @see enableAutoPreviewsFromReq
|
|
418
353
|
*/
|
|
419
354
|
enableAutoPreviews(): void {
|
|
420
|
-
this.
|
|
355
|
+
this.refState.autoPreviewsEnabled = true;
|
|
421
356
|
}
|
|
422
357
|
|
|
423
358
|
/**
|
|
@@ -438,8 +373,8 @@ export class Client {
|
|
|
438
373
|
* @param req - An HTTP server request object containing the request's cookies.
|
|
439
374
|
*/
|
|
440
375
|
enableAutoPreviewsFromReq<R extends HttpRequestLike>(req: R): void {
|
|
441
|
-
this.
|
|
442
|
-
this.
|
|
376
|
+
this.refState.httpRequest = req;
|
|
377
|
+
this.refState.autoPreviewsEnabled = true;
|
|
443
378
|
}
|
|
444
379
|
|
|
445
380
|
/**
|
|
@@ -456,7 +391,7 @@ export class Client {
|
|
|
456
391
|
* ```
|
|
457
392
|
*/
|
|
458
393
|
disableAutoPreviews(): void {
|
|
459
|
-
this.
|
|
394
|
+
this.refState.autoPreviewsEnabled = false;
|
|
460
395
|
}
|
|
461
396
|
|
|
462
397
|
/**
|
|
@@ -466,7 +401,9 @@ export class Client {
|
|
|
466
401
|
* @example
|
|
467
402
|
*
|
|
468
403
|
* ```ts
|
|
469
|
-
* const response = await client.
|
|
404
|
+
* const response = await client.query(
|
|
405
|
+
* prismic.predicate.at("document.type", "page"),
|
|
406
|
+
* );
|
|
470
407
|
* ```
|
|
471
408
|
*
|
|
472
409
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
@@ -474,7 +411,14 @@ export class Client {
|
|
|
474
411
|
*
|
|
475
412
|
* @returns A paginated response containing the result of the query.
|
|
476
413
|
*/
|
|
477
|
-
query
|
|
414
|
+
async query<TDocument extends prismicT.PrismicDocument>(
|
|
415
|
+
predicates: NonNullable<BuildQueryURLArgs["predicates"]>,
|
|
416
|
+
params?: Partial<Omit<BuildQueryURLArgs, "predicates">>,
|
|
417
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
418
|
+
const url = await this.buildQueryURL({ ...params, predicates });
|
|
419
|
+
|
|
420
|
+
return await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
421
|
+
}
|
|
478
422
|
|
|
479
423
|
/**
|
|
480
424
|
* Queries content from the Prismic repository.
|
|
@@ -492,13 +436,10 @@ export class Client {
|
|
|
492
436
|
*/
|
|
493
437
|
async get<TDocument extends prismicT.PrismicDocument>(
|
|
494
438
|
params?: Partial<BuildQueryURLArgs>,
|
|
495
|
-
): Promise<Query<TDocument>> {
|
|
439
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
496
440
|
const url = await this.buildQueryURL(params);
|
|
497
441
|
|
|
498
|
-
|
|
499
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
500
|
-
// return await this.fetch<Query<TDocument>>(url, params);
|
|
501
|
-
return await this.fetch<Query<TDocument>>(url);
|
|
442
|
+
return await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
502
443
|
}
|
|
503
444
|
|
|
504
445
|
/**
|
|
@@ -518,17 +459,23 @@ export class Client {
|
|
|
518
459
|
async getFirst<TDocument extends prismicT.PrismicDocument>(
|
|
519
460
|
params?: Partial<BuildQueryURLArgs>,
|
|
520
461
|
): Promise<TDocument> {
|
|
521
|
-
const
|
|
462
|
+
const url = await this.buildQueryURL(params);
|
|
463
|
+
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
464
|
+
|
|
522
465
|
const firstResult = result.results[0];
|
|
523
466
|
|
|
524
467
|
if (firstResult) {
|
|
525
468
|
return firstResult;
|
|
526
469
|
}
|
|
527
470
|
|
|
528
|
-
throw new
|
|
471
|
+
throw new PrismicError("No documents were returned", url, undefined);
|
|
529
472
|
}
|
|
530
473
|
|
|
531
474
|
/**
|
|
475
|
+
* **IMPORTANT**: Avoid using `dangerouslyGetAll` as it may be slower and
|
|
476
|
+
* require more resources than other methods. Prefer using other methods that
|
|
477
|
+
* filter by predicates such as `getAllByType`.
|
|
478
|
+
*
|
|
532
479
|
* Queries content from the Prismic repository and returns all matching
|
|
533
480
|
* content. If no predicates are provided, all documents will be fetched.
|
|
534
481
|
*
|
|
@@ -537,28 +484,38 @@ export class Client {
|
|
|
537
484
|
* @example
|
|
538
485
|
*
|
|
539
486
|
* ```ts
|
|
540
|
-
* const response = await client.
|
|
487
|
+
* const response = await client.dangerouslyGetAll();
|
|
541
488
|
* ```
|
|
542
489
|
*
|
|
543
|
-
* @typeParam TDocument - Type of Prismic documents returned.
|
|
544
|
-
*
|
|
545
|
-
*
|
|
490
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
491
|
+
* @param params - Parameters to filter, sort, and paginate results.
|
|
492
|
+
*
|
|
493
|
+
* @returns A list of documents matching the query.
|
|
546
494
|
*/
|
|
547
|
-
async
|
|
495
|
+
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(
|
|
548
496
|
params: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams = {},
|
|
549
497
|
): Promise<TDocument[]> {
|
|
550
498
|
const { limit = Infinity, ...actualParams } = params;
|
|
551
|
-
const resolvedParams = {
|
|
499
|
+
const resolvedParams = {
|
|
500
|
+
...actualParams,
|
|
501
|
+
pageSize: actualParams.pageSize || MAX_PAGE_SIZE,
|
|
502
|
+
};
|
|
552
503
|
|
|
553
|
-
const
|
|
504
|
+
const documents: TDocument[] = [];
|
|
505
|
+
let latestResult: prismicT.Query<TDocument> | undefined;
|
|
554
506
|
|
|
555
|
-
|
|
556
|
-
|
|
507
|
+
while (
|
|
508
|
+
(!latestResult || latestResult.next_page) &&
|
|
509
|
+
documents.length < limit
|
|
510
|
+
) {
|
|
511
|
+
const page = latestResult ? latestResult.page + 1 : undefined;
|
|
557
512
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
513
|
+
latestResult = await this.get<TDocument>({ ...resolvedParams, page });
|
|
514
|
+
documents.push(...latestResult.results);
|
|
515
|
+
|
|
516
|
+
if (latestResult.next_page) {
|
|
517
|
+
await new Promise((res) => setTimeout(res, GET_ALL_QUERY_DELAY));
|
|
518
|
+
}
|
|
562
519
|
}
|
|
563
520
|
|
|
564
521
|
return documents.slice(0, limit);
|
|
@@ -589,7 +546,7 @@ export class Client {
|
|
|
589
546
|
params?: Partial<BuildQueryURLArgs>,
|
|
590
547
|
): Promise<TDocument> {
|
|
591
548
|
return await this.getFirst<TDocument>(
|
|
592
|
-
appendPredicates(predicate.at("document.id", id))
|
|
549
|
+
appendPredicates(params, predicate.at("document.id", id)),
|
|
593
550
|
);
|
|
594
551
|
}
|
|
595
552
|
|
|
@@ -619,9 +576,9 @@ export class Client {
|
|
|
619
576
|
async getByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
620
577
|
ids: string[],
|
|
621
578
|
params?: Partial<BuildQueryURLArgs>,
|
|
622
|
-
): Promise<Query<TDocument>> {
|
|
579
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
623
580
|
return await this.get<TDocument>(
|
|
624
|
-
appendPredicates(predicate.in("document.id", ids))
|
|
581
|
+
appendPredicates(params, predicate.in("document.id", ids)),
|
|
625
582
|
);
|
|
626
583
|
}
|
|
627
584
|
|
|
@@ -647,15 +604,14 @@ export class Client {
|
|
|
647
604
|
* @param ids - A list of document IDs.
|
|
648
605
|
* @param params - Parameters to filter, sort, and paginate the results.
|
|
649
606
|
*
|
|
650
|
-
* @returns A list of documents with IDs matching the `ids` parameter
|
|
651
|
-
* matching document exists.
|
|
607
|
+
* @returns A list of documents with IDs matching the `ids` parameter.
|
|
652
608
|
*/
|
|
653
609
|
async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
|
|
654
610
|
ids: string[],
|
|
655
611
|
params?: Partial<BuildQueryURLArgs>,
|
|
656
612
|
): Promise<TDocument[]> {
|
|
657
|
-
return await this.
|
|
658
|
-
appendPredicates(predicate.in("document.id", ids))
|
|
613
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
614
|
+
appendPredicates(params, predicate.in("document.id", ids)),
|
|
659
615
|
);
|
|
660
616
|
}
|
|
661
617
|
|
|
@@ -686,10 +642,85 @@ export class Client {
|
|
|
686
642
|
params?: Partial<BuildQueryURLArgs>,
|
|
687
643
|
): Promise<TDocument> {
|
|
688
644
|
return await this.getFirst<TDocument>(
|
|
689
|
-
appendPredicates(
|
|
645
|
+
appendPredicates(params, [
|
|
690
646
|
typePredicate(documentType),
|
|
691
647
|
predicate.at(`my.${documentType}.uid`, uid),
|
|
692
|
-
)
|
|
648
|
+
]),
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Queries document from the Prismic repository with specific UIDs and Custom Type.
|
|
654
|
+
*
|
|
655
|
+
* @remarks
|
|
656
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
657
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
658
|
+
* provided in the Prismic editor and is unique among all documents of its Custom Type.
|
|
659
|
+
* @example
|
|
660
|
+
*
|
|
661
|
+
* ```ts
|
|
662
|
+
* const document = await client.getByUIDs("blog_post", [
|
|
663
|
+
* "my-first-post",
|
|
664
|
+
* "my-second-post",
|
|
665
|
+
* ]);
|
|
666
|
+
* ```
|
|
667
|
+
*
|
|
668
|
+
* @typeParam TDocument - Type of the Prismic document returned.
|
|
669
|
+
* @param documentType - The API ID of the document's Custom Type.
|
|
670
|
+
* @param uids - A list of document UIDs.
|
|
671
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
672
|
+
*
|
|
673
|
+
* @returns A paginated response containing documents with UIDs matching the
|
|
674
|
+
* `uids` parameter.
|
|
675
|
+
*/
|
|
676
|
+
async getByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
677
|
+
documentType: string,
|
|
678
|
+
uids: string[],
|
|
679
|
+
params?: Partial<BuildQueryURLArgs>,
|
|
680
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
681
|
+
return await this.get<TDocument>(
|
|
682
|
+
appendPredicates(params, [
|
|
683
|
+
typePredicate(documentType),
|
|
684
|
+
predicate.in(`my.${documentType}.uid`, uids),
|
|
685
|
+
]),
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Queries all documents from the Prismic repository with specific UIDs and Custom Type.
|
|
691
|
+
*
|
|
692
|
+
* This method may make multiple network requests to query all matching content.
|
|
693
|
+
*
|
|
694
|
+
* @remarks
|
|
695
|
+
* A document's UID is different from its ID. An ID is automatically generated
|
|
696
|
+
* for all documents and is made available on its `id` property. A UID is
|
|
697
|
+
* provided in the Prismic editor and is unique among all documents of its Custom Type.
|
|
698
|
+
* @example
|
|
699
|
+
*
|
|
700
|
+
* ```ts
|
|
701
|
+
* const response = await client.getAllByUIDs([
|
|
702
|
+
* "my-first-post",
|
|
703
|
+
* "my-second-post",
|
|
704
|
+
* ]);
|
|
705
|
+
* ```
|
|
706
|
+
*
|
|
707
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
708
|
+
* @param documentType - The API ID of the document's Custom Type.
|
|
709
|
+
* @param uids - A list of document UIDs.
|
|
710
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
711
|
+
*
|
|
712
|
+
* @returns A list of documents with UIDs matching the `uids` parameter.
|
|
713
|
+
*/
|
|
714
|
+
async getAllByUIDs<TDocument extends prismicT.PrismicDocument>(
|
|
715
|
+
documentType: string,
|
|
716
|
+
uids: string[],
|
|
717
|
+
params?: Partial<BuildQueryURLArgs>,
|
|
718
|
+
): Promise<TDocument[]> {
|
|
719
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
720
|
+
appendPredicates(params, [
|
|
721
|
+
typePredicate(documentType),
|
|
722
|
+
predicate.in(`my.${documentType}.uid`, uids),
|
|
723
|
+
]),
|
|
693
724
|
);
|
|
694
725
|
}
|
|
695
726
|
|
|
@@ -718,7 +749,7 @@ export class Client {
|
|
|
718
749
|
params?: Partial<BuildQueryURLArgs>,
|
|
719
750
|
): Promise<TDocument> {
|
|
720
751
|
return await this.getFirst<TDocument>(
|
|
721
|
-
appendPredicates(typePredicate(documentType))
|
|
752
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
722
753
|
);
|
|
723
754
|
}
|
|
724
755
|
|
|
@@ -743,9 +774,9 @@ export class Client {
|
|
|
743
774
|
async getByType<TDocument extends prismicT.PrismicDocument>(
|
|
744
775
|
documentType: string,
|
|
745
776
|
params?: Partial<BuildQueryURLArgs>,
|
|
746
|
-
): Promise<Query<TDocument>> {
|
|
777
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
747
778
|
return await this.get<TDocument>(
|
|
748
|
-
appendPredicates(typePredicate(documentType))
|
|
779
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
749
780
|
);
|
|
750
781
|
}
|
|
751
782
|
|
|
@@ -770,8 +801,8 @@ export class Client {
|
|
|
770
801
|
documentType: string,
|
|
771
802
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
772
803
|
): Promise<TDocument[]> {
|
|
773
|
-
return await this.
|
|
774
|
-
appendPredicates(typePredicate(documentType))
|
|
804
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
805
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
775
806
|
);
|
|
776
807
|
}
|
|
777
808
|
|
|
@@ -795,9 +826,9 @@ export class Client {
|
|
|
795
826
|
async getByTag<TDocument extends prismicT.PrismicDocument>(
|
|
796
827
|
tag: string,
|
|
797
828
|
params?: Partial<BuildQueryURLArgs>,
|
|
798
|
-
): Promise<Query<TDocument>> {
|
|
829
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
799
830
|
return await this.get<TDocument>(
|
|
800
|
-
appendPredicates(
|
|
831
|
+
appendPredicates(params, everyTagPredicate(tag)),
|
|
801
832
|
);
|
|
802
833
|
}
|
|
803
834
|
|
|
@@ -822,58 +853,112 @@ export class Client {
|
|
|
822
853
|
tag: string,
|
|
823
854
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
824
855
|
): Promise<TDocument[]> {
|
|
825
|
-
return await this.
|
|
826
|
-
appendPredicates(
|
|
856
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
857
|
+
appendPredicates(params, everyTagPredicate(tag)),
|
|
827
858
|
);
|
|
828
859
|
}
|
|
829
860
|
|
|
830
861
|
/**
|
|
831
|
-
* Queries documents from the Prismic repository with
|
|
862
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
863
|
+
* document must be tagged with all of the queried tags to be included.
|
|
832
864
|
*
|
|
833
865
|
* @example
|
|
834
866
|
*
|
|
835
867
|
* ```ts
|
|
836
|
-
* const response = await client.
|
|
868
|
+
* const response = await client.getByEveryTag(["food", "fruit"]);
|
|
837
869
|
* ```
|
|
838
870
|
*
|
|
839
871
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
840
872
|
* @param tags - A list of tags that must be included on a document.
|
|
841
873
|
* @param params - Parameters to filter, sort, and paginate the results.
|
|
842
874
|
*
|
|
843
|
-
* @returns A paginated response containing documents with the
|
|
875
|
+
* @returns A paginated response containing documents with the tags.
|
|
844
876
|
*/
|
|
845
|
-
async
|
|
877
|
+
async getByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
846
878
|
tags: string[],
|
|
847
879
|
params?: Partial<BuildQueryURLArgs>,
|
|
848
|
-
): Promise<Query<TDocument>> {
|
|
880
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
849
881
|
return await this.get<TDocument>(
|
|
850
|
-
appendPredicates(
|
|
882
|
+
appendPredicates(params, everyTagPredicate(tags)),
|
|
851
883
|
);
|
|
852
884
|
}
|
|
853
885
|
|
|
854
886
|
/**
|
|
855
|
-
* Queries
|
|
887
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
888
|
+
* document must be tagged with all of the queried tags to be included.
|
|
856
889
|
*
|
|
857
890
|
* This method may make multiple network requests to query all matching content.
|
|
858
891
|
*
|
|
859
892
|
* @example
|
|
860
893
|
*
|
|
861
894
|
* ```ts
|
|
862
|
-
* const response = await client.
|
|
895
|
+
* const response = await client.getAllByEveryTag(["food", "fruit"]);
|
|
863
896
|
* ```
|
|
864
897
|
*
|
|
865
898
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
866
899
|
* @param tags - A list of tags that must be included on a document.
|
|
867
900
|
* @param params - Parameters to filter, sort, and paginate the results.
|
|
868
901
|
*
|
|
869
|
-
* @returns A list of all documents with the
|
|
902
|
+
* @returns A list of all documents with the tags.
|
|
903
|
+
*/
|
|
904
|
+
async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
905
|
+
tags: string[],
|
|
906
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
907
|
+
): Promise<TDocument[]> {
|
|
908
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
909
|
+
appendPredicates(params, everyTagPredicate(tags)),
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
915
|
+
* document must be tagged with at least one of the queried tags to be included.
|
|
916
|
+
*
|
|
917
|
+
* @example
|
|
918
|
+
*
|
|
919
|
+
* ```ts
|
|
920
|
+
* const response = await client.getByEveryTag(["food", "fruit"]);
|
|
921
|
+
* ```
|
|
922
|
+
*
|
|
923
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
924
|
+
* @param tags - A list of tags that must be included on a document.
|
|
925
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
926
|
+
*
|
|
927
|
+
* @returns A paginated response containing documents with at least one of the tags.
|
|
928
|
+
*/
|
|
929
|
+
async getBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
930
|
+
tags: string[],
|
|
931
|
+
params?: Partial<BuildQueryURLArgs>,
|
|
932
|
+
): Promise<prismicT.Query<TDocument>> {
|
|
933
|
+
return await this.get<TDocument>(
|
|
934
|
+
appendPredicates(params, someTagsPredicate(tags)),
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
/**
|
|
939
|
+
* Queries documents from the Prismic repository with specific tags. A
|
|
940
|
+
* document must be tagged with at least one of the queried tags to be included.
|
|
941
|
+
*
|
|
942
|
+
* This method may make multiple network requests to query all matching content.
|
|
943
|
+
*
|
|
944
|
+
* @example
|
|
945
|
+
*
|
|
946
|
+
* ```ts
|
|
947
|
+
* const response = await client.getAllByEveryTag(["food", "fruit"]);
|
|
948
|
+
* ```
|
|
949
|
+
*
|
|
950
|
+
* @typeParam TDocument - Type of Prismic documents returned.
|
|
951
|
+
* @param tags - A list of tags that must be included on a document.
|
|
952
|
+
* @param params - Parameters to filter, sort, and paginate the results.
|
|
953
|
+
*
|
|
954
|
+
* @returns A list of all documents with at least one of the tags.
|
|
870
955
|
*/
|
|
871
|
-
async
|
|
956
|
+
async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
872
957
|
tags: string[],
|
|
873
958
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
874
959
|
): Promise<TDocument[]> {
|
|
875
|
-
return await this.
|
|
876
|
-
appendPredicates(
|
|
960
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
961
|
+
appendPredicates(params, someTagsPredicate(tags)),
|
|
877
962
|
);
|
|
878
963
|
}
|
|
879
964
|
|
|
@@ -883,10 +968,9 @@ export class Client {
|
|
|
883
968
|
*
|
|
884
969
|
* @returns Repository metadata.
|
|
885
970
|
*/
|
|
886
|
-
async getRepository(): Promise<Repository> {
|
|
887
|
-
// TODO:
|
|
888
|
-
//
|
|
889
|
-
// return await this.fetch<Repository>(this.endpoint);
|
|
971
|
+
async getRepository(): Promise<prismicT.Repository> {
|
|
972
|
+
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
973
|
+
// return await this.fetch<prismicT.Repository>(this.endpoint);
|
|
890
974
|
|
|
891
975
|
const url = new URL(this.endpoint);
|
|
892
976
|
|
|
@@ -894,7 +978,7 @@ export class Client {
|
|
|
894
978
|
url.searchParams.set("access_token", this.accessToken);
|
|
895
979
|
}
|
|
896
980
|
|
|
897
|
-
return await this.fetch<Repository>(url.toString());
|
|
981
|
+
return await this.fetch<prismicT.Repository>(url.toString());
|
|
898
982
|
}
|
|
899
983
|
|
|
900
984
|
/**
|
|
@@ -906,10 +990,10 @@ export class Client {
|
|
|
906
990
|
*
|
|
907
991
|
* @returns A list of all refs for the Prismic repository.
|
|
908
992
|
*/
|
|
909
|
-
async getRefs(): Promise<Ref[]> {
|
|
910
|
-
const
|
|
993
|
+
async getRefs(): Promise<prismicT.Ref[]> {
|
|
994
|
+
const repository = await this.getRepository();
|
|
911
995
|
|
|
912
|
-
return
|
|
996
|
+
return repository.refs;
|
|
913
997
|
}
|
|
914
998
|
|
|
915
999
|
/**
|
|
@@ -919,10 +1003,10 @@ export class Client {
|
|
|
919
1003
|
*
|
|
920
1004
|
* @returns The ref with a matching ID, if it exists.
|
|
921
1005
|
*/
|
|
922
|
-
async
|
|
1006
|
+
async getRefByID(id: string): Promise<prismicT.Ref> {
|
|
923
1007
|
const refs = await this.getRefs();
|
|
924
1008
|
|
|
925
|
-
return
|
|
1009
|
+
return findRefByID(refs, id);
|
|
926
1010
|
}
|
|
927
1011
|
|
|
928
1012
|
/**
|
|
@@ -932,10 +1016,10 @@ export class Client {
|
|
|
932
1016
|
*
|
|
933
1017
|
* @returns The ref with a matching label, if it exists.
|
|
934
1018
|
*/
|
|
935
|
-
async getRefByLabel(label: string): Promise<Ref> {
|
|
1019
|
+
async getRefByLabel(label: string): Promise<prismicT.Ref> {
|
|
936
1020
|
const refs = await this.getRefs();
|
|
937
1021
|
|
|
938
|
-
return
|
|
1022
|
+
return findRefByLabel(refs, label);
|
|
939
1023
|
}
|
|
940
1024
|
|
|
941
1025
|
/**
|
|
@@ -944,10 +1028,10 @@ export class Client {
|
|
|
944
1028
|
*
|
|
945
1029
|
* @returns The repository's master ref.
|
|
946
1030
|
*/
|
|
947
|
-
async getMasterRef(): Promise<Ref> {
|
|
1031
|
+
async getMasterRef(): Promise<prismicT.Ref> {
|
|
948
1032
|
const refs = await this.getRefs();
|
|
949
1033
|
|
|
950
|
-
return
|
|
1034
|
+
return findMasterRef(refs);
|
|
951
1035
|
}
|
|
952
1036
|
|
|
953
1037
|
/**
|
|
@@ -956,7 +1040,7 @@ export class Client {
|
|
|
956
1040
|
*
|
|
957
1041
|
* @returns A list of all Releases for the Prismic repository.
|
|
958
1042
|
*/
|
|
959
|
-
async getReleases(): Promise<Ref[]> {
|
|
1043
|
+
async getReleases(): Promise<prismicT.Ref[]> {
|
|
960
1044
|
const refs = await this.getRefs();
|
|
961
1045
|
|
|
962
1046
|
return refs.filter((ref) => !ref.isMasterRef);
|
|
@@ -969,10 +1053,10 @@ export class Client {
|
|
|
969
1053
|
*
|
|
970
1054
|
* @returns The Release with a matching ID, if it exists.
|
|
971
1055
|
*/
|
|
972
|
-
async getReleaseByID(id: string): Promise<Ref> {
|
|
1056
|
+
async getReleaseByID(id: string): Promise<prismicT.Ref> {
|
|
973
1057
|
const releases = await this.getReleases();
|
|
974
1058
|
|
|
975
|
-
return
|
|
1059
|
+
return findRefByID(releases, id);
|
|
976
1060
|
}
|
|
977
1061
|
|
|
978
1062
|
/**
|
|
@@ -982,10 +1066,10 @@ export class Client {
|
|
|
982
1066
|
*
|
|
983
1067
|
* @returns The ref with a matching label, if it exists.
|
|
984
1068
|
*/
|
|
985
|
-
async getReleaseByLabel(label: string): Promise<Ref> {
|
|
1069
|
+
async getReleaseByLabel(label: string): Promise<prismicT.Ref> {
|
|
986
1070
|
const releases = await this.getReleases();
|
|
987
1071
|
|
|
988
|
-
return
|
|
1072
|
+
return findRefByLabel(releases, label);
|
|
989
1073
|
}
|
|
990
1074
|
|
|
991
1075
|
/**
|
|
@@ -999,9 +1083,9 @@ export class Client {
|
|
|
999
1083
|
|
|
1000
1084
|
return await this.fetch<string[]>(tagsForm.action);
|
|
1001
1085
|
} catch {
|
|
1002
|
-
const
|
|
1086
|
+
const repository = await this.getRepository();
|
|
1003
1087
|
|
|
1004
|
-
return
|
|
1088
|
+
return repository.tags;
|
|
1005
1089
|
}
|
|
1006
1090
|
}
|
|
1007
1091
|
|
|
@@ -1015,26 +1099,19 @@ export class Client {
|
|
|
1015
1099
|
async buildQueryURL(
|
|
1016
1100
|
params: Partial<BuildQueryURLArgs> = {},
|
|
1017
1101
|
): Promise<string> {
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
// ...actualParams
|
|
1024
|
-
// } = params;
|
|
1025
|
-
const {
|
|
1026
|
-
ref = await this.getResolvedRefString(),
|
|
1027
|
-
integrationFieldsRef = await this.getResolvedIntegrationFieldsRef(),
|
|
1028
|
-
accessToken = this.accessToken,
|
|
1029
|
-
...actualParams
|
|
1030
|
-
} = params;
|
|
1102
|
+
const ref = params.ref || (await this.getResolvedRefString());
|
|
1103
|
+
const integrationFieldsRef =
|
|
1104
|
+
params.integrationFieldsRef ||
|
|
1105
|
+
(await this.getCachedRepository()).integrationFieldsRef ||
|
|
1106
|
+
undefined;
|
|
1031
1107
|
|
|
1032
1108
|
return buildQueryURL(this.endpoint, {
|
|
1033
1109
|
...this.defaultParams,
|
|
1034
|
-
...
|
|
1035
|
-
accessToken,
|
|
1110
|
+
...params,
|
|
1036
1111
|
ref,
|
|
1037
1112
|
integrationFieldsRef,
|
|
1113
|
+
routes: params.routes || this.routes,
|
|
1114
|
+
accessToken: params.accessToken || this.accessToken,
|
|
1038
1115
|
});
|
|
1039
1116
|
}
|
|
1040
1117
|
|
|
@@ -1058,33 +1135,29 @@ export class Client {
|
|
|
1058
1135
|
* session. The user should be redirected to this URL.
|
|
1059
1136
|
*/
|
|
1060
1137
|
async resolvePreviewURL(args: ResolvePreviewArgs): Promise<string> {
|
|
1061
|
-
let
|
|
1138
|
+
let documentID = args.documentID;
|
|
1062
1139
|
let previewToken = args.previewToken;
|
|
1063
1140
|
|
|
1064
1141
|
if (typeof globalThis.location !== "undefined") {
|
|
1065
1142
|
const searchParams = new URLSearchParams(globalThis.location.search);
|
|
1066
|
-
|
|
1143
|
+
|
|
1144
|
+
documentID = documentID || searchParams.get("documentId") || undefined;
|
|
1067
1145
|
previewToken = previewToken || searchParams.get("token") || undefined;
|
|
1068
|
-
} else if (this.
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
previewToken = previewToken || this.refMode.httpRequest.query.token;
|
|
1074
|
-
}
|
|
1146
|
+
} else if (this.refState.httpRequest?.query) {
|
|
1147
|
+
documentID =
|
|
1148
|
+
documentID || (this.refState.httpRequest.query.documentId as string);
|
|
1149
|
+
previewToken =
|
|
1150
|
+
previewToken || (this.refState.httpRequest.query.token as string);
|
|
1075
1151
|
}
|
|
1076
1152
|
|
|
1077
|
-
if (
|
|
1078
|
-
const document = await this.getByID(
|
|
1153
|
+
if (documentID != null) {
|
|
1154
|
+
const document = await this.getByID(documentID, {
|
|
1079
1155
|
ref: previewToken,
|
|
1080
1156
|
});
|
|
1081
1157
|
|
|
1082
1158
|
// We know we have a valid field to resolve since we are using prismicH.documentToLinkField
|
|
1083
1159
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1084
|
-
return prismicH.asLink(
|
|
1085
|
-
prismicH.documentToLinkField(document),
|
|
1086
|
-
args.linkResolver,
|
|
1087
|
-
)!;
|
|
1160
|
+
return prismicH.asLink(document, args.linkResolver)!;
|
|
1088
1161
|
} else {
|
|
1089
1162
|
return args.defaultURL;
|
|
1090
1163
|
}
|
|
@@ -1103,10 +1176,7 @@ export class Client {
|
|
|
1103
1176
|
* ```
|
|
1104
1177
|
*/
|
|
1105
1178
|
queryLatestContent(): void {
|
|
1106
|
-
this.
|
|
1107
|
-
...this.refMode,
|
|
1108
|
-
type: RefStateType.Master,
|
|
1109
|
-
};
|
|
1179
|
+
this.refState.mode = RefStateMode.Master;
|
|
1110
1180
|
}
|
|
1111
1181
|
|
|
1112
1182
|
/**
|
|
@@ -1124,11 +1194,11 @@ export class Client {
|
|
|
1124
1194
|
*
|
|
1125
1195
|
* @param id - The ID of the Release.
|
|
1126
1196
|
*/
|
|
1127
|
-
queryContentFromReleaseByID(
|
|
1128
|
-
this.
|
|
1129
|
-
...this.
|
|
1130
|
-
|
|
1131
|
-
|
|
1197
|
+
queryContentFromReleaseByID(releaseID: string): void {
|
|
1198
|
+
this.refState = {
|
|
1199
|
+
...this.refState,
|
|
1200
|
+
mode: RefStateMode.ReleaseID,
|
|
1201
|
+
releaseID,
|
|
1132
1202
|
};
|
|
1133
1203
|
}
|
|
1134
1204
|
|
|
@@ -1148,10 +1218,10 @@ export class Client {
|
|
|
1148
1218
|
* @param label - The label of the Release.
|
|
1149
1219
|
*/
|
|
1150
1220
|
queryContentFromReleaseByLabel(releaseLabel: string): void {
|
|
1151
|
-
this.
|
|
1152
|
-
...this.
|
|
1153
|
-
|
|
1154
|
-
|
|
1221
|
+
this.refState = {
|
|
1222
|
+
...this.refState,
|
|
1223
|
+
mode: RefStateMode.ReleaseLabel,
|
|
1224
|
+
releaseLabel,
|
|
1155
1225
|
};
|
|
1156
1226
|
}
|
|
1157
1227
|
|
|
@@ -1172,10 +1242,10 @@ export class Client {
|
|
|
1172
1242
|
* @param ref - The ref or a function that returns the ref from which to query content.
|
|
1173
1243
|
*/
|
|
1174
1244
|
queryContentFromRef(ref: RefStringOrThunk): void {
|
|
1175
|
-
this.
|
|
1176
|
-
...this.
|
|
1177
|
-
|
|
1178
|
-
|
|
1245
|
+
this.refState = {
|
|
1246
|
+
...this.refState,
|
|
1247
|
+
mode: RefStateMode.Manual,
|
|
1248
|
+
ref,
|
|
1179
1249
|
};
|
|
1180
1250
|
}
|
|
1181
1251
|
|
|
@@ -1184,20 +1254,16 @@ export class Client {
|
|
|
1184
1254
|
*
|
|
1185
1255
|
* @returns Cached repository metadata.
|
|
1186
1256
|
*/
|
|
1187
|
-
private async getCachedRepository(): Promise<Repository> {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1257
|
+
private async getCachedRepository(): Promise<prismicT.Repository> {
|
|
1258
|
+
if (
|
|
1259
|
+
!this.cachedRepository ||
|
|
1260
|
+
Date.now() >= this.cachedRepositoryExpiration
|
|
1261
|
+
) {
|
|
1262
|
+
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
1263
|
+
this.cachedRepository = await this.getRepository();
|
|
1194
1264
|
}
|
|
1195
1265
|
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
this.internalCache.set(cacheKey, repository, REPOSITORY_CACHE_TTL);
|
|
1199
|
-
|
|
1200
|
-
return repository;
|
|
1266
|
+
return this.cachedRepository;
|
|
1201
1267
|
}
|
|
1202
1268
|
|
|
1203
1269
|
/**
|
|
@@ -1209,65 +1275,21 @@ export class Client {
|
|
|
1209
1275
|
* @returns The repository form.
|
|
1210
1276
|
* @throws If a matching form cannot be found.
|
|
1211
1277
|
*/
|
|
1212
|
-
private async getCachedRepositoryForm(name: string): Promise<Form> {
|
|
1278
|
+
private async getCachedRepositoryForm(name: string): Promise<prismicT.Form> {
|
|
1213
1279
|
const cachedRepository = await this.getCachedRepository();
|
|
1214
1280
|
const form = cachedRepository.forms[name];
|
|
1215
1281
|
|
|
1216
1282
|
if (!form) {
|
|
1217
|
-
throw new
|
|
1283
|
+
throw new PrismicError(
|
|
1284
|
+
`Form with name "${name}" could not be found`,
|
|
1285
|
+
undefined,
|
|
1286
|
+
undefined,
|
|
1287
|
+
);
|
|
1218
1288
|
}
|
|
1219
1289
|
|
|
1220
1290
|
return form;
|
|
1221
1291
|
}
|
|
1222
1292
|
|
|
1223
|
-
/**
|
|
1224
|
-
* Returns the preview ref for the client, if one exists.
|
|
1225
|
-
*
|
|
1226
|
-
* If this method is used in the browser, the browser's cookies will be read.
|
|
1227
|
-
* If this method is used on the server, the cookies from the saved HTTP
|
|
1228
|
-
* Request will be read, if an HTTP Request was given.
|
|
1229
|
-
*
|
|
1230
|
-
* @returns The preview ref as a string, if one exists.
|
|
1231
|
-
*/
|
|
1232
|
-
private getPreviewRefString(): string | undefined {
|
|
1233
|
-
if (globalThis.document?.cookie) {
|
|
1234
|
-
return getCookie(cookie.preview, globalThis.document.cookie);
|
|
1235
|
-
} else if (this.refMode.httpRequest?.headers?.cookie) {
|
|
1236
|
-
return getCookie(cookie.preview, this.refMode.httpRequest.headers.cookie);
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
/**
|
|
1241
|
-
* Returns the Integration Fields ref for the client, if one exists. This ref
|
|
1242
|
-
* is used to fetch the latest content from a Integration Field's API.
|
|
1243
|
-
*
|
|
1244
|
-
* @returns The repository's Integration Fields ref.
|
|
1245
|
-
*/
|
|
1246
|
-
private async getIntegrationFieldsRef(): Promise<string | undefined> {
|
|
1247
|
-
const repository = await this.getCachedRepository();
|
|
1248
|
-
|
|
1249
|
-
return repository.integrationFieldsRef || undefined;
|
|
1250
|
-
}
|
|
1251
|
-
|
|
1252
|
-
/**
|
|
1253
|
-
* Returns the Integration Fields ref neeed to query based on the client's
|
|
1254
|
-
* configuration. This method may make a network request to fetch a ref or
|
|
1255
|
-
* resolve the user's Integration Field ref thunk.
|
|
1256
|
-
*
|
|
1257
|
-
* @returns The repository's Integration Fields ref, if one is available.
|
|
1258
|
-
*/
|
|
1259
|
-
private async getResolvedIntegrationFieldsRef(): Promise<string | undefined> {
|
|
1260
|
-
const thisIntegrationFieldsRefThunk = castThunk(this.integrationFieldsRef);
|
|
1261
|
-
|
|
1262
|
-
const res = await thisIntegrationFieldsRefThunk();
|
|
1263
|
-
|
|
1264
|
-
if (typeof res === "string") {
|
|
1265
|
-
return res;
|
|
1266
|
-
} else {
|
|
1267
|
-
return await this.getIntegrationFieldsRef();
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
|
|
1271
1293
|
/**
|
|
1272
1294
|
* Returns the ref needed to query based on the client's current state. This
|
|
1273
1295
|
* method may make a network request to fetch a ref or resolve the user's ref thunk.
|
|
@@ -1290,75 +1312,41 @@ export class Client {
|
|
|
1290
1312
|
* @returns The ref to use during a query.
|
|
1291
1313
|
*/
|
|
1292
1314
|
private async getResolvedRefString(): Promise<string> {
|
|
1293
|
-
if (this.
|
|
1294
|
-
|
|
1315
|
+
if (this.refState.autoPreviewsEnabled) {
|
|
1316
|
+
let previewRef: string | undefined = undefined;
|
|
1317
|
+
|
|
1318
|
+
if (globalThis.document?.cookie) {
|
|
1319
|
+
previewRef = getCookie(cookie.preview, globalThis.document.cookie);
|
|
1320
|
+
} else if (this.refState.httpRequest?.headers?.cookie) {
|
|
1321
|
+
previewRef = getCookie(
|
|
1322
|
+
cookie.preview,
|
|
1323
|
+
this.refState.httpRequest.headers.cookie,
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1295
1326
|
|
|
1296
1327
|
if (previewRef) {
|
|
1297
1328
|
return previewRef;
|
|
1298
1329
|
}
|
|
1299
1330
|
}
|
|
1300
1331
|
|
|
1301
|
-
|
|
1302
|
-
case RefStateType.ReleaseByID: {
|
|
1303
|
-
const releaseId = this.refMode.payload.releaseId;
|
|
1304
|
-
const repository = await this.getCachedRepository();
|
|
1305
|
-
const ref = findRef(repository.refs, (ref) => ref.id === releaseId);
|
|
1306
|
-
|
|
1307
|
-
return ref.ref;
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
case RefStateType.ReleaseByLabel: {
|
|
1311
|
-
const releaseLabel = this.refMode.payload.releaseLabel;
|
|
1312
|
-
const repository = await this.getCachedRepository();
|
|
1313
|
-
const ref = findRef(
|
|
1314
|
-
repository.refs,
|
|
1315
|
-
(ref) => ref.label === releaseLabel,
|
|
1316
|
-
);
|
|
1317
|
-
|
|
1318
|
-
return ref.ref;
|
|
1319
|
-
}
|
|
1320
|
-
|
|
1321
|
-
case RefStateType.Manual: {
|
|
1322
|
-
const thisRefThunk = castThunk(this.refMode.payload.refStringOrFn);
|
|
1323
|
-
|
|
1324
|
-
const res = await thisRefThunk();
|
|
1325
|
-
|
|
1326
|
-
if (typeof res === "string") {
|
|
1327
|
-
return res;
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
case RefStateType.Master:
|
|
1332
|
-
default: {
|
|
1333
|
-
const repository = await this.getCachedRepository();
|
|
1334
|
-
const ref = findRef(repository.refs, (ref) => ref.isMasterRef);
|
|
1332
|
+
const cachedRepository = await this.getCachedRepository();
|
|
1335
1333
|
|
|
1336
|
-
|
|
1334
|
+
const refModeType = this.refState.mode;
|
|
1335
|
+
if (refModeType === RefStateMode.ReleaseID) {
|
|
1336
|
+
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
|
|
1337
|
+
} else if (refModeType === RefStateMode.ReleaseLabel) {
|
|
1338
|
+
return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel)
|
|
1339
|
+
.ref;
|
|
1340
|
+
} else if (refModeType === RefStateMode.Manual) {
|
|
1341
|
+
const res = await castThunk(this.refState.ref)();
|
|
1342
|
+
|
|
1343
|
+
if (typeof res === "string") {
|
|
1344
|
+
return res;
|
|
1337
1345
|
}
|
|
1338
1346
|
}
|
|
1339
|
-
}
|
|
1340
1347
|
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
*
|
|
1344
|
-
* It currently only includes an Authorization header if an access token is provided.
|
|
1345
|
-
*
|
|
1346
|
-
* @param params - Parameters for the query, including an access token.
|
|
1347
|
-
*
|
|
1348
|
-
* @returns Request options that can be used to make a network request to
|
|
1349
|
-
* query the repository.
|
|
1350
|
-
*/
|
|
1351
|
-
// TODO: Uncomment when the Authorization header can be used
|
|
1352
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
1353
|
-
// private buildRequestOptions(
|
|
1354
|
-
// params?: Partial<BuildQueryURLArgs>
|
|
1355
|
-
// ): RequestInitLike {
|
|
1356
|
-
// const accessToken = params?.accessToken || this.accessToken;
|
|
1357
|
-
|
|
1358
|
-
// return accessToken
|
|
1359
|
-
// ? { headers: { Authorization: `Token ${accessToken}` } }
|
|
1360
|
-
// : {};
|
|
1361
|
-
// }
|
|
1348
|
+
return findMasterRef(cachedRepository.refs).ref;
|
|
1349
|
+
}
|
|
1362
1350
|
|
|
1363
1351
|
/**
|
|
1364
1352
|
* Performs a network request using the configured `fetch` function. It
|
|
@@ -1373,14 +1361,15 @@ export class Client {
|
|
|
1373
1361
|
*/
|
|
1374
1362
|
private async fetch<T = unknown>(
|
|
1375
1363
|
url: string,
|
|
1376
|
-
// TODO:
|
|
1377
|
-
|
|
1378
|
-
// params?: Partial<BuildQueryURLArgs>
|
|
1364
|
+
// TODO: Change to `params` when Authorization header support works in browsers with CORS.
|
|
1365
|
+
_params?: Partial<BuildQueryURLArgs>,
|
|
1379
1366
|
): Promise<T> {
|
|
1380
|
-
// TODO:
|
|
1381
|
-
//
|
|
1382
|
-
// const options =
|
|
1383
|
-
//
|
|
1367
|
+
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
1368
|
+
// const accessToken = (params && params.accessToken) || this.accessToken;
|
|
1369
|
+
// const options = accessToken
|
|
1370
|
+
// ? { headers: { Authorization: `Token ${accessToken}` } }
|
|
1371
|
+
// : {};
|
|
1372
|
+
|
|
1384
1373
|
const res = await this.fetchFn(url);
|
|
1385
1374
|
|
|
1386
1375
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1390,7 +1379,7 @@ export class Client {
|
|
|
1390
1379
|
// Content Type. If not, this will throw, signaling an invalid response.
|
|
1391
1380
|
json = await res.json();
|
|
1392
1381
|
} catch {
|
|
1393
|
-
throw new PrismicError(undefined,
|
|
1382
|
+
throw new PrismicError(undefined, url, undefined);
|
|
1394
1383
|
}
|
|
1395
1384
|
|
|
1396
1385
|
switch (res.status) {
|
|
@@ -1403,14 +1392,7 @@ export class Client {
|
|
|
1403
1392
|
// - Invalid predicate syntax
|
|
1404
1393
|
// - Ref not provided (ignored)
|
|
1405
1394
|
case 400: {
|
|
1406
|
-
|
|
1407
|
-
throw new ParsingError(json.message, {
|
|
1408
|
-
url,
|
|
1409
|
-
response: json,
|
|
1410
|
-
});
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
break;
|
|
1395
|
+
throw new ParsingError(json.message, url, json);
|
|
1414
1396
|
}
|
|
1415
1397
|
|
|
1416
1398
|
// Unauthorized
|
|
@@ -1421,18 +1403,14 @@ export class Client {
|
|
|
1421
1403
|
// - Missing access token for query endpoint
|
|
1422
1404
|
// - Incorrect access token for query endpoint
|
|
1423
1405
|
case 403: {
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
response: json,
|
|
1430
|
-
},
|
|
1431
|
-
);
|
|
1432
|
-
}
|
|
1406
|
+
throw new ForbiddenError(
|
|
1407
|
+
"error" in json ? json.error : json.message,
|
|
1408
|
+
url,
|
|
1409
|
+
json,
|
|
1410
|
+
);
|
|
1433
1411
|
}
|
|
1434
1412
|
}
|
|
1435
1413
|
|
|
1436
|
-
throw new PrismicError(undefined,
|
|
1414
|
+
throw new PrismicError(undefined, url, json);
|
|
1437
1415
|
}
|
|
1438
1416
|
}
|