@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.
Files changed (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +682 -2
  3. package/admin-dist/assets/index-B3EgqtT6.js +9 -0
  4. package/admin-dist/assets/index-D2m0Eb4k.css +2 -0
  5. package/admin-dist/index.html +17 -0
  6. package/dist/auth/auth.server.d.ts +31 -0
  7. package/dist/auth/auth.server.js +141 -0
  8. package/dist/auth/config.d.ts +32 -0
  9. package/dist/auth/config.js +25 -0
  10. package/dist/auth/cookies.server.d.ts +7 -0
  11. package/dist/auth/cookies.server.js +65 -0
  12. package/dist/auth/http.server.d.ts +8 -0
  13. package/dist/auth/http.server.js +30 -0
  14. package/dist/auth/password.server.d.ts +2 -0
  15. package/dist/auth/password.server.js +21 -0
  16. package/dist/auth/session.server.d.ts +2 -0
  17. package/dist/auth/session.server.js +14 -0
  18. package/dist/auth/users.server.d.ts +16 -0
  19. package/dist/auth/users.server.js +123 -0
  20. package/dist/config.d.ts +91 -0
  21. package/dist/config.js +39 -0
  22. package/dist/core/backup.server.d.ts +52 -0
  23. package/dist/core/backup.server.js +349 -0
  24. package/dist/core/db.server.d.ts +24 -0
  25. package/dist/core/db.server.js +526 -0
  26. package/dist/core/media.d.ts +22 -0
  27. package/dist/core/media.js +76 -0
  28. package/dist/core/media.server.d.ts +18 -0
  29. package/dist/core/media.server.js +102 -0
  30. package/dist/core/schema.d.ts +26 -0
  31. package/dist/core/schema.js +99 -0
  32. package/dist/core/types.d.ts +143 -0
  33. package/dist/core/types.js +1 -0
  34. package/dist/core/validation.d.ts +16 -0
  35. package/dist/core/validation.js +192 -0
  36. package/dist/locale.d.ts +2 -0
  37. package/dist/locale.js +14 -0
  38. package/dist/runtime.d.ts +8 -0
  39. package/dist/runtime.js +37 -0
  40. package/dist/server/app.d.ts +20 -0
  41. package/dist/server/app.js +661 -0
  42. package/dist/ui/admin/admin-shell.d.ts +16 -0
  43. package/dist/ui/admin/admin-shell.js +38 -0
  44. package/dist/ui/admin/backup-manager.d.ts +5 -0
  45. package/dist/ui/admin/backup-manager.js +165 -0
  46. package/dist/ui/admin/collection-icon.d.ts +22 -0
  47. package/dist/ui/admin/collection-icon.js +34 -0
  48. package/dist/ui/admin/collection-manager.d.ts +7 -0
  49. package/dist/ui/admin/collection-manager.js +212 -0
  50. package/dist/ui/admin/display.d.ts +7 -0
  51. package/dist/ui/admin/display.js +65 -0
  52. package/dist/ui/admin/field-control.d.ts +14 -0
  53. package/dist/ui/admin/field-control.js +58 -0
  54. package/dist/ui/admin/i18n.d.ts +27 -0
  55. package/dist/ui/admin/i18n.js +324 -0
  56. package/dist/ui/admin/item-form.d.ts +20 -0
  57. package/dist/ui/admin/item-form.js +60 -0
  58. package/dist/ui/admin/list-control.d.ts +13 -0
  59. package/dist/ui/admin/list-control.js +31 -0
  60. package/dist/ui/admin/locales.d.ts +4 -0
  61. package/dist/ui/admin/locales.js +8 -0
  62. package/dist/ui/admin/login-form.d.ts +3 -0
  63. package/dist/ui/admin/login-form.js +52 -0
  64. package/dist/ui/admin/media-control.d.ts +14 -0
  65. package/dist/ui/admin/media-control.js +74 -0
  66. package/dist/ui/admin/media-list-control.d.ts +14 -0
  67. package/dist/ui/admin/media-list-control.js +91 -0
  68. package/dist/ui/admin/pagination.d.ts +11 -0
  69. package/dist/ui/admin/pagination.js +14 -0
  70. package/dist/ui/admin/relation-control.d.ts +14 -0
  71. package/dist/ui/admin/relation-control.js +33 -0
  72. package/dist/ui/admin/router.d.ts +17 -0
  73. package/dist/ui/admin/router.js +58 -0
  74. package/dist/ui/admin/runtime.d.ts +28 -0
  75. package/dist/ui/admin/runtime.js +29 -0
  76. package/dist/ui/admin/sidebar.d.ts +13 -0
  77. package/dist/ui/admin/sidebar.js +29 -0
  78. package/dist/ui/admin/standalone-manager.d.ts +11 -0
  79. package/dist/ui/admin/standalone-manager.js +51 -0
  80. package/dist/ui/admin/table.d.ts +21 -0
  81. package/dist/ui/admin/table.js +23 -0
  82. package/dist/ui/admin/toast.d.ts +16 -0
  83. package/dist/ui/admin/toast.js +59 -0
  84. package/dist/ui/admin/users-manager.d.ts +6 -0
  85. package/dist/ui/admin/users-manager.js +157 -0
  86. package/dist/ui/badge.d.ts +9 -0
  87. package/dist/ui/badge.js +14 -0
  88. package/dist/ui/box.d.ts +17 -0
  89. package/dist/ui/box.js +14 -0
  90. package/dist/ui/button.d.ts +25 -0
  91. package/dist/ui/button.js +28 -0
  92. package/dist/ui/card.d.ts +45 -0
  93. package/dist/ui/card.js +49 -0
  94. package/dist/ui/container.d.ts +13 -0
  95. package/dist/ui/container.js +13 -0
  96. package/dist/ui/drawer.d.ts +15 -0
  97. package/dist/ui/drawer.js +31 -0
  98. package/dist/ui/field-wrapper.d.ts +15 -0
  99. package/dist/ui/field-wrapper.js +11 -0
  100. package/dist/ui/index.d.ts +8 -0
  101. package/dist/ui/index.js +8 -0
  102. package/dist/ui/text.d.ts +19 -0
  103. package/dist/ui/text.js +40 -0
  104. package/package.json +81 -3
  105. package/src/admin/app.tsx +371 -0
  106. package/src/admin/index.html +16 -0
  107. package/src/admin/main.tsx +42 -0
  108. package/src/admin/styles.css +9 -0
  109. package/src/auth/auth.server.ts +203 -0
  110. package/src/auth/config.ts +46 -0
  111. package/src/auth/cookies.server.ts +84 -0
  112. package/src/auth/http.server.ts +32 -0
  113. package/src/auth/password.server.ts +25 -0
  114. package/src/auth/session.server.ts +16 -0
  115. package/src/auth/users.server.ts +159 -0
  116. package/src/config.ts +120 -0
  117. package/src/core/backup.server.ts +446 -0
  118. package/src/core/db.server.ts +685 -0
  119. package/src/core/media.server.ts +136 -0
  120. package/src/core/media.ts +91 -0
  121. package/src/core/schema.ts +126 -0
  122. package/src/core/types.ts +194 -0
  123. package/src/core/validation.ts +215 -0
  124. package/src/global.d.ts +1 -0
  125. package/src/locale.ts +16 -0
  126. package/src/runtime.ts +54 -0
  127. package/src/server/app.ts +737 -0
  128. package/src/ui/admin/admin-shell.tsx +106 -0
  129. package/src/ui/admin/backup-manager.tsx +327 -0
  130. package/src/ui/admin/collection-icon.tsx +69 -0
  131. package/src/ui/admin/collection-manager.tsx +408 -0
  132. package/src/ui/admin/display.tsx +138 -0
  133. package/src/ui/admin/field-control.tsx +214 -0
  134. package/src/ui/admin/i18n.tsx +401 -0
  135. package/src/ui/admin/item-form.tsx +200 -0
  136. package/src/ui/admin/list-control.tsx +121 -0
  137. package/src/ui/admin/locales.ts +12 -0
  138. package/src/ui/admin/login-form.tsx +98 -0
  139. package/src/ui/admin/media-control.tsx +161 -0
  140. package/src/ui/admin/media-list-control.tsx +242 -0
  141. package/src/ui/admin/pagination.tsx +58 -0
  142. package/src/ui/admin/relation-control.tsx +142 -0
  143. package/src/ui/admin/router.tsx +105 -0
  144. package/src/ui/admin/runtime.tsx +58 -0
  145. package/src/ui/admin/sidebar.tsx +178 -0
  146. package/src/ui/admin/standalone-manager.tsx +103 -0
  147. package/src/ui/admin/table.tsx +129 -0
  148. package/src/ui/admin/toast.tsx +126 -0
  149. package/src/ui/admin/users-manager.tsx +344 -0
  150. package/src/ui/badge.tsx +27 -0
  151. package/src/ui/box.tsx +33 -0
  152. package/src/ui/button.tsx +77 -0
  153. package/src/ui/card.tsx +92 -0
  154. package/src/ui/container.tsx +28 -0
  155. package/src/ui/drawer.tsx +104 -0
  156. package/src/ui/field-wrapper.tsx +54 -0
  157. package/src/ui/index.ts +8 -0
  158. package/src/ui/text.tsx +93 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tobias Taurian
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.
package/README.md CHANGED
@@ -1,3 +1,683 @@
1
- # Temporary Holding Version
1
+ # SerenoJS
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Package: `@eutobias/serenojs`
4
+
5
+ A schema-driven CMS for [Waku](https://waku.gg) (React 19 RSC +
6
+ file-system routing). The admin panel, REST API, form validation and SQLite
7
+ schema are generated from collection descriptors.
8
+
9
+ The package ships:
10
+
11
+ - an auto-generated **admin SPA** with list/search/pagination, create/edit
12
+ drawers, standalone editors, users and backups;
13
+ - a **REST API** with per-operation access control;
14
+ - **cookie authentication** with rotating refresh tokens and user management;
15
+ - **media uploads** and a safe file endpoint;
16
+ - **backup/restore** with an optional periodic scheduler;
17
+ - a **Hono middleware** that mounts everything in one line.
18
+
19
+ It works as a regular CMS with the bundled admin, or as a **headless CMS**
20
+ consumed by any frontend.
21
+
22
+ ## Features
23
+
24
+ - **Schema-driven** — the admin, API validation and SQLite schema come from
25
+ `CollectionDef` descriptors.
26
+ - **Configurable, hideable admin** — mount path, enable/disable and `noindex`
27
+ are configuration options. Disabling the admin returns `404` for the panel and
28
+ its assets.
29
+ - **Admin UI language selector** — English (default) and Portuguese (BR),
30
+ persisted in `localStorage`. Only admin UI strings are translated; collection
31
+ and field labels from the schema are not translated.
32
+ - **Headless-ready** — the REST API is the content delivery layer; the admin is
33
+ only an editing client. Any frontend or stack can consume the content.
34
+ - **Rich field types** — text, e-mail, textarea, number, boolean, select, date,
35
+ relation, media, media-list and list.
36
+ - **Content localization (i18n)** — localized fields stored per locale
37
+ (`name_pt`, `name_en`, ...) with nested API/form values.
38
+ - **Relations** — `one-to-one`, `one-to-many` and `many-to-many`, with cascade
39
+ cleanup on delete.
40
+ - **Authentication** — opaque cookie sessions (no JWT), rotating refresh tokens,
41
+ user management and CSRF protection for unsafe requests.
42
+ - **Media uploads** — type/size validation, safe file serving and media lists.
43
+ - **Backups** — consistent SQLite snapshot plus assets in a single archive, with
44
+ restore and an optional periodic scheduler.
45
+
46
+ ## Stack / requirements
47
+
48
+ - Waku `1.0.0-rc.1`, React `~19.3`, `react-server-dom-webpack`
49
+ - Hono for the API/auth/admin middleware
50
+ - Admin SPA: Vite + React 19 + Tailwind CSS v4 (prebuilt in this package)
51
+ - `node:sqlite` (`DatabaseSync`), no ORM
52
+ - `react-icons` (Feather icons) in the admin
53
+ - TypeScript
54
+
55
+ **Node:** `>=22.15` (required for `node:sqlite`).
56
+
57
+ Peer dependencies: `waku`, `react`, `react-dom`.
58
+
59
+ ## Getting started
60
+
61
+ ### Create a new project (recommended)
62
+
63
+ The fastest way to start is the official starter. It generates a Waku app with
64
+ the admin panel, REST API, auth, media, backups, SQLite configuration, a small
65
+ todos example app and agent skills already wired up.
66
+
67
+ ```sh
68
+ npm create start-serenojs@latest my-app
69
+ # or
70
+ pnpm create start-serenojs my-app
71
+ # or
72
+ yarn create start-serenojs my-app
73
+ # or
74
+ bun create start-serenojs my-app
75
+ ```
76
+
77
+ The CLI prompts for the directory and asks whether to install dependencies and
78
+ initialize a git repository. For automation it accepts `--yes`, `--no-install`,
79
+ `--no-git` and `--pm <pnpm|npm|yarn|bun>`. See the
80
+ [`create-start-serenojs`](https://github.com/eutobias/serenojs/tree/main/packages/create-start-serenojs)
81
+ package for the full option list.
82
+
83
+ Then start the app:
84
+
85
+ ```sh
86
+ cd my-app
87
+ pnpm dev
88
+ ```
89
+
90
+ Open <http://localhost:3000/admin> and sign in with `admin@example.com` /
91
+ `admin123`. The example app is at <http://localhost:3000>.
92
+
93
+ The generated project points at the published `@eutobias/serenojs` range, so
94
+ there is nothing else to install manually.
95
+
96
+ ### Add to an existing project (manual)
97
+
98
+ If you already have a Waku app and want to add the CMS yourself:
99
+
100
+ ```sh
101
+ pnpm add @eutobias/serenojs
102
+ ```
103
+
104
+ The package is ESM-only and ships compiled `dist/` plus the prebuilt
105
+ `admin-dist/` SPA. Continue with the [Setup](#setup) steps below to create
106
+ `serenojs.config.ts` and mount the middleware.
107
+
108
+ ## Setup
109
+
110
+ ### 1. Create `serenojs.config.ts`
111
+
112
+ ```ts
113
+ import { defineCmsConfig } from "@eutobias/serenojs/config";
114
+
115
+ export default defineCmsConfig({
116
+ title: "My CMS",
117
+ collections: [/* CollectionDef[] */],
118
+ admin: { path: "/admin", enabled: true, noindex: true },
119
+ api: { path: "/api" },
120
+ auth: { path: "/auth" },
121
+ media: { path: "/media" },
122
+ files: { path: "/files" },
123
+ backup: { path: "/backup", enabled: true, intervalHours: 24, keep: 10 },
124
+ db: { path: ".data/admin.db" },
125
+ });
126
+ ```
127
+
128
+ All paths are configurable. The package defaults are exactly the values above;
129
+ change `api.path` to `/content` or any other prefix if `/api` conflicts with
130
+ existing host routes.
131
+
132
+ ### 2. Mount the middleware
133
+
134
+ Create `src/middleware/serenojs.ts`:
135
+
136
+ ```ts
137
+ import config from "../../serenojs.config";
138
+ import { createCmsMiddleware } from "@eutobias/serenojs/server";
139
+
140
+ export default createCmsMiddleware(config);
141
+ ```
142
+
143
+ Waku auto-loads every module in `src/middleware`. Requests under the configured
144
+ admin/API/auth/media/files/backup prefixes are answered by the package; every
145
+ other request falls through to the host's Waku pages.
146
+
147
+ ### 3. Build the host
148
+
149
+ The admin SPA is already built inside the package. You do **not** need to add
150
+ Tailwind scanning for the package. Run your normal Waku build:
151
+
152
+ ```sh
153
+ pnpm build
154
+ pnpm start
155
+ ```
156
+
157
+ ### First login
158
+
159
+ 1. Start the host app and open `/admin` (or your configured `admin.path`).
160
+ 2. Sign in with the seeded account:
161
+ - **E-mail:** `admin@example.com`
162
+ - **Password:** `admin123`
163
+
164
+ The SQLite database is created at `.data/admin.db` and migrated automatically on
165
+ first use. The admin user is seeded only when the auth table is empty, so set
166
+ `ADMIN_EMAIL` and `ADMIN_PASSWORD` **before the first boot** to override the
167
+ defaults.
168
+
169
+ ### Hiding the admin
170
+
171
+ ```ts
172
+ admin: { path: "/painel", enabled: true, noindex: true }
173
+ ```
174
+
175
+ Set `enabled: false` to return `404` for the admin shell and
176
+ `/_serenojs/admin/*` assets. `noindex: true` sends `X-Robots-Tag: noindex,
177
+ nofollow` from the package (the shell also contains a robots meta tag).
178
+
179
+ This is a discoverability layer, not a security boundary: the SPA shell is
180
+ public, while private APIs and admin data remain protected by authentication.
181
+
182
+ ## Headless usage
183
+
184
+ The admin is only an editing client; the REST API is the delivery layer. You can
185
+ run the package as a **headless CMS** and consume the API from any frontend or
186
+ service — Next.js, Astro, SvelteKit, mobile apps or a static site generator.
187
+
188
+ ```ts
189
+ const response = await fetch("https://cms.example.com/api/posts?limit=20");
190
+ const { items } = await response.json();
191
+ ```
192
+
193
+ Set `access.read` to `"public"` for content the frontend should read
194
+ anonymously, and keep `create`/`update`/`delete` private. The API is same-origin
195
+ by default; add CORS at the host/proxy layer if a browser frontend consumes it
196
+ cross-origin. Server-side consumers can call the API directly.
197
+
198
+ Media values are returned as URLs (`/files/...` or `/images/...`), so a frontend
199
+ can render them directly. For localized content, parse the nested values:
200
+
201
+ ```json
202
+ { "title": { "pt": "Olá", "en": "Hello" } }
203
+ ```
204
+
205
+ ## Configuration
206
+
207
+ | Option | Default | Description |
208
+ |---|---|---|
209
+ | `title` | `"SerenoJS"` | Admin title |
210
+ | `collections` | — | Array of `CollectionDef` |
211
+ | `locales` | `pt`, `en` | Locale registry used by admin content tabs |
212
+ | `defaultLocale` | `"pt"` | Fallback/storage locale |
213
+ | `admin.path` | `"/admin"` | Admin SPA mount path |
214
+ | `admin.enabled` | `true` | `false` returns 404 for the panel and assets |
215
+ | `admin.noindex` | `true` | Send noindex headers/meta tags |
216
+ | `api.path` | `"/api"` | REST API mount path |
217
+ | `auth.path` | `"/auth"` | Auth API mount path |
218
+ | `media.path` | `"/media"` | Upload/delete endpoint |
219
+ | `files.path` | `"/files"` | Served uploads |
220
+ | `backup.path` | `"/backup"` | Backup API |
221
+ | `backup.enabled` | `true` | Enable the periodic backup scheduler |
222
+ | `backup.intervalHours` | `24` | Scheduler interval |
223
+ | `backup.keep` | `10` | Number of stored backups to keep |
224
+ | `db.path` | `".data/admin.db"` | SQLite file path |
225
+
226
+ The package also reads these environment variables:
227
+
228
+ | Variable | Effect |
229
+ |---|---|
230
+ | `ADMIN_DB_PATH` | Overrides `db.path` |
231
+ | `ADMIN_EMAIL` | Initial admin e-mail (first boot only) |
232
+ | `ADMIN_PASSWORD` | Initial admin password (first boot only) |
233
+ | `ADMIN_BACKUP` | `"false"` disables the scheduler |
234
+ | `ADMIN_BACKUP_INTERVAL_HOURS` | Scheduler interval |
235
+ | `ADMIN_BACKUP_KEEP` | Number of backups to retain |
236
+ | `AUTH_COOKIE_SECURE` | `"true"` / `"false"` forces the cookie `Secure` flag |
237
+
238
+ Path options can be wired to your own environment variables in
239
+ `serenojs.config.ts`.
240
+
241
+ ## Schema documentation
242
+
243
+ Each collection is a `CollectionDef` exported from a schema file and collected
244
+ into an array:
245
+
246
+ ```ts
247
+ import type { CollectionDef } from "@eutobias/serenojs/config";
248
+
249
+ export const posts: CollectionDef = {
250
+ slug: "posts",
251
+ label: "Posts",
252
+ icon: "fileText",
253
+ description: "Blog posts",
254
+ access: {
255
+ read: "public",
256
+ create: "private",
257
+ update: "private",
258
+ delete: "private",
259
+ },
260
+ locales: ["pt", "en"],
261
+ list: ["id", "title", "publishedAt", "createdAt"],
262
+ fields: [
263
+ { name: "title", label: "Title", type: "text", required: true },
264
+ { name: "slug", label: "Slug", type: "text", required: true, localized: false },
265
+ { name: "body", label: "Body", type: "textarea" },
266
+ { name: "publishedAt", label: "Published at", type: "date" },
267
+ { name: "published", label: "Published", type: "boolean", defaultValue: false },
268
+ { name: "cover", label: "Cover", type: "media", accept: ["image/*"] },
269
+ ],
270
+ };
271
+ ```
272
+
273
+ ```ts
274
+ // src/schemas/index.ts
275
+ import type { CollectionDef } from "@eutobias/serenojs/config";
276
+ import { posts } from "./posts";
277
+
278
+ export const collections: CollectionDef[] = [posts];
279
+ ```
280
+
281
+ ### `CollectionDef`
282
+
283
+ | Property | Type | Default | Description |
284
+ |---|---|---|---|
285
+ | `slug` | `string` | — | Unique id; used in the API URL and table name (`c_<slug>`) |
286
+ | `label` | `string` | — | Admin label (not translated by the UI language selector) |
287
+ | `icon` | `CollectionIconName?` | `grid` | Feather icon from the admin registry |
288
+ | `description` | `string?` | — | Optional admin help text |
289
+ | `access` | `CollectionAccess?` | see below | Per-operation API access |
290
+ | `standalone` | `boolean?` | `false` | Exactly one record; no table/create/delete |
291
+ | `locales` | `string[]?` | `[defaultLocale]` | Content locales stored by this collection |
292
+ | `list` | `string[]?` | all fields | Admin list columns; also accepts `id`, `createdAt`, `updatedAt` |
293
+ | `fields` | `FieldDef[]` | — | Field definitions |
294
+
295
+ Available icons: `archive`, `box`, `fileText`, `folder`, `grid`, `image`,
296
+ `layout`, `settings`, `star`, `tag`, `user`, `users`.
297
+
298
+ ### `FieldDef`
299
+
300
+ | Property | Type | Applies to | Description |
301
+ |---|---|---|---|
302
+ | `name` | `string` | all | Stable key and column name |
303
+ | `label` | `string` | all | Admin label (schema-defined, not UI-translated) |
304
+ | `type` | `FieldType` | all | One of the types below |
305
+ | `required` | `boolean?` | all | Required per locale for localized fields |
306
+ | `placeholder` | `string?` | all | Input placeholder |
307
+ | `help` | `string?` | all | Help text below the input |
308
+ | `localized` | `boolean?` | all | Overrides the type's default localization |
309
+ | `defaultValue` | `string \| number \| boolean?` | all | Initial value |
310
+ | `options` | `{ label, value }[]?` | `select` | Allowed options |
311
+ | `min` / `max` | `number?` | `number` | Numeric range |
312
+ | `relation` | `{ to, type }?` | `relation` | Target collection and relation type |
313
+ | `accept` | `string[]?` | `media`, `media-list` | MIME types and/or extensions |
314
+ | `maxSize` | `number?` | `media`, `media-list` | Maximum upload size in bytes |
315
+
316
+ ### Field types
317
+
318
+ | Type | Value | Localized by default | Notes |
319
+ |---|---|---|---|
320
+ | `text` | string | yes | Single-line text |
321
+ | `email` | string | yes | Validated e-mail |
322
+ | `textarea` | string | yes | Multi-line text |
323
+ | `number` | number | no | Uses `min`/`max` |
324
+ | `boolean` | boolean | no | Stored as INTEGER `0`/`1` |
325
+ | `select` | string | no | Requires `options` |
326
+ | `date` | string | no | Date input |
327
+ | `relation` | number / number[] | no | See relations below |
328
+ | `media` | string URL | no | One upload (`/files/...` or `/images/...`) |
329
+ | `media-list` | `{ url, alt?, primary? }[]` | no | Ordered list; at most one `primary` |
330
+ | `list` | string[] | yes | Ordered list of short strings |
331
+
332
+ Set `localized: false` on a `text` field to keep it language-independent (for
333
+ example a slug).
334
+
335
+ ### Access control
336
+
337
+ `access` maps HTTP operations to `"public"` or `"private"`:
338
+
339
+ | Key | Methods | Default |
340
+ |---|---|---|
341
+ | `read` | `GET`, `HEAD`, `OPTIONS` | `public` |
342
+ | `create` | `POST` | `private` |
343
+ | `update` | `PATCH`, `PUT` | `private` |
344
+ | `delete` | `DELETE` | `private` |
345
+
346
+ `private` requires a valid session cookie. Omit `access` to use these defaults.
347
+
348
+ ### Content localization
349
+
350
+ - Localized by default: `text`, `textarea`, `email`, `list`.
351
+ - Never localized: `number`, `boolean`, `date`, `select`, `relation`, `media`,
352
+ `media-list`.
353
+ - Storage uses one column per locale: `title_pt`, `title_en`, etc.
354
+ - API and form values are nested:
355
+
356
+ ```json
357
+ {
358
+ "title": { "pt": "Olá", "en": "Hello" }
359
+ }
360
+ ```
361
+
362
+ - Partial updates only touch the locales present in the payload.
363
+ - `required` is enforced in every locale. Validation errors are keyed
364
+ `<field>_<locale>` (for example `title_en`).
365
+
366
+ ### Relations
367
+
368
+ ```ts
369
+ relation: { to: "authors", type: "one-to-one" }
370
+ relation: { to: "tags", type: "many-to-many" }
371
+ ```
372
+
373
+ - `one-to-one` stores a single target id in an INTEGER column.
374
+ - `one-to-many` and `many-to-many` use a join table named
375
+ `r_<collection>_<field>`.
376
+ - `to` must be the slug of another registered collection.
377
+ - Deleting an item removes join rows and clears references from other
378
+ collections.
379
+
380
+ ### Standalone collections
381
+
382
+ A standalone collection (`standalone: true`) has exactly one record. The admin
383
+ renders a single inline form, and the API exposes:
384
+
385
+ - `GET /api/<collection>` → `{ item }`
386
+ - `PATCH /api/<collection>` → `{ item }`
387
+
388
+ `POST` and `DELETE` return `405` for standalone collections.
389
+
390
+ ### Database migration
391
+
392
+ The SQLite schema is created and migrated lazily on first open. Missing tables
393
+ and columns are added automatically; existing data is not dropped. Legacy
394
+ single-column localized data is renamed to `<field>_<defaultLocale>` during
395
+ migration.
396
+
397
+ ## REST API
398
+
399
+ Default mount points:
400
+
401
+ - CMS API: `/api`
402
+ - Auth API: `/auth`
403
+ - Media uploads: `/media`
404
+ - Served files: `/files/*`
405
+ - Backups: `/backup`
406
+ - Admin: `/admin` (assets at `/_serenojs/admin/*`)
407
+
408
+ JSON is used for requests and responses, except for file downloads/uploads.
409
+ Errors use:
410
+
411
+ ```json
412
+ { "message": "Dados inválidos.", "errors": { "title_en": "Campo obrigatório." } }
413
+ ```
414
+
415
+ Common status codes: `400`, `401`, `403`, `404`, `405`, `409`, `413`, `422`,
416
+ `500`.
417
+
418
+ ### CMS endpoints
419
+
420
+ | Method | Path | Description |
421
+ |---|---|---|
422
+ | `GET` | `/api/:collection` | List items (`?page=1&limit=10&q=search`) |
423
+ | `POST` | `/api/:collection` | Create an item |
424
+ | `GET` | `/api/:collection/:id` | Read one item |
425
+ | `PATCH` / `PUT` | `/api/:collection/:id` | Partially update an item |
426
+ | `DELETE` | `/api/:collection/:id` | Delete an item |
427
+ | `GET` | `/api/:collection` | Standalone: read the single record |
428
+ | `PATCH` | `/api/:collection` | Standalone: update the single record |
429
+ | `GET` | `/api/:collection/relations` | Relation picker options (auth required) |
430
+
431
+ List response:
432
+
433
+ ```json
434
+ {
435
+ "items": [{ "id": 1, "createdAt": "...", "updatedAt": "...", "title": "..." }],
436
+ "total": 1,
437
+ "page": 1,
438
+ "limit": 10,
439
+ "totalPages": 1
440
+ }
441
+ ```
442
+
443
+ Mutations return `{ "item": { ... } }`; deletes return `{ "ok": true }`.
444
+ Relation options return `{ "options": { "author": [{ "id": 1, "label": "..." }] } }`.
445
+
446
+ ### Auth endpoints
447
+
448
+ | Method | Path | Description |
449
+ |---|---|---|
450
+ | `POST` | `/auth/login` | Body `{ email, password }`; sets session cookies |
451
+ | `POST` | `/auth/logout` | Revokes the session and clears cookies |
452
+ | `POST` | `/auth/refresh` | Rotates the refresh token |
453
+ | `GET` | `/auth/me` | Current user |
454
+ | `GET` | `/auth/users` | List users (auth) |
455
+ | `POST` | `/auth/users` | Create user (auth) |
456
+ | `GET` | `/auth/users/:id` | Read user (auth) |
457
+ | `PATCH` / `PUT` | `/auth/users/:id` | Update user (auth) |
458
+ | `DELETE` | `/auth/users/:id` | Delete user (auth; cannot delete self or the last user) |
459
+
460
+ Session cookies are `serenojs_access` (30 min) and `serenojs_refresh` (24 h), httpOnly and
461
+ `SameSite=Lax`. Login/refresh rotate the token pair, so only one session per
462
+ user is active. Unsafe cross-origin requests are rejected with `403`.
463
+
464
+ ### Media and files
465
+
466
+ | Method | Path | Description |
467
+ |---|---|---|
468
+ | `POST` | `/media?collection=<slug>&field=<name>` | Multipart upload, field name `file` (auth) |
469
+ | `DELETE` | `/media?url=/files/<name>` | Delete an uploaded file (auth) |
470
+ | `GET` | `/files/<name>` | Serve an uploaded file |
471
+
472
+ `accept` accepts MIME types (`image/png`, `image/*`) or extensions (`png`,
473
+ `.pdf`). When omitted, images and PDFs are allowed. HTML/script/executable
474
+ extensions are always blocked. The default maximum size is 10 MB unless
475
+ `maxSize` is set. `media` values must be `/files/...` or `/images/...`.
476
+
477
+ ### Backup endpoints
478
+
479
+ | Method | Path | Description |
480
+ |---|---|---|
481
+ | `GET` | `/backup` | List stored backups |
482
+ | `GET` | `/backup?name=<file>` | Download one backup |
483
+ | `GET` | `/backup?latest=1` | Create and download a fresh backup |
484
+ | `POST` | `/backup` | Create and store a backup |
485
+ | `PUT` | `/backup` | Restore a raw `.serenojs` archive body |
486
+ | `DELETE` | `/backup?name=<file>` | Delete a stored backup |
487
+
488
+ All backup endpoints require authentication. A backup contains a consistent
489
+ SQLite snapshot plus files under `public/images` and `public/files`. Restoring
490
+ replaces the live tables in a transaction and rewrites assets; it may invalidate
491
+ the current session.
492
+
493
+ ## API examples
494
+
495
+ The examples assume `http://localhost:3000` and the default paths. The shown
496
+ collection is named `posts`; replace it with your own slug. Writes use the
497
+ default `private` access, so they include the session cookie.
498
+
499
+ ### List and create
500
+
501
+ ```sh
502
+ curl "http://localhost:3000/api/posts?page=1&limit=10&q=test"
503
+
504
+ curl -b cookies.txt -X POST "http://localhost:3000/api/posts" \
505
+ -H "Content-Type: application/json" \
506
+ -d '{
507
+ "title": { "pt": "Olá", "en": "Hello" },
508
+ "slug": "hello",
509
+ "published": false
510
+ }'
511
+ ```
512
+
513
+ ### Read, update and delete
514
+
515
+ ```sh
516
+ curl "http://localhost:3000/api/posts/1"
517
+
518
+ curl -b cookies.txt -X PATCH "http://localhost:3000/api/posts/1" \
519
+ -H "Content-Type: application/json" \
520
+ -d '{ "published": true }'
521
+
522
+ curl -b cookies.txt -X DELETE "http://localhost:3000/api/posts/1"
523
+ ```
524
+
525
+ ### Login and authenticated requests
526
+
527
+ ```sh
528
+ curl -c cookies.txt -X POST "http://localhost:3000/auth/login" \
529
+ -H "Content-Type: application/json" \
530
+ -d '{ "email": "admin@example.com", "password": "admin123" }'
531
+
532
+ curl -b cookies.txt "http://localhost:3000/auth/me"
533
+ curl -b cookies.txt "http://localhost:3000/auth/users"
534
+ curl -b cookies.txt "http://localhost:3000/backup"
535
+ ```
536
+
537
+ ### Localized payload
538
+
539
+ ```sh
540
+ curl -X PATCH "http://localhost:3000/api/posts/1" \
541
+ -H "Content-Type: application/json" \
542
+ -b cookies.txt \
543
+ -d '{
544
+ "title": { "pt": "Sobre nós", "en": "About us" }
545
+ }'
546
+ ```
547
+
548
+ If the field has `localized: false`, send a flat value instead:
549
+ `{ "title": "About us" }`.
550
+
551
+ ### Standalone record
552
+
553
+ ```sh
554
+ curl -b cookies.txt "http://localhost:3000/api/settings"
555
+
556
+ curl -X PATCH "http://localhost:3000/api/settings" \
557
+ -H "Content-Type: application/json" \
558
+ -b cookies.txt \
559
+ -d '{ "siteName": "My site" }'
560
+ ```
561
+
562
+ ### Relation options
563
+
564
+ ```sh
565
+ curl -b cookies.txt "http://localhost:3000/api/posts/relations"
566
+ ```
567
+
568
+ ### Media upload
569
+
570
+ ```sh
571
+ curl -b cookies.txt -X POST \
572
+ "http://localhost:3000/media?collection=posts&field=cover" \
573
+ -F "file=@./cover.png"
574
+ ```
575
+
576
+ Response:
577
+
578
+ ```json
579
+ {
580
+ "media": {
581
+ "url": "/files/1720000000000-abc123.png",
582
+ "name": "1720000000000-abc123.png",
583
+ "size": 12345,
584
+ "type": "image/png"
585
+ }
586
+ }
587
+ ```
588
+
589
+ ### Backup
590
+
591
+ ```sh
592
+ curl -b cookies.txt "http://localhost:3000/backup"
593
+ curl -b cookies.txt -o backup.serenojs "http://localhost:3000/backup?latest=1"
594
+ curl -b cookies.txt -X PUT "http://localhost:3000/backup" --data-binary @backup.serenojs
595
+ ```
596
+
597
+ ## Storage
598
+
599
+ - Database: `.data/admin.db` (WAL mode; override with `ADMIN_DB_PATH` or
600
+ `db.path`).
601
+ - Uploads: `public/files/`.
602
+ - Backups: `.data/backups/`.
603
+ - The database and admin user are created automatically on first use.
604
+
605
+ ## Exports
606
+
607
+ | Subpath | Contents |
608
+ |---|---|
609
+ | `@eutobias/serenojs/config` | `defineCmsConfig`, `CollectionDef`, `FieldDef` and related types |
610
+ | `@eutobias/serenojs/runtime` | `configureCms`, `getCmsConfig`, `getCollections` (server) |
611
+ | `@eutobias/serenojs/server` | `createCmsMiddleware` |
612
+ | `@eutobias/serenojs/ui` | Base UI components (`Box`, `Text`, `Button`, ...) |
613
+
614
+ ## Notes
615
+
616
+ - The admin UI ships an English/Portuguese (BR) language selector. English is
617
+ the default; the choice is stored in `localStorage`. Schema labels are not
618
+ translated.
619
+ - The admin SPA is prebuilt (`admin-dist/`); host Tailwind does not need to scan
620
+ the package.
621
+ - The package is server-first: `dist/core/*.server.js` imports `node:sqlite`
622
+ and must never be imported by client code.
623
+ - `src/` is included for source maps/debugging; published code resolves from
624
+ `dist/`.
625
+
626
+ ## Testing
627
+
628
+ The package uses Node's built-in test runner (`node --test`) against source
629
+ modules compiled to `dist/`. No external services are required: the API test
630
+ creates a temporary SQLite database and cleans it up afterwards.
631
+
632
+ From the repository root (recommended, because it also builds the admin SPA):
633
+
634
+ ```sh
635
+ pnpm test # build admin + dist, then run all tests
636
+ pnpm test:coverage # same suite with Node coverage report
637
+ ```
638
+
639
+ From `packages/serenojs`, `pnpm test` compiles `dist/` and runs the suite. The
640
+ admin-shell tests are skipped automatically when `admin-dist/` has not been
641
+ built yet.
642
+
643
+ Coverage includes:
644
+
645
+ - configuration defaults and mount-path normalization;
646
+ - schema helpers, localized values and relation helpers;
647
+ - validation/coercion for field types, defaults, partial updates and errors;
648
+ - REST API integration: auth cookies, public/private access, CRUD, standalone
649
+ collections, relation options and error responses;
650
+ - refresh-token rotation and expired access-token rotation;
651
+ - user CRUD and validation;
652
+ - media upload/serve/delete, type/size limits and path traversal;
653
+ - backup create/list/download/restore/delete and format validation;
654
+ - admin shell/assets, history fallback, disable switch and `noindex` header.
655
+
656
+ ## Building and publishing (maintainers)
657
+
658
+ From the repository root:
659
+
660
+ ```sh
661
+ pnpm test # build admin + dist, run the test suite
662
+ pnpm pack:package # build everything and create the tarball
663
+ pnpm publish:package # build everything and publish to npm
664
+ ```
665
+
666
+ Or manually:
667
+
668
+ ```sh
669
+ pnpm build:package # tsc -> packages/serenojs/dist
670
+ pnpm build:admin # Vite -> packages/serenojs/admin-dist
671
+ cd packages/serenojs
672
+ npm pack --dry-run # inspect the tarball
673
+ npm publish # access: public is set in publishConfig
674
+ ```
675
+
676
+ Both `dist/` and `admin-dist/` must exist before packing/publishing. The package
677
+ `files` list includes `dist`, `admin-dist`, `src` and `README.md`; tests are
678
+ excluded. `publishConfig.access` is `public`, so the first publish of the scoped
679
+ package does not need `--access public` (passing it is harmless).
680
+
681
+ ## License
682
+
683
+ MIT