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