@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/dist/index.js
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
import * as prismicH from '@prismicio/helpers';
|
|
2
|
+
|
|
3
|
+
const getEndpoint = (repositoryName) => `https://${repositoryName}.cdn.prismic.io/api/v2`;
|
|
4
|
+
|
|
5
|
+
const castArray = (a) => Array.isArray(a) ? a : [a];
|
|
6
|
+
|
|
7
|
+
const RENAMED_PARAMS = {
|
|
8
|
+
accessToken: "access_token"
|
|
9
|
+
};
|
|
10
|
+
const castOrderingToString = (ordering) => typeof ordering === "string" ? ordering : [
|
|
11
|
+
ordering.field,
|
|
12
|
+
ordering.direction === "desc" ? ordering.direction : void 0
|
|
13
|
+
].filter(Boolean).join(" ");
|
|
14
|
+
const buildQueryURL = (endpoint, args) => {
|
|
15
|
+
var _a;
|
|
16
|
+
const { predicates, ...params } = args;
|
|
17
|
+
const url = new URL(`documents/search`, `${endpoint}/`);
|
|
18
|
+
if (predicates) {
|
|
19
|
+
for (const predicate of castArray(predicates)) {
|
|
20
|
+
url.searchParams.append("q", `[${predicate}]`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
for (const k in params) {
|
|
24
|
+
const name = (_a = RENAMED_PARAMS[k]) != null ? _a : k;
|
|
25
|
+
let value = params[k];
|
|
26
|
+
if (name === "orderings") {
|
|
27
|
+
const scopedValue = params[name];
|
|
28
|
+
if (scopedValue != null) {
|
|
29
|
+
const v = castArray(scopedValue).map((ordering) => castOrderingToString(ordering)).join(",");
|
|
30
|
+
value = `[${v}]`;
|
|
31
|
+
}
|
|
32
|
+
} else if (name === "routes") {
|
|
33
|
+
if (typeof params[name] === "object") {
|
|
34
|
+
value = JSON.stringify(castArray(params[name]));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (value != null) {
|
|
38
|
+
url.searchParams.set(name, castArray(value).join(","));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return url.toString();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const appendPredicates = (objWithPredicates = {}, predicates) => {
|
|
45
|
+
return {
|
|
46
|
+
...objWithPredicates,
|
|
47
|
+
predicates: [
|
|
48
|
+
...objWithPredicates.predicates || [],
|
|
49
|
+
...castArray(predicates)
|
|
50
|
+
]
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const castThunk = (a) => typeof a === "function" ? a : () => a;
|
|
55
|
+
|
|
56
|
+
class PrismicError extends Error {
|
|
57
|
+
constructor(message = "An invalid API response was returned", url, response) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.url = url;
|
|
60
|
+
this.response = response;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const findRef = (refs, predicate) => {
|
|
65
|
+
const ref = refs.find((ref2) => predicate(ref2));
|
|
66
|
+
if (!ref) {
|
|
67
|
+
throw new PrismicError("Ref could not be found.", void 0, void 0);
|
|
68
|
+
}
|
|
69
|
+
return ref;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const findMasterRef = (refs) => {
|
|
73
|
+
return findRef(refs, (ref) => ref.isMasterRef);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const findRefByID = (refs, id) => {
|
|
77
|
+
return findRef(refs, (ref) => ref.id === id);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const findRefByLabel = (refs, label) => {
|
|
81
|
+
return findRef(refs, (ref) => ref.label === label);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const readValue = (value) => {
|
|
85
|
+
return value.replace(/%3B/g, ";");
|
|
86
|
+
};
|
|
87
|
+
const parse = (cookieString) => {
|
|
88
|
+
const result = {};
|
|
89
|
+
const cookies = cookieString.split("; ");
|
|
90
|
+
for (const cookie of cookies) {
|
|
91
|
+
const parts = cookie.split("=");
|
|
92
|
+
const value = parts.slice(1).join("=");
|
|
93
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
94
|
+
result[name] = readValue(value);
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
};
|
|
98
|
+
const getAll = (cookieStore) => parse(cookieStore);
|
|
99
|
+
const getCookie = (name, cookieStore) => getAll(cookieStore)[name];
|
|
100
|
+
|
|
101
|
+
class ForbiddenError extends PrismicError {
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class ParsingError extends PrismicError {
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const formatValue = (value) => {
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
return `[${value.map(formatValue).join(", ")}]`;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === "string") {
|
|
112
|
+
return `"${value}"`;
|
|
113
|
+
}
|
|
114
|
+
if (value instanceof Date) {
|
|
115
|
+
return `${value.getTime()}`;
|
|
116
|
+
}
|
|
117
|
+
return `${value}`;
|
|
118
|
+
};
|
|
119
|
+
const pathWithArgsPredicate = (name) => {
|
|
120
|
+
const fn = (path, ...args) => {
|
|
121
|
+
const formattedArgs = args.map(formatValue).join(", ");
|
|
122
|
+
const joiner = path && args.length ? ", " : "";
|
|
123
|
+
return `[${name}(${path}${joiner}${formattedArgs})]`;
|
|
124
|
+
};
|
|
125
|
+
return fn;
|
|
126
|
+
};
|
|
127
|
+
const pathPredicate = (name) => {
|
|
128
|
+
const predicateFn = pathWithArgsPredicate(name);
|
|
129
|
+
const fn = (path) => {
|
|
130
|
+
return predicateFn(path);
|
|
131
|
+
};
|
|
132
|
+
return fn;
|
|
133
|
+
};
|
|
134
|
+
const argsPredicate = (name) => {
|
|
135
|
+
const predicateFn = pathWithArgsPredicate(name);
|
|
136
|
+
const fn = (...args) => {
|
|
137
|
+
return predicateFn("", ...args);
|
|
138
|
+
};
|
|
139
|
+
return fn;
|
|
140
|
+
};
|
|
141
|
+
const predicate = {
|
|
142
|
+
at: pathWithArgsPredicate("at"),
|
|
143
|
+
not: pathWithArgsPredicate("not"),
|
|
144
|
+
any: pathWithArgsPredicate("any"),
|
|
145
|
+
in: pathWithArgsPredicate("in"),
|
|
146
|
+
fulltext: pathWithArgsPredicate("fulltext"),
|
|
147
|
+
has: pathPredicate("has"),
|
|
148
|
+
missing: pathPredicate("missing"),
|
|
149
|
+
similar: argsPredicate("similar"),
|
|
150
|
+
geopointNear: pathWithArgsPredicate("geopoint.near"),
|
|
151
|
+
numberLessThan: pathWithArgsPredicate("number.lt"),
|
|
152
|
+
numberGreaterThan: pathWithArgsPredicate("number.gt"),
|
|
153
|
+
numberInRange: pathWithArgsPredicate("number.inRange"),
|
|
154
|
+
dateAfter: pathWithArgsPredicate("date.after"),
|
|
155
|
+
dateBefore: pathWithArgsPredicate("date.before"),
|
|
156
|
+
dateBetween: pathWithArgsPredicate("date.between"),
|
|
157
|
+
dateDayOfMonth: pathWithArgsPredicate("date.day-of-month"),
|
|
158
|
+
dateDayOfMonthAfter: pathWithArgsPredicate("date.day-of-month-after"),
|
|
159
|
+
dateDayOfMonthBefore: pathWithArgsPredicate("date.day-of-month-before"),
|
|
160
|
+
dateDayOfWeek: pathWithArgsPredicate("date.day-of-week"),
|
|
161
|
+
dateDayOfWeekAfter: pathWithArgsPredicate("date.day-of-week-after"),
|
|
162
|
+
dateDayOfWeekBefore: pathWithArgsPredicate("date.day-of-week-before"),
|
|
163
|
+
dateMonth: pathWithArgsPredicate("date.month"),
|
|
164
|
+
dateMonthAfter: pathWithArgsPredicate("date.month-after"),
|
|
165
|
+
dateMonthBefore: pathWithArgsPredicate("date.month-before"),
|
|
166
|
+
dateYear: pathWithArgsPredicate("date.year"),
|
|
167
|
+
dateHour: pathWithArgsPredicate("date.hour"),
|
|
168
|
+
dateHourAfter: pathWithArgsPredicate("date.hour-after"),
|
|
169
|
+
dateHourBefore: pathWithArgsPredicate("date.hour-before")
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const preview = "io.prismic.preview";
|
|
173
|
+
|
|
174
|
+
var cookie = /*#__PURE__*/Object.freeze({
|
|
175
|
+
__proto__: null,
|
|
176
|
+
preview: preview
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const MAX_PAGE_SIZE = 100;
|
|
180
|
+
const REPOSITORY_CACHE_TTL = 5e3;
|
|
181
|
+
const GET_ALL_QUERY_DELAY = 500;
|
|
182
|
+
var RefStateMode;
|
|
183
|
+
(function(RefStateMode2) {
|
|
184
|
+
RefStateMode2["Master"] = "Master";
|
|
185
|
+
RefStateMode2["ReleaseID"] = "ReleaseID";
|
|
186
|
+
RefStateMode2["ReleaseLabel"] = "ReleaseLabel";
|
|
187
|
+
RefStateMode2["Manual"] = "Manual";
|
|
188
|
+
})(RefStateMode || (RefStateMode = {}));
|
|
189
|
+
const typePredicate = (documentType) => predicate.at("document.type", documentType);
|
|
190
|
+
const everyTagPredicate = (tags) => predicate.at("document.tags", tags);
|
|
191
|
+
const someTagsPredicate = (tags) => predicate.any("document.tags", tags);
|
|
192
|
+
const createClient = (...args) => new Client(...args);
|
|
193
|
+
class Client {
|
|
194
|
+
constructor(endpoint, options = {}) {
|
|
195
|
+
this.refState = {
|
|
196
|
+
mode: RefStateMode.Master,
|
|
197
|
+
autoPreviewsEnabled: true
|
|
198
|
+
};
|
|
199
|
+
this.cachedRepositoryExpiration = 0;
|
|
200
|
+
this.endpoint = endpoint;
|
|
201
|
+
this.accessToken = options.accessToken;
|
|
202
|
+
this.routes = options.routes;
|
|
203
|
+
this.defaultParams = options.defaultParams;
|
|
204
|
+
if (options.ref) {
|
|
205
|
+
this.queryContentFromRef(options.ref);
|
|
206
|
+
}
|
|
207
|
+
if (typeof options.fetch === "function") {
|
|
208
|
+
this.fetchFn = options.fetch;
|
|
209
|
+
} else if (typeof globalThis.fetch === "function") {
|
|
210
|
+
this.fetchFn = globalThis.fetch;
|
|
211
|
+
} else {
|
|
212
|
+
throw new PrismicError("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.", void 0, void 0);
|
|
213
|
+
}
|
|
214
|
+
if (this.fetchFn === globalThis.fetch) {
|
|
215
|
+
this.fetchFn = this.fetchFn.bind(globalThis);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
enableAutoPreviews() {
|
|
219
|
+
this.refState.autoPreviewsEnabled = true;
|
|
220
|
+
}
|
|
221
|
+
enableAutoPreviewsFromReq(req) {
|
|
222
|
+
this.refState.httpRequest = req;
|
|
223
|
+
this.refState.autoPreviewsEnabled = true;
|
|
224
|
+
}
|
|
225
|
+
disableAutoPreviews() {
|
|
226
|
+
this.refState.autoPreviewsEnabled = false;
|
|
227
|
+
}
|
|
228
|
+
async query(predicates, params) {
|
|
229
|
+
const url = await this.buildQueryURL({ ...params, predicates });
|
|
230
|
+
return await this.fetch(url, params);
|
|
231
|
+
}
|
|
232
|
+
async get(params) {
|
|
233
|
+
const url = await this.buildQueryURL(params);
|
|
234
|
+
return await this.fetch(url, params);
|
|
235
|
+
}
|
|
236
|
+
async getFirst(params) {
|
|
237
|
+
const url = await this.buildQueryURL(params);
|
|
238
|
+
const result = await this.fetch(url, params);
|
|
239
|
+
const firstResult = result.results[0];
|
|
240
|
+
if (firstResult) {
|
|
241
|
+
return firstResult;
|
|
242
|
+
}
|
|
243
|
+
throw new PrismicError("No documents were returned", url, void 0);
|
|
244
|
+
}
|
|
245
|
+
async dangerouslyGetAll(params = {}) {
|
|
246
|
+
const { limit = Infinity, ...actualParams } = params;
|
|
247
|
+
const resolvedParams = {
|
|
248
|
+
...actualParams,
|
|
249
|
+
pageSize: actualParams.pageSize || MAX_PAGE_SIZE
|
|
250
|
+
};
|
|
251
|
+
const documents = [];
|
|
252
|
+
let latestResult;
|
|
253
|
+
while ((!latestResult || latestResult.next_page) && documents.length < limit) {
|
|
254
|
+
const page = latestResult ? latestResult.page + 1 : void 0;
|
|
255
|
+
latestResult = await this.get({ ...resolvedParams, page });
|
|
256
|
+
documents.push(...latestResult.results);
|
|
257
|
+
if (latestResult.next_page) {
|
|
258
|
+
await new Promise((res) => setTimeout(res, GET_ALL_QUERY_DELAY));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return documents.slice(0, limit);
|
|
262
|
+
}
|
|
263
|
+
async getByID(id, params) {
|
|
264
|
+
return await this.getFirst(appendPredicates(params, predicate.at("document.id", id)));
|
|
265
|
+
}
|
|
266
|
+
async getByIDs(ids, params) {
|
|
267
|
+
return await this.get(appendPredicates(params, predicate.in("document.id", ids)));
|
|
268
|
+
}
|
|
269
|
+
async getAllByIDs(ids, params) {
|
|
270
|
+
return await this.dangerouslyGetAll(appendPredicates(params, predicate.in("document.id", ids)));
|
|
271
|
+
}
|
|
272
|
+
async getByUID(documentType, uid, params) {
|
|
273
|
+
return await this.getFirst(appendPredicates(params, [
|
|
274
|
+
typePredicate(documentType),
|
|
275
|
+
predicate.at(`my.${documentType}.uid`, uid)
|
|
276
|
+
]));
|
|
277
|
+
}
|
|
278
|
+
async getByUIDs(documentType, uids, params) {
|
|
279
|
+
return await this.get(appendPredicates(params, [
|
|
280
|
+
typePredicate(documentType),
|
|
281
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
282
|
+
]));
|
|
283
|
+
}
|
|
284
|
+
async getAllByUIDs(documentType, uids, params) {
|
|
285
|
+
return await this.dangerouslyGetAll(appendPredicates(params, [
|
|
286
|
+
typePredicate(documentType),
|
|
287
|
+
predicate.in(`my.${documentType}.uid`, uids)
|
|
288
|
+
]));
|
|
289
|
+
}
|
|
290
|
+
async getSingle(documentType, params) {
|
|
291
|
+
return await this.getFirst(appendPredicates(params, typePredicate(documentType)));
|
|
292
|
+
}
|
|
293
|
+
async getByType(documentType, params) {
|
|
294
|
+
return await this.get(appendPredicates(params, typePredicate(documentType)));
|
|
295
|
+
}
|
|
296
|
+
async getAllByType(documentType, params) {
|
|
297
|
+
return await this.dangerouslyGetAll(appendPredicates(params, typePredicate(documentType)));
|
|
298
|
+
}
|
|
299
|
+
async getByTag(tag, params) {
|
|
300
|
+
return await this.get(appendPredicates(params, everyTagPredicate(tag)));
|
|
301
|
+
}
|
|
302
|
+
async getAllByTag(tag, params) {
|
|
303
|
+
return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tag)));
|
|
304
|
+
}
|
|
305
|
+
async getByEveryTag(tags, params) {
|
|
306
|
+
return await this.get(appendPredicates(params, everyTagPredicate(tags)));
|
|
307
|
+
}
|
|
308
|
+
async getAllByEveryTag(tags, params) {
|
|
309
|
+
return await this.dangerouslyGetAll(appendPredicates(params, everyTagPredicate(tags)));
|
|
310
|
+
}
|
|
311
|
+
async getBySomeTags(tags, params) {
|
|
312
|
+
return await this.get(appendPredicates(params, someTagsPredicate(tags)));
|
|
313
|
+
}
|
|
314
|
+
async getAllBySomeTags(tags, params) {
|
|
315
|
+
return await this.dangerouslyGetAll(appendPredicates(params, someTagsPredicate(tags)));
|
|
316
|
+
}
|
|
317
|
+
async getRepository() {
|
|
318
|
+
const url = new URL(this.endpoint);
|
|
319
|
+
if (this.accessToken) {
|
|
320
|
+
url.searchParams.set("access_token", this.accessToken);
|
|
321
|
+
}
|
|
322
|
+
return await this.fetch(url.toString());
|
|
323
|
+
}
|
|
324
|
+
async getRefs() {
|
|
325
|
+
const repository = await this.getRepository();
|
|
326
|
+
return repository.refs;
|
|
327
|
+
}
|
|
328
|
+
async getRefByID(id) {
|
|
329
|
+
const refs = await this.getRefs();
|
|
330
|
+
return findRefByID(refs, id);
|
|
331
|
+
}
|
|
332
|
+
async getRefByLabel(label) {
|
|
333
|
+
const refs = await this.getRefs();
|
|
334
|
+
return findRefByLabel(refs, label);
|
|
335
|
+
}
|
|
336
|
+
async getMasterRef() {
|
|
337
|
+
const refs = await this.getRefs();
|
|
338
|
+
return findMasterRef(refs);
|
|
339
|
+
}
|
|
340
|
+
async getReleases() {
|
|
341
|
+
const refs = await this.getRefs();
|
|
342
|
+
return refs.filter((ref) => !ref.isMasterRef);
|
|
343
|
+
}
|
|
344
|
+
async getReleaseByID(id) {
|
|
345
|
+
const releases = await this.getReleases();
|
|
346
|
+
return findRefByID(releases, id);
|
|
347
|
+
}
|
|
348
|
+
async getReleaseByLabel(label) {
|
|
349
|
+
const releases = await this.getReleases();
|
|
350
|
+
return findRefByLabel(releases, label);
|
|
351
|
+
}
|
|
352
|
+
async getTags() {
|
|
353
|
+
try {
|
|
354
|
+
const tagsForm = await this.getCachedRepositoryForm("tags");
|
|
355
|
+
return await this.fetch(tagsForm.action);
|
|
356
|
+
} catch (e) {
|
|
357
|
+
const repository = await this.getRepository();
|
|
358
|
+
return repository.tags;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async buildQueryURL(params = {}) {
|
|
362
|
+
const ref = params.ref || await this.getResolvedRefString();
|
|
363
|
+
const integrationFieldsRef = params.integrationFieldsRef || (await this.getCachedRepository()).integrationFieldsRef || void 0;
|
|
364
|
+
return buildQueryURL(this.endpoint, {
|
|
365
|
+
...this.defaultParams,
|
|
366
|
+
...params,
|
|
367
|
+
ref,
|
|
368
|
+
integrationFieldsRef,
|
|
369
|
+
routes: params.routes || this.routes,
|
|
370
|
+
accessToken: params.accessToken || this.accessToken
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async resolvePreviewURL(args) {
|
|
374
|
+
var _a;
|
|
375
|
+
let documentID = args.documentID;
|
|
376
|
+
let previewToken = args.previewToken;
|
|
377
|
+
if (typeof globalThis.location !== "undefined") {
|
|
378
|
+
const searchParams = new URLSearchParams(globalThis.location.search);
|
|
379
|
+
documentID = documentID || searchParams.get("documentId") || void 0;
|
|
380
|
+
previewToken = previewToken || searchParams.get("token") || void 0;
|
|
381
|
+
} else if ((_a = this.refState.httpRequest) == null ? void 0 : _a.query) {
|
|
382
|
+
documentID = documentID || this.refState.httpRequest.query.documentId;
|
|
383
|
+
previewToken = previewToken || this.refState.httpRequest.query.token;
|
|
384
|
+
}
|
|
385
|
+
if (documentID != null) {
|
|
386
|
+
const document = await this.getByID(documentID, {
|
|
387
|
+
ref: previewToken
|
|
388
|
+
});
|
|
389
|
+
return prismicH.asLink(document, args.linkResolver);
|
|
390
|
+
} else {
|
|
391
|
+
return args.defaultURL;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
queryLatestContent() {
|
|
395
|
+
this.refState.mode = RefStateMode.Master;
|
|
396
|
+
}
|
|
397
|
+
queryContentFromReleaseByID(releaseID) {
|
|
398
|
+
this.refState = {
|
|
399
|
+
...this.refState,
|
|
400
|
+
mode: RefStateMode.ReleaseID,
|
|
401
|
+
releaseID
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
queryContentFromReleaseByLabel(releaseLabel) {
|
|
405
|
+
this.refState = {
|
|
406
|
+
...this.refState,
|
|
407
|
+
mode: RefStateMode.ReleaseLabel,
|
|
408
|
+
releaseLabel
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
queryContentFromRef(ref) {
|
|
412
|
+
this.refState = {
|
|
413
|
+
...this.refState,
|
|
414
|
+
mode: RefStateMode.Manual,
|
|
415
|
+
ref
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
async getCachedRepository() {
|
|
419
|
+
if (!this.cachedRepository || Date.now() >= this.cachedRepositoryExpiration) {
|
|
420
|
+
this.cachedRepositoryExpiration = Date.now() + REPOSITORY_CACHE_TTL;
|
|
421
|
+
this.cachedRepository = await this.getRepository();
|
|
422
|
+
}
|
|
423
|
+
return this.cachedRepository;
|
|
424
|
+
}
|
|
425
|
+
async getCachedRepositoryForm(name) {
|
|
426
|
+
const cachedRepository = await this.getCachedRepository();
|
|
427
|
+
const form = cachedRepository.forms[name];
|
|
428
|
+
if (!form) {
|
|
429
|
+
throw new PrismicError(`Form with name "${name}" could not be found`, void 0, void 0);
|
|
430
|
+
}
|
|
431
|
+
return form;
|
|
432
|
+
}
|
|
433
|
+
async getResolvedRefString() {
|
|
434
|
+
var _a, _b, _c;
|
|
435
|
+
if (this.refState.autoPreviewsEnabled) {
|
|
436
|
+
let previewRef = void 0;
|
|
437
|
+
if ((_a = globalThis.document) == null ? void 0 : _a.cookie) {
|
|
438
|
+
previewRef = getCookie(preview, globalThis.document.cookie);
|
|
439
|
+
} else if ((_c = (_b = this.refState.httpRequest) == null ? void 0 : _b.headers) == null ? void 0 : _c.cookie) {
|
|
440
|
+
previewRef = getCookie(preview, this.refState.httpRequest.headers.cookie);
|
|
441
|
+
}
|
|
442
|
+
if (previewRef) {
|
|
443
|
+
return previewRef;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const cachedRepository = await this.getCachedRepository();
|
|
447
|
+
const refModeType = this.refState.mode;
|
|
448
|
+
if (refModeType === RefStateMode.ReleaseID) {
|
|
449
|
+
return findRefByID(cachedRepository.refs, this.refState.releaseID).ref;
|
|
450
|
+
} else if (refModeType === RefStateMode.ReleaseLabel) {
|
|
451
|
+
return findRefByLabel(cachedRepository.refs, this.refState.releaseLabel).ref;
|
|
452
|
+
} else if (refModeType === RefStateMode.Manual) {
|
|
453
|
+
const res = await castThunk(this.refState.ref)();
|
|
454
|
+
if (typeof res === "string") {
|
|
455
|
+
return res;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return findMasterRef(cachedRepository.refs).ref;
|
|
459
|
+
}
|
|
460
|
+
async fetch(url, _params) {
|
|
461
|
+
const res = await this.fetchFn(url);
|
|
462
|
+
let json;
|
|
463
|
+
try {
|
|
464
|
+
json = await res.json();
|
|
465
|
+
} catch (e) {
|
|
466
|
+
throw new PrismicError(void 0, url, void 0);
|
|
467
|
+
}
|
|
468
|
+
switch (res.status) {
|
|
469
|
+
case 200: {
|
|
470
|
+
return json;
|
|
471
|
+
}
|
|
472
|
+
case 400: {
|
|
473
|
+
throw new ParsingError(json.message, url, json);
|
|
474
|
+
}
|
|
475
|
+
case 401:
|
|
476
|
+
case 403: {
|
|
477
|
+
throw new ForbiddenError("error" in json ? json.error : json.message, url, json);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
throw new PrismicError(void 0, url, json);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const predicates = predicate;
|
|
485
|
+
const Predicates = predicate;
|
|
486
|
+
|
|
487
|
+
export { Client, ForbiddenError, ParsingError, Predicates, PrismicError, buildQueryURL, cookie, createClient, getEndpoint, predicate, predicates };
|
|
488
|
+
//# sourceMappingURL=index.js.map
|