@cancia/astro 0.0.1
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/LICENSE +21 -0
- package/dist/chunk-22DJVJBR.js +169 -0
- package/dist/chunk-5IPHDIC6.js +19 -0
- package/dist/chunk-7IA5B5CF.js +11 -0
- package/dist/chunk-DGCGIEFD.js +14 -0
- package/dist/chunk-NG5GJME5.js +39 -0
- package/dist/chunk-QAM5VKAF.js +73 -0
- package/dist/chunk-YPVZDWTW.js +34 -0
- package/dist/chunk-YQDZQSES.js +393 -0
- package/dist/endpoints/auth.d.ts +5 -0
- package/dist/endpoints/auth.js +56 -0
- package/dist/endpoints/content.d.ts +11 -0
- package/dist/endpoints/content.js +56 -0
- package/dist/endpoints/health.d.ts +3 -0
- package/dist/endpoints/health.js +9 -0
- package/dist/endpoints/lists.d.ts +14 -0
- package/dist/endpoints/lists.js +36 -0
- package/dist/endpoints/publish.d.ts +5 -0
- package/dist/endpoints/publish.js +34 -0
- package/dist/endpoints/schemas.d.ts +5 -0
- package/dist/endpoints/schemas.js +20 -0
- package/dist/endpoints/upload.d.ts +5 -0
- package/dist/endpoints/upload.js +41 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.js +712 -0
- package/dist/loader/index.d.ts +26 -0
- package/dist/loader/index.js +7 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +8 -0
- package/dist/schema/index.d.ts +96 -0
- package/dist/schema/index.js +10 -0
- package/dist/types-BMlLS-OS.d.ts +161 -0
- package/dist/upload-DwCGjXbz.d.ts +13 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Antonino Dell'Albani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadListSchema
|
|
3
|
+
} from "./chunk-NG5GJME5.js";
|
|
4
|
+
import {
|
|
5
|
+
RevConflictError
|
|
6
|
+
} from "./chunk-7IA5B5CF.js";
|
|
7
|
+
|
|
8
|
+
// src/routes/lists.ts
|
|
9
|
+
function parseListPath(url) {
|
|
10
|
+
const path = new URL(url, "http://localhost").pathname;
|
|
11
|
+
const m = path.match(/^\/api\/cancia\/lists\/([a-z][a-z0-9_]*)(?:\/(.+))?$/);
|
|
12
|
+
if (!m) return null;
|
|
13
|
+
const listName = m[1];
|
|
14
|
+
const rest = m[2];
|
|
15
|
+
if (!rest) return { kind: "collection", listName };
|
|
16
|
+
if (rest === "reorder") return { kind: "reorder", listName };
|
|
17
|
+
if (rest === "_translations") return { kind: "translations", listName };
|
|
18
|
+
return { kind: "entry", listName, id: rest };
|
|
19
|
+
}
|
|
20
|
+
function getSite(request) {
|
|
21
|
+
return new URL(request.url).searchParams.get("site");
|
|
22
|
+
}
|
|
23
|
+
function getLocale(request) {
|
|
24
|
+
return new URL(request.url).searchParams.get("locale");
|
|
25
|
+
}
|
|
26
|
+
function checkAuth(request, secret) {
|
|
27
|
+
if (!secret) return null;
|
|
28
|
+
const token = request.headers.get("Authorization")?.replace("Bearer ", "").trim();
|
|
29
|
+
if (token !== secret) {
|
|
30
|
+
return json({ error: "Unauthorized" }, 401);
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function json(body, status = 200) {
|
|
35
|
+
return new Response(JSON.stringify(body), {
|
|
36
|
+
status,
|
|
37
|
+
headers: { "Content-Type": "application/json" }
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function unknownListResponse(listName) {
|
|
41
|
+
return json({ error: `Unknown list "${listName}"`, code: "UNKNOWN_LIST" }, 404);
|
|
42
|
+
}
|
|
43
|
+
function makeListsRoutes(ctx) {
|
|
44
|
+
async function handle(request, method) {
|
|
45
|
+
if (!ctx.storageV2) {
|
|
46
|
+
return json({ error: "Storage v2 not configured", code: "NO_STORAGE_V2" }, 503);
|
|
47
|
+
}
|
|
48
|
+
const deny = checkAuth(request, ctx.secret);
|
|
49
|
+
if (deny) return deny;
|
|
50
|
+
const parsed = parseListPath(request.url);
|
|
51
|
+
if (!parsed) return json({ error: "Bad request" }, 400);
|
|
52
|
+
const site = getSite(request);
|
|
53
|
+
if (!site) return json({ error: "Missing ?site=" }, 400);
|
|
54
|
+
const schema = await loadListSchema(ctx.projectRoot, parsed.listName, ctx.schemasPath);
|
|
55
|
+
if (!schema) return unknownListResponse(parsed.listName);
|
|
56
|
+
const { lists } = ctx.storageV2;
|
|
57
|
+
const requestedLocale = getLocale(request);
|
|
58
|
+
const writeLocale = requestedLocale ?? ctx.defaultLocale;
|
|
59
|
+
if (parsed.kind === "translations") {
|
|
60
|
+
if (method !== "GET") return json({ error: "Method not allowed" }, 405);
|
|
61
|
+
const statuses = await lists.translations(site, parsed.listName);
|
|
62
|
+
return json({ translations: statuses });
|
|
63
|
+
}
|
|
64
|
+
if (parsed.kind === "collection") {
|
|
65
|
+
if (method === "GET") {
|
|
66
|
+
const entries = await lists.list(site, parsed.listName, requestedLocale ?? void 0);
|
|
67
|
+
return json({ entries });
|
|
68
|
+
}
|
|
69
|
+
if (method === "POST") {
|
|
70
|
+
const body = await request.json().catch(() => null);
|
|
71
|
+
if (!body || typeof body.data !== "object" || body.data === null) {
|
|
72
|
+
return json({ error: "Missing or invalid `data` object" }, 400);
|
|
73
|
+
}
|
|
74
|
+
const parsedData = schema.validator.safeParse(body.data);
|
|
75
|
+
if (!parsedData.success) {
|
|
76
|
+
return json({
|
|
77
|
+
error: "Validation failed",
|
|
78
|
+
code: "VALIDATION_FAILED",
|
|
79
|
+
issues: parsedData.error.issues
|
|
80
|
+
}, 400);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const entry = await lists.create(
|
|
84
|
+
site,
|
|
85
|
+
parsed.listName,
|
|
86
|
+
parsedData.data,
|
|
87
|
+
writeLocale,
|
|
88
|
+
body.id
|
|
89
|
+
);
|
|
90
|
+
return json({ entry }, 201);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return json({
|
|
93
|
+
error: e instanceof Error ? e.message : String(e),
|
|
94
|
+
code: "CREATE_FAILED"
|
|
95
|
+
}, 409);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return json({ error: `Method ${method} not allowed` }, 405);
|
|
99
|
+
}
|
|
100
|
+
if (parsed.kind === "reorder") {
|
|
101
|
+
if (method !== "POST") return json({ error: "Method not allowed" }, 405);
|
|
102
|
+
const body = await request.json().catch(() => null);
|
|
103
|
+
if (!body || !Array.isArray(body.ids) || !body.ids.every((id2) => typeof id2 === "string")) {
|
|
104
|
+
return json({ error: "`ids` must be a string array" }, 400);
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
await lists.reorder(site, parsed.listName, body.ids);
|
|
108
|
+
return json({ ok: true });
|
|
109
|
+
} catch (e) {
|
|
110
|
+
return json({
|
|
111
|
+
error: e instanceof Error ? e.message : String(e),
|
|
112
|
+
code: "REORDER_FAILED"
|
|
113
|
+
}, 409);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const { id } = parsed;
|
|
117
|
+
if (method === "GET") {
|
|
118
|
+
const entry = await lists.get(site, parsed.listName, id, writeLocale);
|
|
119
|
+
if (!entry) return json({ error: "Not found" }, 404);
|
|
120
|
+
return json({ entry });
|
|
121
|
+
}
|
|
122
|
+
if (method === "PATCH") {
|
|
123
|
+
const body = await request.json().catch(() => null);
|
|
124
|
+
if (!body || typeof body.data !== "object" || body.data === null) {
|
|
125
|
+
return json({ error: "Missing or invalid `data` object" }, 400);
|
|
126
|
+
}
|
|
127
|
+
if (typeof body._rev !== "string") {
|
|
128
|
+
return json({ error: "Missing `_rev`", code: "REV_REQUIRED" }, 400);
|
|
129
|
+
}
|
|
130
|
+
const parsedData = schema.validator.safeParse(body.data);
|
|
131
|
+
if (!parsedData.success) {
|
|
132
|
+
return json({
|
|
133
|
+
error: "Validation failed",
|
|
134
|
+
code: "VALIDATION_FAILED",
|
|
135
|
+
issues: parsedData.error.issues
|
|
136
|
+
}, 400);
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const entry = await lists.update(
|
|
140
|
+
site,
|
|
141
|
+
parsed.listName,
|
|
142
|
+
id,
|
|
143
|
+
writeLocale,
|
|
144
|
+
parsedData.data,
|
|
145
|
+
body._rev
|
|
146
|
+
);
|
|
147
|
+
return json({ entry });
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (e instanceof RevConflictError) {
|
|
150
|
+
return json({ error: e.message, code: "REV_CONFLICT" }, 409);
|
|
151
|
+
}
|
|
152
|
+
return json({
|
|
153
|
+
error: e instanceof Error ? e.message : String(e),
|
|
154
|
+
code: "UPDATE_FAILED"
|
|
155
|
+
}, 400);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (method === "DELETE") {
|
|
159
|
+
await lists.delete(site, parsed.listName, id, writeLocale);
|
|
160
|
+
return json({ ok: true });
|
|
161
|
+
}
|
|
162
|
+
return json({ error: `Method ${method} not allowed` }, 405);
|
|
163
|
+
}
|
|
164
|
+
return { handle };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export {
|
|
168
|
+
makeListsRoutes
|
|
169
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/upload-validate.ts
|
|
2
|
+
var SITE_RE = /^[a-z0-9][a-z0-9._-]*$/i;
|
|
3
|
+
function isValidSite(site) {
|
|
4
|
+
return SITE_RE.test(site);
|
|
5
|
+
}
|
|
6
|
+
function detectImageType(buf) {
|
|
7
|
+
const startsWith = (sig, offset = 0) => sig.every((b, i) => buf[offset + i] === b);
|
|
8
|
+
if (startsWith([255, 216, 255])) return { mime: "image/jpeg", ext: "jpg" };
|
|
9
|
+
if (startsWith([137, 80, 78, 71, 13, 10, 26, 10])) return { mime: "image/png", ext: "png" };
|
|
10
|
+
if (startsWith([71, 73, 70, 56])) return { mime: "image/gif", ext: "gif" };
|
|
11
|
+
if (startsWith([82, 73, 70, 70]) && startsWith([87, 69, 66, 80], 8)) return { mime: "image/webp", ext: "webp" };
|
|
12
|
+
if (startsWith([102, 116, 121, 112], 4) && (startsWith([97, 118, 105, 102], 8) || startsWith([97, 118, 105, 115], 8))) return { mime: "image/avif", ext: "avif" };
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
isValidSite,
|
|
18
|
+
detectImageType
|
|
19
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/storage/types.ts
|
|
2
|
+
var RevConflictError = class extends Error {
|
|
3
|
+
constructor(message = "Revision mismatch \u2014 item was modified by another writer") {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "RevConflictError";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
RevConflictError
|
|
11
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/runtime.ts
|
|
2
|
+
var _runtime = null;
|
|
3
|
+
function setCanciaRuntime(runtime) {
|
|
4
|
+
_runtime = runtime;
|
|
5
|
+
}
|
|
6
|
+
function getCanciaRuntime() {
|
|
7
|
+
if (!_runtime) throw new Error("[cancia] Runtime not initialised. This is a bug.");
|
|
8
|
+
return _runtime;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
setCanciaRuntime,
|
|
13
|
+
getCanciaRuntime
|
|
14
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/schema/loader.ts
|
|
2
|
+
import { existsSync, statSync } from "fs";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
var _cache = null;
|
|
6
|
+
var _cachedPath = null;
|
|
7
|
+
var _cachedMtime = 0;
|
|
8
|
+
function resolveSchemasPath(projectRoot, overridePath) {
|
|
9
|
+
if (overridePath) {
|
|
10
|
+
return existsSync(overridePath) ? overridePath : null;
|
|
11
|
+
}
|
|
12
|
+
const tsPath = join(projectRoot, "src", "cms", "schemas.ts");
|
|
13
|
+
if (existsSync(tsPath)) return tsPath;
|
|
14
|
+
const jsPath = join(projectRoot, "src", "cms", "schemas.js");
|
|
15
|
+
if (existsSync(jsPath)) return jsPath;
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
async function loadSchemas(projectRoot, overridePath) {
|
|
19
|
+
const path = resolveSchemasPath(projectRoot, overridePath);
|
|
20
|
+
if (!path) return {};
|
|
21
|
+
const mtime = statSync(path).mtimeMs;
|
|
22
|
+
if (_cache && _cachedPath === path && _cachedMtime === mtime) return _cache;
|
|
23
|
+
const url = pathToFileURL(path).href;
|
|
24
|
+
const mod = await import(`${url}?mtime=${mtime}`);
|
|
25
|
+
const schemas = mod.schemas ?? mod.default ?? {};
|
|
26
|
+
_cache = schemas;
|
|
27
|
+
_cachedPath = path;
|
|
28
|
+
_cachedMtime = mtime;
|
|
29
|
+
return schemas;
|
|
30
|
+
}
|
|
31
|
+
async function loadListSchema(projectRoot, listName, overridePath) {
|
|
32
|
+
const schemas = await loadSchemas(projectRoot, overridePath);
|
|
33
|
+
return schemas[listName] ?? null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
loadSchemas,
|
|
38
|
+
loadListSchema
|
|
39
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/schema/index.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function defineList(opts) {
|
|
4
|
+
return {
|
|
5
|
+
label: opts.label,
|
|
6
|
+
labelSingular: opts.labelSingular ?? opts.label.replace(/s$/, ""),
|
|
7
|
+
titleField: opts.titleField,
|
|
8
|
+
bodyField: opts.bodyField,
|
|
9
|
+
slugField: opts.slugField,
|
|
10
|
+
fields: opts.fields,
|
|
11
|
+
validator: z.object(opts.fields)
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function humanise(name) {
|
|
15
|
+
return name.replace(/([A-Z])/g, " $1").replace(/[_-]/g, " ").replace(/^\w/, (c) => c.toUpperCase()).trim();
|
|
16
|
+
}
|
|
17
|
+
function inferWidget(prop, label) {
|
|
18
|
+
if (prop.widget) return prop.widget;
|
|
19
|
+
if (prop.enum) return "select";
|
|
20
|
+
if (prop.type === "boolean") return "checkbox";
|
|
21
|
+
if (prop.type === "number" || prop.type === "integer") return "number";
|
|
22
|
+
if (prop.type === "string") {
|
|
23
|
+
if (prop.format === "uri" || prop.format === "url") return "url";
|
|
24
|
+
if (prop.format === "email") return "email";
|
|
25
|
+
if (prop.format === "date-time" || prop.format === "date") return "datetime";
|
|
26
|
+
if (label.toLowerCase() === "body" || label.toLowerCase() === "content") return "textarea";
|
|
27
|
+
if (prop.maxLength && prop.maxLength > 200) return "textarea";
|
|
28
|
+
return "text";
|
|
29
|
+
}
|
|
30
|
+
return "text";
|
|
31
|
+
}
|
|
32
|
+
function describeProperty(name, prop, required) {
|
|
33
|
+
const label = prop.label ?? humanise(name);
|
|
34
|
+
const desc = {
|
|
35
|
+
name,
|
|
36
|
+
label,
|
|
37
|
+
widget: inferWidget(prop, label),
|
|
38
|
+
required
|
|
39
|
+
};
|
|
40
|
+
if (prop.description) desc.description = prop.description;
|
|
41
|
+
if (prop.placeholder) desc.placeholder = prop.placeholder;
|
|
42
|
+
if (prop.minLength !== void 0) desc.minLength = prop.minLength;
|
|
43
|
+
if (prop.maxLength !== void 0) desc.maxLength = prop.maxLength;
|
|
44
|
+
if (prop.minimum !== void 0) desc.min = prop.minimum;
|
|
45
|
+
if (prop.maximum !== void 0) desc.max = prop.maximum;
|
|
46
|
+
if (prop.enum) desc.options = prop.enum;
|
|
47
|
+
return desc;
|
|
48
|
+
}
|
|
49
|
+
function describeList(name, schema) {
|
|
50
|
+
const json = z.toJSONSchema(schema.validator, {
|
|
51
|
+
metadata: z.globalRegistry
|
|
52
|
+
});
|
|
53
|
+
const properties = json.properties ?? {};
|
|
54
|
+
const requiredSet = new Set(json.required ?? []);
|
|
55
|
+
const fields = Object.keys(schema.fields).map(
|
|
56
|
+
(fieldName) => describeProperty(fieldName, properties[fieldName] ?? {}, requiredSet.has(fieldName))
|
|
57
|
+
);
|
|
58
|
+
return {
|
|
59
|
+
name,
|
|
60
|
+
label: schema.label,
|
|
61
|
+
labelSingular: schema.labelSingular,
|
|
62
|
+
titleField: schema.titleField,
|
|
63
|
+
bodyField: schema.bodyField,
|
|
64
|
+
slugField: schema.slugField,
|
|
65
|
+
fields
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
z,
|
|
71
|
+
defineList,
|
|
72
|
+
describeList
|
|
73
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadSchemas
|
|
3
|
+
} from "./chunk-NG5GJME5.js";
|
|
4
|
+
import {
|
|
5
|
+
describeList
|
|
6
|
+
} from "./chunk-QAM5VKAF.js";
|
|
7
|
+
|
|
8
|
+
// src/routes/schemas.ts
|
|
9
|
+
function json(body, status = 200) {
|
|
10
|
+
return new Response(JSON.stringify(body), {
|
|
11
|
+
status,
|
|
12
|
+
headers: { "Content-Type": "application/json" }
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function makeSchemasRoute(ctx) {
|
|
16
|
+
return async function schemasRoute(request) {
|
|
17
|
+
if (ctx.secret) {
|
|
18
|
+
const token = request.headers.get("Authorization")?.replace("Bearer ", "").trim();
|
|
19
|
+
if (token !== ctx.secret) {
|
|
20
|
+
return json({ error: "Unauthorized" }, 401);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const schemas = await loadSchemas(ctx.projectRoot, ctx.schemasPath);
|
|
24
|
+
const descriptions = {};
|
|
25
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
26
|
+
descriptions[name] = describeList(name, schema);
|
|
27
|
+
}
|
|
28
|
+
return json({ schemas: descriptions });
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
makeSchemasRoute
|
|
34
|
+
};
|