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