@usethrottle/builder 0.1.0 → 0.3.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 +19 -4
- package/dist/chunk-XYWLWXKG.js +619 -0
- package/dist/chunk-ZLNUG6ZZ.cjs +619 -0
- package/dist/index.cjs +7 -269
- package/dist/index.d.cts +123 -1
- package/dist/index.d.ts +123 -1
- package/dist/index.js +12 -274
- package/dist/next.cjs +71 -6
- package/dist/next.d.cts +30 -1
- package/dist/next.d.ts +30 -1
- package/dist/next.js +66 -1
- package/dist/watch.d.cts +28 -0
- package/dist/watch.d.ts +28 -0
- package/package.json +3 -3
- package/dist/chunk-EERLS5TR.js +0 -98
- package/dist/chunk-UFIR4HJA.cjs +0 -98
package/dist/index.cjs
CHANGED
|
@@ -1,283 +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
8
|
|
|
36
|
-
// src/client.ts
|
|
37
|
-
var _builderschema = require('@usethrottle/builder-schema');
|
|
38
9
|
|
|
39
|
-
// src/cache.ts
|
|
40
|
-
var UNTIL_CHANGED_REVALIDATE_S = 3600;
|
|
41
|
-
function cacheInit(tags, preview) {
|
|
42
|
-
if (preview) return { cache: "no-store" };
|
|
43
|
-
return { next: { revalidate: UNTIL_CHANGED_REVALIDATE_S, tags: [...tags] } };
|
|
44
|
-
}
|
|
45
10
|
|
|
46
|
-
// src/last-good.ts
|
|
47
|
-
var LAST_GOOD_MS = 10 * 60 * 1e3;
|
|
48
|
-
var MAX_ENTRIES = 200;
|
|
49
|
-
var store = /* @__PURE__ */ new Map();
|
|
50
|
-
function rememberGood(key, value) {
|
|
51
|
-
store.delete(key);
|
|
52
|
-
store.set(key, { value, at: Date.now(), warned: false });
|
|
53
|
-
if (store.size > MAX_ENTRIES) {
|
|
54
|
-
const oldest = store.keys().next();
|
|
55
|
-
if (!oldest.done) store.delete(oldest.value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function recallGood(key, warn) {
|
|
59
|
-
const entry = store.get(key);
|
|
60
|
-
if (entry === void 0) return void 0;
|
|
61
|
-
if (Date.now() - entry.at > LAST_GOOD_MS) {
|
|
62
|
-
store.delete(key);
|
|
63
|
-
return void 0;
|
|
64
|
-
}
|
|
65
|
-
if (!entry.warned) {
|
|
66
|
-
entry.warned = true;
|
|
67
|
-
warn();
|
|
68
|
-
}
|
|
69
|
-
return { value: entry.value };
|
|
70
|
-
}
|
|
71
11
|
|
|
72
|
-
// src/client.ts
|
|
73
|
-
var DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
|
|
74
|
-
var DEFAULT_TIMEOUT_MS = 3e3;
|
|
75
|
-
var MAX_NAME_CHARS = 128;
|
|
76
|
-
function isRecord(value) {
|
|
77
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
78
|
-
}
|
|
79
|
-
function queryString(query) {
|
|
80
|
-
const pairs = Object.entries(query).filter((entry) => entry[1] !== void 0 && entry[1] !== "").sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
81
|
-
return pairs.length === 0 ? "" : `?${pairs.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&")}`;
|
|
82
|
-
}
|
|
83
|
-
function checkName(kind, value) {
|
|
84
|
-
if (typeof value !== "string" || value === "" || value.length > MAX_NAME_CHARS)
|
|
85
|
-
throw new TypeError(`A Builder ${kind} is a non-empty string of at most ${String(MAX_NAME_CHARS)} characters`);
|
|
86
|
-
return value;
|
|
87
|
-
}
|
|
88
|
-
function currentDocument(answer, where) {
|
|
89
|
-
const doc = answer.document;
|
|
90
|
-
if (!isRecord(doc))
|
|
91
|
-
throw new BuilderUnavailableError("invalid-response", `Builder ${where} answer has no document`);
|
|
92
|
-
if (doc.schemaVersion === _builderschema.CURRENT_SCHEMA_VERSION) return doc;
|
|
93
|
-
const upgraded = _builderschema.upgradeDocument.call(void 0, doc);
|
|
94
|
-
if (!upgraded.ok)
|
|
95
|
-
throw new BuilderUnavailableError(
|
|
96
|
-
"unsupported-document",
|
|
97
|
-
`Builder ${where} document cannot be read by this SDK version (${_nullishCoalesce(_optionalChain([upgraded, 'access', _ => _.issues, 'access', _2 => _2[0], 'optionalAccess', _3 => _3.code]), () => ( "invalid"))})`
|
|
98
|
-
);
|
|
99
|
-
return upgraded.doc;
|
|
100
|
-
}
|
|
101
|
-
function withDocument(answer, where) {
|
|
102
|
-
return { ...answer, document: currentDocument(answer, where) };
|
|
103
|
-
}
|
|
104
|
-
var invalid = (message) => new BuilderUnavailableError("invalid-response", message);
|
|
105
|
-
function generationOf(answer, where) {
|
|
106
|
-
const { generation } = answer;
|
|
107
|
-
if (typeof generation !== "number" || !Number.isSafeInteger(generation) || generation < 0)
|
|
108
|
-
throw invalid(`Builder ${where} answer has no generation`);
|
|
109
|
-
return generation;
|
|
110
|
-
}
|
|
111
|
-
function readSite(answer) {
|
|
112
|
-
generationOf(answer, "site");
|
|
113
|
-
if (typeof answer.siteVersion !== "number" || !Number.isSafeInteger(answer.siteVersion))
|
|
114
|
-
throw invalid("Builder site answer has no siteVersion");
|
|
115
|
-
if (!isRecord(answer.settings)) throw invalid("Builder site answer has no settings");
|
|
116
|
-
return answer;
|
|
117
|
-
}
|
|
118
|
-
var SUMMARY_KEYS = ["id", "slug", "pageType", "title", "updatedAt", "path"];
|
|
119
|
-
function readPages(answer) {
|
|
120
|
-
const { pages } = answer;
|
|
121
|
-
if (!Array.isArray(pages)) throw invalid("Builder pages answer is not a list");
|
|
122
|
-
for (const page of pages)
|
|
123
|
-
if (!isRecord(page) || SUMMARY_KEYS.some((key) => typeof page[key] !== "string"))
|
|
124
|
-
throw invalid("Builder pages answer has a page without its fields");
|
|
125
|
-
return pages;
|
|
126
|
-
}
|
|
127
|
-
function readDocument(answer, where) {
|
|
128
|
-
generationOf(answer, where);
|
|
129
|
-
return withDocument(answer, where);
|
|
130
|
-
}
|
|
131
|
-
function readPage(answer) {
|
|
132
|
-
if (typeof answer.redirectTo === "string") return { redirectTo: answer.redirectTo };
|
|
133
|
-
generationOf(answer, "page");
|
|
134
|
-
if (typeof answer.slug !== "string") throw invalid("Builder page answer has no slug");
|
|
135
|
-
return withDocument(answer, "page");
|
|
136
|
-
}
|
|
137
|
-
function readBundle(answer) {
|
|
138
|
-
const generation = generationOf(answer, "bundle");
|
|
139
|
-
if (!isRecord(answer.site) || !isRecord(answer.header) || !isRecord(answer.footer))
|
|
140
|
-
throw invalid("Builder bundle answer has no site, header or footer");
|
|
141
|
-
const slots = answer.slots === void 0 ? {} : answer.slots;
|
|
142
|
-
if (!isRecord(slots)) throw invalid("Builder bundle answer has slots that are not an object");
|
|
143
|
-
return {
|
|
144
|
-
generation,
|
|
145
|
-
site: readSite(answer.site),
|
|
146
|
-
header: readDocument(answer.header, "header"),
|
|
147
|
-
footer: readDocument(answer.footer, "footer"),
|
|
148
|
-
...isRecord(answer.page) ? { page: readPage(answer.page) } : {},
|
|
149
|
-
slots: Object.fromEntries(
|
|
150
|
-
Object.entries(slots).map(([name, value]) => {
|
|
151
|
-
if (!isRecord(value)) throw invalid(`Builder slot ${name} answer is not an object`);
|
|
152
|
-
return [name, readDocument(value, `slot ${name}`)];
|
|
153
|
-
})
|
|
154
|
-
)
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
function createBuilderClient(options) {
|
|
158
|
-
const { builderKey, previewToken } = options;
|
|
159
|
-
if (typeof builderKey !== "string" || builderKey === "")
|
|
160
|
-
throw new TypeError("createBuilderClient needs a builderKey");
|
|
161
|
-
if (builderKey.startsWith("tb_sk_"))
|
|
162
|
-
throw new TypeError("createBuilderClient takes the publishable Builder key (tb_pk_\u2026), never a secret key");
|
|
163
|
-
const previewHeader = previewToken !== void 0 && previewToken !== "" ? previewToken : void 0;
|
|
164
|
-
const preview = previewHeader !== void 0;
|
|
165
|
-
const base = (_nullishCoalesce(options.baseUrl, () => ( DEFAULT_BASE_URL))).replace(/\/+$/u, "");
|
|
166
|
-
const timeoutMs = _nullishCoalesce(options.timeoutMs, () => ( DEFAULT_TIMEOUT_MS));
|
|
167
|
-
const doFetch = options.fetch;
|
|
168
|
-
async function once(url, init) {
|
|
169
|
-
const controller = new AbortController();
|
|
170
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
171
|
-
try {
|
|
172
|
-
const response = await (_nullishCoalesce(doFetch, () => ( globalThis.fetch)))(url, {
|
|
173
|
-
...init,
|
|
174
|
-
signal: controller.signal
|
|
175
|
-
});
|
|
176
|
-
let body;
|
|
177
|
-
try {
|
|
178
|
-
body = response.status === 204 ? void 0 : await response.json();
|
|
179
|
-
} catch (cause) {
|
|
180
|
-
if (controller.signal.aborted) throw cause;
|
|
181
|
-
body = void 0;
|
|
182
|
-
}
|
|
183
|
-
return { body, status: response.status };
|
|
184
|
-
} catch (cause) {
|
|
185
|
-
throw new BuilderUnavailableError(
|
|
186
|
-
controller.signal.aborted ? "timeout" : "network",
|
|
187
|
-
controller.signal.aborted ? `Builder did not answer within ${String(timeoutMs)} ms` : "Builder could not be reached",
|
|
188
|
-
{ cause }
|
|
189
|
-
);
|
|
190
|
-
} finally {
|
|
191
|
-
clearTimeout(timer);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
async function fetchAnswer(path, read2) {
|
|
195
|
-
const headers = { accept: "application/json", "X-Builder-Key": builderKey };
|
|
196
|
-
if (previewHeader !== void 0) headers["X-Builder-Preview"] = previewHeader;
|
|
197
|
-
const url = `${base}${path}${queryString(_nullishCoalesce(read2.query, () => ( {})))}`;
|
|
198
|
-
const init = { method: "GET", headers, ...cacheInit(_chunkUFIR4HJAcjs.fitTags.call(void 0, read2.tags), preview) };
|
|
199
|
-
const { body, status } = await once(url, init);
|
|
200
|
-
if (status === 401 && preview) throw new PreviewExpiredError();
|
|
201
|
-
if (status === 404 && read2.pageSlug !== void 0) throw new BuilderNotFoundError(read2.pageSlug);
|
|
202
|
-
if (status < 200 || status >= 300)
|
|
203
|
-
throw new BuilderUnavailableError("status", `Builder answered ${String(status)}`, { status });
|
|
204
|
-
if (!isRecord(body) && !Array.isArray(body))
|
|
205
|
-
throw new BuilderUnavailableError("invalid-response", "Builder answered a body that is not JSON");
|
|
206
|
-
return Array.isArray(body) ? { pages: body } : body;
|
|
207
|
-
}
|
|
208
|
-
async function read(path, options2, shape) {
|
|
209
|
-
const goodKey = `${base}|${builderKey}|${path}${queryString(_nullishCoalesce(options2.query, () => ( {})))}`;
|
|
210
|
-
try {
|
|
211
|
-
const value = shape(await fetchAnswer(path, options2));
|
|
212
|
-
if (!preview) rememberGood(goodKey, value);
|
|
213
|
-
return value;
|
|
214
|
-
} catch (error) {
|
|
215
|
-
if (preview || !(error instanceof BuilderUnavailableError)) throw error;
|
|
216
|
-
const good = recallGood(goodKey, () => {
|
|
217
|
-
console.warn(
|
|
218
|
-
`[throttle-builder] ${path} failed (${error.reason}); serving the last good answer`
|
|
219
|
-
);
|
|
220
|
-
});
|
|
221
|
-
if (good === void 0) throw error;
|
|
222
|
-
return good.value;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
site: ({ locale } = {}) => read("/v1/site", { query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.site] }, readSite),
|
|
227
|
-
pages: {
|
|
228
|
-
list: () => read(
|
|
229
|
-
"/v1/pages",
|
|
230
|
-
{ tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.pages] },
|
|
231
|
-
readPages
|
|
232
|
-
),
|
|
233
|
-
get: async (slug, { locale } = {}) => read(
|
|
234
|
-
`/v1/pages/${encodeURIComponent(checkName("page slug", slug))}`,
|
|
235
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.page(slug)], pageSlug: slug },
|
|
236
|
-
readPage
|
|
237
|
-
)
|
|
238
|
-
},
|
|
239
|
-
parts: {
|
|
240
|
-
get: async (name, { locale } = {}) => {
|
|
241
|
-
if (name !== "header" && name !== "footer")
|
|
242
|
-
throw new TypeError('A Builder part is "header" or "footer"');
|
|
243
|
-
return read(
|
|
244
|
-
`/v1/parts/${name}`,
|
|
245
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.part(name)] },
|
|
246
|
-
(answer) => readDocument(answer, `part ${name}`)
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
slots: {
|
|
251
|
-
get: async (name, { locale } = {}) => read(
|
|
252
|
-
`/v1/slots/${encodeURIComponent(checkName("slot name", name))}`,
|
|
253
|
-
{ query: { locale }, tags: [_chunkUFIR4HJAcjs.BUILDER_TAGS.all, _chunkUFIR4HJAcjs.BUILDER_TAGS.slot(name)] },
|
|
254
|
-
(answer) => readDocument(answer, `slot ${name}`)
|
|
255
|
-
)
|
|
256
|
-
},
|
|
257
|
-
bundle: async ({ page: pageSlug, slots, locale } = {}) => {
|
|
258
|
-
const names = [...new Set(_nullishCoalesce(slots, () => ( [])))].map((name) => checkName("slot name", name)).sort();
|
|
259
|
-
if (pageSlug !== void 0) checkName("page slug", pageSlug);
|
|
260
|
-
return read(
|
|
261
|
-
"/v1/bundle",
|
|
262
|
-
{
|
|
263
|
-
query: { page: pageSlug, slots: names.join(","), locale },
|
|
264
|
-
pageSlug,
|
|
265
|
-
tags: [
|
|
266
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.all,
|
|
267
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.site,
|
|
268
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.part("header"),
|
|
269
|
-
_chunkUFIR4HJAcjs.BUILDER_TAGS.part("footer"),
|
|
270
|
-
...pageSlug === void 0 ? [] : [_chunkUFIR4HJAcjs.BUILDER_TAGS.page(pageSlug)],
|
|
271
|
-
...names.map((name) => _chunkUFIR4HJAcjs.BUILDER_TAGS.slot(name))
|
|
272
|
-
]
|
|
273
|
-
},
|
|
274
|
-
readBundle
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
12
|
|
|
280
13
|
|
|
14
|
+
var _chunkZLNUG6ZZcjs = require('./chunk-ZLNUG6ZZ.cjs');
|
|
281
15
|
|
|
282
16
|
|
|
283
17
|
|
|
@@ -287,4 +21,8 @@ function createBuilderClient(options) {
|
|
|
287
21
|
|
|
288
22
|
|
|
289
23
|
|
|
290
|
-
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
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;
|
package/dist/index.d.cts
CHANGED
|
@@ -30,6 +30,16 @@ declare class BuilderNotFoundError extends Error {
|
|
|
30
30
|
declare class PreviewExpiredError extends Error {
|
|
31
31
|
constructor();
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The release a build asked for does not exist (`404 RELEASE_NOT_FOUND`): a wrong
|
|
35
|
+
* `BUILDER_RELEASE_ID`, a release pruned from the history, or, for `latest`, a
|
|
36
|
+
* site that has never been released. A build with no release must fail, not
|
|
37
|
+
* publish an empty store.
|
|
38
|
+
*/
|
|
39
|
+
declare class BuilderReleaseNotFoundError extends Error {
|
|
40
|
+
readonly release: string;
|
|
41
|
+
constructor(release: string);
|
|
42
|
+
}
|
|
33
43
|
|
|
34
44
|
/** Published site settings resolved for one locale (localized values are strings). */
|
|
35
45
|
type BuilderSite = {
|
|
@@ -44,6 +54,8 @@ type BuilderPageSummary = {
|
|
|
44
54
|
title: string;
|
|
45
55
|
updatedAt: string;
|
|
46
56
|
path: string;
|
|
57
|
+
/** The page asks search engines to skip it (`seo.noindex`). Absent from a Worker that predates it. */
|
|
58
|
+
noindex?: boolean;
|
|
47
59
|
};
|
|
48
60
|
/** A published page: its metadata and its document (with frozen custom widgets and assets). */
|
|
49
61
|
type LoadedPage = BuilderPageSummary & {
|
|
@@ -89,6 +101,8 @@ type LocaleOptions = {
|
|
|
89
101
|
locale?: string;
|
|
90
102
|
};
|
|
91
103
|
type BuilderClient = {
|
|
104
|
+
/** `build` for a client made with `mode: 'build'`: it reads a frozen release and never the live API. */
|
|
105
|
+
readonly mode?: 'live' | 'build';
|
|
92
106
|
site(options?: LocaleOptions): Promise<BuilderSite>;
|
|
93
107
|
pages: {
|
|
94
108
|
list(): Promise<BuilderPageSummary[]>;
|
|
@@ -105,11 +119,119 @@ type BuilderClient = {
|
|
|
105
119
|
slots?: readonly string[];
|
|
106
120
|
}): Promise<BuilderBundle>;
|
|
107
121
|
};
|
|
122
|
+
/** A published part or slot as a release holds it. */
|
|
123
|
+
type ReleaseDocument = {
|
|
124
|
+
version: number;
|
|
125
|
+
document: PageDocument;
|
|
126
|
+
};
|
|
127
|
+
/** One page of a release. Titles and `seo` text keep their locale maps; the document does too. */
|
|
128
|
+
type ReleasePage = {
|
|
129
|
+
id: string;
|
|
130
|
+
slug: string;
|
|
131
|
+
pageType: string;
|
|
132
|
+
path: string;
|
|
133
|
+
version: number;
|
|
134
|
+
publishedAt: string;
|
|
135
|
+
title: Record<string, string>;
|
|
136
|
+
seo: {
|
|
137
|
+
title?: Record<string, string>;
|
|
138
|
+
description?: Record<string, string>;
|
|
139
|
+
shareImage: unknown;
|
|
140
|
+
noindex: boolean;
|
|
141
|
+
};
|
|
142
|
+
document: PageDocument;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* The whole site as one release froze it: `GET /v1/releases/:id/bundle`. The
|
|
146
|
+
* same release id always gives the same bundle. `site` holds the settings
|
|
147
|
+
* resolved for each locale of the site; the announcement is NOT windowed in it
|
|
148
|
+
* (`announcement.enabled` is the merchant's switch, `startsAt` and `endsAt`
|
|
149
|
+
* beside it), because the moment of a visit is not known when a release is
|
|
150
|
+
* built. The build client decides it at every read.
|
|
151
|
+
*/
|
|
152
|
+
type ReleaseBundle = {
|
|
153
|
+
bundleVersion: number;
|
|
154
|
+
releaseId: string;
|
|
155
|
+
kind: 'release' | 'rollback';
|
|
156
|
+
sourceReleaseId: string | null;
|
|
157
|
+
createdAt: string;
|
|
158
|
+
generation: number;
|
|
159
|
+
siteVersion: number;
|
|
160
|
+
defaultLocale: string;
|
|
161
|
+
locales: string[];
|
|
162
|
+
site: Record<string, {
|
|
163
|
+
brand: {
|
|
164
|
+
storeName: string;
|
|
165
|
+
};
|
|
166
|
+
settings: Record<string, unknown>;
|
|
167
|
+
}>;
|
|
168
|
+
pages: ReleasePage[];
|
|
169
|
+
header: ReleaseDocument;
|
|
170
|
+
footer: ReleaseDocument;
|
|
171
|
+
slots: Record<string, ReleaseDocument>;
|
|
172
|
+
/** An old slug and the store path of the page that lives there now. */
|
|
173
|
+
redirects: Record<string, string>;
|
|
174
|
+
/** The status each frozen custom widget had when the release was made. */
|
|
175
|
+
customWidgets: Record<string, 'active' | 'disabled'>;
|
|
176
|
+
};
|
|
177
|
+
type BuildClientCommon = {
|
|
178
|
+
mode: 'build';
|
|
179
|
+
/** Defaults to `https://builder.usethrottle.dev`. */
|
|
180
|
+
baseUrl?: string;
|
|
181
|
+
fetch?: typeof fetch;
|
|
182
|
+
/** The bundle read, body included. Default 60000 (a bundle is the whole site). */
|
|
183
|
+
timeoutMs?: number;
|
|
184
|
+
/**
|
|
185
|
+
* The release this client reads, instead of `BUILDER_RELEASE_ID`. A store sets it from a value Next inlines at
|
|
186
|
+
* build, so a page Next renders again later reads the release it was built from, not a newer one.
|
|
187
|
+
*/
|
|
188
|
+
release?: string;
|
|
189
|
+
/** The clock that decides the announcement window at every read. Default `Date.now`. */
|
|
190
|
+
now?: () => number;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* A build client needs a way to get its release: a key to read it with, or a bundle you already hold (a fixture, a
|
|
194
|
+
* file read at build). With neither the call does not compile.
|
|
195
|
+
*/
|
|
196
|
+
type BuildClientOptions = BuildClientCommon & ({
|
|
197
|
+
/**
|
|
198
|
+
* A publishable key of a server channel (`tb_pk_...`, the build key), or a secret key holding
|
|
199
|
+
* `releases:read` (`tb_sk_...`). Keep it on the server and in your build; never in a `NEXT_PUBLIC_` variable.
|
|
200
|
+
*/
|
|
201
|
+
builderKey: string;
|
|
202
|
+
/** A bundle you already hold: the client then makes no request at all. */
|
|
203
|
+
releaseBundle?: ReleaseBundle;
|
|
204
|
+
} | {
|
|
205
|
+
/** A bundle you already hold: the client then makes no request at all and needs no key. */
|
|
206
|
+
releaseBundle: ReleaseBundle;
|
|
207
|
+
builderKey?: string;
|
|
208
|
+
});
|
|
209
|
+
type BuildBuilderClient = BuilderClient & {
|
|
210
|
+
readonly mode: 'build';
|
|
211
|
+
/**
|
|
212
|
+
* The release's bundle, read once per process and kept in memory. The release
|
|
213
|
+
* is `release` when given, else the `BUILDER_RELEASE_ID` environment variable,
|
|
214
|
+
* else the newest (`latest`). Every other read of this client (`site`, `pages`,
|
|
215
|
+
* `parts`, `slots`, `bundle`) is answered from this bundle with no request.
|
|
216
|
+
*/
|
|
217
|
+
getReleaseBundle(options?: {
|
|
218
|
+
release?: string;
|
|
219
|
+
}): Promise<ReleaseBundle>;
|
|
220
|
+
};
|
|
108
221
|
|
|
109
222
|
declare const DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
|
|
110
223
|
declare const DEFAULT_TIMEOUT_MS = 3000;
|
|
224
|
+
/**
|
|
225
|
+
* `mode: 'build'` makes the client of a build-time site: it reads one frozen
|
|
226
|
+
* release (`getReleaseBundle`) and answers every other read from it. Without a
|
|
227
|
+
* mode, the client reads the live API.
|
|
228
|
+
*/
|
|
229
|
+
declare function createBuilderClient(options: BuildClientOptions): BuildBuilderClient;
|
|
111
230
|
declare function createBuilderClient(options: BuilderClientOptions): BuilderClient;
|
|
112
231
|
|
|
232
|
+
/** The bundle format this SDK reads (`BUNDLE_VERSION` of the Builder). */
|
|
233
|
+
declare const SUPPORTED_BUNDLE_VERSION = 1;
|
|
234
|
+
|
|
113
235
|
declare const BUILDER_TAGS: {
|
|
114
236
|
readonly all: "throttle-builder";
|
|
115
237
|
readonly site: "throttle-builder:site";
|
|
@@ -130,4 +252,4 @@ declare function scopesToTags(scopes: readonly string[]): string[];
|
|
|
130
252
|
/** Next's `revalidate` for a published read: only a safety net behind the change notices. */
|
|
131
253
|
declare const UNTIL_CHANGED_REVALIDATE_S = 3600;
|
|
132
254
|
|
|
133
|
-
export { BUILDER_TAGS, type BuilderBundle, type BuilderClient, type BuilderClientOptions, BuilderNotFoundError, type BuilderPageSummary, type BuilderSite, BuilderUnavailableError, type BuilderUnavailableReason, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, type LoadedDocument, type LoadedPage, type LocaleOptions, type PageRedirect, type PartName, PreviewExpiredError, UNTIL_CHANGED_REVALIDATE_S, createBuilderClient, scopesToTags };
|
|
255
|
+
export { BUILDER_TAGS, type BuildBuilderClient, type BuildClientOptions, type BuilderBundle, type BuilderClient, type BuilderClientOptions, BuilderNotFoundError, type BuilderPageSummary, BuilderReleaseNotFoundError, type BuilderSite, BuilderUnavailableError, type BuilderUnavailableReason, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, type LoadedDocument, type LoadedPage, type LocaleOptions, type PageRedirect, type PartName, PreviewExpiredError, type ReleaseBundle, type ReleaseDocument, type ReleasePage, SUPPORTED_BUNDLE_VERSION, UNTIL_CHANGED_REVALIDATE_S, createBuilderClient, scopesToTags };
|
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,16 @@ declare class BuilderNotFoundError extends Error {
|
|
|
30
30
|
declare class PreviewExpiredError extends Error {
|
|
31
31
|
constructor();
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The release a build asked for does not exist (`404 RELEASE_NOT_FOUND`): a wrong
|
|
35
|
+
* `BUILDER_RELEASE_ID`, a release pruned from the history, or, for `latest`, a
|
|
36
|
+
* site that has never been released. A build with no release must fail, not
|
|
37
|
+
* publish an empty store.
|
|
38
|
+
*/
|
|
39
|
+
declare class BuilderReleaseNotFoundError extends Error {
|
|
40
|
+
readonly release: string;
|
|
41
|
+
constructor(release: string);
|
|
42
|
+
}
|
|
33
43
|
|
|
34
44
|
/** Published site settings resolved for one locale (localized values are strings). */
|
|
35
45
|
type BuilderSite = {
|
|
@@ -44,6 +54,8 @@ type BuilderPageSummary = {
|
|
|
44
54
|
title: string;
|
|
45
55
|
updatedAt: string;
|
|
46
56
|
path: string;
|
|
57
|
+
/** The page asks search engines to skip it (`seo.noindex`). Absent from a Worker that predates it. */
|
|
58
|
+
noindex?: boolean;
|
|
47
59
|
};
|
|
48
60
|
/** A published page: its metadata and its document (with frozen custom widgets and assets). */
|
|
49
61
|
type LoadedPage = BuilderPageSummary & {
|
|
@@ -89,6 +101,8 @@ type LocaleOptions = {
|
|
|
89
101
|
locale?: string;
|
|
90
102
|
};
|
|
91
103
|
type BuilderClient = {
|
|
104
|
+
/** `build` for a client made with `mode: 'build'`: it reads a frozen release and never the live API. */
|
|
105
|
+
readonly mode?: 'live' | 'build';
|
|
92
106
|
site(options?: LocaleOptions): Promise<BuilderSite>;
|
|
93
107
|
pages: {
|
|
94
108
|
list(): Promise<BuilderPageSummary[]>;
|
|
@@ -105,11 +119,119 @@ type BuilderClient = {
|
|
|
105
119
|
slots?: readonly string[];
|
|
106
120
|
}): Promise<BuilderBundle>;
|
|
107
121
|
};
|
|
122
|
+
/** A published part or slot as a release holds it. */
|
|
123
|
+
type ReleaseDocument = {
|
|
124
|
+
version: number;
|
|
125
|
+
document: PageDocument;
|
|
126
|
+
};
|
|
127
|
+
/** One page of a release. Titles and `seo` text keep their locale maps; the document does too. */
|
|
128
|
+
type ReleasePage = {
|
|
129
|
+
id: string;
|
|
130
|
+
slug: string;
|
|
131
|
+
pageType: string;
|
|
132
|
+
path: string;
|
|
133
|
+
version: number;
|
|
134
|
+
publishedAt: string;
|
|
135
|
+
title: Record<string, string>;
|
|
136
|
+
seo: {
|
|
137
|
+
title?: Record<string, string>;
|
|
138
|
+
description?: Record<string, string>;
|
|
139
|
+
shareImage: unknown;
|
|
140
|
+
noindex: boolean;
|
|
141
|
+
};
|
|
142
|
+
document: PageDocument;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* The whole site as one release froze it: `GET /v1/releases/:id/bundle`. The
|
|
146
|
+
* same release id always gives the same bundle. `site` holds the settings
|
|
147
|
+
* resolved for each locale of the site; the announcement is NOT windowed in it
|
|
148
|
+
* (`announcement.enabled` is the merchant's switch, `startsAt` and `endsAt`
|
|
149
|
+
* beside it), because the moment of a visit is not known when a release is
|
|
150
|
+
* built. The build client decides it at every read.
|
|
151
|
+
*/
|
|
152
|
+
type ReleaseBundle = {
|
|
153
|
+
bundleVersion: number;
|
|
154
|
+
releaseId: string;
|
|
155
|
+
kind: 'release' | 'rollback';
|
|
156
|
+
sourceReleaseId: string | null;
|
|
157
|
+
createdAt: string;
|
|
158
|
+
generation: number;
|
|
159
|
+
siteVersion: number;
|
|
160
|
+
defaultLocale: string;
|
|
161
|
+
locales: string[];
|
|
162
|
+
site: Record<string, {
|
|
163
|
+
brand: {
|
|
164
|
+
storeName: string;
|
|
165
|
+
};
|
|
166
|
+
settings: Record<string, unknown>;
|
|
167
|
+
}>;
|
|
168
|
+
pages: ReleasePage[];
|
|
169
|
+
header: ReleaseDocument;
|
|
170
|
+
footer: ReleaseDocument;
|
|
171
|
+
slots: Record<string, ReleaseDocument>;
|
|
172
|
+
/** An old slug and the store path of the page that lives there now. */
|
|
173
|
+
redirects: Record<string, string>;
|
|
174
|
+
/** The status each frozen custom widget had when the release was made. */
|
|
175
|
+
customWidgets: Record<string, 'active' | 'disabled'>;
|
|
176
|
+
};
|
|
177
|
+
type BuildClientCommon = {
|
|
178
|
+
mode: 'build';
|
|
179
|
+
/** Defaults to `https://builder.usethrottle.dev`. */
|
|
180
|
+
baseUrl?: string;
|
|
181
|
+
fetch?: typeof fetch;
|
|
182
|
+
/** The bundle read, body included. Default 60000 (a bundle is the whole site). */
|
|
183
|
+
timeoutMs?: number;
|
|
184
|
+
/**
|
|
185
|
+
* The release this client reads, instead of `BUILDER_RELEASE_ID`. A store sets it from a value Next inlines at
|
|
186
|
+
* build, so a page Next renders again later reads the release it was built from, not a newer one.
|
|
187
|
+
*/
|
|
188
|
+
release?: string;
|
|
189
|
+
/** The clock that decides the announcement window at every read. Default `Date.now`. */
|
|
190
|
+
now?: () => number;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* A build client needs a way to get its release: a key to read it with, or a bundle you already hold (a fixture, a
|
|
194
|
+
* file read at build). With neither the call does not compile.
|
|
195
|
+
*/
|
|
196
|
+
type BuildClientOptions = BuildClientCommon & ({
|
|
197
|
+
/**
|
|
198
|
+
* A publishable key of a server channel (`tb_pk_...`, the build key), or a secret key holding
|
|
199
|
+
* `releases:read` (`tb_sk_...`). Keep it on the server and in your build; never in a `NEXT_PUBLIC_` variable.
|
|
200
|
+
*/
|
|
201
|
+
builderKey: string;
|
|
202
|
+
/** A bundle you already hold: the client then makes no request at all. */
|
|
203
|
+
releaseBundle?: ReleaseBundle;
|
|
204
|
+
} | {
|
|
205
|
+
/** A bundle you already hold: the client then makes no request at all and needs no key. */
|
|
206
|
+
releaseBundle: ReleaseBundle;
|
|
207
|
+
builderKey?: string;
|
|
208
|
+
});
|
|
209
|
+
type BuildBuilderClient = BuilderClient & {
|
|
210
|
+
readonly mode: 'build';
|
|
211
|
+
/**
|
|
212
|
+
* The release's bundle, read once per process and kept in memory. The release
|
|
213
|
+
* is `release` when given, else the `BUILDER_RELEASE_ID` environment variable,
|
|
214
|
+
* else the newest (`latest`). Every other read of this client (`site`, `pages`,
|
|
215
|
+
* `parts`, `slots`, `bundle`) is answered from this bundle with no request.
|
|
216
|
+
*/
|
|
217
|
+
getReleaseBundle(options?: {
|
|
218
|
+
release?: string;
|
|
219
|
+
}): Promise<ReleaseBundle>;
|
|
220
|
+
};
|
|
108
221
|
|
|
109
222
|
declare const DEFAULT_BASE_URL = "https://builder.usethrottle.dev";
|
|
110
223
|
declare const DEFAULT_TIMEOUT_MS = 3000;
|
|
224
|
+
/**
|
|
225
|
+
* `mode: 'build'` makes the client of a build-time site: it reads one frozen
|
|
226
|
+
* release (`getReleaseBundle`) and answers every other read from it. Without a
|
|
227
|
+
* mode, the client reads the live API.
|
|
228
|
+
*/
|
|
229
|
+
declare function createBuilderClient(options: BuildClientOptions): BuildBuilderClient;
|
|
111
230
|
declare function createBuilderClient(options: BuilderClientOptions): BuilderClient;
|
|
112
231
|
|
|
232
|
+
/** The bundle format this SDK reads (`BUNDLE_VERSION` of the Builder). */
|
|
233
|
+
declare const SUPPORTED_BUNDLE_VERSION = 1;
|
|
234
|
+
|
|
113
235
|
declare const BUILDER_TAGS: {
|
|
114
236
|
readonly all: "throttle-builder";
|
|
115
237
|
readonly site: "throttle-builder:site";
|
|
@@ -130,4 +252,4 @@ declare function scopesToTags(scopes: readonly string[]): string[];
|
|
|
130
252
|
/** Next's `revalidate` for a published read: only a safety net behind the change notices. */
|
|
131
253
|
declare const UNTIL_CHANGED_REVALIDATE_S = 3600;
|
|
132
254
|
|
|
133
|
-
export { BUILDER_TAGS, type BuilderBundle, type BuilderClient, type BuilderClientOptions, BuilderNotFoundError, type BuilderPageSummary, type BuilderSite, BuilderUnavailableError, type BuilderUnavailableReason, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, type LoadedDocument, type LoadedPage, type LocaleOptions, type PageRedirect, type PartName, PreviewExpiredError, UNTIL_CHANGED_REVALIDATE_S, createBuilderClient, scopesToTags };
|
|
255
|
+
export { BUILDER_TAGS, type BuildBuilderClient, type BuildClientOptions, type BuilderBundle, type BuilderClient, type BuilderClientOptions, BuilderNotFoundError, type BuilderPageSummary, BuilderReleaseNotFoundError, type BuilderSite, BuilderUnavailableError, type BuilderUnavailableReason, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, type LoadedDocument, type LoadedPage, type LocaleOptions, type PageRedirect, type PartName, PreviewExpiredError, type ReleaseBundle, type ReleaseDocument, type ReleasePage, SUPPORTED_BUNDLE_VERSION, UNTIL_CHANGED_REVALIDATE_S, createBuilderClient, scopesToTags };
|