@seip/bluebird-js 0.1.7 → 0.2.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 (151) hide show
  1. package/AGENTS.md +727 -620
  2. package/README.md +670 -607
  3. package/app/models/user.js +19 -0
  4. package/app/resources/html/home.html +137 -0
  5. package/app/resources/html/layouts/main.html +55 -0
  6. package/app/resources/html/notFound.html +14 -0
  7. package/app/resources/html/table.html +15 -0
  8. package/app/resources/html/terms.html +46 -0
  9. package/app/resources/index.html +27 -0
  10. package/app/resources/layouts/AppLayout.jsx +92 -0
  11. package/app/resources/main.jsx +18 -0
  12. package/app/resources/pages/Dashboard.jsx +93 -0
  13. package/app/resources/pages/Home.jsx +140 -0
  14. package/app/resources/pages/Login.jsx +35 -0
  15. package/app/resources/pages/NotFound.jsx +16 -0
  16. package/app/resources/pages/Register.jsx +55 -0
  17. package/app/resources/pages/TableDemo.jsx +85 -0
  18. package/app/resources/routes.jsx +59 -0
  19. package/app/resources/spa.js +73 -0
  20. package/app/resources/styles.css +9 -0
  21. package/app/routes/api.js +129 -50
  22. package/app/routes/auth.js +209 -0
  23. package/app/routes/web.js +89 -47
  24. package/core/ai.js +30 -10
  25. package/core/app.d.ts +2 -18
  26. package/core/app.js +987 -801
  27. package/core/auth.d.ts +2 -13
  28. package/core/auth.js +444 -326
  29. package/core/cli/build.js +37 -0
  30. package/core/cli/dev.js +155 -0
  31. package/core/cli/doctor.js +37 -2
  32. package/core/cli/export.js +191 -0
  33. package/core/cli/init.js +91 -322
  34. package/core/cli/nginx.js +1 -1
  35. package/core/cli/templates.js +139 -0
  36. package/core/cli/view.js +98 -161
  37. package/core/config.js +44 -2
  38. package/core/csrf.js +156 -24
  39. package/core/database.js +1184 -1131
  40. package/core/debug.js +1 -1
  41. package/core/html.js +8 -1
  42. package/core/index.d.ts +126 -310
  43. package/core/paths.js +55 -0
  44. package/core/ratelimit.js +10 -1
  45. package/core/react/client/api.js +239 -0
  46. package/core/react/client/auth.js +236 -0
  47. package/core/react/client/components.js +1321 -0
  48. package/core/react/client/datatable.js +595 -0
  49. package/core/react/client/i18n.js +440 -0
  50. package/core/react/client/index.d.ts +314 -0
  51. package/core/react/client/index.js +50 -0
  52. package/core/react/client/router.js +26 -0
  53. package/core/react/client/theme.css +512 -0
  54. package/core/render-cache.js +250 -212
  55. package/core/render.d.ts +25 -21
  56. package/core/render.js +738 -736
  57. package/core/router.d.ts +2 -3
  58. package/core/router.js +171 -171
  59. package/core/seo.js +10 -0
  60. package/core/spa.js +599 -0
  61. package/core/view-loader.js +155 -613
  62. package/core/vite-plugin.d.ts +18 -0
  63. package/core/vite-plugin.js +162 -0
  64. package/core/vite.js +250 -145
  65. package/index.js +22 -18
  66. package/jsconfig.json +5 -3
  67. package/package.json +114 -92
  68. package/production.md +8 -3
  69. package/public/js/bluebird.js +1886 -0
  70. package/templates/_auth/app/database.js +19 -0
  71. package/templates/_auth/app/models/user.js +22 -0
  72. package/templates/_auth/app/routes/auth-router.js +304 -0
  73. package/templates/_base/app/resources/html/layouts/main.html +55 -0
  74. package/templates/_base/app/resources/html/notFound.html +14 -0
  75. package/templates/_base/app/resources/index.html +27 -0
  76. package/templates/_base/app/resources/styles.css +9 -0
  77. package/templates/api/app/models/note.js +18 -0
  78. package/templates/api/app/routes/api.js +82 -0
  79. package/templates/api/app/routes/auth.js +11 -0
  80. package/templates/api/index.js +19 -0
  81. package/templates/api/template.json +13 -0
  82. package/templates/auth/app/mail.js +11 -0
  83. package/templates/auth/app/models/password_reset.js +18 -0
  84. package/templates/auth/app/resources/html/home.html +21 -0
  85. package/templates/auth/app/resources/html/partials/nav.html +2 -0
  86. package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
  87. package/templates/auth/app/resources/main.jsx +18 -0
  88. package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
  89. package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
  90. package/templates/auth/app/resources/pages/Login.jsx +36 -0
  91. package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
  92. package/templates/auth/app/resources/pages/Profile.jsx +99 -0
  93. package/templates/auth/app/resources/pages/Register.jsx +50 -0
  94. package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
  95. package/templates/auth/app/resources/routes.jsx +54 -0
  96. package/templates/auth/app/resources/spa.js +29 -0
  97. package/templates/auth/app/routes/api.js +17 -0
  98. package/templates/auth/app/routes/auth.js +13 -0
  99. package/templates/auth/app/routes/web.js +21 -0
  100. package/templates/auth/index.js +19 -0
  101. package/templates/auth/template.json +15 -0
  102. package/templates/blank/app/models/.gitkeep +0 -0
  103. package/templates/blank/app/resources/html/home.html +27 -0
  104. package/templates/blank/app/resources/html/partials/nav.html +1 -0
  105. package/templates/blank/app/resources/main.jsx +16 -0
  106. package/templates/blank/app/resources/pages/Home.jsx +42 -0
  107. package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
  108. package/templates/blank/app/resources/routes.jsx +15 -0
  109. package/templates/blank/app/resources/spa.js +24 -0
  110. package/templates/blank/app/routes/api.js +11 -0
  111. package/templates/blank/app/routes/web.js +21 -0
  112. package/templates/blank/index.js +13 -0
  113. package/templates/blank/template.json +12 -0
  114. package/templates/blog/app/lib/blog.js +149 -0
  115. package/templates/blog/app/models/post.js +22 -0
  116. package/templates/blog/app/resources/html/home.html +28 -0
  117. package/templates/blog/app/resources/html/not-found.html +5 -0
  118. package/templates/blog/app/resources/html/partials/nav.html +2 -0
  119. package/templates/blog/app/resources/html/partials/post-card.html +9 -0
  120. package/templates/blog/app/resources/html/post.html +8 -0
  121. package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
  122. package/templates/blog/app/resources/main.jsx +18 -0
  123. package/templates/blog/app/resources/pages/Login.jsx +31 -0
  124. package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
  125. package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
  126. package/templates/blog/app/resources/pages/Posts.jsx +83 -0
  127. package/templates/blog/app/resources/pages/Register.jsx +43 -0
  128. package/templates/blog/app/resources/routes.jsx +51 -0
  129. package/templates/blog/app/resources/spa.js +25 -0
  130. package/templates/blog/app/resources/styles.css +40 -0
  131. package/templates/blog/app/routes/api.js +77 -0
  132. package/templates/blog/app/routes/auth.js +10 -0
  133. package/templates/blog/app/routes/web.js +114 -0
  134. package/templates/blog/index.js +21 -0
  135. package/templates/blog/template.json +14 -0
  136. package/vite.config.js +11 -0
  137. package/app/views/components/Header.jsx +0 -42
  138. package/app/views/components/LiveCounter.jsx +0 -67
  139. package/app/views/layouts/main/view.jsx +0 -201
  140. package/app/views/legal/terms.html +0 -29
  141. package/app/views/pages/about.jsx +0 -98
  142. package/app/views/pages/dashboard.jsx +0 -278
  143. package/app/views/pages/index.jsx +0 -123
  144. package/app/views/pages/login.jsx +0 -141
  145. package/core/actions.d.ts +0 -4
  146. package/core/actions.js +0 -355
  147. package/core/jsx.d.ts +0 -38
  148. package/core/jsx.js +0 -1597
  149. package/core/vite.d.ts +0 -3
  150. package/public/js/bluebird-client.js +0 -3265
  151. package/public/js/tailwind.js +0 -8
@@ -0,0 +1,35 @@
1
+ import { Card, Form, Input, Link, SubmitButton, useAuth } from "@seip/bluebird-js/react";
2
+
3
+ /**
4
+ * Sign-in page bound to `POST /api/auth/login` via `useAuth().login`.
5
+ */
6
+ export default function Login() {
7
+ const { login, registerEnabled } = useAuth();
8
+
9
+ return (
10
+ <div className="mx-auto max-w-md py-8">
11
+ <Card variant="glass" title="Sign in" description="Access your Bluebird workspace.">
12
+ <Form
13
+ onSubmitRequest={(values) => login(values)}
14
+ successMessage="Welcome back!"
15
+ className="space-y-4"
16
+ >
17
+ <Input name="email" type="email" label="Email" placeholder="you@example.com" autoComplete="email" required />
18
+ <Input name="password" type="password" label="Password" placeholder="Minimum 6 characters" autoComplete="current-password" required />
19
+ <SubmitButton className="w-full" loadingText="Signing in...">
20
+ Sign In
21
+ </SubmitButton>
22
+ </Form>
23
+
24
+ {registerEnabled ? (
25
+ <p className="mt-5 text-center text-sm text-muted-foreground">
26
+ New here?{" "}
27
+ <Link to="/register" className="font-semibold text-foreground underline-offset-4 hover:underline">
28
+ Create an account
29
+ </Link>
30
+ </p>
31
+ ) : null}
32
+ </Card>
33
+ </div>
34
+ );
35
+ }
@@ -0,0 +1,16 @@
1
+ import { Card, Link } from "@seip/bluebird-js/react";
2
+
3
+ /**
4
+ * Fallback view for unmatched client routes inside `/app`.
5
+ */
6
+ export default function NotFound() {
7
+ return (
8
+ <div className="mx-auto max-w-md py-12">
9
+ <Card variant="glass" title="Page not found" description="The requested route does not exist in this workspace.">
10
+ <Link to="/" className="btn btn-primary btn-sm">
11
+ Back to Home
12
+ </Link>
13
+ </Card>
14
+ </div>
15
+ );
16
+ }
@@ -0,0 +1,55 @@
1
+ import { Card, Form, Input, Link, SubmitButton, useAuth } from "@seip/bluebird-js/react";
2
+
3
+ /**
4
+ * Registration page bound to `POST /api/auth/register` when enabled on the backend.
5
+ */
6
+ export default function Register() {
7
+ const { register, registerEnabled } = useAuth();
8
+
9
+ if (!registerEnabled) {
10
+ return (
11
+ <div className="mx-auto max-w-md py-8">
12
+ <Card variant="glass" title="Registration disabled" description="Self-registration is disabled for this application.">
13
+ <Link to="/login" className="btn btn-outline btn-sm">
14
+ Return to Sign In
15
+ </Link>
16
+ </Card>
17
+ </div>
18
+ );
19
+ }
20
+
21
+ return (
22
+ <div className="mx-auto max-w-md py-8">
23
+ <Card variant="glass" title="Create account" description="Register a new user account.">
24
+ <Form
25
+ onSubmitRequest={(values) => register(values)}
26
+ successMessage="Account created!"
27
+ className="space-y-4"
28
+ >
29
+ <Input name="name" type="text" label="Name" placeholder="Alex Rivera" autoComplete="name" required />
30
+ <Input name="email" type="email" label="Email" placeholder="alex@example.com" autoComplete="email" required />
31
+ <Input
32
+ name="password"
33
+ type="password"
34
+ label="Password"
35
+ hint="At least 6 characters"
36
+ placeholder="Choose a strong password"
37
+ autoComplete="new-password"
38
+ minLength={6}
39
+ required
40
+ />
41
+ <SubmitButton className="w-full" loadingText="Creating account...">
42
+ Create Account
43
+ </SubmitButton>
44
+ </Form>
45
+
46
+ <p className="mt-5 text-center text-sm text-muted-foreground">
47
+ Already registered?{" "}
48
+ <Link to="/login" className="font-semibold text-foreground underline-offset-4 hover:underline">
49
+ Sign in
50
+ </Link>
51
+ </p>
52
+ </Card>
53
+ </div>
54
+ );
55
+ }
@@ -0,0 +1,85 @@
1
+ import { useRef, useState } from "react";
2
+ import { Badge, Button, DataTable, Modal, api, toast } from "@seip/bluebird-js/react";
3
+
4
+ const STATUS_TONES = { active: "success", invited: "info", suspended: "warning" };
5
+
6
+ /**
7
+ * DataTable showcase: server-side pagination (`/api/demo/people` answered with `res.paginate()`), search, sorting,
8
+ * view/edit/delete actions with confirmation, a custom action, and cards on narrow screens.
9
+ */
10
+ export default function TableDemo() {
11
+ const tableRef = useRef(null);
12
+ const [selected, setSelected] = useState(null);
13
+
14
+ const columns = [
15
+ { key: "name", label: "Name", sortable: true, primary: true },
16
+ { key: "email", label: "Email", sortable: true },
17
+ { key: "role", label: "Role", sortable: true, render: (p) => <Badge>{p.role}</Badge> },
18
+ {
19
+ key: "status",
20
+ label: "Status",
21
+ sortable: true,
22
+ render: (p) => <Badge tone={STATUS_TONES[p.status]}>{p.status}</Badge>,
23
+ },
24
+ {
25
+ key: "sales",
26
+ label: "Sales",
27
+ sortable: true,
28
+ numeric: true,
29
+ render: (p) => p.sales.toLocaleString(undefined, { style: "currency", currency: "USD" }),
30
+ },
31
+ { key: "joined", label: "Joined", sortable: true, mobile: "details" },
32
+ ];
33
+
34
+ return (
35
+ <div className="space-y-6">
36
+ <div className="border-b border-border/75 pb-5">
37
+ <h1 className="text-3xl font-semibold tracking-tight text-foreground">DataTable</h1>
38
+ <p className="mt-1 max-w-2xl text-sm text-muted-foreground">
39
+ Server-side search, sorting, and pagination from <code>res.paginate()</code>, row actions with a confirmation
40
+ dialog, and cards with expandable details on narrow screens.
41
+ </p>
42
+ </div>
43
+
44
+ <DataTable
45
+ ref={tableRef}
46
+ url="/api/demo/people"
47
+ columns={columns}
48
+ defaultSort={{ key: "name" }}
49
+ caption="People"
50
+ onView={(person) => setSelected(person)}
51
+ onEdit={(person) => toast.info(`Edit ${person.name}`, "Open your form or navigate to an edit page here.")}
52
+ onDelete={async (person) => {
53
+ await api.delete(`/api/demo/people/${person.id}`);
54
+ toast.success(`${person.name} deleted`);
55
+ }}
56
+ actions={[
57
+ {
58
+ key: "invite",
59
+ label: "Resend invite",
60
+ hidden: (person) => person.status !== "invited",
61
+ onClick: (person) => toast.success("Invite sent", person.email),
62
+ },
63
+ ]}
64
+ toolbar={
65
+ <Button size="sm" variant="outline" onClick={() => tableRef.current?.reload()}>
66
+ Refresh
67
+ </Button>
68
+ }
69
+ />
70
+
71
+ <Modal open={Boolean(selected)} onClose={() => setSelected(null)} title={selected?.name} description={selected?.email}>
72
+ {selected ? (
73
+ <dl className="grid grid-cols-2 gap-3 text-sm">
74
+ {columns.slice(2).map((c) => (
75
+ <div key={c.key}>
76
+ <dt className="text-xs text-muted-foreground">{c.label}</dt>
77
+ <dd className="mt-0.5">{c.render ? c.render(selected) : selected[c.key]}</dd>
78
+ </div>
79
+ ))}
80
+ </dl>
81
+ ) : null}
82
+ </Modal>
83
+ </div>
84
+ );
85
+ }
@@ -0,0 +1,59 @@
1
+ import { Suspense, lazy } from "react";
2
+ import { GuestOnly, PageSkeleton, RequireAuth, createRouter } from "@seip/bluebird-js/react";
3
+ import AppLayout from "./layouts/AppLayout.jsx";
4
+
5
+ const Home = lazy(() => import("./pages/Home.jsx"));
6
+ const Login = lazy(() => import("./pages/Login.jsx"));
7
+ const Register = lazy(() => import("./pages/Register.jsx"));
8
+ const Dashboard = lazy(() => import("./pages/Dashboard.jsx"));
9
+ const TableDemo = lazy(() => import("./pages/TableDemo.jsx"));
10
+ const NotFound = lazy(() => import("./pages/NotFound.jsx"));
11
+
12
+ /**
13
+ * Wraps lazy route components in a full-width Suspense fallback.
14
+ * @param {import('react').ReactNode} element
15
+ * @returns {import('react').ReactNode}
16
+ */
17
+ function withSuspense(element) {
18
+ return <Suspense fallback={<PageSkeleton />}>{element}</Suspense>;
19
+ }
20
+
21
+ /**
22
+ * React Router configuration: browser router under `/app` on the server, hash router in static exports.
23
+ */
24
+ export const router = createRouter(
25
+ [
26
+ {
27
+ path: "/",
28
+ element: <AppLayout />,
29
+ children: [
30
+ {
31
+ index: true,
32
+ element: withSuspense(<Home />),
33
+ },
34
+ {
35
+ path: "table",
36
+ element: withSuspense(<TableDemo />),
37
+ },
38
+ {
39
+ element: <GuestOnly redirectTo="/dashboard" />,
40
+ children: [
41
+ { path: "login", element: withSuspense(<Login />) },
42
+ { path: "register", element: withSuspense(<Register />) },
43
+ ],
44
+ },
45
+ {
46
+ element: <RequireAuth redirectTo="/login" />,
47
+ children: [
48
+ { path: "dashboard", element: withSuspense(<Dashboard />) },
49
+ ],
50
+ },
51
+ {
52
+ path: "*",
53
+ element: withSuspense(<NotFound />),
54
+ },
55
+ ],
56
+ },
57
+ ],
58
+ { basename: "/app" }
59
+ );
@@ -0,0 +1,73 @@
1
+ /**
2
+ * React SPA shell configuration shared by `routerWeb.spa(spa.mount, spa)` (web, per-URL SEO)
3
+ * and `npx bluebird-js export` (static HTML/JS/CSS build for Electron, Capacitor, or any static host).
4
+ * The shell template is `app/resources/index.html`; the React entry is `app/resources/main.jsx`.
5
+ * @type {import('@seip/bluebird-js/core/index.js').SpaOptions & { mount: string }}
6
+ */
7
+ const spa = {
8
+ mount: "/app",
9
+ lang: "en",
10
+ titleTemplate: "%s — Bluebird App",
11
+ title: "Workspace",
12
+ description: "Bluebird.js client-rendered React SPA powered by Vite, React Router, and Tailwind CSS v4.",
13
+ keywords: "bluebird-js, react, react-router, vite, tailwind, spa",
14
+ canonical: true,
15
+ head: [{ tag: "link", rel: "icon", href: "/images/favicon.ico" }],
16
+ htmlAttrs: { class: "dark", "data-theme": "dark" },
17
+ bodyAttrs: {
18
+ class: "min-h-screen bg-app text-foreground antialiased",
19
+ },
20
+ skeleton: "app",
21
+ meta: {
22
+ "*": {
23
+ title: "Workspace",
24
+ description: "Bluebird.js React Router application.",
25
+ robots: "noindex, nofollow",
26
+ skeleton: "app",
27
+ },
28
+ "/": {
29
+ title: "React SPA",
30
+ description: "Client-side React demo with carousel, toasts, Tailwind dark/light theme, and Auth.",
31
+ robots: "index, follow",
32
+ skeleton: "page",
33
+ jsonld: {
34
+ "@context": "https://schema.org",
35
+ "@type": "WebApplication",
36
+ name: "Bluebird App",
37
+ applicationCategory: "DeveloperApplication",
38
+ },
39
+ },
40
+ login: {
41
+ title: "Sign In",
42
+ description: "Sign in to your Bluebird account.",
43
+ keywords: "login, authentication, bluebird",
44
+ robots: "index, follow",
45
+ skeleton: "auth",
46
+ jsonld: {
47
+ "@context": "https://schema.org",
48
+ "@type": "WebPage",
49
+ name: "Sign In",
50
+ },
51
+ },
52
+ "/app/register": {
53
+ title: "Create Account",
54
+ description: "Register a new account.",
55
+ robots: "index, follow",
56
+ skeleton: "auth",
57
+ },
58
+ table: {
59
+ title: "DataTable",
60
+ description: "Responsive DataTable with search, sorting, pagination, and row actions.",
61
+ robots: "index, follow",
62
+ skeleton: "app",
63
+ },
64
+ dashboard: {
65
+ title: "Dashboard",
66
+ description: "Authenticated user dashboard.",
67
+ robots: "noindex, nofollow",
68
+ skeleton: "app",
69
+ },
70
+ },
71
+ };
72
+
73
+ export default spa;
@@ -0,0 +1,9 @@
1
+ @import "tailwindcss";
2
+ @source "./";
3
+ @source "../../public/js";
4
+
5
+ @custom-variant dark (&:where(.dark, .dark *, [data-theme="dark"], [data-theme="dark"] *));
6
+
7
+ @theme {
8
+ --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif;
9
+ }
package/app/routes/api.js CHANGED
@@ -1,50 +1,129 @@
1
- import Router from "@seip/bluebird-js/core/router.js";
2
- import Validator from "@seip/bluebird-js/core/validate.js";
3
- import Cache from "@seip/bluebird-js/core/cache.js";
4
- import Auth from "@seip/bluebird-js/core/auth.js";
5
-
6
- const routerApi = new Router("/api");
7
-
8
- routerApi.get("/", (req, res) => {
9
- res.json({ api: true, message: "Blue Bird API", time: Date.now() });
10
- });
11
-
12
- routerApi.get("/users", (req, res) => {
13
- const users = [
14
- { name: "John Doe", email: "john.doe@example.com" },
15
- { name: "Jane Doe", email: "jane.doe@example.com" },
16
- ];
17
- res.json(users);
18
- });
19
-
20
- const loginSchema = {
21
- email: { required: true, email: true },
22
- password: { required: true, min: 6 },
23
- };
24
-
25
- const loginValidator = new Validator(loginSchema, "en");
26
-
27
- routerApi.post("/login", loginValidator.middleware(), (req, res) => {
28
- res.json({ message: "Login successful", body: req.body });
29
- });
30
-
31
- routerApi.get("/cache", Cache.middleware(60), async (req, res) => {
32
- await new Promise((resolve) => setTimeout(resolve, 2000));
33
- res.json({ message: "Cache successful" });
34
- });
35
-
36
- routerApi.get("/auth_generate", async (req, res) => {
37
- const token = await Auth.login(res, { id: 1, name: "John Doe" }, "auth");
38
- res.json({ message: "Auth successful", token });
39
- });
40
-
41
- routerApi.get("/auth_logout", async (req, res) => {
42
- await Auth.logout(res, "auth", {}, req);
43
- res.json({ message: "Logged out" });
44
- });
45
-
46
- routerApi.get("/auth_verify", Auth.protect(), (req, res) => {
47
- res.json({ message: "Auth successful", user: req.user });
48
- });
49
-
50
- export default routerApi;
1
+ import Router from "@seip/bluebird-js/core/router.js";
2
+ import Validator from "@seip/bluebird-js/core/validate.js";
3
+ import Cache from "@seip/bluebird-js/core/cache.js";
4
+ import Auth from "@seip/bluebird-js/core/auth.js";
5
+ import Csrf from "@seip/bluebird-js/core/csrf.js";
6
+
7
+ const routerApi = new Router("/api");
8
+ routerApi.use(Csrf.middleware());
9
+
10
+ routerApi.get("/", (req, res) => {
11
+ res.ok({ message: "Blue Bird API", time: Date.now() });
12
+ });
13
+
14
+ routerApi.get("/cache", Cache.middleware(60), async (req, res) => {
15
+ res.ok({ message: "Cached response", time: Date.now() });
16
+ });
17
+
18
+ routerApi.get("/admin/stats", Auth.protect({ roles: "admin" }), (req, res) => {
19
+ res.ok({ role: req.user.role, uptime: Math.floor(process.uptime()) }, "Admin metrics");
20
+ });
21
+
22
+ const demoTasks = [
23
+ { id: 1, title: "Render landing page via HTML {{ }} SSR", status: "done" },
24
+ { id: 2, title: "Serve React Router SPA via router.spa('/app')", status: "active" },
25
+ { id: 3, title: "Sync AuthProvider session with /api/auth/me", status: "active" },
26
+ ];
27
+
28
+ routerApi.get("/tasks", Auth.protect(), (req, res) => {
29
+ res.ok(demoTasks, "Tasks loaded");
30
+ });
31
+
32
+ const taskValidator = new Validator(
33
+ {
34
+ title: { required: true, min: 3, max: 140 },
35
+ },
36
+ "en"
37
+ );
38
+
39
+ routerApi.post("/tasks", Auth.protect(), taskValidator.middleware(), (req, res) => {
40
+ const newTask = {
41
+ id: Date.now(),
42
+ title: String(req.body.title).trim(),
43
+ status: "active",
44
+ };
45
+ demoTasks.unshift(newTask);
46
+ if (demoTasks.length > 15) demoTasks.pop();
47
+ res.created(newTask, `Task "${newTask.title}" created`);
48
+ });
49
+
50
+ routerApi.post("/tasks/clear", Auth.protect(), (req, res) => {
51
+ demoTasks.splice(3);
52
+ res.ok(demoTasks, "Task list restored");
53
+ });
54
+
55
+ routerApi.delete("/tasks/:id", Auth.protect(), (req, res) => {
56
+ const id = Number(req.params.id);
57
+ const idx = demoTasks.findIndex((t) => Number(t.id) === id);
58
+ if (idx !== -1 && demoTasks.length > 1) {
59
+ const [removed] = demoTasks.splice(idx, 1);
60
+ return res.ok(removed, `Task #${id} deleted`);
61
+ }
62
+ return res.ok({ id }, "Task processed");
63
+ });
64
+
65
+ const FIRST_NAMES = ["Ana", "Bruno", "Carla", "Diego", "Elena", "Facundo", "Gabriela", "Hugo", "Inés", "Julián", "Karen", "Lucas"];
66
+ const LAST_NAMES = ["García", "Martínez", "López", "Silva", "Pereira", "Fernández", "Gómez", "Rossi", "Díaz", "Torres"];
67
+ const demoPeople = Array.from({ length: 57 }, (_, i) => {
68
+ const first = FIRST_NAMES[i % FIRST_NAMES.length];
69
+ const last = LAST_NAMES[(i * 7) % LAST_NAMES.length];
70
+ return {
71
+ id: i + 1,
72
+ name: `${first} ${last}`,
73
+ email: `${first}.${last}${i + 1}@example.com`.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
74
+ role: ["admin", "editor", "user", "user"][i % 4],
75
+ status: i % 5 === 0 ? "invited" : i % 7 === 0 ? "suspended" : "active",
76
+ sales: Math.round(((i * 7919) % 100000) / 10) / 10,
77
+ joined: new Date(Date.UTC(2026, i % 12, (i % 27) + 1)).toISOString().slice(0, 10),
78
+ };
79
+ });
80
+ const PEOPLE_SORTABLE = new Set(["name", "email", "role", "status", "sales", "joined"]);
81
+
82
+ /**
83
+ * Server-side DataTable demo: `?page&limit&search&sort&order` answered with `res.paginate()`.
84
+ */
85
+ routerApi.get("/demo/people", (req, res) => {
86
+ const page = Math.max(1, Number(req.query.page) || 1);
87
+ const limit = Math.min(100, Math.max(1, Number(req.query.limit) || 10));
88
+ const search = String(req.query.search || "").trim().toLowerCase();
89
+ const sort = PEOPLE_SORTABLE.has(req.query.sort) ? req.query.sort : null;
90
+ const order = req.query.order === "desc" ? -1 : 1;
91
+ let list = search
92
+ ? demoPeople.filter((p) => [p.name, p.email, p.role, p.status].some((v) => v.toLowerCase().includes(search)))
93
+ : demoPeople;
94
+ if (sort) {
95
+ list = [...list].sort((a, b) =>
96
+ (typeof a[sort] === "number" ? a[sort] - b[sort] : String(a[sort]).localeCompare(String(b[sort]))) * order
97
+ );
98
+ }
99
+ res.paginate(list.slice((page - 1) * limit, page * limit), { page, limit, total: list.length });
100
+ });
101
+
102
+ routerApi.delete("/demo/people/:id", (req, res) => {
103
+ const idx = demoPeople.findIndex((p) => p.id === Number(req.params.id));
104
+ if (idx === -1) return res.notFound("Person not found");
105
+ const [removed] = demoPeople.splice(idx, 1);
106
+ return res.ok(removed, `${removed.name} deleted`);
107
+ });
108
+
109
+ routerApi.get("/stream", (req, res) => {
110
+ const stream = res.sse({ keepAliveMs: 15000, retry: 3000 });
111
+ let tick = 1;
112
+
113
+ const sendMetrics = () => {
114
+ const memMb = Math.round((process.memoryUsage().rss / 1024 / 1024) * 10) / 10;
115
+ stream.send({
116
+ tick,
117
+ uptime: Math.floor(process.uptime()),
118
+ rssMb: memMb,
119
+ timestamp: new Date().toLocaleTimeString(),
120
+ });
121
+ tick++;
122
+ };
123
+
124
+ sendMetrics();
125
+ const timer = setInterval(sendMetrics, 2000);
126
+ stream.onClose(() => clearInterval(timer));
127
+ });
128
+
129
+ export default routerApi;