@dokebi/app 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI.md +41 -8
- package/README.md +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +125 -17
- package/package.json +9 -3
- package/templates/app/views/default/errors/error.ejs +4 -1
- package/templates/app-inertia/views/app.js +1 -1
- package/templates/app-inertia-ssr/views/ssr.js +1 -1
package/AI.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# AI guide — @dokebi/app 0.4.
|
|
1
|
+
# AI guide — @dokebi/app 0.4.1
|
|
2
2
|
|
|
3
|
-
A plugin for several apps in one project that **picks the app and template by request domain** (D-0030). One app is one folder `src/apps/<app>/`, defined with `defineApp`. Node >=24.0.0, core ^0.4.0. The human-oriented description is the [README](README.md).
|
|
3
|
+
A plugin for several apps in one project that **picks the app and template by request domain** (D-0030). One app is one folder `src/apps/<app>/`, defined with `defineApp`. Node >=24.0.0, core ^0.4.0, web ^0.4.1; @dokebi/session optional (0.4.1: a refused HTML form goes back to its page, §4). The human-oriented description is the [README](README.md).
|
|
4
4
|
|
|
5
5
|
## 1. Install and what it creates
|
|
6
6
|
|
|
@@ -29,7 +29,7 @@ src/apps/<app>/
|
|
|
29
29
|
routes.ts routes
|
|
30
30
|
controllers/ this app's defineController tokens
|
|
31
31
|
models/ plain code
|
|
32
|
-
views/default/ the default template (required). EJS: home.ejs / Inertia: Layout.vue, Pages
|
|
32
|
+
views/default/ the default template (required). EJS: home.ejs / Inertia: Layout.vue, Pages/**/*.vue (subfolders work: `Posts/Show` is Pages/Posts/Show.vue)
|
|
33
33
|
views/<template>/errors/404.ejs, errors/error.ejs (EJS, optional) error pages for browser pages (D-0074)
|
|
34
34
|
views/<template>/Pages/Error.vue (Inertia, with inertia.errorPage) the error page component
|
|
35
35
|
views/<template>/ only the files that change
|
|
@@ -81,7 +81,7 @@ export default defineApp({
|
|
|
81
81
|
|
|
82
82
|
- `route`: `get`, `post`, `put`, `patch`, `delete(url, handler, options?)` and `route(options)`. Handlers are `(request, reply, page)`. Options are `WebRoute` options without `handler` and `constraints` (so `seal` works).
|
|
83
83
|
- `page`: `{ app: { name, template, domain }, module, base, view, inertia, sealed }`. In app routes `module` is undefined and `base` is `''`.
|
|
84
|
-
- `view`: the EJS renderer. Lookup order `<app>/<template>` → `<app>/default` → `src/shared/views` (layouts and `$include` too).
|
|
84
|
+
- `view`: the EJS renderer. Lookup order `<app>/<template>` → `<app>/default` → `src/shared/views` (layouts and `$include` too). Every page's data also has `errors`, `old` and `flash` (0.4.1, below), always objects.
|
|
85
85
|
- `inertia`: an Inertia renderer with the app's document settings (when the app has `inertia`). It adds `app` (`{ name, template, domain }`) and `sealed` to the shared props.
|
|
86
86
|
- `sealed`: whether the request host has `seal: true`. EJS data gets it as `sealed` too (the app's `shared` and the page data win).
|
|
87
87
|
- `app` (AppContext): `name`, `logger`, `signal`, `get`, `getDependency`, `getOptionalDependency`, `controller`, `defer`, `realtime(namespace?)` (§4 realtime namespaces). **Reading something not in `requires` is an error** (`NOT_REQUIRED`: `App <app> does not list <plugin> in requires`). Controller factories have the same limit.
|
|
@@ -93,10 +93,43 @@ export default defineApp({
|
|
|
93
93
|
Every error answer on an app's host — a thrown error, a refused schema, an unknown route (404) — goes through this app first (a dokebi-web error responder):
|
|
94
94
|
|
|
95
95
|
1. **Inertia** (`inertia` set): the app's scope answers an Inertia validation error with the redirect back (`errors` in the session flash, template-inertia §3.3) and other errors with `inertia.errorPage` when set.
|
|
96
|
-
2. **
|
|
97
|
-
3.
|
|
96
|
+
2. **HTML form back** (0.4.1, D-0074 addendum 1): an HTML form post (body `application/x-www-form-urlencoded` or `multipart/form-data`, answered with HTML — not `fetch`, XHR, Bearer, JSON or Inertia) refused with 422 `VALIDATION_FAILED` (a route schema or `validationError()`) goes **back to its page** when the request has a session (@dokebi/session registered) and a `Referer` of the same host: `errors` (`{ field: message }`) and `old` (the body's top-level values without password-like fields such as `password`, `token`, `secret`, `api_key`, `card`, `cvc`, `otp`, `pin`; nothing when larger than 8 KB) are flashed and the answer is **303 to the Referer** (its path and query). Without a session, or with a missing or foreign Referer, it is the 422 error view below (no redirect, so no loop).
|
|
97
|
+
3. **EJS error views** (an HTML answer: a browser navigation or an HTML form post): `errors/<status>` (for example `errors/404.ejs`), else `errors/error`, looked up like any page (`<app>/<template>` → `<app>/default` → `src/shared/views`, so a shared `errors/404.ejs` serves every app), with the app's `shared` data and the default layout. Data: `{ status, title ('404 Not Found'), error, message, hint, fields ([{ field, message }], empty unless 422), errors ({ field: message } of a 422), old (the refused form's input, 422 only), fix, stack }` — `fix` and `stack` (5xx) only outside production, otherwise null. An `errors/422.ejs` can therefore show the form again without a session (`<%- await data.$include('posts/form') %>` with the same `data.errors`/`data.old`).
|
|
98
|
+
4. Otherwise dokebi-web's own answer: JSON for scripts (Inertia, `fetch`, XHR, Bearer, JSON bodies), a small HTML page for browser pages.
|
|
98
99
|
|
|
99
|
-
JSON clients never get a view. A view that fails to render (a missing data field, a broken include) falls back to web's page and logs `error` `Web error responder failed` (`WEB_ERROR_RESPONDER_FAILED`, once per route and interval). Refuse input in handlers with `throw validationError({ name: 'Already taken' })` from @dokebi/web (422, the same answer as a refused schema). Debug: `App error view rendered` (app, template, view, status).
|
|
100
|
+
JSON clients never get a view, and are never redirected. A view that fails to render (a missing data field, a broken include) falls back to web's page and logs `error` `Web error responder failed` (`WEB_ERROR_RESPONDER_FAILED`, once per route and interval). Refuse input in handlers with `throw validationError({ name: 'Already taken' })` from @dokebi/web (422, the same answer as a refused schema). Debug: `App error view rendered` (app, template, view, status), `App form validation redirect` (app, method, route, fields, old count, target path), `App form validation answered with the error view` (app, reason `no-session` or `no-way-back`, method, route).
|
|
101
|
+
|
|
102
|
+
### Forms: `errors`, `old` and `flash` in every view (0.4.1)
|
|
103
|
+
|
|
104
|
+
With @dokebi/session registered, the first render of a request **takes the session flash once** (`takeFlash()`, shared by every render of that request) and every EJS page of the app (its own and its modules') gets:
|
|
105
|
+
|
|
106
|
+
- `data.errors` — `{ field: message }` of the form this request was sent back from (`{}` otherwise);
|
|
107
|
+
- `data.old` — that form's input (`{}` otherwise);
|
|
108
|
+
- `data.flash` — the rest of the flash, for example `data.flash.notice` after `await request.session.flash('notice', 'Saved')` (`{}` otherwise).
|
|
109
|
+
|
|
110
|
+
Without the session plugin all three are `{}`, so templates need no checks. The handler's own `errors` and `old` win field by field; the app's `shared` data and the page data win over `flash`. Error views do not take the flash (it is for the next page). If a handler calls `request.session.takeFlash()` itself before rendering, it takes `errors` and `old` too (pass them on, or read `data.flash` instead). A write form:
|
|
111
|
+
|
|
112
|
+
```ejs
|
|
113
|
+
<form method="post" action="/posts">
|
|
114
|
+
<input name="title" value="<%= data.old.title ?? '' %>">
|
|
115
|
+
<% if (data.errors.title) { %><small class="error"><%= data.errors.title %></small><% } %>
|
|
116
|
+
<textarea name="body"><%= data.old.body ?? '' %></textarea>
|
|
117
|
+
<% if (data.errors.body) { %><small class="error"><%= data.errors.body %></small><% } %>
|
|
118
|
+
<button>Save</button>
|
|
119
|
+
</form>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```ts snippet
|
|
123
|
+
import { validationError } from '@dokebi/web';
|
|
124
|
+
route.post('/posts', async (request, reply, page) => {
|
|
125
|
+
const { title } = request.body as { title: string; body: string };
|
|
126
|
+
if (await titleTaken(title)) throw validationError({ title: 'Already taken' }); // back to the form, 303
|
|
127
|
+
await request.session.flash('notice', 'Saved');
|
|
128
|
+
return reply.redirect('/posts', 303);
|
|
129
|
+
}, { schema: { body: { type: 'object', required: ['title', 'body'], properties: { title: { type: 'string', minLength: 1, maxLength: 120 }, body: { type: 'string', minLength: 1 } } } } });
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
HTML forms need `dokebi-web.formBody: true` (urlencoded bodies). The flow needs `npx dokebi add @dokebi/session`; without it the refused form shows `errors/422.ejs` (or `errors/error.ejs`) with `fields`, `errors` and `old`.
|
|
100
133
|
|
|
101
134
|
### Mounting modules (D-0032)
|
|
102
135
|
|
|
@@ -260,7 +293,7 @@ Debug logs (D-0047): `App domains configured` (domain → app/template, `(sealed
|
|
|
260
293
|
- Apps do not import each other. Shared code goes into `src/shared/` (models, views) or a module.
|
|
261
294
|
- Do not edit the generated registration code or `.dokebi/state.json`. Change apps and domains with `dokebi app` commands (they are recorded in dokebi.example.yaml too).
|
|
262
295
|
- An Inertia app without its build files fails to start. Run `npm run build` first.
|
|
263
|
-
- Do not answer validation with `reply.code(422).send(…)` by hand: `throw validationError({ field: 'message' })` so Inertia forms get the redirect with `errors` and other clients the standard `fields`/`errors` body (D-0074).
|
|
296
|
+
- Do not answer validation with `reply.code(422).send(…)` by hand: `throw validationError({ field: 'message' })` so Inertia and HTML forms get the redirect with `errors` (with a session) and other clients the standard `fields`/`errors` body (D-0074).
|
|
264
297
|
|
|
265
298
|
## 8. References
|
|
266
299
|
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @dokebi/app 0.4.
|
|
1
|
+
# @dokebi/app 0.4.1
|
|
2
2
|
|
|
3
3
|
An optional plugin for several apps in one project that picks the app and template by request domain. An optional package for Node >=24.0.0 and core ^0.4.0 (public on npm). The design is [D-0030](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0030-multi-app-and-domains.md).
|
|
4
4
|
|
|
@@ -86,6 +86,7 @@ export default defineApp({
|
|
|
86
86
|
|
|
87
87
|
- A handler's `page` is `{ app: { name, template, domain }, module, base, view, inertia, sealed }`. In app routes `module` is undefined and `base` is `''`. `view` is an EJS renderer that looks in `<app>/<template>` → `<app>/default` → shared src/shared/views; layouts and `$include` follow the same order. `inertia` uses the app's document settings (Vite entry and so on) and adds `app` to the shared props.
|
|
88
88
|
- **Error pages ([D-0074](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0074-error-responses.md)):** errors on an app's hosts (unknown routes included) go through the app first. An Inertia app answers Inertia validation errors with the redirect back (`errors`) and other errors with `inertia.errorPage` (for example `Error`); for a browser page an EJS app renders `views/<template>/errors/<status>.ejs`, else `errors/error.ejs` (same lookup as pages, with `status`, `title`, `error`, `message`, `hint`, `fields`), else @dokebi/web's small page. JSON clients always get JSON. The `app add` skeletons include `errors/404.ejs` and `errors/error.ejs` (EJS) or `Pages/Error.vue` with `errorPage: 'Error'` (Inertia). Refuse input with `throw validationError({ name: 'Already taken' })` from @dokebi/web.
|
|
89
|
+
- **HTML forms come back (0.4.1, D-0074 addendum 1):** with [@dokebi/session](https://git.nyx-zone.com/dokebi/framework/-/blob/main/plugins/dokebi-session/README.md) registered, an HTML form post refused with 422 `VALIDATION_FAILED` flashes `errors` and `old` (without password-like fields) and answers 303 to the `Referer` of the same host, and every EJS page gets `data.errors`, `data.old` and `data.flash` (always objects, the flash read once per request), so the form shows `<%= data.old.title ?? '' %>` and `data.errors.title`. Without a session, or without a same-host Referer, the refused form gets `errors/422.ejs` (else `errors/error.ejs`) with `fields`, `errors` and `old`. Scripts (`fetch`, XHR, Bearer, JSON, Inertia) keep their JSON. Needs @dokebi/web ^0.4.1.
|
|
89
90
|
- **Mounting modules ([D-0032](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0032-feature-modules.md)):** `modules: { '/prefix': module }`, where the module was made with `defineModule`.
|
|
90
91
|
- The module's `routes` are registered with this app's route constraint and the prefix; handlers get `page.module` and `page.base`.
|
|
91
92
|
- The module's `sockets` are registered under the prefix. Their tenant is `<namespace>:<tenant>`, as for app sockets.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ControllerDefinition } from '@dokebi/controller';
|
|
2
2
|
import { type Awaitable, type Plugin, type PluginContext, type PluginDependency } from '@dokebi/core';
|
|
3
|
-
import type
|
|
3
|
+
import { type WebErrorResponse, type WebRoute } from '@dokebi/web';
|
|
4
4
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
5
5
|
export { AppError, type AppErrorCode } from './errors.js';
|
|
6
6
|
export { type AppPluginOptions, options } from './options.js';
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,8 @@ import { stat } from 'node:fs/promises';
|
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { types } from 'node:util';
|
|
4
4
|
import { createControllers, } from '@dokebi/controller';
|
|
5
|
-
import { definePlugin, dependencyOf, optionalDependency, } from '@dokebi/core';
|
|
6
|
-
import {
|
|
5
|
+
import { definePlugin, dependencyOf, optionalDependency, providerDependency, } from '@dokebi/core';
|
|
6
|
+
import { formBackTarget, formFieldErrors, formOldInput, isFormSubmission, } from '@dokebi/web';
|
|
7
7
|
import { realtime, viewEngines, } from '@dokebi/web/points';
|
|
8
8
|
import { AppError } from './errors.js';
|
|
9
9
|
import { options as declaration } from './options.js';
|
|
@@ -109,6 +109,31 @@ function parseEntry(input) {
|
|
|
109
109
|
}
|
|
110
110
|
return apps;
|
|
111
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* dokebi-web 0.4.1 or later: the HTML form validation redirect uses its form helpers (D-0074
|
|
114
|
+
* addendum 1). A local descriptor instead of '@dokebi/web/dependency' (whose range stays ^0.4.0
|
|
115
|
+
* for the plugins that need nothing newer).
|
|
116
|
+
*/
|
|
117
|
+
const web = providerDependency({
|
|
118
|
+
name: 'dokebi-web',
|
|
119
|
+
version: '^0.4.1',
|
|
120
|
+
packageName: '@dokebi/web',
|
|
121
|
+
load: () => import('@dokebi/web'),
|
|
122
|
+
});
|
|
123
|
+
const session = optionalDependency({ name: 'dokebi-session', version: '^0.4.0' });
|
|
124
|
+
const plainRecord = (value) => value !== null &&
|
|
125
|
+
typeof value === 'object' &&
|
|
126
|
+
!Array.isArray(value) &&
|
|
127
|
+
[Object.prototype, null].includes(Object.getPrototypeOf(value));
|
|
128
|
+
/** Splits a taken flash into the view data; fresh objects for every request. */
|
|
129
|
+
function viewFlash(taken) {
|
|
130
|
+
const { errors, old, ...rest } = taken ?? {};
|
|
131
|
+
return {
|
|
132
|
+
errors: plainRecord(errors) ? { ...errors } : {},
|
|
133
|
+
old: plainRecord(old) ? { ...old } : {},
|
|
134
|
+
flash: rest,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
112
137
|
/**
|
|
113
138
|
* The seal plugin (D-0059 §4, D-0062), looked up by name only: a host with `seal: true` needs it
|
|
114
139
|
* (it serves `/dokebi/seal/*`, seals the sockets through the websocket transport and fills the
|
|
@@ -317,6 +342,63 @@ class AppRuntime {
|
|
|
317
342
|
throw new AppError('UNKNOWN_APP', `dokebi-app apps.${app} names an app that is not defined (defined: ${[...this.apps.keys()].join(', ') || 'none'})`);
|
|
318
343
|
const http = context.getDependency(web);
|
|
319
344
|
const logger = context.logger;
|
|
345
|
+
const sessions = context.getOptionalDependency(this.#declaredFor(session.name));
|
|
346
|
+
// D-0074 addendum 1: the flash of a request, taken once (every render of the request shares
|
|
347
|
+
// it). Paths the session plugin excludes have no session (`request.session` is null).
|
|
348
|
+
const flashes = new WeakMap();
|
|
349
|
+
const sessionOf = (request) => sessions && request.session ? sessions.of(request) : undefined;
|
|
350
|
+
const flashOf = (request) => {
|
|
351
|
+
const current = sessionOf(request);
|
|
352
|
+
if (!current)
|
|
353
|
+
return undefined;
|
|
354
|
+
let taken = flashes.get(request);
|
|
355
|
+
if (!taken) {
|
|
356
|
+
taken = current.takeFlash().then(viewFlash);
|
|
357
|
+
flashes.set(request, taken);
|
|
358
|
+
}
|
|
359
|
+
return taken;
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* D-0074 addendum 1: an HTML form (urlencoded or multipart body) refused with 422
|
|
363
|
+
* VALIDATION_FAILED goes back to the page it came from — `errors` and `old` flashed in the
|
|
364
|
+
* session, 303 to the Referer of this host — when the request has a session and a way back.
|
|
365
|
+
* False otherwise: the error view (errors/422.ejs, errors/error.ejs) or dokebi-web's page.
|
|
366
|
+
*/
|
|
367
|
+
const formBack = async (response, app) => {
|
|
368
|
+
const { request, reply, status, body } = response;
|
|
369
|
+
if (status !== 422 || !isFormSubmission(request.headers))
|
|
370
|
+
return false;
|
|
371
|
+
const errors = formFieldErrors(body);
|
|
372
|
+
if (!errors)
|
|
373
|
+
return false;
|
|
374
|
+
const current = sessionOf(request);
|
|
375
|
+
const target = current ? formBackTarget(request, http.requestHost(request)) : undefined;
|
|
376
|
+
if (!current || target === undefined) {
|
|
377
|
+
if (logger.isEnabled('debug'))
|
|
378
|
+
logger.log('debug', 'App form validation answered with the error view', {
|
|
379
|
+
app,
|
|
380
|
+
reason: current ? 'no-way-back' : 'no-session',
|
|
381
|
+
method: request.method,
|
|
382
|
+
route: request.routeOptions.url ?? 'unmatched',
|
|
383
|
+
});
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
await current.flash('errors', errors);
|
|
387
|
+
const old = formOldInput(request.body);
|
|
388
|
+
if (old)
|
|
389
|
+
await current.flash('old', old);
|
|
390
|
+
if (logger.isEnabled('debug'))
|
|
391
|
+
logger.log('debug', 'App form validation redirect', {
|
|
392
|
+
app,
|
|
393
|
+
method: request.method,
|
|
394
|
+
route: request.routeOptions.url ?? 'unmatched',
|
|
395
|
+
fields: Object.keys(errors).slice(0, 32),
|
|
396
|
+
old: old ? Object.keys(old).length : 0,
|
|
397
|
+
target: target.split('?', 1)[0]?.slice(0, 200),
|
|
398
|
+
});
|
|
399
|
+
reply.redirect(target, 303);
|
|
400
|
+
return true;
|
|
401
|
+
};
|
|
320
402
|
// D-0063 §5: view engines and realtime transports by name, whoever contributes them.
|
|
321
403
|
const engines = byKey(context, viewEngines, 'name');
|
|
322
404
|
const transports = byKey(context, realtime, 'kind');
|
|
@@ -337,7 +419,8 @@ class AppRuntime {
|
|
|
337
419
|
transports: [...transports.keys()],
|
|
338
420
|
});
|
|
339
421
|
// Fail closed (D-0059 §4): a sealed host without the seal plugin would talk plain text.
|
|
340
|
-
if (sealedDomains.length &&
|
|
422
|
+
if (sealedDomains.length &&
|
|
423
|
+
context.getOptionalDependency(this.#declaredFor(seal.name)) === undefined)
|
|
341
424
|
throw new AppError('SEAL_PROVIDER_MISSING', `Domains ${sealedDomains.slice(0, 8).join(', ')} set seal: true; register the dokebi-seal plugin`);
|
|
342
425
|
/** The engine an app needs, or VIEW_ENGINE_MISSING naming the package that offers it. */
|
|
343
426
|
const engine = (name, app) => {
|
|
@@ -542,13 +625,25 @@ class AppRuntime {
|
|
|
542
625
|
...appChain(template),
|
|
543
626
|
];
|
|
544
627
|
};
|
|
545
|
-
// `sealed
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
628
|
+
// `sealed`, the session flash (`errors`, `old`, `flash`; D-0074 addendum 1) and the app's
|
|
629
|
+
// shared data under the page data, computed once per render call. An error view passes
|
|
630
|
+
// its own `errors` and `old` and leaves the flash to the next page.
|
|
631
|
+
const withShared = (view, request, info, sealed, pageFlash = true) => {
|
|
632
|
+
const data = async (own) => {
|
|
633
|
+
const taken = (pageFlash ? await flashOf(request) : undefined) ?? viewFlash(undefined);
|
|
634
|
+
const result = {
|
|
635
|
+
sealed,
|
|
636
|
+
...taken,
|
|
637
|
+
...(await definition.shared?.(request, { app: info })),
|
|
638
|
+
...own,
|
|
639
|
+
};
|
|
640
|
+
// The handler's own `errors` and `old` win field by field.
|
|
641
|
+
if (plainRecord(own.errors))
|
|
642
|
+
result.errors = { ...taken.errors, ...own.errors };
|
|
643
|
+
if (plainRecord(own.old))
|
|
644
|
+
result.old = { ...taken.old, ...own.old };
|
|
645
|
+
return result;
|
|
646
|
+
};
|
|
552
647
|
return Object.freeze({
|
|
553
648
|
render: async (name, own, options) => view.render(name, await data(own), options),
|
|
554
649
|
reply: async (reply, name, own, options) => view.reply(reply, name, await data(own), options),
|
|
@@ -637,15 +732,20 @@ class AppRuntime {
|
|
|
637
732
|
}
|
|
638
733
|
};
|
|
639
734
|
// D-0074 §3-4: the error answers of this app's hosts. Inertia first (the validation
|
|
640
|
-
// redirect, the app's error page component), then for an HTML answer the
|
|
641
|
-
// `errors/<status>` → `errors/error` in
|
|
735
|
+
// redirect, the app's error page component), then for an HTML answer the form redirect
|
|
736
|
+
// (addendum 1) and the app's error view: `errors/<status>` → `errors/error` in
|
|
737
|
+
// <app>/<template> → <app>/default → shared views.
|
|
642
738
|
const errorViews = new Map();
|
|
643
739
|
answers.set(name, async (response, entry) => {
|
|
644
740
|
if (inertiaApi && typeof inertiaApi.answerError === 'function') {
|
|
645
741
|
if (await inertiaApi.answerError(response))
|
|
646
742
|
return true;
|
|
647
743
|
}
|
|
648
|
-
if (response.format !== 'html'
|
|
744
|
+
if (response.format !== 'html')
|
|
745
|
+
return false;
|
|
746
|
+
if (await formBack(response, name))
|
|
747
|
+
return true;
|
|
748
|
+
if (!ejsRoot)
|
|
649
749
|
return false;
|
|
650
750
|
const { info, sealed } = entry;
|
|
651
751
|
let view = errorViews.get(info.template);
|
|
@@ -662,13 +762,17 @@ class AppRuntime {
|
|
|
662
762
|
if (!page)
|
|
663
763
|
return false;
|
|
664
764
|
const fields = Array.isArray(body.fields) ? body.fields : [];
|
|
665
|
-
await withShared(view, request, info, sealed).reply(reply, page, {
|
|
765
|
+
await withShared(view, request, info, sealed, false).reply(reply, page, {
|
|
666
766
|
status,
|
|
667
767
|
title: `${status} ${STATUS_CODES[status] ?? 'Error'}`,
|
|
668
768
|
error: body.error,
|
|
669
769
|
message: typeof body.message === 'string' ? body.message : null,
|
|
670
770
|
hint: typeof body.hint === 'string' ? body.hint : null,
|
|
671
771
|
fields,
|
|
772
|
+
// A refused form without a way back (no session, no Referer of this host): an
|
|
773
|
+
// errors/422.ejs can show the form again with these (D-0074 addendum 1).
|
|
774
|
+
errors: formFieldErrors(body) ?? {},
|
|
775
|
+
old: (status === 422 && formOldInput(request.body)) || {},
|
|
672
776
|
// Development only (dokebi-web already cut `fix` from a production body).
|
|
673
777
|
fix: typeof body.fix === 'string' ? body.fix : null,
|
|
674
778
|
stack: !production && status >= 500 && error instanceof Error ? (error.stack ?? null) : null,
|
|
@@ -785,7 +889,7 @@ function missingViews(name) {
|
|
|
785
889
|
}
|
|
786
890
|
export const createPlugin = definePlugin({
|
|
787
891
|
name: 'dokebi-app',
|
|
788
|
-
version: '0.4.
|
|
892
|
+
version: '0.4.1',
|
|
789
893
|
core: '^0.4.0',
|
|
790
894
|
options: declaration,
|
|
791
895
|
entry: parseEntry,
|
|
@@ -793,12 +897,16 @@ export const createPlugin = definePlugin({
|
|
|
793
897
|
collects: [viewEngines, realtime],
|
|
794
898
|
dependencies: ({ entry }) => {
|
|
795
899
|
const apps = [...(entry ?? new Map()).values()];
|
|
796
|
-
const list = [web, seal];
|
|
900
|
+
const list = [web, seal, session];
|
|
797
901
|
for (const app of apps)
|
|
798
902
|
for (const item of [...(app.requires ?? []), ...Object.values(app.modules ?? {})]) {
|
|
799
903
|
const dependency = requirement(item);
|
|
800
|
-
|
|
904
|
+
const index = list.findIndex((existing) => existing.name === dependency.name);
|
|
905
|
+
if (index === -1)
|
|
801
906
|
list.push(dependency);
|
|
907
|
+
// An app that requires an optional provider (session, seal) makes it required.
|
|
908
|
+
else if (list[index]?.optional && !dependency.optional)
|
|
909
|
+
list[index] = dependency;
|
|
802
910
|
}
|
|
803
911
|
return list;
|
|
804
912
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dokebi/app",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Multiple apps per project for Dokebi: pick the app and template by request domain, with EJS and Inertia skeletons.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dokebi",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@dokebi/core": "^0.4.0",
|
|
43
43
|
"@dokebi/module": "^0.4.0",
|
|
44
44
|
"@dokebi/template-ejs": "^0.4.0",
|
|
45
|
-
"@dokebi/web": "^0.4.
|
|
45
|
+
"@dokebi/web": "^0.4.1",
|
|
46
46
|
"fastify": "^5.12.7"
|
|
47
47
|
},
|
|
48
48
|
"peerDependenciesMeta": {
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@dokebi/module": "^0.4.0",
|
|
55
|
+
"@dokebi/session": "^0.4.0",
|
|
55
56
|
"@dokebi/sse": "^0.4.0",
|
|
56
57
|
"@dokebi/template-ejs": "^0.4.0",
|
|
57
58
|
"@dokebi/template-inertia": "^0.4.0",
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
"dependencies": [
|
|
67
68
|
{
|
|
68
69
|
"name": "dokebi-web",
|
|
69
|
-
"version": "^0.4.
|
|
70
|
+
"version": "^0.4.1",
|
|
70
71
|
"package": "@dokebi/web"
|
|
71
72
|
},
|
|
72
73
|
{
|
|
@@ -83,6 +84,11 @@
|
|
|
83
84
|
"name": "dokebi-seal",
|
|
84
85
|
"version": "^0.4.0",
|
|
85
86
|
"optional": true
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"name": "dokebi-session",
|
|
90
|
+
"version": "^0.4.0",
|
|
91
|
+
"optional": true
|
|
86
92
|
}
|
|
87
93
|
],
|
|
88
94
|
"collects": [
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
<%# Every other error page of this app (D-0074): 403, 500, an HTML form refused with 422 (the
|
|
2
|
-
fields are listed). Make errors/<status>.ejs for a status that needs its own page.
|
|
2
|
+
fields are listed). Make errors/<status>.ejs for a status that needs its own page.
|
|
3
|
+
With @dokebi/session (npx dokebi add @dokebi/session) a refused form goes back to its own
|
|
4
|
+
page instead, which shows data.errors.<field> and data.old.<field>; this page is the
|
|
5
|
+
fallback (no session, no Referer of this host). Data also has errors and old. %>
|
|
3
6
|
<main>
|
|
4
7
|
<h1><%= data.title %></h1>
|
|
5
8
|
<p><% if (data.message) { %><%= data.message %><% } %></p>
|
|
@@ -10,7 +10,7 @@ import { createApp, createSSRApp, h } from 'vue';
|
|
|
10
10
|
if (document.querySelector('script[data-page][data-sealed="true"]'))
|
|
11
11
|
await (await import(/* @vite-ignore */ '/dokebi/seal/http.mjs')).sealInertia(http);
|
|
12
12
|
|
|
13
|
-
const files = import.meta.glob('./*/{Layout,Pages
|
|
13
|
+
const files = import.meta.glob('./*/{Layout,Pages/**/*}.vue', { eager: true });
|
|
14
14
|
const first = JSON.parse(document.querySelector('script[data-page]')?.textContent || '{}');
|
|
15
15
|
const template = first.props?.app?.template ?? 'default';
|
|
16
16
|
const pick = (path) => files[`./${template}/${path}.vue`] ?? files[`./default/${path}.vue`];
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { createInertiaApp } from '@inertiajs/vue3';
|
|
6
6
|
import { renderToString } from 'vue/server-renderer';
|
|
7
7
|
|
|
8
|
-
const files = import.meta.glob('./*/{Layout,Pages
|
|
8
|
+
const files = import.meta.glob('./*/{Layout,Pages/**/*}.vue', { eager: true });
|
|
9
9
|
|
|
10
10
|
export async function render(page) {
|
|
11
11
|
const template = page.props?.app?.template ?? 'default';
|