brease-client 0.8.2 → 0.9.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 +170 -22
- package/dist/astro.js +2 -3
- package/dist/chunk-ES4KMYDH.js +771 -0
- package/dist/{chunk-QZXUMDB7.js → chunk-VQGKMILO.js} +9 -2
- package/dist/cli.cjs +892 -39
- package/dist/cli.js +262 -20
- package/dist/html.js +1 -2
- package/dist/image.cjs +2 -2
- package/dist/image.d.cts +7 -3
- package/dist/image.d.ts +7 -3
- package/dist/image.js +2 -2
- package/dist/index.cjs +412 -46
- package/dist/index.d.cts +13 -3
- package/dist/index.d.ts +13 -3
- package/dist/index.js +2 -3
- package/dist/next.cjs +89 -42
- package/dist/next.js +4 -4
- package/package.json +1 -1
- package/dist/chunk-CQZLTZGN.js +0 -79
- package/dist/chunk-MWTYINWA.js +0 -383
- package/dist/chunk-N3QOL5EU.js +0 -10
package/dist/next.cjs
CHANGED
|
@@ -43,15 +43,35 @@ var BreaseError = class _BreaseError extends Error {
|
|
|
43
43
|
function normalizeSlug(input) {
|
|
44
44
|
return input.trim().replace(/^\/+|\/+$/g, "").replace(/\/{2,}/g, "/");
|
|
45
45
|
}
|
|
46
|
+
function customPattern(template) {
|
|
47
|
+
const parts = normalizeSlug(template).split(/(\{locale\}|\{slug\})/);
|
|
48
|
+
const slashBefore = (i) => parts[i] === "{slug}" && !!parts[i - 1]?.endsWith("/");
|
|
49
|
+
const slashAfter = (i) => parts[i] === "{slug}" && !slashBefore(i) && !!parts[i + 1]?.startsWith("/");
|
|
50
|
+
const pattern = parts.map((part, i) => {
|
|
51
|
+
if (part === "{locale}") return "(?<locale>[^/]+)";
|
|
52
|
+
if (part === "{slug}") {
|
|
53
|
+
if (slashBefore(i)) return "(?:/(?<slug>.*))?";
|
|
54
|
+
if (slashAfter(i)) return "(?:(?<slug>.*)/)?";
|
|
55
|
+
return "(?<slug>.*)";
|
|
56
|
+
}
|
|
57
|
+
let text = part;
|
|
58
|
+
if (slashBefore(i + 1)) text = text.slice(0, -1);
|
|
59
|
+
if (slashAfter(i - 1)) text = text.slice(1);
|
|
60
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
61
|
+
}).join("");
|
|
62
|
+
return new RegExp(`^${pattern}$`);
|
|
63
|
+
}
|
|
64
|
+
function safeDecode(value) {
|
|
65
|
+
try {
|
|
66
|
+
return decodeURIComponent(value);
|
|
67
|
+
} catch {
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
46
71
|
function resolvePath(settings, pathname) {
|
|
47
|
-
const clean = normalizeSlug(
|
|
72
|
+
const clean = normalizeSlug(safeDecode(pathname.split(/[?#]/)[0] ?? ""));
|
|
48
73
|
if (settings.localeStrategy === "CUSTOM" && settings.urlTemplate) {
|
|
49
|
-
const
|
|
50
|
-
if (part === "{locale}") return "(?<locale>[^/]+)";
|
|
51
|
-
if (part === "{slug}") return "(?<slug>.*)";
|
|
52
|
-
return part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
53
|
-
}).join("");
|
|
54
|
-
const match = new RegExp(`^${pattern}$`).exec(clean);
|
|
74
|
+
const match = customPattern(settings.urlTemplate).exec(clean);
|
|
55
75
|
if (match?.groups) {
|
|
56
76
|
return {
|
|
57
77
|
slug: normalizeSlug(match.groups.slug ?? ""),
|
|
@@ -118,15 +138,55 @@ async function verifyRevalidate(req, secret) {
|
|
|
118
138
|
// src/index.ts
|
|
119
139
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
120
140
|
var SITE_TTL_MS = 6e4;
|
|
141
|
+
var DRAFT_BRANCH_PREFIX = "brease/draft/";
|
|
121
142
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
143
|
+
var HOST_GIT_ENV = [
|
|
144
|
+
["VERCEL_GIT_COMMIT_SHA", "VERCEL_GIT_COMMIT_REF"],
|
|
145
|
+
["WORKERS_CI_COMMIT_SHA", "WORKERS_CI_BRANCH"],
|
|
146
|
+
["CF_PAGES_COMMIT_SHA", "CF_PAGES_BRANCH"]
|
|
147
|
+
];
|
|
148
|
+
function hostGit() {
|
|
149
|
+
for (const [commit, branch] of HOST_GIT_ENV) {
|
|
150
|
+
const sha = env(commit);
|
|
151
|
+
if (sha) return { commit: sha, branch: env(branch) };
|
|
152
|
+
}
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
var cacheGeneration = 0;
|
|
156
|
+
function clearBreaseCache() {
|
|
157
|
+
cacheGeneration++;
|
|
158
|
+
}
|
|
122
159
|
function createBrease(opts = {}) {
|
|
123
160
|
const apiUrl = (opts.apiUrl ?? env("BREASE_API_URL") ?? "https://api.brease.io").replace(/\/+$/, "");
|
|
124
|
-
const
|
|
161
|
+
const host = hostGit();
|
|
162
|
+
const commit = opts.commit === false ? void 0 : opts.commit ?? host.commit;
|
|
163
|
+
const branch = opts.branch === false ? void 0 : opts.branch ?? host.branch;
|
|
164
|
+
const draftBranch = branch?.startsWith(DRAFT_BRANCH_PREFIX) ? branch : void 0;
|
|
125
165
|
const retries = opts.retries ?? 2;
|
|
126
166
|
const baseMs = opts.retryBaseMs ?? 300;
|
|
167
|
+
const timeoutMs = opts.timeoutMs ?? 5e3;
|
|
127
168
|
const doFetch = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
128
|
-
|
|
129
|
-
|
|
169
|
+
function cached(path) {
|
|
170
|
+
let entry;
|
|
171
|
+
let last;
|
|
172
|
+
return () => {
|
|
173
|
+
if (!entry || Date.now() - entry.at > SITE_TTL_MS || entry.generation !== cacheGeneration || env("BREASE_PREVIEW")) {
|
|
174
|
+
const promise = request(path).then(
|
|
175
|
+
(value) => {
|
|
176
|
+
last = { value };
|
|
177
|
+
return value;
|
|
178
|
+
},
|
|
179
|
+
(err) => {
|
|
180
|
+
if (last) return last.value;
|
|
181
|
+
if (entry?.promise === promise) entry = void 0;
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
entry = { promise, at: Date.now(), generation: cacheGeneration };
|
|
186
|
+
}
|
|
187
|
+
return entry.promise;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
130
190
|
async function request(path, query = {}, nullOn404 = false) {
|
|
131
191
|
const token = opts.token ?? env("BREASE_TOKEN");
|
|
132
192
|
if (!token)
|
|
@@ -137,12 +197,14 @@ function createBrease(opts = {}) {
|
|
|
137
197
|
const url = `${apiUrl}${path}${qs ? `?${qs}` : ""}`;
|
|
138
198
|
const headers = { authorization: `Bearer ${token}`, accept: "application/json" };
|
|
139
199
|
if (commit) headers["x-brease-commit"] = commit;
|
|
200
|
+
if (draftBranch) headers["x-brease-branch"] = draftBranch;
|
|
140
201
|
for (let attempt = 0; ; attempt++) {
|
|
141
202
|
let res;
|
|
142
203
|
try {
|
|
143
|
-
res = await doFetch(url, { method: "GET", headers });
|
|
204
|
+
res = await doFetch(url, { method: "GET", headers, signal: AbortSignal.timeout(timeoutMs) });
|
|
144
205
|
} catch (err) {
|
|
145
|
-
|
|
206
|
+
const timedOut = err.name === "TimeoutError";
|
|
207
|
+
if (attempt < retries && !timedOut) {
|
|
146
208
|
await sleep(backoff(attempt));
|
|
147
209
|
continue;
|
|
148
210
|
}
|
|
@@ -161,24 +223,17 @@ function createBrease(opts = {}) {
|
|
|
161
223
|
function backoff(attempt) {
|
|
162
224
|
return baseMs * 2 ** attempt + Math.random() * baseMs;
|
|
163
225
|
}
|
|
226
|
+
const cachedSite = cached("/content-api/site");
|
|
227
|
+
const cachedRedirects = cached("/content-api/redirects");
|
|
164
228
|
const client = {
|
|
165
|
-
getSite
|
|
166
|
-
if (!site || Date.now() - site.at > SITE_TTL_MS) {
|
|
167
|
-
const promise = request("/content-api/site").catch((err) => {
|
|
168
|
-
if (site?.promise === promise) site = void 0;
|
|
169
|
-
throw err;
|
|
170
|
-
});
|
|
171
|
-
site = { promise, at: Date.now() };
|
|
172
|
-
}
|
|
173
|
-
return site.promise;
|
|
174
|
-
},
|
|
229
|
+
getSite: cachedSite,
|
|
175
230
|
getPage(slug, { locale }) {
|
|
176
231
|
return request("/content-api/page", { slug, locale }, true);
|
|
177
232
|
},
|
|
178
233
|
getPages(o = {}) {
|
|
179
234
|
return request("/content-api/pages", {
|
|
180
235
|
locale: o.locale,
|
|
181
|
-
parentId: o.parentId,
|
|
236
|
+
parentId: o.parentId === null ? "" : o.parentId,
|
|
182
237
|
template: o.template,
|
|
183
238
|
withContent: o.withContent ? "1" : void 0
|
|
184
239
|
});
|
|
@@ -187,26 +242,17 @@ function createBrease(opts = {}) {
|
|
|
187
242
|
return request(`/content-api/navigations/${encodeURIComponent(key)}`, { locale }, true);
|
|
188
243
|
},
|
|
189
244
|
// Cached for a minute, like the site: a proxy applies redirects per request (matchRedirect).
|
|
190
|
-
getRedirects
|
|
191
|
-
if (!redirects || Date.now() - redirects.at > SITE_TTL_MS) {
|
|
192
|
-
const promise = request("/content-api/redirects").catch((err) => {
|
|
193
|
-
if (redirects?.promise === promise) redirects = void 0;
|
|
194
|
-
throw err;
|
|
195
|
-
});
|
|
196
|
-
redirects = { promise, at: Date.now() };
|
|
197
|
-
}
|
|
198
|
-
return redirects.promise;
|
|
199
|
-
},
|
|
245
|
+
getRedirects: cachedRedirects,
|
|
200
246
|
getRelease() {
|
|
201
247
|
return request("/content-api/release");
|
|
202
248
|
},
|
|
203
249
|
async resolve(pathname) {
|
|
204
|
-
const
|
|
205
|
-
const result = resolvePath(
|
|
206
|
-
return
|
|
250
|
+
const site = await client.getSite();
|
|
251
|
+
const result = resolvePath(site, pathname);
|
|
252
|
+
return site.locales.includes(result.locale) ? result : null;
|
|
207
253
|
},
|
|
208
254
|
async route(path) {
|
|
209
|
-
const joined = Array.isArray(path) ? path.
|
|
255
|
+
const joined = Array.isArray(path) ? path.join("/") : path ?? "";
|
|
210
256
|
const result = await client.resolve(`/${joined}`);
|
|
211
257
|
return result && !result.slug.split("/").some((part) => part.startsWith("_")) ? result : null;
|
|
212
258
|
},
|
|
@@ -217,16 +263,16 @@ function createBrease(opts = {}) {
|
|
|
217
263
|
async localeContext(path) {
|
|
218
264
|
const target = await client.route(path);
|
|
219
265
|
if (!target) return null;
|
|
220
|
-
const [
|
|
266
|
+
const [site, page, home] = await Promise.all([
|
|
221
267
|
client.getSite(),
|
|
222
268
|
client.getPage(target.slug, { locale: target.locale }),
|
|
223
269
|
client.getPage("", { locale: target.locale })
|
|
224
270
|
]);
|
|
225
271
|
const settings = {
|
|
226
|
-
locales:
|
|
227
|
-
defaultLocale:
|
|
228
|
-
localeStrategy:
|
|
229
|
-
urlTemplate:
|
|
272
|
+
locales: site.locales,
|
|
273
|
+
defaultLocale: site.defaultLocale,
|
|
274
|
+
localeStrategy: site.localeStrategy,
|
|
275
|
+
urlTemplate: site.urlTemplate
|
|
230
276
|
};
|
|
231
277
|
if (!page && !home) return null;
|
|
232
278
|
const alternates = {};
|
|
@@ -303,6 +349,7 @@ function revalidateHandler(secret) {
|
|
|
303
349
|
if (!target.ok)
|
|
304
350
|
return target.reason === "body" ? json(400, { error: { code: "validation", message: "Body must be JSON" } }) : json(401, { error: { code: "unauthorized", message: "Invalid or expired signature" } });
|
|
305
351
|
const cache = await import('next/cache');
|
|
352
|
+
clearBreaseCache();
|
|
306
353
|
for (const path of target.paths) cache.revalidatePath(path);
|
|
307
354
|
for (const tag of target.tags) cache.revalidateTag(tag, { expire: 0 });
|
|
308
355
|
return json(200, { revalidated: true, paths: target.paths, tags: target.tags });
|
package/dist/next.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { env, verifyRevalidate, createBrease } from './chunk-
|
|
2
|
-
export { hmacHex, verifySignature } from './chunk-
|
|
3
|
-
import './chunk-
|
|
4
|
-
import './chunk-N3QOL5EU.js';
|
|
1
|
+
import { env, verifyRevalidate, clearBreaseCache, createBrease } from './chunk-ES4KMYDH.js';
|
|
2
|
+
export { hmacHex, verifySignature } from './chunk-ES4KMYDH.js';
|
|
3
|
+
import './chunk-VQGKMILO.js';
|
|
5
4
|
import './chunk-PKBMQBKP.js';
|
|
6
5
|
|
|
7
6
|
// src/next.ts
|
|
@@ -48,6 +47,7 @@ function revalidateHandler(secret) {
|
|
|
48
47
|
if (!target.ok)
|
|
49
48
|
return target.reason === "body" ? json(400, { error: { code: "validation", message: "Body must be JSON" } }) : json(401, { error: { code: "unauthorized", message: "Invalid or expired signature" } });
|
|
50
49
|
const cache = await import('next/cache');
|
|
50
|
+
clearBreaseCache();
|
|
51
51
|
for (const path of target.paths) cache.revalidatePath(path);
|
|
52
52
|
for (const tag of target.tags) cache.revalidateTag(tag, { expire: 0 });
|
|
53
53
|
return json(200, { revalidated: true, paths: target.paths, tags: target.tags });
|
package/package.json
CHANGED
package/dist/chunk-CQZLTZGN.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// src/sections.ts
|
|
2
|
-
var sectionsAttrs = { "data-brease-sections": "sections" };
|
|
3
|
-
function sectionAttrs(section) {
|
|
4
|
-
return { "data-brease-section": section.id, "data-brease-block": section.type };
|
|
5
|
-
}
|
|
6
|
-
var SECTION_FOLDERS = ["src/brease/sections", "brease/sections"];
|
|
7
|
-
var TYPE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
8
|
-
function checkSections(files, pages = {}) {
|
|
9
|
-
const warnings = [];
|
|
10
|
-
const folder = SECTION_FOLDERS.find((f) => Object.keys(files).some((p) => p.startsWith(`${f}/`)));
|
|
11
|
-
if (!folder) return warnings;
|
|
12
|
-
const inFolder = Object.entries(files).map(([p, text]) => [p.slice(folder.length + 1), text]);
|
|
13
|
-
const types = [
|
|
14
|
-
...new Set(
|
|
15
|
-
inFolder.map(([p]) => p.split("/")).filter((s) => s.length > 1).map((s) => s[0])
|
|
16
|
-
)
|
|
17
|
-
];
|
|
18
|
-
const map = inFolder.find(([p]) => /^index\.[cm]?[jt]sx?$/.test(p));
|
|
19
|
-
if (!map)
|
|
20
|
-
warnings.push(`${folder} has no map file (index.tsx, index.ts or index.js) rendering the section types`);
|
|
21
|
-
const marked = inFolder.some(
|
|
22
|
-
([, text]) => text.includes("data-brease-section") || text.includes("sectionAttrs(")
|
|
23
|
-
);
|
|
24
|
-
if (!marked)
|
|
25
|
-
warnings.push(
|
|
26
|
-
`No component in ${folder} renders data-brease-section (or sectionAttrs), so editors can't add sections`
|
|
27
|
-
);
|
|
28
|
-
for (const type of types) {
|
|
29
|
-
if (!TYPE_NAME.test(type)) {
|
|
30
|
-
warnings.push(`${folder}/${type}: use lowercase letters, numbers and - in section folder names`);
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
const json = files[`${folder}/${type}/section.json`];
|
|
34
|
-
if (json === void 0) {
|
|
35
|
-
warnings.push(`${folder}/${type} has no section.json`);
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
try {
|
|
39
|
-
const meta = JSON.parse(json);
|
|
40
|
-
if (typeof meta.name !== "string" || !meta.name.trim())
|
|
41
|
-
warnings.push(`${folder}/${type}/section.json has no name`);
|
|
42
|
-
if (meta.fields !== void 0 && (typeof meta.fields !== "object" || meta.fields === null))
|
|
43
|
-
warnings.push(`${folder}/${type}/section.json: fields must be a JSON Schema object`);
|
|
44
|
-
} catch {
|
|
45
|
-
warnings.push(`${folder}/${type}/section.json is not valid JSON`);
|
|
46
|
-
}
|
|
47
|
-
if (map && !map[1].includes(type)) warnings.push(`${type} is in ${folder} but not in its map file`);
|
|
48
|
-
}
|
|
49
|
-
const known = new Set(types);
|
|
50
|
-
for (const [pageId, locales] of Object.entries(pages)) {
|
|
51
|
-
let reference = null;
|
|
52
|
-
for (const [locale, file] of Object.entries(locales)) {
|
|
53
|
-
const list = file.content?.sections;
|
|
54
|
-
if (list === void 0) continue;
|
|
55
|
-
if (!Array.isArray(list)) {
|
|
56
|
-
warnings.push(`${pageId} (${locale}): sections is not a list`);
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const ids = /* @__PURE__ */ new Set();
|
|
60
|
-
for (const item of list) {
|
|
61
|
-
if (typeof item?.id !== "string" || typeof item.type !== "string") {
|
|
62
|
-
warnings.push(`${pageId} (${locale}): a section has no id or type`);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (ids.has(item.id)) warnings.push(`${pageId} (${locale}): section ${item.id} appears twice`);
|
|
66
|
-
ids.add(item.id);
|
|
67
|
-
if (!known.has(item.type))
|
|
68
|
-
warnings.push(`${pageId} (${locale}): section type "${item.type}" has no folder`);
|
|
69
|
-
}
|
|
70
|
-
const shape = list.map((s) => `${s?.id}:${s?.type}`).join(",");
|
|
71
|
-
if (reference === null) reference = shape;
|
|
72
|
-
else if (shape !== reference)
|
|
73
|
-
warnings.push(`${pageId}: the sections in ${locale} differ from the other locales`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return warnings;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export { SECTION_FOLDERS, checkSections, sectionAttrs, sectionsAttrs };
|