@usethrottle/builder 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/dist/chunk-XYWLWXKG.js +619 -0
- package/dist/chunk-ZLNUG6ZZ.cjs +619 -0
- package/dist/index.cjs +3 -508
- package/dist/index.js +11 -516
- package/dist/next.cjs +74 -6
- package/dist/next.d.cts +31 -1
- package/dist/next.d.ts +31 -1
- package/dist/next.js +69 -1
- package/dist/watch.d.cts +28 -0
- package/dist/watch.d.ts +28 -0
- package/package.json +2 -2
- package/dist/chunk-EERLS5TR.js +0 -98
- package/dist/chunk-UFIR4HJA.cjs +0 -98
package/dist/index.cjs
CHANGED
|
@@ -1,522 +1,17 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var _chunkUFIR4HJAcjs = require('./chunk-UFIR4HJA.cjs');
|
|
8
7
|
|
|
9
|
-
// src/errors.ts
|
|
10
|
-
var BuilderUnavailableError = class extends Error {
|
|
11
|
-
constructor(reason, message, options = {}) {
|
|
12
|
-
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
13
|
-
_chunkUFIR4HJAcjs.__publicField.call(void 0, this, "reason");
|
|
14
|
-
/** The HTTP status, when the Builder answered. */
|
|
15
|
-
_chunkUFIR4HJAcjs.__publicField.call(void 0, this, "status");
|
|
16
|
-
this.name = "BuilderUnavailableError";
|
|
17
|
-
this.reason = reason;
|
|
18
|
-
this.status = options.status;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
var BuilderNotFoundError = class extends Error {
|
|
22
|
-
constructor(slug) {
|
|
23
|
-
super(slug === void 0 ? "Builder page not found" : `Builder page "${slug}" not found`);
|
|
24
|
-
_chunkUFIR4HJAcjs.__publicField.call(void 0, this, "slug");
|
|
25
|
-
this.name = "BuilderNotFoundError";
|
|
26
|
-
this.slug = slug;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var PreviewExpiredError = class extends Error {
|
|
30
|
-
constructor() {
|
|
31
|
-
super("The Builder preview link has expired");
|
|
32
|
-
this.name = "PreviewExpiredError";
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var BuilderReleaseNotFoundError = class extends Error {
|
|
36
|
-
constructor(release) {
|
|
37
|
-
super(
|
|
38
|
-
release === "latest" ? "This Builder site has no release yet. Press Release in the Builder, then build again." : `Builder has no release "${release}" for this site. Check BUILDER_RELEASE_ID.`
|
|
39
|
-
);
|
|
40
|
-
_chunkUFIR4HJAcjs.__publicField.call(void 0, this, "release");
|
|
41
|
-
this.name = "BuilderReleaseNotFoundError";
|
|
42
|
-
this.release = release;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
8
|
|
|
46
|
-
// src/cache.ts
|
|
47
|
-
var UNTIL_CHANGED_REVALIDATE_S = 3600;
|
|
48
|
-
function cacheInit(tags, preview) {
|
|
49
|
-
if (preview) return { cache: "no-store" };
|
|
50
|
-
return { next: { revalidate: UNTIL_CHANGED_REVALIDATE_S, tags: [...tags] } };
|
|
51
|
-
}
|
|
52
9
|
|
|
53
|
-
// src/documents.ts
|
|
54
|
-
var _builderschema = require('@usethrottle/builder-schema');
|
|
55
|
-
var MAX_NAME_CHARS = 128;
|
|
56
|
-
function isRecord(value) {
|
|
57
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
58
|
-
}
|
|
59
|
-
function checkName(kind, value) {
|
|
60
|
-
if (typeof value !== "string" || value === "" || value.length > MAX_NAME_CHARS)
|
|
61
|
-
throw new TypeError(`A Builder ${kind} is a non-empty string of at most ${String(MAX_NAME_CHARS)} characters`);
|
|
62
|
-
return value;
|
|
63
|
-
}
|
|
64
|
-
function currentDocument(answer, where) {
|
|
65
|
-
const doc = answer.document;
|
|
66
|
-
if (!isRecord(doc))
|
|
67
|
-
throw new BuilderUnavailableError("invalid-response", `Builder ${where} answer has no document`);
|
|
68
|
-
if (doc.schemaVersion === _builderschema.CURRENT_SCHEMA_VERSION) return doc;
|
|
69
|
-
const upgraded = _builderschema.upgradeDocument.call(void 0, doc);
|
|
70
|
-
if (!upgraded.ok)
|
|
71
|
-
throw new BuilderUnavailableError(
|
|
72
|
-
"unsupported-document",
|
|
73
|
-
`Builder ${where} document cannot be read by this SDK version (${_nullishCoalesce(_optionalChain([upgraded, 'access', _ => _.issues, 'access', _2 => _2[0], 'optionalAccess', _3 => _3.code]), () => ( "invalid"))})`
|
|
74
|
-
);
|
|
75
|
-
return upgraded.doc;
|
|
76
|
-
}
|
|
77
10
|
|
|
78
|
-
// src/last-good.ts
|
|
79
|
-
var LAST_GOOD_MS = 10 * 60 * 1e3;
|
|
80
|
-
var MAX_ENTRIES = 200;
|
|
81
|
-
var store = /* @__PURE__ */ new Map();
|
|
82
|
-
function rememberGood(key, value) {
|
|
83
|
-
store.delete(key);
|
|
84
|
-
store.set(key, { value, at: Date.now(), warned: false });
|
|
85
|
-
if (store.size > MAX_ENTRIES) {
|
|
86
|
-
const oldest = store.keys().next();
|
|
87
|
-
if (!oldest.done) store.delete(oldest.value);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function recallGood(key, warn) {
|
|
91
|
-
const entry = store.get(key);
|
|
92
|
-
if (entry === void 0) return void 0;
|
|
93
|
-
if (Date.now() - entry.at > LAST_GOOD_MS) {
|
|
94
|
-
store.delete(key);
|
|
95
|
-
return void 0;
|
|
96
|
-
}
|
|
97
|
-
if (!entry.warned) {
|
|
98
|
-
entry.warned = true;
|
|
99
|
-
warn();
|
|
100
|
-
}
|
|
101
|
-
return { value: entry.value };
|
|
102
|
-
}
|
|
103
11
|
|
|
104
|
-
// src/release.ts
|
|
105
12
|
|
|
106
|
-
var DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
|
|
107
|
-
var DEFAULT_BUNDLE_TIMEOUT_MS = 6e4;
|
|
108
|
-
var SUPPORTED_BUNDLE_VERSION = 1;
|
|
109
|
-
var RELEASE_ID_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/u;
|
|
110
|
-
function fromEnvironment() {
|
|
111
|
-
const env = _optionalChain([globalThis, 'access', _4 => _4.process, 'optionalAccess', _5 => _5.env]);
|
|
112
|
-
const value = _optionalChain([env, 'optionalAccess', _6 => _6.BUILDER_RELEASE_ID, 'optionalAccess', _7 => _7.trim, 'call', _8 => _8()]);
|
|
113
|
-
return value === void 0 || value === "" ? void 0 : value;
|
|
114
|
-
}
|
|
115
|
-
function releaseRef(argument, pinned) {
|
|
116
|
-
const given = argument !== void 0 && argument !== "" ? argument : pinned !== void 0 && pinned !== "" ? pinned : void 0;
|
|
117
|
-
const ref = _nullishCoalesce(_nullishCoalesce(given, () => ( fromEnvironment())), () => ( "latest"));
|
|
118
|
-
if (ref === "latest" || RELEASE_ID_RE.test(ref)) return ref;
|
|
119
|
-
throw new TypeError(
|
|
120
|
-
`${given !== void 0 ? "The release" : "BUILDER_RELEASE_ID"} "${ref.slice(0, 64)}" is not a Builder release id. Use the 26-character id shown in the Builder release history, or leave it unset to build the newest release.`
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
var invalid = (message) => new BuilderUnavailableError("invalid-response", message);
|
|
124
|
-
function readDocument(value, where) {
|
|
125
|
-
if (!isRecord(value) || typeof value.version !== "number") throw invalid(`Builder release ${where} is malformed`);
|
|
126
|
-
return { version: value.version, document: currentDocument(value, where) };
|
|
127
|
-
}
|
|
128
|
-
function readPage(value) {
|
|
129
|
-
if (!isRecord(value) || typeof value.slug !== "string" || typeof value.id !== "string" || typeof value.path !== "string" || typeof value.pageType !== "string" || !isRecord(value.title) || !isRecord(value.seo))
|
|
130
|
-
throw invalid("Builder release has a page without its fields");
|
|
131
|
-
return { ...value, document: currentDocument(value, `page ${value.slug}`) };
|
|
132
|
-
}
|
|
133
|
-
function readBundle(body) {
|
|
134
|
-
if (!isRecord(body)) throw invalid("Builder answered a release bundle that is not an object");
|
|
135
|
-
if (body.bundleVersion !== SUPPORTED_BUNDLE_VERSION)
|
|
136
|
-
throw new BuilderUnavailableError(
|
|
137
|
-
"unsupported-document",
|
|
138
|
-
`This SDK reads release bundle version ${String(SUPPORTED_BUNDLE_VERSION)}, and Builder sent ${String(body.bundleVersion)}. Update @usethrottle/builder.`
|
|
139
|
-
);
|
|
140
|
-
if (typeof body.releaseId !== "string" || typeof body.generation !== "number" || typeof body.siteVersion !== "number" || typeof body.defaultLocale !== "string" || !Array.isArray(body.pages) || !isRecord(body.site) || !isRecord(body.site[body.defaultLocale]) || !isRecord(body.slots) || !isRecord(body.redirects))
|
|
141
|
-
throw invalid("Builder release bundle is missing fields");
|
|
142
|
-
return {
|
|
143
|
-
...body,
|
|
144
|
-
pages: body.pages.map(readPage),
|
|
145
|
-
header: readDocument(body.header, "header"),
|
|
146
|
-
footer: readDocument(body.footer, "footer"),
|
|
147
|
-
slots: Object.fromEntries(Object.entries(body.slots).map(([name, value]) => [name, readDocument(value, `slot ${name}`)]))
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
async function fetchBundle({ base, key, ref, timeoutMs, doFetch }) {
|
|
151
|
-
const headers = { accept: "application/json" };
|
|
152
|
-
if (key.startsWith("tb_sk_")) headers.authorization = `Bearer ${key}`;
|
|
153
|
-
else headers["X-Builder-Key"] = key;
|
|
154
|
-
const controller = new AbortController();
|
|
155
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
156
|
-
let status;
|
|
157
|
-
let body;
|
|
158
|
-
let unreadable;
|
|
159
|
-
try {
|
|
160
|
-
const response = await (_nullishCoalesce(doFetch, () => ( globalThis.fetch)))(`${base}/v1/releases/${ref}/bundle`, {
|
|
161
|
-
method: "GET",
|
|
162
|
-
headers,
|
|
163
|
-
signal: controller.signal
|
|
164
|
-
});
|
|
165
|
-
status = response.status;
|
|
166
|
-
try {
|
|
167
|
-
body = await response.json();
|
|
168
|
-
} catch (cause) {
|
|
169
|
-
if (controller.signal.aborted) throw cause;
|
|
170
|
-
unreadable = cause;
|
|
171
|
-
body = void 0;
|
|
172
|
-
}
|
|
173
|
-
} catch (cause) {
|
|
174
|
-
throw new BuilderUnavailableError(
|
|
175
|
-
controller.signal.aborted ? "timeout" : "network",
|
|
176
|
-
controller.signal.aborted ? `Builder did not send the release bundle within ${String(timeoutMs)} ms` : "Builder could not be reached to read the release bundle",
|
|
177
|
-
{ cause }
|
|
178
|
-
);
|
|
179
|
-
} finally {
|
|
180
|
-
clearTimeout(timer);
|
|
181
|
-
}
|
|
182
|
-
if (unreadable !== void 0 && status >= 200 && status < 300)
|
|
183
|
-
throw new BuilderUnavailableError(
|
|
184
|
-
"invalid-response",
|
|
185
|
-
"Builder release bundle truncated (too large or interrupted). A bundle is read into memory whole, up to 48 MB; retry the build, and check that its machine has the memory.",
|
|
186
|
-
{ cause: unreadable }
|
|
187
|
-
);
|
|
188
|
-
if (status === 404) throw new BuilderReleaseNotFoundError(ref);
|
|
189
|
-
if (status === 401 || status === 403)
|
|
190
|
-
throw new BuilderUnavailableError(
|
|
191
|
-
"status",
|
|
192
|
-
`Builder refused the key (${String(status)}). A build reads with the publishable key of a server channel (tb_pk_...) or a secret key holding releases:read, never a browser key.`,
|
|
193
|
-
{ status }
|
|
194
|
-
);
|
|
195
|
-
if (status < 200 || status >= 300)
|
|
196
|
-
throw new BuilderUnavailableError("status", `Builder answered ${String(status)} for the release bundle`, { status });
|
|
197
|
-
return readBundle(body);
|
|
198
|
-
}
|
|
199
|
-
var STORE = /* @__PURE__ */ Symbol.for("@usethrottle/builder:release-bundles");
|
|
200
|
-
var stored = () => {
|
|
201
|
-
const holder = globalThis;
|
|
202
|
-
return _nullishCoalesce(holder[STORE], () => ( (holder[STORE] = /* @__PURE__ */ new Map())));
|
|
203
|
-
};
|
|
204
|
-
function loadOnce(load) {
|
|
205
|
-
const memo = stored();
|
|
206
|
-
const id = `${load.base}|${load.key}|${load.ref}`;
|
|
207
|
-
let held = memo.get(id);
|
|
208
|
-
if (held === void 0) {
|
|
209
|
-
held = fetchBundle(load);
|
|
210
|
-
memo.set(id, held);
|
|
211
|
-
held.catch(() => memo.delete(id));
|
|
212
|
-
}
|
|
213
|
-
return held;
|
|
214
|
-
}
|
|
215
|
-
var pick = (map, chain) => {
|
|
216
|
-
for (const locale of chain) {
|
|
217
|
-
const value = map === void 0 ? void 0 : map[locale];
|
|
218
|
-
if (typeof value === "string" && value !== "") return value;
|
|
219
|
-
}
|
|
220
|
-
return void 0;
|
|
221
|
-
};
|
|
222
|
-
var chainOf = (bundle, locale) => locale === void 0 || locale === bundle.defaultLocale ? [bundle.defaultLocale] : [locale, bundle.defaultLocale];
|
|
223
|
-
function siteFor(bundle, locale, now) {
|
|
224
|
-
const found = locale === void 0 ? void 0 : bundle.site[locale];
|
|
225
|
-
const settings = (_nullishCoalesce(found, () => ( bundle.site[bundle.defaultLocale]))).settings;
|
|
226
|
-
const announcement = settings.announcement;
|
|
227
|
-
const windowed = isRecord(announcement) ? {
|
|
228
|
-
...settings,
|
|
229
|
-
announcement: {
|
|
230
|
-
...announcement,
|
|
231
|
-
enabled: announcement.enabled === true && (announcement.startsAt == null || Date.parse(String(announcement.startsAt)) <= now) && (announcement.endsAt == null || now < Date.parse(String(announcement.endsAt)))
|
|
232
|
-
}
|
|
233
|
-
} : settings;
|
|
234
|
-
return { generation: bundle.generation, siteVersion: bundle.siteVersion, settings: windowed };
|
|
235
|
-
}
|
|
236
|
-
function summaryOf(page, chain) {
|
|
237
|
-
return {
|
|
238
|
-
id: page.id,
|
|
239
|
-
slug: page.slug,
|
|
240
|
-
pageType: page.pageType,
|
|
241
|
-
title: _nullishCoalesce(pick(page.title, chain), () => ( page.slug)),
|
|
242
|
-
updatedAt: page.publishedAt,
|
|
243
|
-
path: page.path,
|
|
244
|
-
noindex: page.seo.noindex === true
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
function pageFor(bundle, slug, chain) {
|
|
248
|
-
const page = bundle.pages.find((candidate) => candidate.slug === slug);
|
|
249
|
-
if (page === void 0) {
|
|
250
|
-
if (Object.hasOwn(bundle.redirects, slug)) return { redirectTo: bundle.redirects[slug] };
|
|
251
|
-
throw new BuilderNotFoundError(slug);
|
|
252
|
-
}
|
|
253
|
-
const title = pick(page.seo.title, chain);
|
|
254
|
-
const description = pick(page.seo.description, chain);
|
|
255
|
-
return {
|
|
256
|
-
...summaryOf(page, chain),
|
|
257
|
-
generation: bundle.generation,
|
|
258
|
-
siteVersion: bundle.siteVersion,
|
|
259
|
-
version: page.version,
|
|
260
|
-
seo: {
|
|
261
|
-
...title === void 0 ? {} : { title },
|
|
262
|
-
...description === void 0 ? {} : { description },
|
|
263
|
-
shareImage: page.seo.shareImage,
|
|
264
|
-
noindex: page.seo.noindex
|
|
265
|
-
},
|
|
266
|
-
document: page.document
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
var documentOf = (bundle, held) => ({
|
|
270
|
-
generation: bundle.generation,
|
|
271
|
-
document: held === void 0 ? _builderschema.emptyDocument.call(void 0, ) : held.document
|
|
272
|
-
});
|
|
273
|
-
function createBuildClient(options) {
|
|
274
|
-
const builderKey = _nullishCoalesce(options.builderKey, () => ( ""));
|
|
275
|
-
if (typeof builderKey !== "string" || builderKey === "" && options.releaseBundle === void 0)
|
|
276
|
-
throw new TypeError("createBuilderClient needs a builderKey");
|
|
277
|
-
const base = (_nullishCoalesce(options.baseUrl, () => ( DEFAULT_BASE_URL))).replace(/\/+$/u, "");
|
|
278
|
-
const timeoutMs = _nullishCoalesce(options.timeoutMs, () => ( DEFAULT_BUNDLE_TIMEOUT_MS));
|
|
279
|
-
const clock = _nullishCoalesce(options.now, () => ( Date.now));
|
|
280
|
-
const getReleaseBundle = async ({ release } = {}) => {
|
|
281
|
-
if (options.releaseBundle !== void 0) return options.releaseBundle;
|
|
282
|
-
return loadOnce({ base, key: builderKey, ref: releaseRef(release, options.release), timeoutMs, doFetch: options.fetch });
|
|
283
|
-
};
|
|
284
|
-
return {
|
|
285
|
-
mode: "build",
|
|
286
|
-
getReleaseBundle,
|
|
287
|
-
site: async ({ locale } = {}) => siteFor(await getReleaseBundle(), locale, clock()),
|
|
288
|
-
pages: {
|
|
289
|
-
list: async () => {
|
|
290
|
-
const bundle = await getReleaseBundle();
|
|
291
|
-
const chain = chainOf(bundle, void 0);
|
|
292
|
-
return bundle.pages.map((page) => summaryOf(page, chain));
|
|
293
|
-
},
|
|
294
|
-
get: async (slug, { locale } = {}) => {
|
|
295
|
-
const bundle = await getReleaseBundle();
|
|
296
|
-
return pageFor(bundle, checkName("page slug", slug), chainOf(bundle, locale));
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
parts: {
|
|
300
|
-
get: async (name) => {
|
|
301
|
-
if (name !== "header" && name !== "footer") throw new TypeError('A Builder part is "header" or "footer"');
|
|
302
|
-
const bundle = await getReleaseBundle();
|
|
303
|
-
return documentOf(bundle, bundle[name]);
|
|
304
|
-
}
|
|
305
|
-
},
|
|
306
|
-
slots: {
|
|
307
|
-
get: async (name) => {
|
|
308
|
-
checkName("slot name", name);
|
|
309
|
-
const bundle = await getReleaseBundle();
|
|
310
|
-
return documentOf(bundle, Object.hasOwn(bundle.slots, name) ? bundle.slots[name] : void 0);
|
|
311
|
-
}
|
|
312
|
-
},
|
|
313
|
-
bundle: async ({ page: pageSlug, slots, locale } = {}) => {
|
|
314
|
-
const names = [...new Set(_nullishCoalesce(slots, () => ( [])))].map((name) => checkName("slot name", name)).sort();
|
|
315
|
-
if (pageSlug !== void 0) checkName("page slug", pageSlug);
|
|
316
|
-
const bundle = await getReleaseBundle();
|
|
317
|
-
return {
|
|
318
|
-
generation: bundle.generation,
|
|
319
|
-
site: siteFor(bundle, locale, clock()),
|
|
320
|
-
header: documentOf(bundle, bundle.header),
|
|
321
|
-
footer: documentOf(bundle, bundle.footer),
|
|
322
|
-
...pageSlug === void 0 ? {} : { page: pageFor(bundle, pageSlug, chainOf(bundle, locale)) },
|
|
323
|
-
slots: Object.fromEntries(
|
|
324
|
-
names.map((name) => [name, documentOf(bundle, Object.hasOwn(bundle.slots, name) ? bundle.slots[name] : void 0)])
|
|
325
|
-
)
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
13
|
|
|
331
|
-
|
|
332
|
-
var DEFAULT_BASE_URL2 = "https://builder.usethrottle.dev";
|
|
333
|
-
var DEFAULT_TIMEOUT_MS = 3e3;
|
|
334
|
-
function queryString(query) {
|
|
335
|
-
const pairs = Object.entries(query).filter((entry) => entry[1] !== void 0 && entry[1] !== "").sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
336
|
-
return pairs.length === 0 ? "" : `?${pairs.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&")}`;
|
|
337
|
-
}
|
|
338
|
-
function withDocument(answer, where) {
|
|
339
|
-
return { ...answer, document: currentDocument(answer, where) };
|
|
340
|
-
}
|
|
341
|
-
var invalid2 = (message) => new BuilderUnavailableError("invalid-response", message);
|
|
342
|
-
function generationOf(answer, where) {
|
|
343
|
-
const { generation } = answer;
|
|
344
|
-
if (typeof generation !== "number" || !Number.isSafeInteger(generation) || generation < 0)
|
|
345
|
-
throw invalid2(`Builder ${where} answer has no generation`);
|
|
346
|
-
return generation;
|
|
347
|
-
}
|
|
348
|
-
function readSite(answer) {
|
|
349
|
-
generationOf(answer, "site");
|
|
350
|
-
if (typeof answer.siteVersion !== "number" || !Number.isSafeInteger(answer.siteVersion))
|
|
351
|
-
throw invalid2("Builder site answer has no siteVersion");
|
|
352
|
-
if (!isRecord(answer.settings)) throw invalid2("Builder site answer has no settings");
|
|
353
|
-
return answer;
|
|
354
|
-
}
|
|
355
|
-
var SUMMARY_KEYS = ["id", "slug", "pageType", "title", "updatedAt", "path"];
|
|
356
|
-
function readPages(answer) {
|
|
357
|
-
const { pages } = answer;
|
|
358
|
-
if (!Array.isArray(pages)) throw invalid2("Builder pages answer is not a list");
|
|
359
|
-
for (const page of pages)
|
|
360
|
-
if (!isRecord(page) || SUMMARY_KEYS.some((key) => typeof page[key] !== "string"))
|
|
361
|
-
throw invalid2("Builder pages answer has a page without its fields");
|
|
362
|
-
return pages;
|
|
363
|
-
}
|
|
364
|
-
function readDocument2(answer, where) {
|
|
365
|
-
generationOf(answer, where);
|
|
366
|
-
return withDocument(answer, where);
|
|
367
|
-
}
|
|
368
|
-
function readPage2(answer) {
|
|
369
|
-
if (typeof answer.redirectTo === "string") return { redirectTo: answer.redirectTo };
|
|
370
|
-
generationOf(answer, "page");
|
|
371
|
-
if (typeof answer.slug !== "string") throw invalid2("Builder page answer has no slug");
|
|
372
|
-
return withDocument(answer, "page");
|
|
373
|
-
}
|
|
374
|
-
function readBundle2(answer) {
|
|
375
|
-
const generation = generationOf(answer, "bundle");
|
|
376
|
-
if (!isRecord(answer.site) || !isRecord(answer.header) || !isRecord(answer.footer))
|
|
377
|
-
throw invalid2("Builder bundle answer has no site, header or footer");
|
|
378
|
-
const slots = answer.slots === void 0 ? {} : answer.slots;
|
|
379
|
-
if (!isRecord(slots)) throw invalid2("Builder bundle answer has slots that are not an object");
|
|
380
|
-
return {
|
|
381
|
-
generation,
|
|
382
|
-
site: readSite(answer.site),
|
|
383
|
-
header: readDocument2(answer.header, "header"),
|
|
384
|
-
footer: readDocument2(answer.footer, "footer"),
|
|
385
|
-
...isRecord(answer.page) ? { page: readPage2(answer.page) } : {},
|
|
386
|
-
slots: Object.fromEntries(
|
|
387
|
-
Object.entries(slots).map(([name, value]) => {
|
|
388
|
-
if (!isRecord(value)) throw invalid2(`Builder slot ${name} answer is not an object`);
|
|
389
|
-
return [name, readDocument2(value, `slot ${name}`)];
|
|
390
|
-
})
|
|
391
|
-
)
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
function createBuilderClient(options) {
|
|
395
|
-
if ("mode" in options && options.mode === "build") return createBuildClient(options);
|
|
396
|
-
return createLiveClient(options);
|
|
397
|
-
}
|
|
398
|
-
function createLiveClient(options) {
|
|
399
|
-
const { builderKey, previewToken } = options;
|
|
400
|
-
if (typeof builderKey !== "string" || builderKey === "")
|
|
401
|
-
throw new TypeError("createBuilderClient needs a builderKey");
|
|
402
|
-
if (builderKey.startsWith("tb_sk_"))
|
|
403
|
-
throw new TypeError("createBuilderClient takes the publishable Builder key (tb_pk_\u2026), never a secret key");
|
|
404
|
-
const previewHeader = previewToken !== void 0 && previewToken !== "" ? previewToken : void 0;
|
|
405
|
-
const preview = previewHeader !== void 0;
|
|
406
|
-
const base = (_nullishCoalesce(options.baseUrl, () => ( DEFAULT_BASE_URL2))).replace(/\/+$/u, "");
|
|
407
|
-
const timeoutMs = _nullishCoalesce(options.timeoutMs, () => ( DEFAULT_TIMEOUT_MS));
|
|
408
|
-
const doFetch = options.fetch;
|
|
409
|
-
async function once(url, init) {
|
|
410
|
-
const controller = new AbortController();
|
|
411
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
412
|
-
try {
|
|
413
|
-
const response = await (_nullishCoalesce(doFetch, () => ( globalThis.fetch)))(url, {
|
|
414
|
-
...init,
|
|
415
|
-
signal: controller.signal
|
|
416
|
-
});
|
|
417
|
-
let body;
|
|
418
|
-
try {
|
|
419
|
-
body = response.status === 204 ? void 0 : await response.json();
|
|
420
|
-
} catch (cause) {
|
|
421
|
-
if (controller.signal.aborted) throw cause;
|
|
422
|
-
body = void 0;
|
|
423
|
-
}
|
|
424
|
-
return { body, status: response.status };
|
|
425
|
-
} catch (cause) {
|
|
426
|
-
throw new BuilderUnavailableError(
|
|
427
|
-
controller.signal.aborted ? "timeout" : "network",
|
|
428
|
-
controller.signal.aborted ? `Builder did not answer within ${String(timeoutMs)} ms` : "Builder could not be reached",
|
|
429
|
-
{ cause }
|
|
430
|
-
);
|
|
431
|
-
} finally {
|
|
432
|
-
clearTimeout(timer);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
async function fetchAnswer(path, read2) {
|
|
436
|
-
const headers = { accept: "application/json", "X-Builder-Key": builderKey };
|
|
437
|
-
if (previewHeader !== void 0) headers["X-Builder-Preview"] = previewHeader;
|
|
438
|
-
const url = `${base}${path}${queryString(_nullishCoalesce(read2.query, () => ( {})))}`;
|
|
439
|
-
const init = { method: "GET", headers, ...cacheInit(_chunkUFIR4HJAcjs.fitTags.call(void 0, read2.tags), preview) };
|
|
440
|
-
const { body, status } = await once(url, init);
|
|
441
|
-
if (status === 401 && preview) throw new PreviewExpiredError();
|
|
442
|
-
if (status === 404 && read2.pageSlug !== void 0) throw new BuilderNotFoundError(read2.pageSlug);
|
|
443
|
-
if (status < 200 || status >= 300)
|
|
444
|
-
throw new BuilderUnavailableError("status", `Builder answered ${String(status)}`, { status });
|
|
445
|
-
if (!isRecord(body) && !Array.isArray(body))
|
|
446
|
-
throw new BuilderUnavailableError("invalid-response", "Builder answered a body that is not JSON");
|
|
447
|
-
return Array.isArray(body) ? { pages: body } : body;
|
|
448
|
-
}
|
|
449
|
-
async function read(path, options2, shape) {
|
|
450
|
-
const goodKey = `${base}|${builderKey}|${path}${queryString(_nullishCoalesce(options2.query, () => ( {})))}`;
|
|
451
|
-
try {
|
|
452
|
-
const value = shape(await fetchAnswer(path, options2));
|
|
453
|
-
if (!preview) rememberGood(goodKey, value);
|
|
454
|
-
return value;
|
|
455
|
-
} catch (error) {
|
|
456
|
-
if (preview || !(error instanceof BuilderUnavailableError)) throw error;
|
|
457
|
-
const good = recallGood(goodKey, () => {
|
|
458
|
-
console.warn(
|
|
459
|
-
`[throttle-builder] ${path} failed (${error.reason}); serving the last good answer`
|
|
460
|
-
);
|
|
461
|
-
});
|
|
462
|
-
if (good === void 0) throw error;
|
|
463
|
-
return good.value;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
return {
|
|
467
|
-
site: ({ locale } = {}) => read("/v1/site", { query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.site] }, readSite),
|
|
468
|
-
pages: {
|
|
469
|
-
list: () => read(
|
|
470
|
-
"/v1/pages",
|
|
471
|
-
{ tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.pages] },
|
|
472
|
-
readPages
|
|
473
|
-
),
|
|
474
|
-
get: async (slug, { locale } = {}) => read(
|
|
475
|
-
`/v1/pages/${encodeURIComponent(checkName("page slug", slug))}`,
|
|
476
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.page(slug)], pageSlug: slug },
|
|
477
|
-
readPage2
|
|
478
|
-
)
|
|
479
|
-
},
|
|
480
|
-
parts: {
|
|
481
|
-
get: async (name, { locale } = {}) => {
|
|
482
|
-
if (name !== "header" && name !== "footer")
|
|
483
|
-
throw new TypeError('A Builder part is "header" or "footer"');
|
|
484
|
-
return read(
|
|
485
|
-
`/v1/parts/${name}`,
|
|
486
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.part(name)] },
|
|
487
|
-
(answer) => readDocument2(answer, `part ${name}`)
|
|
488
|
-
);
|
|
489
|
-
}
|
|
490
|
-
},
|
|
491
|
-
slots: {
|
|
492
|
-
get: async (name, { locale } = {}) => read(
|
|
493
|
-
`/v1/slots/${encodeURIComponent(checkName("slot name", name))}`,
|
|
494
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.slot(name)] },
|
|
495
|
-
(answer) => readDocument2(answer, `slot ${name}`)
|
|
496
|
-
)
|
|
497
|
-
},
|
|
498
|
-
bundle: async ({ page: pageSlug, slots, locale } = {}) => {
|
|
499
|
-
const names = [...new Set(_nullishCoalesce(slots, () => ( [])))].map((name) => checkName("slot name", name)).sort();
|
|
500
|
-
if (pageSlug !== void 0) checkName("page slug", pageSlug);
|
|
501
|
-
return read(
|
|
502
|
-
"/v1/bundle",
|
|
503
|
-
{
|
|
504
|
-
query: { page: pageSlug, slots: names.join(","), locale },
|
|
505
|
-
pageSlug,
|
|
506
|
-
tags: [
|
|
507
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.all,
|
|
508
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.site,
|
|
509
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.part("header"),
|
|
510
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.part("footer"),
|
|
511
|
-
...pageSlug === void 0 ? [] : [_chunkUFIR4HJAcjs.BUILDER_TAGS.page(pageSlug)],
|
|
512
|
-
...names.map((name) => _chunkUFIR4HJAcjs.BUILDER_TAGS.slot(name))
|
|
513
|
-
]
|
|
514
|
-
},
|
|
515
|
-
readBundle2
|
|
516
|
-
);
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
}
|
|
14
|
+
var _chunkZLNUG6ZZcjs = require('./chunk-ZLNUG6ZZ.cjs');
|
|
520
15
|
|
|
521
16
|
|
|
522
17
|
|
|
@@ -530,4 +25,4 @@ function createLiveClient(options) {
|
|
|
530
25
|
|
|
531
26
|
|
|
532
27
|
|
|
533
|
-
exports.BUILDER_TAGS =
|
|
28
|
+
exports.BUILDER_TAGS = _chunkZLNUG6ZZcjs.BUILDER_TAGS; exports.BuilderNotFoundError = _chunkZLNUG6ZZcjs.BuilderNotFoundError; exports.BuilderReleaseNotFoundError = _chunkZLNUG6ZZcjs.BuilderReleaseNotFoundError; exports.BuilderUnavailableError = _chunkZLNUG6ZZcjs.BuilderUnavailableError; exports.DEFAULT_BASE_URL = _chunkZLNUG6ZZcjs.DEFAULT_BASE_URL; exports.DEFAULT_TIMEOUT_MS = _chunkZLNUG6ZZcjs.DEFAULT_TIMEOUT_MS; exports.PreviewExpiredError = _chunkZLNUG6ZZcjs.PreviewExpiredError; exports.SUPPORTED_BUNDLE_VERSION = _chunkZLNUG6ZZcjs.SUPPORTED_BUNDLE_VERSION; exports.UNTIL_CHANGED_REVALIDATE_S = _chunkZLNUG6ZZcjs.UNTIL_CHANGED_REVALIDATE_S; exports.createBuilderClient = _chunkZLNUG6ZZcjs.createBuilderClient; exports.scopesToTags = _chunkZLNUG6ZZcjs.scopesToTags; exports.verifyBuilderNotice = _chunkZLNUG6ZZcjs.verifyBuilderNotice;
|