@prismicio/client 6.0.0-beta.0 → 6.0.0-beta.4
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/dist/index.cjs +212 -289
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +354 -369
- package/dist/index.js +488 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -15
- 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 +280 -401
- package/src/index.ts +12 -1
- 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/dist/index.mjs +0 -567
- package/dist/index.mjs.map +0 -1
package/src/client.ts
CHANGED
|
@@ -3,78 +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
|
-
HttpRequestLike,
|
|
11
|
-
// TODO: Uncomment when the Authorization header can be used
|
|
12
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
13
|
-
// RequestInitLike
|
|
14
|
-
} from "./types";
|
|
11
|
+
import { FetchLike, HttpRequestLike } from "./types";
|
|
15
12
|
import { buildQueryURL, BuildQueryURLArgs } from "./buildQueryURL";
|
|
16
|
-
import { ForbiddenError
|
|
17
|
-
import { ParsingError
|
|
13
|
+
import { ForbiddenError } from "./ForbiddenError";
|
|
14
|
+
import { ParsingError } from "./ParsingError";
|
|
18
15
|
import { PrismicError } from "./PrismicError";
|
|
16
|
+
import { predicate } from "./predicate";
|
|
19
17
|
import * as cookie from "./cookie";
|
|
20
|
-
import * as predicate from "./predicate";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* A container for items in a SimpleTTLCache.
|
|
24
|
-
*/
|
|
25
|
-
type SimpleTTLCacheValueContainer<T = unknown> = {
|
|
26
|
-
expiresAt: number;
|
|
27
|
-
value: T;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* A simple time-to-live cache where items are only valid for a given number of
|
|
32
|
-
* milliseconds.
|
|
33
|
-
*/
|
|
34
|
-
interface SimpleTTLCache {
|
|
35
|
-
get<T>(key: string): T | undefined;
|
|
36
|
-
set<T>(key: string, value: T, ttl?: number): void;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Creates a simple cache where each element has a given time to live (TTL).
|
|
41
|
-
* After the TTL has ellapsed, the value is considered stale and will not be
|
|
42
|
-
* returned if requested.
|
|
43
|
-
*/
|
|
44
|
-
const createSimpleTTLCache = (): SimpleTTLCache => {
|
|
45
|
-
const cache = new Map<string, SimpleTTLCacheValueContainer>();
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
/**
|
|
49
|
-
* Get a value from the cache. If the item's TTL has ellapsed, `undefined`
|
|
50
|
-
* will be returned.
|
|
51
|
-
*
|
|
52
|
-
* @param key - Key for the item.
|
|
53
|
-
*
|
|
54
|
-
* @returns The value for the key, if a value exists.
|
|
55
|
-
*/
|
|
56
|
-
get<T>(key: string): T | undefined {
|
|
57
|
-
const cacheValue = cache.get(key);
|
|
58
|
-
|
|
59
|
-
if (cacheValue) {
|
|
60
|
-
if (new Date().getTime() < cacheValue.expiresAt) {
|
|
61
|
-
return cacheValue.value as T;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Set a value in the cache for a given key and TTL.
|
|
68
|
-
*
|
|
69
|
-
* @param key - Key to identify the item.
|
|
70
|
-
* @param value - Value for the key.
|
|
71
|
-
* @param ttl - Number of milliseconds to consider the value fresh.
|
|
72
|
-
*/
|
|
73
|
-
set<T>(key: string, value: T, ttl: number): void {
|
|
74
|
-
cache.set(key, { expiresAt: new Date().getTime() + ttl, value });
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
18
|
|
|
79
19
|
/**
|
|
80
20
|
* The largest page size allowed by the Prismic REST API V2. This value is used
|
|
@@ -91,37 +31,37 @@ const MAX_PAGE_SIZE = 100;
|
|
|
91
31
|
export const REPOSITORY_CACHE_TTL = 5000;
|
|
92
32
|
|
|
93
33
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
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.
|
|
97
39
|
*/
|
|
98
|
-
|
|
99
|
-
| string
|
|
100
|
-
| (() => string | undefined | Promise<string | undefined>);
|
|
40
|
+
export const GET_ALL_QUERY_DELAY = 500;
|
|
101
41
|
|
|
102
42
|
/**
|
|
103
|
-
*
|
|
43
|
+
* Modes for client ref management.
|
|
104
44
|
*/
|
|
105
|
-
|
|
45
|
+
enum RefStateMode {
|
|
106
46
|
/**
|
|
107
47
|
* Use the repository's master ref.
|
|
108
48
|
*/
|
|
109
|
-
Master,
|
|
49
|
+
Master = "Master",
|
|
110
50
|
|
|
111
51
|
/**
|
|
112
52
|
* Use a given Release identified by its ID.
|
|
113
53
|
*/
|
|
114
|
-
|
|
54
|
+
ReleaseID = "ReleaseID",
|
|
115
55
|
|
|
116
56
|
/**
|
|
117
57
|
* Use a given Release identified by its label.
|
|
118
58
|
*/
|
|
119
|
-
|
|
59
|
+
ReleaseLabel = "ReleaseLabel",
|
|
120
60
|
|
|
121
61
|
/**
|
|
122
62
|
* Use a given ref.
|
|
123
63
|
*/
|
|
124
|
-
Manual,
|
|
64
|
+
Manual = "Manual",
|
|
125
65
|
}
|
|
126
66
|
|
|
127
67
|
/**
|
|
@@ -140,37 +80,31 @@ type RefState = {
|
|
|
140
80
|
httpRequest?: HttpRequestLike;
|
|
141
81
|
} & (
|
|
142
82
|
| {
|
|
143
|
-
|
|
83
|
+
mode: RefStateMode.Master;
|
|
144
84
|
}
|
|
145
85
|
| {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* The ID of the Release.
|
|
150
|
-
*/
|
|
151
|
-
releaseID: string;
|
|
152
|
-
};
|
|
86
|
+
mode: RefStateMode.ReleaseID;
|
|
87
|
+
releaseID: string;
|
|
153
88
|
}
|
|
154
89
|
| {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* The label of the Release.
|
|
159
|
-
*/
|
|
160
|
-
releaseLabel: string;
|
|
161
|
-
};
|
|
90
|
+
mode: RefStateMode.ReleaseLabel;
|
|
91
|
+
releaseLabel: string;
|
|
162
92
|
}
|
|
163
93
|
| {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* The user-provided ref or ref thunk.
|
|
168
|
-
*/
|
|
169
|
-
refStringOrFn: RefStringOrThunk;
|
|
170
|
-
};
|
|
94
|
+
mode: RefStateMode.Manual;
|
|
95
|
+
ref: RefStringOrThunk;
|
|
171
96
|
}
|
|
172
97
|
);
|
|
173
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
|
+
|
|
174
108
|
/**
|
|
175
109
|
* Configuration for clients that determine how content is queried.
|
|
176
110
|
*/
|
|
@@ -188,12 +122,6 @@ export type ClientConfig = {
|
|
|
188
122
|
*/
|
|
189
123
|
ref?: RefStringOrThunk;
|
|
190
124
|
|
|
191
|
-
/**
|
|
192
|
-
* A string representing a version of the Prismic repository's Integration
|
|
193
|
-
* Fields content.
|
|
194
|
-
*/
|
|
195
|
-
integrationFieldsRef?: RefStringOrThunk;
|
|
196
|
-
|
|
197
125
|
/**
|
|
198
126
|
* A list of Route Resolver objects that define how a document's `url` field
|
|
199
127
|
* is resolved.
|
|
@@ -268,38 +196,26 @@ const typePredicate = (documentType: string): string =>
|
|
|
268
196
|
predicate.at("document.type", documentType);
|
|
269
197
|
|
|
270
198
|
/**
|
|
271
|
-
* 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.
|
|
272
201
|
*
|
|
273
202
|
* @param documentType - Document tags to filter queried content.
|
|
274
203
|
*
|
|
275
204
|
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
276
205
|
*/
|
|
277
|
-
const
|
|
206
|
+
const everyTagPredicate = (tags: string | string[]): string =>
|
|
278
207
|
predicate.at("document.tags", tags);
|
|
279
208
|
|
|
280
209
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
210
|
+
* Creates a predicate to filter content by document tags. At least one matching
|
|
211
|
+
* tag is required on the document.
|
|
283
212
|
*
|
|
284
|
-
* @param
|
|
285
|
-
* @param predicate - A function that determines if a ref from the list matches
|
|
286
|
-
* the criteria.
|
|
213
|
+
* @param documentType - Document tags to filter queried content.
|
|
287
214
|
*
|
|
288
|
-
* @returns
|
|
289
|
-
* @throws If a matching ref cannot be found.
|
|
215
|
+
* @returns A predicate that can be used in a Prismic REST API V2 request.
|
|
290
216
|
*/
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
predicate: (ref: prismicT.Ref) => boolean,
|
|
294
|
-
): prismicT.Ref => {
|
|
295
|
-
const ref = refs.find((ref) => predicate(ref));
|
|
296
|
-
|
|
297
|
-
if (!ref) {
|
|
298
|
-
throw new Error("Ref could not be found.");
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return ref;
|
|
302
|
-
};
|
|
217
|
+
const someTagsPredicate = (tags: string | string[]): string =>
|
|
218
|
+
predicate.any("document.tags", tags);
|
|
303
219
|
|
|
304
220
|
/**
|
|
305
221
|
* Creates a Prismic client that can be used to query a repository.
|
|
@@ -337,13 +253,6 @@ export class Client {
|
|
|
337
253
|
*/
|
|
338
254
|
accessToken?: string;
|
|
339
255
|
|
|
340
|
-
/**
|
|
341
|
-
* The client's Integration Fields ref for fetching the latest Integration Fields data.
|
|
342
|
-
*
|
|
343
|
-
* {@link https://prismic.io/docs/core-concepts/integration-fields}
|
|
344
|
-
*/
|
|
345
|
-
integrationFieldsRef?: RefStringOrThunk;
|
|
346
|
-
|
|
347
256
|
/**
|
|
348
257
|
* A list of Route Resolver objects that define how a document's `url` field
|
|
349
258
|
* is resolved.
|
|
@@ -371,12 +280,20 @@ export class Client {
|
|
|
371
280
|
/**
|
|
372
281
|
* The client's ref mode state. This determines which ref is used during queries.
|
|
373
282
|
*/
|
|
374
|
-
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;
|
|
375
292
|
|
|
376
293
|
/**
|
|
377
|
-
*
|
|
294
|
+
* Timestamp at which the cached repository data is considered stale.
|
|
378
295
|
*/
|
|
379
|
-
private
|
|
296
|
+
private cachedRepositoryExpiration = 0;
|
|
380
297
|
|
|
381
298
|
/**
|
|
382
299
|
* Creates a Prismic client that can be used to query a repository.
|
|
@@ -395,14 +312,8 @@ export class Client {
|
|
|
395
312
|
constructor(endpoint: string, options: ClientConfig = {}) {
|
|
396
313
|
this.endpoint = endpoint;
|
|
397
314
|
this.accessToken = options.accessToken;
|
|
398
|
-
this.integrationFieldsRef = options.integrationFieldsRef;
|
|
399
315
|
this.routes = options.routes;
|
|
400
316
|
this.defaultParams = options.defaultParams;
|
|
401
|
-
this.internalCache = createSimpleTTLCache();
|
|
402
|
-
this.refMode = {
|
|
403
|
-
type: RefStateType.Master,
|
|
404
|
-
autoPreviewsEnabled: true,
|
|
405
|
-
};
|
|
406
317
|
|
|
407
318
|
if (options.ref) {
|
|
408
319
|
this.queryContentFromRef(options.ref);
|
|
@@ -413,8 +324,10 @@ export class Client {
|
|
|
413
324
|
} else if (typeof globalThis.fetch === "function") {
|
|
414
325
|
this.fetchFn = globalThis.fetch;
|
|
415
326
|
} else {
|
|
416
|
-
throw new
|
|
327
|
+
throw new PrismicError(
|
|
417
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,
|
|
418
331
|
);
|
|
419
332
|
}
|
|
420
333
|
|
|
@@ -439,7 +352,7 @@ export class Client {
|
|
|
439
352
|
* @see enableAutoPreviewsFromReq
|
|
440
353
|
*/
|
|
441
354
|
enableAutoPreviews(): void {
|
|
442
|
-
this.
|
|
355
|
+
this.refState.autoPreviewsEnabled = true;
|
|
443
356
|
}
|
|
444
357
|
|
|
445
358
|
/**
|
|
@@ -460,8 +373,8 @@ export class Client {
|
|
|
460
373
|
* @param req - An HTTP server request object containing the request's cookies.
|
|
461
374
|
*/
|
|
462
375
|
enableAutoPreviewsFromReq<R extends HttpRequestLike>(req: R): void {
|
|
463
|
-
this.
|
|
464
|
-
this.
|
|
376
|
+
this.refState.httpRequest = req;
|
|
377
|
+
this.refState.autoPreviewsEnabled = true;
|
|
465
378
|
}
|
|
466
379
|
|
|
467
380
|
/**
|
|
@@ -478,7 +391,7 @@ export class Client {
|
|
|
478
391
|
* ```
|
|
479
392
|
*/
|
|
480
393
|
disableAutoPreviews(): void {
|
|
481
|
-
this.
|
|
394
|
+
this.refState.autoPreviewsEnabled = false;
|
|
482
395
|
}
|
|
483
396
|
|
|
484
397
|
/**
|
|
@@ -488,7 +401,9 @@ export class Client {
|
|
|
488
401
|
* @example
|
|
489
402
|
*
|
|
490
403
|
* ```ts
|
|
491
|
-
* const response = await client.
|
|
404
|
+
* const response = await client.query(
|
|
405
|
+
* prismic.predicate.at("document.type", "page"),
|
|
406
|
+
* );
|
|
492
407
|
* ```
|
|
493
408
|
*
|
|
494
409
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
@@ -496,7 +411,14 @@ export class Client {
|
|
|
496
411
|
*
|
|
497
412
|
* @returns A paginated response containing the result of the query.
|
|
498
413
|
*/
|
|
499
|
-
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
|
+
}
|
|
500
422
|
|
|
501
423
|
/**
|
|
502
424
|
* Queries content from the Prismic repository.
|
|
@@ -517,10 +439,7 @@ export class Client {
|
|
|
517
439
|
): Promise<prismicT.Query<TDocument>> {
|
|
518
440
|
const url = await this.buildQueryURL(params);
|
|
519
441
|
|
|
520
|
-
|
|
521
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
522
|
-
// return await this.fetch<Query<TDocument>>(url, params);
|
|
523
|
-
return await this.fetch<prismicT.Query<TDocument>>(url);
|
|
442
|
+
return await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
524
443
|
}
|
|
525
444
|
|
|
526
445
|
/**
|
|
@@ -540,17 +459,23 @@ export class Client {
|
|
|
540
459
|
async getFirst<TDocument extends prismicT.PrismicDocument>(
|
|
541
460
|
params?: Partial<BuildQueryURLArgs>,
|
|
542
461
|
): Promise<TDocument> {
|
|
543
|
-
const
|
|
462
|
+
const url = await this.buildQueryURL(params);
|
|
463
|
+
const result = await this.fetch<prismicT.Query<TDocument>>(url, params);
|
|
464
|
+
|
|
544
465
|
const firstResult = result.results[0];
|
|
545
466
|
|
|
546
467
|
if (firstResult) {
|
|
547
468
|
return firstResult;
|
|
548
469
|
}
|
|
549
470
|
|
|
550
|
-
throw new
|
|
471
|
+
throw new PrismicError("No documents were returned", url, undefined);
|
|
551
472
|
}
|
|
552
473
|
|
|
553
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
|
+
*
|
|
554
479
|
* Queries content from the Prismic repository and returns all matching
|
|
555
480
|
* content. If no predicates are provided, all documents will be fetched.
|
|
556
481
|
*
|
|
@@ -559,28 +484,38 @@ export class Client {
|
|
|
559
484
|
* @example
|
|
560
485
|
*
|
|
561
486
|
* ```ts
|
|
562
|
-
* const response = await client.
|
|
487
|
+
* const response = await client.dangerouslyGetAll();
|
|
563
488
|
* ```
|
|
564
489
|
*
|
|
565
|
-
* @typeParam TDocument - Type of Prismic documents returned.
|
|
566
|
-
*
|
|
567
|
-
*
|
|
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.
|
|
568
494
|
*/
|
|
569
|
-
async
|
|
495
|
+
async dangerouslyGetAll<TDocument extends prismicT.PrismicDocument>(
|
|
570
496
|
params: Partial<Omit<BuildQueryURLArgs, "page">> & GetAllParams = {},
|
|
571
497
|
): Promise<TDocument[]> {
|
|
572
498
|
const { limit = Infinity, ...actualParams } = params;
|
|
573
|
-
const resolvedParams = {
|
|
499
|
+
const resolvedParams = {
|
|
500
|
+
...actualParams,
|
|
501
|
+
pageSize: actualParams.pageSize || MAX_PAGE_SIZE,
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const documents: TDocument[] = [];
|
|
505
|
+
let latestResult: prismicT.Query<TDocument> | undefined;
|
|
574
506
|
|
|
575
|
-
|
|
507
|
+
while (
|
|
508
|
+
(!latestResult || latestResult.next_page) &&
|
|
509
|
+
documents.length < limit
|
|
510
|
+
) {
|
|
511
|
+
const page = latestResult ? latestResult.page + 1 : undefined;
|
|
576
512
|
|
|
577
|
-
|
|
578
|
-
|
|
513
|
+
latestResult = await this.get<TDocument>({ ...resolvedParams, page });
|
|
514
|
+
documents.push(...latestResult.results);
|
|
579
515
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
documents = [...documents, ...result.results];
|
|
516
|
+
if (latestResult.next_page) {
|
|
517
|
+
await new Promise((res) => setTimeout(res, GET_ALL_QUERY_DELAY));
|
|
518
|
+
}
|
|
584
519
|
}
|
|
585
520
|
|
|
586
521
|
return documents.slice(0, limit);
|
|
@@ -611,7 +546,7 @@ export class Client {
|
|
|
611
546
|
params?: Partial<BuildQueryURLArgs>,
|
|
612
547
|
): Promise<TDocument> {
|
|
613
548
|
return await this.getFirst<TDocument>(
|
|
614
|
-
appendPredicates(predicate.at("document.id", id))
|
|
549
|
+
appendPredicates(params, predicate.at("document.id", id)),
|
|
615
550
|
);
|
|
616
551
|
}
|
|
617
552
|
|
|
@@ -643,7 +578,7 @@ export class Client {
|
|
|
643
578
|
params?: Partial<BuildQueryURLArgs>,
|
|
644
579
|
): Promise<prismicT.Query<TDocument>> {
|
|
645
580
|
return await this.get<TDocument>(
|
|
646
|
-
appendPredicates(predicate.in("document.id", ids))
|
|
581
|
+
appendPredicates(params, predicate.in("document.id", ids)),
|
|
647
582
|
);
|
|
648
583
|
}
|
|
649
584
|
|
|
@@ -675,8 +610,8 @@ export class Client {
|
|
|
675
610
|
ids: string[],
|
|
676
611
|
params?: Partial<BuildQueryURLArgs>,
|
|
677
612
|
): Promise<TDocument[]> {
|
|
678
|
-
return await this.
|
|
679
|
-
appendPredicates(predicate.in("document.id", ids))
|
|
613
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
614
|
+
appendPredicates(params, predicate.in("document.id", ids)),
|
|
680
615
|
);
|
|
681
616
|
}
|
|
682
617
|
|
|
@@ -707,10 +642,10 @@ export class Client {
|
|
|
707
642
|
params?: Partial<BuildQueryURLArgs>,
|
|
708
643
|
): Promise<TDocument> {
|
|
709
644
|
return await this.getFirst<TDocument>(
|
|
710
|
-
appendPredicates(
|
|
645
|
+
appendPredicates(params, [
|
|
711
646
|
typePredicate(documentType),
|
|
712
647
|
predicate.at(`my.${documentType}.uid`, uid),
|
|
713
|
-
)
|
|
648
|
+
]),
|
|
714
649
|
);
|
|
715
650
|
}
|
|
716
651
|
|
|
@@ -744,10 +679,10 @@ export class Client {
|
|
|
744
679
|
params?: Partial<BuildQueryURLArgs>,
|
|
745
680
|
): Promise<prismicT.Query<TDocument>> {
|
|
746
681
|
return await this.get<TDocument>(
|
|
747
|
-
appendPredicates(
|
|
682
|
+
appendPredicates(params, [
|
|
748
683
|
typePredicate(documentType),
|
|
749
684
|
predicate.in(`my.${documentType}.uid`, uids),
|
|
750
|
-
)
|
|
685
|
+
]),
|
|
751
686
|
);
|
|
752
687
|
}
|
|
753
688
|
|
|
@@ -781,11 +716,11 @@ export class Client {
|
|
|
781
716
|
uids: string[],
|
|
782
717
|
params?: Partial<BuildQueryURLArgs>,
|
|
783
718
|
): Promise<TDocument[]> {
|
|
784
|
-
return await this.
|
|
785
|
-
appendPredicates(
|
|
719
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
720
|
+
appendPredicates(params, [
|
|
786
721
|
typePredicate(documentType),
|
|
787
722
|
predicate.in(`my.${documentType}.uid`, uids),
|
|
788
|
-
)
|
|
723
|
+
]),
|
|
789
724
|
);
|
|
790
725
|
}
|
|
791
726
|
|
|
@@ -814,7 +749,7 @@ export class Client {
|
|
|
814
749
|
params?: Partial<BuildQueryURLArgs>,
|
|
815
750
|
): Promise<TDocument> {
|
|
816
751
|
return await this.getFirst<TDocument>(
|
|
817
|
-
appendPredicates(typePredicate(documentType))
|
|
752
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
818
753
|
);
|
|
819
754
|
}
|
|
820
755
|
|
|
@@ -841,7 +776,7 @@ export class Client {
|
|
|
841
776
|
params?: Partial<BuildQueryURLArgs>,
|
|
842
777
|
): Promise<prismicT.Query<TDocument>> {
|
|
843
778
|
return await this.get<TDocument>(
|
|
844
|
-
appendPredicates(typePredicate(documentType))
|
|
779
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
845
780
|
);
|
|
846
781
|
}
|
|
847
782
|
|
|
@@ -866,8 +801,8 @@ export class Client {
|
|
|
866
801
|
documentType: string,
|
|
867
802
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
868
803
|
): Promise<TDocument[]> {
|
|
869
|
-
return await this.
|
|
870
|
-
appendPredicates(typePredicate(documentType))
|
|
804
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
805
|
+
appendPredicates(params, typePredicate(documentType)),
|
|
871
806
|
);
|
|
872
807
|
}
|
|
873
808
|
|
|
@@ -893,7 +828,7 @@ export class Client {
|
|
|
893
828
|
params?: Partial<BuildQueryURLArgs>,
|
|
894
829
|
): Promise<prismicT.Query<TDocument>> {
|
|
895
830
|
return await this.get<TDocument>(
|
|
896
|
-
appendPredicates(
|
|
831
|
+
appendPredicates(params, everyTagPredicate(tag)),
|
|
897
832
|
);
|
|
898
833
|
}
|
|
899
834
|
|
|
@@ -918,58 +853,112 @@ export class Client {
|
|
|
918
853
|
tag: string,
|
|
919
854
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
920
855
|
): Promise<TDocument[]> {
|
|
921
|
-
return await this.
|
|
922
|
-
appendPredicates(
|
|
856
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
857
|
+
appendPredicates(params, everyTagPredicate(tag)),
|
|
923
858
|
);
|
|
924
859
|
}
|
|
925
860
|
|
|
926
861
|
/**
|
|
927
|
-
* 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.
|
|
928
864
|
*
|
|
929
865
|
* @example
|
|
930
866
|
*
|
|
931
867
|
* ```ts
|
|
932
|
-
* const response = await client.
|
|
868
|
+
* const response = await client.getByEveryTag(["food", "fruit"]);
|
|
933
869
|
* ```
|
|
934
870
|
*
|
|
935
871
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
936
872
|
* @param tags - A list of tags that must be included on a document.
|
|
937
873
|
* @param params - Parameters to filter, sort, and paginate the results.
|
|
938
874
|
*
|
|
939
|
-
* @returns A paginated response containing documents with the
|
|
875
|
+
* @returns A paginated response containing documents with the tags.
|
|
940
876
|
*/
|
|
941
|
-
async
|
|
877
|
+
async getByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
942
878
|
tags: string[],
|
|
943
879
|
params?: Partial<BuildQueryURLArgs>,
|
|
944
880
|
): Promise<prismicT.Query<TDocument>> {
|
|
945
881
|
return await this.get<TDocument>(
|
|
946
|
-
appendPredicates(
|
|
882
|
+
appendPredicates(params, everyTagPredicate(tags)),
|
|
947
883
|
);
|
|
948
884
|
}
|
|
949
885
|
|
|
950
886
|
/**
|
|
951
|
-
* 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.
|
|
952
889
|
*
|
|
953
890
|
* This method may make multiple network requests to query all matching content.
|
|
954
891
|
*
|
|
955
892
|
* @example
|
|
956
893
|
*
|
|
957
894
|
* ```ts
|
|
958
|
-
* const response = await client.
|
|
895
|
+
* const response = await client.getAllByEveryTag(["food", "fruit"]);
|
|
959
896
|
* ```
|
|
960
897
|
*
|
|
961
898
|
* @typeParam TDocument - Type of Prismic documents returned.
|
|
962
899
|
* @param tags - A list of tags that must be included on a document.
|
|
963
900
|
* @param params - Parameters to filter, sort, and paginate the results.
|
|
964
901
|
*
|
|
965
|
-
* @returns A list of all documents with the
|
|
902
|
+
* @returns A list of all documents with the tags.
|
|
966
903
|
*/
|
|
967
|
-
async
|
|
904
|
+
async getAllByEveryTag<TDocument extends prismicT.PrismicDocument>(
|
|
968
905
|
tags: string[],
|
|
969
906
|
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
970
907
|
): Promise<TDocument[]> {
|
|
971
|
-
return await this.
|
|
972
|
-
appendPredicates(
|
|
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.
|
|
955
|
+
*/
|
|
956
|
+
async getAllBySomeTags<TDocument extends prismicT.PrismicDocument>(
|
|
957
|
+
tags: string[],
|
|
958
|
+
params?: Partial<Omit<BuildQueryURLArgs, "page">>,
|
|
959
|
+
): Promise<TDocument[]> {
|
|
960
|
+
return await this.dangerouslyGetAll<TDocument>(
|
|
961
|
+
appendPredicates(params, someTagsPredicate(tags)),
|
|
973
962
|
);
|
|
974
963
|
}
|
|
975
964
|
|
|
@@ -980,9 +969,8 @@ export class Client {
|
|
|
980
969
|
* @returns Repository metadata.
|
|
981
970
|
*/
|
|
982
971
|
async getRepository(): Promise<prismicT.Repository> {
|
|
983
|
-
// TODO:
|
|
984
|
-
//
|
|
985
|
-
// return await this.fetch<Repository>(this.endpoint);
|
|
972
|
+
// TODO: Restore when Authorization header support works in browsers with CORS.
|
|
973
|
+
// return await this.fetch<prismicT.Repository>(this.endpoint);
|
|
986
974
|
|
|
987
975
|
const url = new URL(this.endpoint);
|
|
988
976
|
|
|
@@ -1003,9 +991,9 @@ export class Client {
|
|
|
1003
991
|
* @returns A list of all refs for the Prismic repository.
|
|
1004
992
|
*/
|
|
1005
993
|
async getRefs(): Promise<prismicT.Ref[]> {
|
|
1006
|
-
const
|
|
994
|
+
const repository = await this.getRepository();
|
|
1007
995
|
|
|
1008
|
-
return
|
|
996
|
+
return repository.refs;
|
|
1009
997
|
}
|
|
1010
998
|
|
|
1011
999
|
/**
|
|
@@ -1018,7 +1006,7 @@ export class Client {
|
|
|
1018
1006
|
async getRefByID(id: string): Promise<prismicT.Ref> {
|
|
1019
1007
|
const refs = await this.getRefs();
|
|
1020
1008
|
|
|
1021
|
-
return
|
|
1009
|
+
return findRefByID(refs, id);
|
|
1022
1010
|
}
|
|
1023
1011
|
|
|
1024
1012
|
/**
|
|
@@ -1031,7 +1019,7 @@ export class Client {
|
|
|
1031
1019
|
async getRefByLabel(label: string): Promise<prismicT.Ref> {
|
|
1032
1020
|
const refs = await this.getRefs();
|
|
1033
1021
|
|
|
1034
|
-
return
|
|
1022
|
+
return findRefByLabel(refs, label);
|
|
1035
1023
|
}
|
|
1036
1024
|
|
|
1037
1025
|
/**
|
|
@@ -1043,7 +1031,7 @@ export class Client {
|
|
|
1043
1031
|
async getMasterRef(): Promise<prismicT.Ref> {
|
|
1044
1032
|
const refs = await this.getRefs();
|
|
1045
1033
|
|
|
1046
|
-
return
|
|
1034
|
+
return findMasterRef(refs);
|
|
1047
1035
|
}
|
|
1048
1036
|
|
|
1049
1037
|
/**
|
|
@@ -1068,7 +1056,7 @@ export class Client {
|
|
|
1068
1056
|
async getReleaseByID(id: string): Promise<prismicT.Ref> {
|
|
1069
1057
|
const releases = await this.getReleases();
|
|
1070
1058
|
|
|
1071
|
-
return
|
|
1059
|
+
return findRefByID(releases, id);
|
|
1072
1060
|
}
|
|
1073
1061
|
|
|
1074
1062
|
/**
|
|
@@ -1081,7 +1069,7 @@ export class Client {
|
|
|
1081
1069
|
async getReleaseByLabel(label: string): Promise<prismicT.Ref> {
|
|
1082
1070
|
const releases = await this.getReleases();
|
|
1083
1071
|
|
|
1084
|
-
return
|
|
1072
|
+
return findRefByLabel(releases, label);
|
|
1085
1073
|
}
|
|
1086
1074
|
|
|
1087
1075
|
/**
|
|
@@ -1095,9 +1083,9 @@ export class Client {
|
|
|
1095
1083
|
|
|
1096
1084
|
return await this.fetch<string[]>(tagsForm.action);
|
|
1097
1085
|
} catch {
|
|
1098
|
-
const
|
|
1086
|
+
const repository = await this.getRepository();
|
|
1099
1087
|
|
|
1100
|
-
return
|
|
1088
|
+
return repository.tags;
|
|
1101
1089
|
}
|
|
1102
1090
|
}
|
|
1103
1091
|
|
|
@@ -1111,28 +1099,19 @@ export class Client {
|
|
|
1111
1099
|
async buildQueryURL(
|
|
1112
1100
|
params: Partial<BuildQueryURLArgs> = {},
|
|
1113
1101
|
): Promise<string> {
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
// ...actualParams
|
|
1120
|
-
// } = params;
|
|
1121
|
-
const {
|
|
1122
|
-
ref = await this.getResolvedRefString(),
|
|
1123
|
-
integrationFieldsRef = await this.getResolvedIntegrationFieldsRef(),
|
|
1124
|
-
accessToken = this.accessToken,
|
|
1125
|
-
routes = this.routes,
|
|
1126
|
-
...actualParams
|
|
1127
|
-
} = params;
|
|
1102
|
+
const ref = params.ref || (await this.getResolvedRefString());
|
|
1103
|
+
const integrationFieldsRef =
|
|
1104
|
+
params.integrationFieldsRef ||
|
|
1105
|
+
(await this.getCachedRepository()).integrationFieldsRef ||
|
|
1106
|
+
undefined;
|
|
1128
1107
|
|
|
1129
1108
|
return buildQueryURL(this.endpoint, {
|
|
1130
1109
|
...this.defaultParams,
|
|
1131
|
-
...
|
|
1132
|
-
accessToken,
|
|
1110
|
+
...params,
|
|
1133
1111
|
ref,
|
|
1134
1112
|
integrationFieldsRef,
|
|
1135
|
-
routes,
|
|
1113
|
+
routes: params.routes || this.routes,
|
|
1114
|
+
accessToken: params.accessToken || this.accessToken,
|
|
1136
1115
|
});
|
|
1137
1116
|
}
|
|
1138
1117
|
|
|
@@ -1161,15 +1140,14 @@ export class Client {
|
|
|
1161
1140
|
|
|
1162
1141
|
if (typeof globalThis.location !== "undefined") {
|
|
1163
1142
|
const searchParams = new URLSearchParams(globalThis.location.search);
|
|
1143
|
+
|
|
1164
1144
|
documentID = documentID || searchParams.get("documentId") || undefined;
|
|
1165
1145
|
previewToken = previewToken || searchParams.get("token") || undefined;
|
|
1166
|
-
} else if (this.
|
|
1167
|
-
|
|
1168
|
-
documentID
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
previewToken = previewToken || this.refMode.httpRequest.query.token;
|
|
1172
|
-
}
|
|
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);
|
|
1173
1151
|
}
|
|
1174
1152
|
|
|
1175
1153
|
if (documentID != null) {
|
|
@@ -1179,10 +1157,7 @@ export class Client {
|
|
|
1179
1157
|
|
|
1180
1158
|
// We know we have a valid field to resolve since we are using prismicH.documentToLinkField
|
|
1181
1159
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1182
|
-
return prismicH.asLink(
|
|
1183
|
-
prismicH.documentToLinkField(document),
|
|
1184
|
-
args.linkResolver,
|
|
1185
|
-
)!;
|
|
1160
|
+
return prismicH.asLink(document, args.linkResolver)!;
|
|
1186
1161
|
} else {
|
|
1187
1162
|
return args.defaultURL;
|
|
1188
1163
|
}
|
|
@@ -1201,10 +1176,7 @@ export class Client {
|
|
|
1201
1176
|
* ```
|
|
1202
1177
|
*/
|
|
1203
1178
|
queryLatestContent(): void {
|
|
1204
|
-
this.
|
|
1205
|
-
...this.refMode,
|
|
1206
|
-
type: RefStateType.Master,
|
|
1207
|
-
};
|
|
1179
|
+
this.refState.mode = RefStateMode.Master;
|
|
1208
1180
|
}
|
|
1209
1181
|
|
|
1210
1182
|
/**
|
|
@@ -1223,10 +1195,10 @@ export class Client {
|
|
|
1223
1195
|
* @param id - The ID of the Release.
|
|
1224
1196
|
*/
|
|
1225
1197
|
queryContentFromReleaseByID(releaseID: string): void {
|
|
1226
|
-
this.
|
|
1227
|
-
...this.
|
|
1228
|
-
|
|
1229
|
-
|
|
1198
|
+
this.refState = {
|
|
1199
|
+
...this.refState,
|
|
1200
|
+
mode: RefStateMode.ReleaseID,
|
|
1201
|
+
releaseID,
|
|
1230
1202
|
};
|
|
1231
1203
|
}
|
|
1232
1204
|
|
|
@@ -1246,10 +1218,10 @@ export class Client {
|
|
|
1246
1218
|
* @param label - The label of the Release.
|
|
1247
1219
|
*/
|
|
1248
1220
|
queryContentFromReleaseByLabel(releaseLabel: string): void {
|
|
1249
|
-
this.
|
|
1250
|
-
...this.
|
|
1251
|
-
|
|
1252
|
-
|
|
1221
|
+
this.refState = {
|
|
1222
|
+
...this.refState,
|
|
1223
|
+
mode: RefStateMode.ReleaseLabel,
|
|
1224
|
+
releaseLabel,
|
|
1253
1225
|
};
|
|
1254
1226
|
}
|
|
1255
1227
|
|
|
@@ -1270,10 +1242,10 @@ export class Client {
|
|
|
1270
1242
|
* @param ref - The ref or a function that returns the ref from which to query content.
|
|
1271
1243
|
*/
|
|
1272
1244
|
queryContentFromRef(ref: RefStringOrThunk): void {
|
|
1273
|
-
this.
|
|
1274
|
-
...this.
|
|
1275
|
-
|
|
1276
|
-
|
|
1245
|
+
this.refState = {
|
|
1246
|
+
...this.refState,
|
|
1247
|
+
mode: RefStateMode.Manual,
|
|
1248
|
+
ref,
|
|
1277
1249
|
};
|
|
1278
1250
|
}
|
|
1279
1251
|
|
|
@@ -1283,20 +1255,15 @@ export class Client {
|
|
|
1283
1255
|
* @returns Cached repository metadata.
|
|
1284
1256
|
*/
|
|
1285
1257
|
private async getCachedRepository(): Promise<prismicT.Repository> {
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
return cachedRepository;
|
|
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();
|
|
1293
1264
|
}
|
|
1294
1265
|
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
this.internalCache.set(cacheKey, repository, REPOSITORY_CACHE_TTL);
|
|
1298
|
-
|
|
1299
|
-
return repository;
|
|
1266
|
+
return this.cachedRepository;
|
|
1300
1267
|
}
|
|
1301
1268
|
|
|
1302
1269
|
/**
|
|
@@ -1313,60 +1280,16 @@ export class Client {
|
|
|
1313
1280
|
const form = cachedRepository.forms[name];
|
|
1314
1281
|
|
|
1315
1282
|
if (!form) {
|
|
1316
|
-
throw new
|
|
1283
|
+
throw new PrismicError(
|
|
1284
|
+
`Form with name "${name}" could not be found`,
|
|
1285
|
+
undefined,
|
|
1286
|
+
undefined,
|
|
1287
|
+
);
|
|
1317
1288
|
}
|
|
1318
1289
|
|
|
1319
1290
|
return form;
|
|
1320
1291
|
}
|
|
1321
1292
|
|
|
1322
|
-
/**
|
|
1323
|
-
* Returns the preview ref for the client, if one exists.
|
|
1324
|
-
*
|
|
1325
|
-
* If this method is used in the browser, the browser's cookies will be read.
|
|
1326
|
-
* If this method is used on the server, the cookies from the saved HTTP
|
|
1327
|
-
* Request will be read, if an HTTP Request was given.
|
|
1328
|
-
*
|
|
1329
|
-
* @returns The preview ref as a string, if one exists.
|
|
1330
|
-
*/
|
|
1331
|
-
private getPreviewRefString(): string | undefined {
|
|
1332
|
-
if (globalThis.document?.cookie) {
|
|
1333
|
-
return getCookie(cookie.preview, globalThis.document.cookie);
|
|
1334
|
-
} else if (this.refMode.httpRequest?.headers?.cookie) {
|
|
1335
|
-
return getCookie(cookie.preview, this.refMode.httpRequest.headers.cookie);
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1339
|
-
/**
|
|
1340
|
-
* Returns the Integration Fields ref for the client, if one exists. This ref
|
|
1341
|
-
* is used to fetch the latest content from a Integration Field's API.
|
|
1342
|
-
*
|
|
1343
|
-
* @returns The repository's Integration Fields ref.
|
|
1344
|
-
*/
|
|
1345
|
-
private async getIntegrationFieldsRef(): Promise<string | undefined> {
|
|
1346
|
-
const repository = await this.getCachedRepository();
|
|
1347
|
-
|
|
1348
|
-
return repository.integrationFieldsRef || undefined;
|
|
1349
|
-
}
|
|
1350
|
-
|
|
1351
|
-
/**
|
|
1352
|
-
* Returns the Integration Fields ref neeed to query based on the client's
|
|
1353
|
-
* configuration. This method may make a network request to fetch a ref or
|
|
1354
|
-
* resolve the user's Integration Field ref thunk.
|
|
1355
|
-
*
|
|
1356
|
-
* @returns The repository's Integration Fields ref, if one is available.
|
|
1357
|
-
*/
|
|
1358
|
-
private async getResolvedIntegrationFieldsRef(): Promise<string | undefined> {
|
|
1359
|
-
const thisIntegrationFieldsRefThunk = castThunk(this.integrationFieldsRef);
|
|
1360
|
-
|
|
1361
|
-
const res = await thisIntegrationFieldsRefThunk();
|
|
1362
|
-
|
|
1363
|
-
if (typeof res === "string") {
|
|
1364
|
-
return res;
|
|
1365
|
-
} else {
|
|
1366
|
-
return await this.getIntegrationFieldsRef();
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
|
|
1370
1293
|
/**
|
|
1371
1294
|
* Returns the ref needed to query based on the client's current state. This
|
|
1372
1295
|
* method may make a network request to fetch a ref or resolve the user's ref thunk.
|
|
@@ -1389,75 +1312,41 @@ export class Client {
|
|
|
1389
1312
|
* @returns The ref to use during a query.
|
|
1390
1313
|
*/
|
|
1391
1314
|
private async getResolvedRefString(): Promise<string> {
|
|
1392
|
-
if (this.
|
|
1393
|
-
|
|
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
|
+
}
|
|
1394
1326
|
|
|
1395
1327
|
if (previewRef) {
|
|
1396
1328
|
return previewRef;
|
|
1397
1329
|
}
|
|
1398
1330
|
}
|
|
1399
1331
|
|
|
1400
|
-
|
|
1401
|
-
case RefStateType.ReleaseByID: {
|
|
1402
|
-
const releaseID = this.refMode.payload.releaseID;
|
|
1403
|
-
const repository = await this.getCachedRepository();
|
|
1404
|
-
const ref = findRef(repository.refs, (ref) => ref.id === releaseID);
|
|
1405
|
-
|
|
1406
|
-
return ref.ref;
|
|
1407
|
-
}
|
|
1408
|
-
|
|
1409
|
-
case RefStateType.ReleaseByLabel: {
|
|
1410
|
-
const releaseLabel = this.refMode.payload.releaseLabel;
|
|
1411
|
-
const repository = await this.getCachedRepository();
|
|
1412
|
-
const ref = findRef(
|
|
1413
|
-
repository.refs,
|
|
1414
|
-
(ref) => ref.label === releaseLabel,
|
|
1415
|
-
);
|
|
1416
|
-
|
|
1417
|
-
return ref.ref;
|
|
1418
|
-
}
|
|
1419
|
-
|
|
1420
|
-
case RefStateType.Manual: {
|
|
1421
|
-
const thisRefThunk = castThunk(this.refMode.payload.refStringOrFn);
|
|
1422
|
-
|
|
1423
|
-
const res = await thisRefThunk();
|
|
1424
|
-
|
|
1425
|
-
if (typeof res === "string") {
|
|
1426
|
-
return res;
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1430
|
-
case RefStateType.Master:
|
|
1431
|
-
default: {
|
|
1432
|
-
const repository = await this.getCachedRepository();
|
|
1433
|
-
const ref = findRef(repository.refs, (ref) => ref.isMasterRef);
|
|
1332
|
+
const cachedRepository = await this.getCachedRepository();
|
|
1434
1333
|
|
|
1435
|
-
|
|
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;
|
|
1436
1345
|
}
|
|
1437
1346
|
}
|
|
1438
|
-
}
|
|
1439
|
-
|
|
1440
|
-
/**
|
|
1441
|
-
* Returns the network request options required by the Prismic repository.
|
|
1442
|
-
*
|
|
1443
|
-
* It currently only includes an Authorization header if an access token is provided.
|
|
1444
|
-
*
|
|
1445
|
-
* @param params - Parameters for the query, including an access token.
|
|
1446
|
-
*
|
|
1447
|
-
* @returns Request options that can be used to make a network request to
|
|
1448
|
-
* query the repository.
|
|
1449
|
-
*/
|
|
1450
|
-
// TODO: Uncomment when the Authorization header can be used
|
|
1451
|
-
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
|
|
1452
|
-
// private buildRequestOptions(
|
|
1453
|
-
// params?: Partial<BuildQueryURLArgs>
|
|
1454
|
-
// ): RequestInitLike {
|
|
1455
|
-
// const accessToken = params?.accessToken || this.accessToken;
|
|
1456
1347
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
// : {};
|
|
1460
|
-
// }
|
|
1348
|
+
return findMasterRef(cachedRepository.refs).ref;
|
|
1349
|
+
}
|
|
1461
1350
|
|
|
1462
1351
|
/**
|
|
1463
1352
|
* Performs a network request using the configured `fetch` function. It
|
|
@@ -1472,14 +1361,15 @@ export class Client {
|
|
|
1472
1361
|
*/
|
|
1473
1362
|
private async fetch<T = unknown>(
|
|
1474
1363
|
url: string,
|
|
1475
|
-
// TODO:
|
|
1476
|
-
|
|
1477
|
-
// params?: Partial<BuildQueryURLArgs>
|
|
1364
|
+
// TODO: Change to `params` when Authorization header support works in browsers with CORS.
|
|
1365
|
+
_params?: Partial<BuildQueryURLArgs>,
|
|
1478
1366
|
): Promise<T> {
|
|
1479
|
-
// TODO:
|
|
1480
|
-
//
|
|
1481
|
-
// const options =
|
|
1482
|
-
//
|
|
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
|
+
|
|
1483
1373
|
const res = await this.fetchFn(url);
|
|
1484
1374
|
|
|
1485
1375
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1489,7 +1379,7 @@ export class Client {
|
|
|
1489
1379
|
// Content Type. If not, this will throw, signaling an invalid response.
|
|
1490
1380
|
json = await res.json();
|
|
1491
1381
|
} catch {
|
|
1492
|
-
throw new PrismicError(undefined,
|
|
1382
|
+
throw new PrismicError(undefined, url, undefined);
|
|
1493
1383
|
}
|
|
1494
1384
|
|
|
1495
1385
|
switch (res.status) {
|
|
@@ -1502,14 +1392,7 @@ export class Client {
|
|
|
1502
1392
|
// - Invalid predicate syntax
|
|
1503
1393
|
// - Ref not provided (ignored)
|
|
1504
1394
|
case 400: {
|
|
1505
|
-
|
|
1506
|
-
throw new ParsingError(json.message, {
|
|
1507
|
-
url,
|
|
1508
|
-
response: json,
|
|
1509
|
-
});
|
|
1510
|
-
}
|
|
1511
|
-
|
|
1512
|
-
break;
|
|
1395
|
+
throw new ParsingError(json.message, url, json);
|
|
1513
1396
|
}
|
|
1514
1397
|
|
|
1515
1398
|
// Unauthorized
|
|
@@ -1520,18 +1403,14 @@ export class Client {
|
|
|
1520
1403
|
// - Missing access token for query endpoint
|
|
1521
1404
|
// - Incorrect access token for query endpoint
|
|
1522
1405
|
case 403: {
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
response: json,
|
|
1529
|
-
},
|
|
1530
|
-
);
|
|
1531
|
-
}
|
|
1406
|
+
throw new ForbiddenError(
|
|
1407
|
+
"error" in json ? json.error : json.message,
|
|
1408
|
+
url,
|
|
1409
|
+
json,
|
|
1410
|
+
);
|
|
1532
1411
|
}
|
|
1533
1412
|
}
|
|
1534
1413
|
|
|
1535
|
-
throw new PrismicError(undefined,
|
|
1414
|
+
throw new PrismicError(undefined, url, json);
|
|
1536
1415
|
}
|
|
1537
1416
|
}
|