@eutobias/serenojs 0.0.0-stage → 0.1.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/README.md +682 -2
- package/admin-dist/assets/index-B3EgqtT6.js +9 -0
- package/admin-dist/assets/index-D2m0Eb4k.css +2 -0
- package/admin-dist/index.html +17 -0
- package/dist/auth/auth.server.d.ts +31 -0
- package/dist/auth/auth.server.js +141 -0
- package/dist/auth/config.d.ts +32 -0
- package/dist/auth/config.js +25 -0
- package/dist/auth/cookies.server.d.ts +7 -0
- package/dist/auth/cookies.server.js +65 -0
- package/dist/auth/http.server.d.ts +8 -0
- package/dist/auth/http.server.js +30 -0
- package/dist/auth/password.server.d.ts +2 -0
- package/dist/auth/password.server.js +21 -0
- package/dist/auth/session.server.d.ts +2 -0
- package/dist/auth/session.server.js +14 -0
- package/dist/auth/users.server.d.ts +16 -0
- package/dist/auth/users.server.js +123 -0
- package/dist/config.d.ts +91 -0
- package/dist/config.js +39 -0
- package/dist/core/backup.server.d.ts +52 -0
- package/dist/core/backup.server.js +349 -0
- package/dist/core/db.server.d.ts +24 -0
- package/dist/core/db.server.js +526 -0
- package/dist/core/media.d.ts +22 -0
- package/dist/core/media.js +76 -0
- package/dist/core/media.server.d.ts +18 -0
- package/dist/core/media.server.js +102 -0
- package/dist/core/schema.d.ts +26 -0
- package/dist/core/schema.js +99 -0
- package/dist/core/types.d.ts +143 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation.d.ts +16 -0
- package/dist/core/validation.js +192 -0
- package/dist/locale.d.ts +2 -0
- package/dist/locale.js +14 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +37 -0
- package/dist/server/app.d.ts +20 -0
- package/dist/server/app.js +661 -0
- package/dist/ui/admin/admin-shell.d.ts +16 -0
- package/dist/ui/admin/admin-shell.js +38 -0
- package/dist/ui/admin/backup-manager.d.ts +5 -0
- package/dist/ui/admin/backup-manager.js +165 -0
- package/dist/ui/admin/collection-icon.d.ts +22 -0
- package/dist/ui/admin/collection-icon.js +34 -0
- package/dist/ui/admin/collection-manager.d.ts +7 -0
- package/dist/ui/admin/collection-manager.js +212 -0
- package/dist/ui/admin/display.d.ts +7 -0
- package/dist/ui/admin/display.js +65 -0
- package/dist/ui/admin/field-control.d.ts +14 -0
- package/dist/ui/admin/field-control.js +58 -0
- package/dist/ui/admin/i18n.d.ts +27 -0
- package/dist/ui/admin/i18n.js +324 -0
- package/dist/ui/admin/item-form.d.ts +20 -0
- package/dist/ui/admin/item-form.js +60 -0
- package/dist/ui/admin/list-control.d.ts +13 -0
- package/dist/ui/admin/list-control.js +31 -0
- package/dist/ui/admin/locales.d.ts +4 -0
- package/dist/ui/admin/locales.js +8 -0
- package/dist/ui/admin/login-form.d.ts +3 -0
- package/dist/ui/admin/login-form.js +52 -0
- package/dist/ui/admin/media-control.d.ts +14 -0
- package/dist/ui/admin/media-control.js +74 -0
- package/dist/ui/admin/media-list-control.d.ts +14 -0
- package/dist/ui/admin/media-list-control.js +91 -0
- package/dist/ui/admin/pagination.d.ts +11 -0
- package/dist/ui/admin/pagination.js +14 -0
- package/dist/ui/admin/relation-control.d.ts +14 -0
- package/dist/ui/admin/relation-control.js +33 -0
- package/dist/ui/admin/router.d.ts +17 -0
- package/dist/ui/admin/router.js +58 -0
- package/dist/ui/admin/runtime.d.ts +28 -0
- package/dist/ui/admin/runtime.js +29 -0
- package/dist/ui/admin/sidebar.d.ts +13 -0
- package/dist/ui/admin/sidebar.js +29 -0
- package/dist/ui/admin/standalone-manager.d.ts +11 -0
- package/dist/ui/admin/standalone-manager.js +51 -0
- package/dist/ui/admin/table.d.ts +21 -0
- package/dist/ui/admin/table.js +23 -0
- package/dist/ui/admin/toast.d.ts +16 -0
- package/dist/ui/admin/toast.js +59 -0
- package/dist/ui/admin/users-manager.d.ts +6 -0
- package/dist/ui/admin/users-manager.js +157 -0
- package/dist/ui/badge.d.ts +9 -0
- package/dist/ui/badge.js +14 -0
- package/dist/ui/box.d.ts +17 -0
- package/dist/ui/box.js +14 -0
- package/dist/ui/button.d.ts +25 -0
- package/dist/ui/button.js +28 -0
- package/dist/ui/card.d.ts +45 -0
- package/dist/ui/card.js +49 -0
- package/dist/ui/container.d.ts +13 -0
- package/dist/ui/container.js +13 -0
- package/dist/ui/drawer.d.ts +15 -0
- package/dist/ui/drawer.js +31 -0
- package/dist/ui/field-wrapper.d.ts +15 -0
- package/dist/ui/field-wrapper.js +11 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +8 -0
- package/dist/ui/text.d.ts +19 -0
- package/dist/ui/text.js +40 -0
- package/package.json +81 -3
- package/src/admin/app.tsx +371 -0
- package/src/admin/index.html +16 -0
- package/src/admin/main.tsx +42 -0
- package/src/admin/styles.css +9 -0
- package/src/auth/auth.server.ts +203 -0
- package/src/auth/config.ts +46 -0
- package/src/auth/cookies.server.ts +84 -0
- package/src/auth/http.server.ts +32 -0
- package/src/auth/password.server.ts +25 -0
- package/src/auth/session.server.ts +16 -0
- package/src/auth/users.server.ts +159 -0
- package/src/config.ts +120 -0
- package/src/core/backup.server.ts +446 -0
- package/src/core/db.server.ts +685 -0
- package/src/core/media.server.ts +136 -0
- package/src/core/media.ts +91 -0
- package/src/core/schema.ts +126 -0
- package/src/core/types.ts +194 -0
- package/src/core/validation.ts +215 -0
- package/src/global.d.ts +1 -0
- package/src/locale.ts +16 -0
- package/src/runtime.ts +54 -0
- package/src/server/app.ts +737 -0
- package/src/ui/admin/admin-shell.tsx +106 -0
- package/src/ui/admin/backup-manager.tsx +327 -0
- package/src/ui/admin/collection-icon.tsx +69 -0
- package/src/ui/admin/collection-manager.tsx +408 -0
- package/src/ui/admin/display.tsx +138 -0
- package/src/ui/admin/field-control.tsx +214 -0
- package/src/ui/admin/i18n.tsx +401 -0
- package/src/ui/admin/item-form.tsx +200 -0
- package/src/ui/admin/list-control.tsx +121 -0
- package/src/ui/admin/locales.ts +12 -0
- package/src/ui/admin/login-form.tsx +98 -0
- package/src/ui/admin/media-control.tsx +161 -0
- package/src/ui/admin/media-list-control.tsx +242 -0
- package/src/ui/admin/pagination.tsx +58 -0
- package/src/ui/admin/relation-control.tsx +142 -0
- package/src/ui/admin/router.tsx +105 -0
- package/src/ui/admin/runtime.tsx +58 -0
- package/src/ui/admin/sidebar.tsx +178 -0
- package/src/ui/admin/standalone-manager.tsx +103 -0
- package/src/ui/admin/table.tsx +129 -0
- package/src/ui/admin/toast.tsx +126 -0
- package/src/ui/admin/users-manager.tsx +344 -0
- package/src/ui/badge.tsx +27 -0
- package/src/ui/box.tsx +33 -0
- package/src/ui/button.tsx +77 -0
- package/src/ui/card.tsx +92 -0
- package/src/ui/container.tsx +28 -0
- package/src/ui/drawer.tsx +104 -0
- package/src/ui/field-wrapper.tsx +54 -0
- package/src/ui/index.ts +8 -0
- package/src/ui/text.tsx +93 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Media uploads stored on disk under `public/files`.
|
|
3
|
+
*
|
|
4
|
+
* Values persisted in the DB are served URLs (`/files/<name>`). Uploads are
|
|
5
|
+
* validated against the field's `accept` / `maxSize` config in the collection
|
|
6
|
+
* descriptor, and always blocked for executable/HTML-ish extensions.
|
|
7
|
+
*
|
|
8
|
+
* In production Waku serves `dist/public` statically, so runtime uploads are
|
|
9
|
+
* streamed by the `GET /files/[...path]` route reading this directory.
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
12
|
+
import { basename, join, resolve } from "node:path";
|
|
13
|
+
import { randomBytes } from "node:crypto";
|
|
14
|
+
import { BLOCKED_EXTENSIONS, DEFAULT_MAX_SIZE, UPLOAD_URL_PREFIX, acceptList, extensionOf, isAllowedType, } from "./media.js";
|
|
15
|
+
export const UPLOADS_DIR = resolve(process.cwd(), "public/files");
|
|
16
|
+
const CONTENT_TYPES = {
|
|
17
|
+
png: "image/png",
|
|
18
|
+
jpg: "image/jpeg",
|
|
19
|
+
jpeg: "image/jpeg",
|
|
20
|
+
gif: "image/gif",
|
|
21
|
+
webp: "image/webp",
|
|
22
|
+
avif: "image/avif",
|
|
23
|
+
svg: "image/svg+xml",
|
|
24
|
+
ico: "image/x-icon",
|
|
25
|
+
pdf: "application/pdf",
|
|
26
|
+
txt: "text/plain; charset=utf-8",
|
|
27
|
+
csv: "text/csv; charset=utf-8",
|
|
28
|
+
json: "application/json; charset=utf-8",
|
|
29
|
+
zip: "application/zip",
|
|
30
|
+
mp4: "video/mp4",
|
|
31
|
+
webm: "video/webm",
|
|
32
|
+
mp3: "audio/mpeg",
|
|
33
|
+
wav: "audio/wav",
|
|
34
|
+
doc: "application/msword",
|
|
35
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
36
|
+
xls: "application/vnd.ms-excel",
|
|
37
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
38
|
+
};
|
|
39
|
+
export class MediaError extends Error {
|
|
40
|
+
status;
|
|
41
|
+
constructor(message, status = 422) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.status = status;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function contentTypeFor(name) {
|
|
47
|
+
return CONTENT_TYPES[extensionOf(name)] ?? "application/octet-stream";
|
|
48
|
+
}
|
|
49
|
+
export async function saveUpload(file, field) {
|
|
50
|
+
const ext = extensionOf(file.name);
|
|
51
|
+
if (!ext) {
|
|
52
|
+
throw new MediaError("Arquivo sem extensão não é permitido.");
|
|
53
|
+
}
|
|
54
|
+
if (BLOCKED_EXTENSIONS.has(ext)) {
|
|
55
|
+
throw new MediaError("Tipo de arquivo não permitido.");
|
|
56
|
+
}
|
|
57
|
+
if (!isAllowedType(field, file.name, file.type || contentTypeFor(file.name))) {
|
|
58
|
+
throw new MediaError(`Tipo de arquivo não permitido. Aceitos: ${acceptList(field).join(", ")}.`);
|
|
59
|
+
}
|
|
60
|
+
const maxSize = field.maxSize ?? DEFAULT_MAX_SIZE;
|
|
61
|
+
if (file.size > maxSize) {
|
|
62
|
+
const mb = (maxSize / 1024 / 1024).toFixed(1);
|
|
63
|
+
throw new MediaError(`Arquivo maior que o limite de ${mb} MB.`, 413);
|
|
64
|
+
}
|
|
65
|
+
const name = `${Date.now()}-${randomBytes(6).toString("hex")}.${ext}`;
|
|
66
|
+
mkdirSync(UPLOADS_DIR, { recursive: true });
|
|
67
|
+
writeFileSync(join(UPLOADS_DIR, name), Buffer.from(await file.arrayBuffer()));
|
|
68
|
+
return {
|
|
69
|
+
url: `${UPLOAD_URL_PREFIX}${name}`,
|
|
70
|
+
name,
|
|
71
|
+
size: file.size,
|
|
72
|
+
type: contentTypeFor(name),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Resolve a stored name to a safe path inside `UPLOADS_DIR`, or null. */
|
|
76
|
+
export function resolveUploadPath(name) {
|
|
77
|
+
const safe = basename(name);
|
|
78
|
+
if (!safe || safe !== name || safe.startsWith("."))
|
|
79
|
+
return null;
|
|
80
|
+
const path = join(UPLOADS_DIR, safe);
|
|
81
|
+
if (!path.startsWith(UPLOADS_DIR))
|
|
82
|
+
return null;
|
|
83
|
+
return existsSync(path) && statSync(path).isFile() ? path : null;
|
|
84
|
+
}
|
|
85
|
+
export function readUpload(name) {
|
|
86
|
+
const path = resolveUploadPath(name);
|
|
87
|
+
return path ? readFileSync(path) : null;
|
|
88
|
+
}
|
|
89
|
+
export function deleteByUrl(url) {
|
|
90
|
+
if (!url.startsWith(UPLOAD_URL_PREFIX))
|
|
91
|
+
return false;
|
|
92
|
+
const path = resolveUploadPath(url.slice(UPLOAD_URL_PREFIX.length));
|
|
93
|
+
if (!path)
|
|
94
|
+
return false;
|
|
95
|
+
try {
|
|
96
|
+
unlinkSync(path);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CollectionDef, FieldDef, FieldValue } from "./types.js";
|
|
2
|
+
/** Locales stored by a collection (falls back to the global default). */
|
|
3
|
+
export declare function collectionLocales(collection: CollectionDef): string[];
|
|
4
|
+
/** Whether a field is stored once per locale (`name_pt`, `name_en`). */
|
|
5
|
+
export declare function isLocalizedField(collection: CollectionDef, field: FieldDef): boolean;
|
|
6
|
+
/** Read the value of a (possibly localized) field for one locale. */
|
|
7
|
+
export declare function valueForLocale(collection: CollectionDef, field: FieldDef, item: Record<string, unknown>, locale?: string): FieldValue;
|
|
8
|
+
export declare function getCollections(): CollectionDef[];
|
|
9
|
+
export declare function getCollection(slug: string): CollectionDef | undefined;
|
|
10
|
+
export declare function getSchemaTitle(): string;
|
|
11
|
+
/** Admins can label locales passed from the host config. */
|
|
12
|
+
export declare function getLocaleLabel(code: string): string;
|
|
13
|
+
/** Whether a collection holds exactly one record. */
|
|
14
|
+
export declare function isStandalone(collection: CollectionDef): boolean;
|
|
15
|
+
/** SQLite table name for a collection. */
|
|
16
|
+
export declare function tableName(slug: string): string;
|
|
17
|
+
/** Whether a relation field stores a list of target ids. */
|
|
18
|
+
export declare function relationIsMany(field: FieldDef): boolean;
|
|
19
|
+
/** Whether a relation field stores a single target id. */
|
|
20
|
+
export declare function relationIsOne(field: FieldDef): boolean;
|
|
21
|
+
/** Default value for a single field. */
|
|
22
|
+
export declare function fieldDefault(field: FieldDef): FieldValue;
|
|
23
|
+
/** Empty record of values for a collection (used by the create form). */
|
|
24
|
+
export declare function emptyValues(collection: CollectionDef): Record<string, FieldValue>;
|
|
25
|
+
/** Human label used when showing a single item (e.g. delete confirmation). */
|
|
26
|
+
export declare function itemLabel(collection: CollectionDef, item: Record<string, unknown>): string;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { getDefaultLocale } from "../locale.js";
|
|
2
|
+
import { getCmsConfig, getCollection as runtimeGetCollection, getCollections as runtimeGetCollections, localeLabel as runtimeLocaleLabel, } from "../runtime.js";
|
|
3
|
+
/** Field types that are localized by default. */
|
|
4
|
+
const LOCALIZED_TYPES = new Set(["text", "textarea", "email", "list"]);
|
|
5
|
+
/** Locales stored by a collection (falls back to the global default). */
|
|
6
|
+
export function collectionLocales(collection) {
|
|
7
|
+
return collection.locales && collection.locales.length > 0
|
|
8
|
+
? collection.locales
|
|
9
|
+
: [getDefaultLocale()];
|
|
10
|
+
}
|
|
11
|
+
/** Whether a field is stored once per locale (`name_pt`, `name_en`). */
|
|
12
|
+
export function isLocalizedField(collection, field) {
|
|
13
|
+
if (field.localized !== undefined)
|
|
14
|
+
return field.localized;
|
|
15
|
+
return LOCALIZED_TYPES.has(field.type);
|
|
16
|
+
}
|
|
17
|
+
/** Read the value of a (possibly localized) field for one locale. */
|
|
18
|
+
export function valueForLocale(collection, field, item, locale = getDefaultLocale()) {
|
|
19
|
+
const raw = item[field.name];
|
|
20
|
+
if (!isLocalizedField(collection, field))
|
|
21
|
+
return raw;
|
|
22
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
23
|
+
return null;
|
|
24
|
+
return raw[locale] ?? null;
|
|
25
|
+
}
|
|
26
|
+
export function getCollections() {
|
|
27
|
+
return runtimeGetCollections();
|
|
28
|
+
}
|
|
29
|
+
export function getCollection(slug) {
|
|
30
|
+
return runtimeGetCollection(slug);
|
|
31
|
+
}
|
|
32
|
+
export function getSchemaTitle() {
|
|
33
|
+
return getCmsConfig().title;
|
|
34
|
+
}
|
|
35
|
+
/** Admins can label locales passed from the host config. */
|
|
36
|
+
export function getLocaleLabel(code) {
|
|
37
|
+
return runtimeLocaleLabel(code);
|
|
38
|
+
}
|
|
39
|
+
/** Whether a collection holds exactly one record. */
|
|
40
|
+
export function isStandalone(collection) {
|
|
41
|
+
return collection.standalone === true;
|
|
42
|
+
}
|
|
43
|
+
/** SQLite table name for a collection. */
|
|
44
|
+
export function tableName(slug) {
|
|
45
|
+
return `c_${slug.replace(/[^a-zA-Z0-9_]/g, "_")}`;
|
|
46
|
+
}
|
|
47
|
+
/** Whether a relation field stores a list of target ids. */
|
|
48
|
+
export function relationIsMany(field) {
|
|
49
|
+
return (field.type === "relation" &&
|
|
50
|
+
field.relation?.type !== "one-to-one");
|
|
51
|
+
}
|
|
52
|
+
/** Whether a relation field stores a single target id. */
|
|
53
|
+
export function relationIsOne(field) {
|
|
54
|
+
return field.type === "relation" && field.relation?.type === "one-to-one";
|
|
55
|
+
}
|
|
56
|
+
/** Default value for a single field. */
|
|
57
|
+
export function fieldDefault(field) {
|
|
58
|
+
if (field.defaultValue !== undefined)
|
|
59
|
+
return field.defaultValue;
|
|
60
|
+
switch (field.type) {
|
|
61
|
+
case "boolean":
|
|
62
|
+
return false;
|
|
63
|
+
case "number":
|
|
64
|
+
return field.min ?? 0;
|
|
65
|
+
case "relation":
|
|
66
|
+
return relationIsMany(field) ? [] : null;
|
|
67
|
+
case "media-list":
|
|
68
|
+
return [];
|
|
69
|
+
case "list":
|
|
70
|
+
return [];
|
|
71
|
+
case "select":
|
|
72
|
+
return field.options?.[0]?.value ?? "";
|
|
73
|
+
default:
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Empty record of values for a collection (used by the create form). */
|
|
78
|
+
export function emptyValues(collection) {
|
|
79
|
+
const values = {};
|
|
80
|
+
const locales = collectionLocales(collection);
|
|
81
|
+
for (const field of collection.fields) {
|
|
82
|
+
const fallback = fieldDefault(field);
|
|
83
|
+
values[field.name] = isLocalizedField(collection, field)
|
|
84
|
+
? Object.fromEntries(locales.map((locale) => [locale, fallback]))
|
|
85
|
+
: fallback;
|
|
86
|
+
}
|
|
87
|
+
return values;
|
|
88
|
+
}
|
|
89
|
+
/** Human label used when showing a single item (e.g. delete confirmation). */
|
|
90
|
+
export function itemLabel(collection, item) {
|
|
91
|
+
const preferred = collection.fields.find((field) => field.type === "text");
|
|
92
|
+
const candidates = preferred ? [preferred, ...collection.fields] : collection.fields;
|
|
93
|
+
for (const field of candidates) {
|
|
94
|
+
const value = valueForLocale(collection, field, item);
|
|
95
|
+
if (typeof value === "string" && value.trim())
|
|
96
|
+
return value.trim();
|
|
97
|
+
}
|
|
98
|
+
return `#${String(item.id ?? "")}`;
|
|
99
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/** Feather icon names curated by the admin icon registry. */
|
|
2
|
+
export type CollectionIconName = "archive" | "box" | "fileText" | "folder" | "grid" | "image" | "layout" | "settings" | "star" | "tag" | "user" | "users";
|
|
3
|
+
/**
|
|
4
|
+
* Shared descriptor types for the SerenoJS.
|
|
5
|
+
*
|
|
6
|
+
* The whole admin panel is generated from the collection descriptors in
|
|
7
|
+
* `src/schemas`. These types describe those descriptors.
|
|
8
|
+
*/
|
|
9
|
+
export type FieldType = "text" | "email" | "textarea" | "number" | "boolean" | "select" | "date" | "relation" | "media" | "media-list" | "list";
|
|
10
|
+
export type SelectOption = {
|
|
11
|
+
label: string;
|
|
12
|
+
value: string;
|
|
13
|
+
};
|
|
14
|
+
export type RelationType = "one-to-one" | "one-to-many" | "many-to-many";
|
|
15
|
+
export type RelationDef = {
|
|
16
|
+
/** Slug of the target collection. */
|
|
17
|
+
to: string;
|
|
18
|
+
type: RelationType;
|
|
19
|
+
};
|
|
20
|
+
export type FieldDef = {
|
|
21
|
+
name: string;
|
|
22
|
+
label: string;
|
|
23
|
+
type: FieldType;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
help?: string;
|
|
27
|
+
/** Only for `select` fields. */
|
|
28
|
+
options?: SelectOption[];
|
|
29
|
+
/** Only for `number` fields. */
|
|
30
|
+
min?: number;
|
|
31
|
+
max?: number;
|
|
32
|
+
/** Only for `relation` fields. */
|
|
33
|
+
relation?: RelationDef;
|
|
34
|
+
/**
|
|
35
|
+
* Only for `media` fields. Accepted types as MIME types (`image/png`,
|
|
36
|
+
* `image/*`) or extensions (`png`, `.png`). Uploads are stored under
|
|
37
|
+
* `public/files` and the field value is the served URL (`/files/<name>`).
|
|
38
|
+
* Omit to allow only images and PDFs.
|
|
39
|
+
*/
|
|
40
|
+
accept?: string[];
|
|
41
|
+
/** Only for `media` fields. Maximum upload size in bytes. */
|
|
42
|
+
maxSize?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Whether the field is stored per locale (`name_pt`, `name_en`). When
|
|
45
|
+
* omitted it defaults to `true` for editorial types (`text`, `textarea`,
|
|
46
|
+
* `email`, `list`) and `false` for structural ones. Set explicitly to force
|
|
47
|
+
* a text field (e.g. `slug`, `icon`) to stay language-independent.
|
|
48
|
+
*/
|
|
49
|
+
localized?: boolean;
|
|
50
|
+
defaultValue?: string | number | boolean;
|
|
51
|
+
};
|
|
52
|
+
export type CollectionAccess = {
|
|
53
|
+
/**
|
|
54
|
+
* Who can read (GET) this collection's API. Defaults to `"public"`.
|
|
55
|
+
*/
|
|
56
|
+
read?: "public" | "private";
|
|
57
|
+
/**
|
|
58
|
+
* Who can create (POST) items. Defaults to `"private"`.
|
|
59
|
+
*/
|
|
60
|
+
create?: "public" | "private";
|
|
61
|
+
/**
|
|
62
|
+
* Who can update (PATCH/PUT) items. Defaults to `"private"`.
|
|
63
|
+
*/
|
|
64
|
+
update?: "public" | "private";
|
|
65
|
+
/**
|
|
66
|
+
* Who can delete (DELETE) items. Defaults to `"private"`.
|
|
67
|
+
*/
|
|
68
|
+
delete?: "public" | "private";
|
|
69
|
+
};
|
|
70
|
+
export type CollectionDef = {
|
|
71
|
+
slug: string;
|
|
72
|
+
label: string;
|
|
73
|
+
/** Optional Feather icon name (see `src/components/admin/collection-icon.tsx`). */
|
|
74
|
+
icon?: CollectionIconName;
|
|
75
|
+
description?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Per-operation access control for the generated REST API.
|
|
78
|
+
* `read` defaults to `"public"`; `create`, `update` and `delete` default to
|
|
79
|
+
* `"private"`.
|
|
80
|
+
*/
|
|
81
|
+
access?: CollectionAccess;
|
|
82
|
+
/**
|
|
83
|
+
* A standalone collection holds exactly one record (no list view). The admin
|
|
84
|
+
* edits the single item directly and the API exposes it at
|
|
85
|
+
* the configured API path via `GET`/`PATCH`.
|
|
86
|
+
*/
|
|
87
|
+
standalone?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Locales this collection stores, in tab order (e.g. `["pt", "en"]`).
|
|
90
|
+
* Drives the per-locale columns generated in the table. Defaults to the
|
|
91
|
+
* global `defaultLocale` when omitted.
|
|
92
|
+
*/
|
|
93
|
+
locales?: string[];
|
|
94
|
+
/**
|
|
95
|
+
* Ordered list of columns shown in the list view. Accepts field names plus
|
|
96
|
+
* the special `id`, `createdAt` and `updatedAt`. When omitted, all fields
|
|
97
|
+
* plus the managed timestamps are shown.
|
|
98
|
+
*/
|
|
99
|
+
list?: string[];
|
|
100
|
+
fields: FieldDef[];
|
|
101
|
+
};
|
|
102
|
+
export type SchemaDef = {
|
|
103
|
+
title?: string;
|
|
104
|
+
collections: CollectionDef[];
|
|
105
|
+
};
|
|
106
|
+
/** A single entry of a `media-list` field. */
|
|
107
|
+
export type MediaItem = {
|
|
108
|
+
url: string;
|
|
109
|
+
alt?: string;
|
|
110
|
+
/** At most one item per list is the principal (cover/highlight). */
|
|
111
|
+
primary?: boolean;
|
|
112
|
+
};
|
|
113
|
+
/** An ordered list of translated values, keyed by locale code. */
|
|
114
|
+
export type LocalizedValue = {
|
|
115
|
+
[locale: string]: FieldValue;
|
|
116
|
+
};
|
|
117
|
+
/** Primitive value a field can hold. Relation values are numeric ids. */
|
|
118
|
+
export type FieldValue = string | number | boolean | null | number[] | string[] | MediaItem[] | LocalizedValue;
|
|
119
|
+
/** An option shown by a relation control and used to render labels. */
|
|
120
|
+
export type RelationOption = {
|
|
121
|
+
id: number;
|
|
122
|
+
label: string;
|
|
123
|
+
};
|
|
124
|
+
/** Map of field name to its resolved relation options. */
|
|
125
|
+
export type RelationOptionsMap = Record<string, RelationOption[]>;
|
|
126
|
+
/** A persisted row plus the two managed timestamps. */
|
|
127
|
+
export type Item = {
|
|
128
|
+
id: number;
|
|
129
|
+
createdAt: string;
|
|
130
|
+
updatedAt: string;
|
|
131
|
+
[key: string]: FieldValue | number;
|
|
132
|
+
};
|
|
133
|
+
export type ListResult = {
|
|
134
|
+
items: Item[];
|
|
135
|
+
total: number;
|
|
136
|
+
page: number;
|
|
137
|
+
limit: number;
|
|
138
|
+
totalPages: number;
|
|
139
|
+
};
|
|
140
|
+
/** Shape returned by the mutation endpoints. */
|
|
141
|
+
export type MutationResult = {
|
|
142
|
+
item: Item;
|
|
143
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CollectionDef, FieldValue } from "./types.js";
|
|
2
|
+
export type ValidationResult = {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
values: Record<string, FieldValue>;
|
|
5
|
+
errors: Record<string, string>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Validate and coerce a raw body into storable values.
|
|
9
|
+
*
|
|
10
|
+
* - `partial: false` (create): every field is resolved, falling back to its
|
|
11
|
+
* default when absent.
|
|
12
|
+
* - `partial: true` (update): only keys present in the body are touched.
|
|
13
|
+
*/
|
|
14
|
+
export declare function coerceInput(collection: CollectionDef, input: unknown, { partial }: {
|
|
15
|
+
partial: boolean;
|
|
16
|
+
}): ValidationResult;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { collectionLocales, fieldDefault, isLocalizedField, relationIsMany, } from "./schema.js";
|
|
2
|
+
import { isMediaUrl } from "./media.js";
|
|
3
|
+
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
4
|
+
function asRecord(input) {
|
|
5
|
+
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
6
|
+
return input;
|
|
7
|
+
}
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
10
|
+
function coerceField(field, raw) {
|
|
11
|
+
switch (field.type) {
|
|
12
|
+
case "boolean": {
|
|
13
|
+
const value = raw === true || raw === "true" || raw === 1 || raw === "1";
|
|
14
|
+
return { value };
|
|
15
|
+
}
|
|
16
|
+
case "number": {
|
|
17
|
+
if (raw === "" || raw === null || raw === undefined) {
|
|
18
|
+
if (field.required)
|
|
19
|
+
return { error: "Campo obrigatório." };
|
|
20
|
+
return { value: field.type === "number" ? (field.min ?? 0) : 0 };
|
|
21
|
+
}
|
|
22
|
+
const value = Number(raw);
|
|
23
|
+
if (Number.isNaN(value))
|
|
24
|
+
return { error: "Número inválido." };
|
|
25
|
+
if (field.min !== undefined && value < field.min) {
|
|
26
|
+
return { error: `Valor mínimo: ${field.min}.` };
|
|
27
|
+
}
|
|
28
|
+
if (field.max !== undefined && value > field.max) {
|
|
29
|
+
return { error: `Valor máximo: ${field.max}.` };
|
|
30
|
+
}
|
|
31
|
+
return { value };
|
|
32
|
+
}
|
|
33
|
+
case "select": {
|
|
34
|
+
const value = raw === null || raw === undefined ? "" : String(raw);
|
|
35
|
+
if (!value) {
|
|
36
|
+
if (field.required)
|
|
37
|
+
return { error: "Selecione uma opção." };
|
|
38
|
+
return { value: "" };
|
|
39
|
+
}
|
|
40
|
+
const allowed = (field.options ?? []).map((option) => option.value);
|
|
41
|
+
if (!allowed.includes(value))
|
|
42
|
+
return { error: "Opção inválida." };
|
|
43
|
+
return { value };
|
|
44
|
+
}
|
|
45
|
+
case "relation": {
|
|
46
|
+
const toId = (entry) => {
|
|
47
|
+
const source = entry && typeof entry === "object" && "id" in entry
|
|
48
|
+
? entry.id
|
|
49
|
+
: entry;
|
|
50
|
+
const id = Number(source);
|
|
51
|
+
return Number.isInteger(id) && id > 0 ? id : null;
|
|
52
|
+
};
|
|
53
|
+
if (relationIsMany(field)) {
|
|
54
|
+
const list = Array.isArray(raw)
|
|
55
|
+
? raw
|
|
56
|
+
: typeof raw === "string" && raw.trim()
|
|
57
|
+
? raw.split(",")
|
|
58
|
+
: [];
|
|
59
|
+
const ids = Array.from(new Set(list.map(toId).filter((id) => id !== null)));
|
|
60
|
+
if (field.required && ids.length === 0) {
|
|
61
|
+
return { error: "Selecione ao menos um item." };
|
|
62
|
+
}
|
|
63
|
+
return { value: ids };
|
|
64
|
+
}
|
|
65
|
+
if (raw === "" || raw === null || raw === undefined) {
|
|
66
|
+
if (field.required)
|
|
67
|
+
return { error: "Selecione um item." };
|
|
68
|
+
return { value: null };
|
|
69
|
+
}
|
|
70
|
+
const id = toId(raw);
|
|
71
|
+
if (id === null)
|
|
72
|
+
return { error: "Relacionamento inválido." };
|
|
73
|
+
return { value: id };
|
|
74
|
+
}
|
|
75
|
+
case "media": {
|
|
76
|
+
const value = raw === null || raw === undefined ? "" : String(raw).trim();
|
|
77
|
+
if (!value) {
|
|
78
|
+
if (field.required)
|
|
79
|
+
return { error: "Campo obrigatório." };
|
|
80
|
+
return { value: "" };
|
|
81
|
+
}
|
|
82
|
+
if (!isMediaUrl(value)) {
|
|
83
|
+
return { error: "Arquivo inválido." };
|
|
84
|
+
}
|
|
85
|
+
return { value };
|
|
86
|
+
}
|
|
87
|
+
case "media-list": {
|
|
88
|
+
const list = Array.isArray(raw) ? raw : [];
|
|
89
|
+
const media = [];
|
|
90
|
+
let primarySeen = false;
|
|
91
|
+
for (const entry of list) {
|
|
92
|
+
const record = (entry && typeof entry === "object"
|
|
93
|
+
? entry
|
|
94
|
+
: { url: entry });
|
|
95
|
+
const url = String(record.url ?? "").trim();
|
|
96
|
+
if (!url)
|
|
97
|
+
continue;
|
|
98
|
+
if (!isMediaUrl(url))
|
|
99
|
+
return { error: "Arquivo inválido." };
|
|
100
|
+
const primary = Boolean(record.primary) && !primarySeen;
|
|
101
|
+
if (primary)
|
|
102
|
+
primarySeen = true;
|
|
103
|
+
media.push({
|
|
104
|
+
url,
|
|
105
|
+
alt: typeof record.alt === "string" ? record.alt : "",
|
|
106
|
+
primary,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (field.required && media.length === 0) {
|
|
110
|
+
return { error: "Envie ao menos uma mídia." };
|
|
111
|
+
}
|
|
112
|
+
if (media.length > 0 && !primarySeen)
|
|
113
|
+
media[0].primary = true;
|
|
114
|
+
return { value: media };
|
|
115
|
+
}
|
|
116
|
+
case "list": {
|
|
117
|
+
const source = Array.isArray(raw)
|
|
118
|
+
? raw
|
|
119
|
+
: typeof raw === "string" && raw.trim()
|
|
120
|
+
? raw.split("\n")
|
|
121
|
+
: [];
|
|
122
|
+
const items = source.map((entry) => String(entry).trim()).filter(Boolean);
|
|
123
|
+
if (field.required && items.length === 0) {
|
|
124
|
+
return { error: "Adicione ao menos um item." };
|
|
125
|
+
}
|
|
126
|
+
return { value: items };
|
|
127
|
+
}
|
|
128
|
+
case "email": {
|
|
129
|
+
const value = raw === null || raw === undefined ? "" : String(raw).trim();
|
|
130
|
+
if (!value) {
|
|
131
|
+
if (field.required)
|
|
132
|
+
return { error: "Campo obrigatório." };
|
|
133
|
+
return { value: "" };
|
|
134
|
+
}
|
|
135
|
+
if (!EMAIL_RE.test(value))
|
|
136
|
+
return { error: "E-mail inválido." };
|
|
137
|
+
return { value };
|
|
138
|
+
}
|
|
139
|
+
default: {
|
|
140
|
+
const value = raw === null || raw === undefined ? "" : String(raw).trim();
|
|
141
|
+
if (!value && field.required)
|
|
142
|
+
return { error: "Campo obrigatório." };
|
|
143
|
+
return { value };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Validate and coerce a raw body into storable values.
|
|
149
|
+
*
|
|
150
|
+
* - `partial: false` (create): every field is resolved, falling back to its
|
|
151
|
+
* default when absent.
|
|
152
|
+
* - `partial: true` (update): only keys present in the body are touched.
|
|
153
|
+
*/
|
|
154
|
+
export function coerceInput(collection, input, { partial }) {
|
|
155
|
+
const body = asRecord(input);
|
|
156
|
+
const values = {};
|
|
157
|
+
const errors = {};
|
|
158
|
+
for (const field of collection.fields) {
|
|
159
|
+
const present = Object.prototype.hasOwnProperty.call(body, field.name);
|
|
160
|
+
if (partial && !present)
|
|
161
|
+
continue;
|
|
162
|
+
if (isLocalizedField(collection, field)) {
|
|
163
|
+
const rawObject = asRecord(present ? body[field.name] : {});
|
|
164
|
+
const value = {};
|
|
165
|
+
for (const locale of collectionLocales(collection)) {
|
|
166
|
+
const hasLocale = Object.prototype.hasOwnProperty.call(rawObject, locale);
|
|
167
|
+
if (partial && present && !hasLocale)
|
|
168
|
+
continue;
|
|
169
|
+
const fallback = fieldDefault(field);
|
|
170
|
+
const raw = hasLocale ? rawObject[locale] : fallback;
|
|
171
|
+
const result = coerceField(field, raw);
|
|
172
|
+
if ("error" in result) {
|
|
173
|
+
errors[`${field.name}_${locale}`] = result.error;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
value[locale] = result.value;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
values[field.name] = value;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const raw = present ? body[field.name] : fieldDefault(field);
|
|
183
|
+
const result = coerceField(field, raw);
|
|
184
|
+
if ("error" in result) {
|
|
185
|
+
errors[field.name] = result.error;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
values[field.name] = result.value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { ok: Object.keys(errors).length === 0, values, errors };
|
|
192
|
+
}
|
package/dist/locale.d.ts
ADDED
package/dist/locale.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale fallback shared by server and client code.
|
|
3
|
+
*
|
|
4
|
+
* Kept separate from `runtime.ts` so isomorphic helpers (schema labels, forms)
|
|
5
|
+
* can use the configured default locale without pulling the server runtime into
|
|
6
|
+
* the admin bundle.
|
|
7
|
+
*/
|
|
8
|
+
let defaultLocaleCode = "pt";
|
|
9
|
+
export function setDefaultLocale(code) {
|
|
10
|
+
defaultLocaleCode = code;
|
|
11
|
+
}
|
|
12
|
+
export function getDefaultLocale() {
|
|
13
|
+
return defaultLocaleCode;
|
|
14
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type CmsConfig, type CollectionDef, type ResolvedCmsConfig } from "./config.js";
|
|
2
|
+
export declare function configureCms(config: CmsConfig): ResolvedCmsConfig;
|
|
3
|
+
export declare function isCmsConfigured(): boolean;
|
|
4
|
+
export declare function getCmsConfig(): ResolvedCmsConfig;
|
|
5
|
+
export declare function getCollections(): CollectionDef[];
|
|
6
|
+
export declare function getCollection(slug: string): CollectionDef | undefined;
|
|
7
|
+
export declare function defaultLocale(): string;
|
|
8
|
+
export declare function localeLabel(code: string): string;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-wide runtime configuration.
|
|
3
|
+
*
|
|
4
|
+
* Server code must never import the host project's schemas directly. The
|
|
5
|
+
* middleware calls `configureCms(config)` once at boot and every module reads
|
|
6
|
+
* the resolved config through these helpers.
|
|
7
|
+
*/
|
|
8
|
+
import { setDefaultLocale } from "./locale.js";
|
|
9
|
+
import { resolveCmsConfig, } from "./config.js";
|
|
10
|
+
let current = null;
|
|
11
|
+
export function configureCms(config) {
|
|
12
|
+
current = resolveCmsConfig(config);
|
|
13
|
+
setDefaultLocale(current.defaultLocale);
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
export function isCmsConfigured() {
|
|
17
|
+
return current !== null;
|
|
18
|
+
}
|
|
19
|
+
export function getCmsConfig() {
|
|
20
|
+
if (!current) {
|
|
21
|
+
throw new Error("[serenojs] Runtime config is missing. Call createCmsMiddleware(config) from src/middleware/serenojs.ts.");
|
|
22
|
+
}
|
|
23
|
+
return current;
|
|
24
|
+
}
|
|
25
|
+
export function getCollections() {
|
|
26
|
+
return getCmsConfig().collections;
|
|
27
|
+
}
|
|
28
|
+
export function getCollection(slug) {
|
|
29
|
+
return getCollections().find((collection) => collection.slug === slug);
|
|
30
|
+
}
|
|
31
|
+
export function defaultLocale() {
|
|
32
|
+
return getCmsConfig().defaultLocale;
|
|
33
|
+
}
|
|
34
|
+
export function localeLabel(code) {
|
|
35
|
+
return (getCmsConfig().locales.find((locale) => locale.code === code)?.label ??
|
|
36
|
+
code.toUpperCase());
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Hono, type MiddlewareHandler } from "hono";
|
|
2
|
+
import type { CmsConfig } from "../config.js";
|
|
3
|
+
import type { AuthUser } from "../auth/config.js";
|
|
4
|
+
type Env = {
|
|
5
|
+
Variables: {
|
|
6
|
+
user: AuthUser;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
declare function buildApp(): Hono<Env>;
|
|
10
|
+
/**
|
|
11
|
+
* Waku middleware factory.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // src/middleware/serenojs.ts
|
|
15
|
+
* import config from "../../serenojs.config.js";
|
|
16
|
+
* import { createCmsMiddleware } from "@eutobias/serenojs/server";
|
|
17
|
+
* export default createCmsMiddleware(config);
|
|
18
|
+
*/
|
|
19
|
+
export declare function createCmsMiddleware(config: CmsConfig): (_opts?: unknown) => MiddlewareHandler;
|
|
20
|
+
export { buildApp as createCmsApp };
|