fondu 0.14.0 → 0.15.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.
- package/dist/client/app.js +1 -1
- package/dist/client/navigation.js +46 -27
- package/dist/client.d.ts +96 -10
- package/dist/client.js +65 -0
- package/dist/server/app.d.ts +2 -2
- package/dist/server/app.js +2 -2
- package/dist/server/handler.d.ts +13 -7
- package/package.json +1 -1
package/dist/client/app.js
CHANGED
|
@@ -210,7 +210,7 @@ export function Deferred({ data, fallback = null, failed = fallback, children, }
|
|
|
210
210
|
if (!page)
|
|
211
211
|
throw new Error('fondu: <Deferred> is for components of a page');
|
|
212
212
|
const names = [data].flat();
|
|
213
|
-
if (names.every((name) =>
|
|
213
|
+
if (names.every((name) => Object.hasOwn(page.props, name)))
|
|
214
214
|
return children;
|
|
215
215
|
return names.some((name) => page.deferred?.includes(name)) ? fallback : failed;
|
|
216
216
|
}
|
|
@@ -43,36 +43,43 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
43
43
|
const headers = () => ({ 'X-Fondu': 'true', ...(version && { 'X-Fondu-Version': version }) });
|
|
44
44
|
/** where a page's JSON is: its URL, or its file (a prerendered build) */
|
|
45
45
|
const source = (url) => prerender ? pageDataPath(url.pathname) + url.search : url.pathname + url.search;
|
|
46
|
-
// What a
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
46
|
+
// What a link's page answered when asked early (`prerender`), by the URL a navigation would ask: its file (a
|
|
47
|
+
// Response, read into memory), or null (no file: 204, its data is asked for at the click, when its loader should
|
|
48
|
+
// run). A file runs nothing, so asking early costs the app nothing. Asked once in 30 seconds (TanStack Router's
|
|
49
|
+
// default: a deploy in between is found at the click); the click waits for an answer on its way and reads a file
|
|
50
|
+
// once, its version checked as for any page object. The last 20
|
|
51
|
+
const freshFor = 30_000;
|
|
50
52
|
const prefetched = new Map();
|
|
51
|
-
const
|
|
52
|
-
const prefetch = async (url) => {
|
|
53
|
+
const prefetch = (url) => {
|
|
53
54
|
if (!prerender)
|
|
54
55
|
return;
|
|
55
56
|
const key = source(url);
|
|
56
|
-
|
|
57
|
+
const entry = prefetched.get(key);
|
|
58
|
+
if (entry && Date.now() - entry.at < freshFor)
|
|
57
59
|
return;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (prefetched.size > 20)
|
|
64
|
-
prefetched.delete(prefetched.keys().next().value);
|
|
60
|
+
const answer = (async () => {
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(key, { headers: { ...headers(), 'X-Fondu-Prefetch': 'true' } });
|
|
63
|
+
if (response.status === 200 && response.headers.has('X-Fondu'))
|
|
64
|
+
return new Response(await response.text(), { headers: response.headers });
|
|
65
65
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
catch {
|
|
67
|
+
// offline: asked for again
|
|
68
|
+
prefetched.delete(key);
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
})();
|
|
72
|
+
prefetched.delete(key);
|
|
73
|
+
prefetched.set(key, { answer, at: Date.now() });
|
|
74
|
+
if (prefetched.size > 20)
|
|
75
|
+
prefetched.delete(prefetched.keys().next().value);
|
|
69
76
|
};
|
|
70
77
|
/**
|
|
71
78
|
* Props of the page on the screen, asked for with a request of its own: the loader's answer for the same
|
|
72
79
|
* component, with these props only. Quiet: no navigation state, no history entry, no scroll; it is aborted when
|
|
73
80
|
* another page is put on the screen, and an answer for a page no longer there is dropped; whatever isn't the
|
|
74
|
-
* page's answer is a failure, never a navigation. What came is merged into the page, its state kept; what didn't
|
|
75
|
-
* says it failed)
|
|
81
|
+
* page's answer is a failure, never a navigation. What came is merged into the page, its state kept; what didn't
|
|
82
|
+
* is no longer pending (`<Deferred>` says it failed)
|
|
76
83
|
*/
|
|
77
84
|
ask = async (names) => {
|
|
78
85
|
const at = shows;
|
|
@@ -80,7 +87,7 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
80
87
|
const controller = new AbortController();
|
|
81
88
|
asking.add(controller);
|
|
82
89
|
// a prop asked for that is neither here nor coming (it failed): pending again, `<Deferred>` shows its fallback
|
|
83
|
-
const absent = names.filter((n) => !(
|
|
90
|
+
const absent = names.filter((n) => !Object.hasOwn(page.props, n) && !page.deferred?.includes(n));
|
|
84
91
|
if (absent.length)
|
|
85
92
|
void show((on = { ...page, deferred: [...(page.deferred ?? []), ...absent] }), false, () => shows !== at);
|
|
86
93
|
let got = {};
|
|
@@ -150,9 +157,9 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
150
157
|
if (!post)
|
|
151
158
|
preload(url.pathname);
|
|
152
159
|
const from = !post && prerender ? source(url) : url.pathname + url.search;
|
|
153
|
-
// a GET that is not asked for again: the answer kept for it
|
|
154
|
-
const
|
|
155
|
-
|
|
160
|
+
// a GET that is not asked for again: the answer kept for it (never a form's POST, reload() or Back's)
|
|
161
|
+
const found = !post && !options.keep && !shown ? prefetched.get(from) : undefined;
|
|
162
|
+
const entry = found && Date.now() - found.at < freshFor ? found : undefined;
|
|
156
163
|
void (async () => {
|
|
157
164
|
try {
|
|
158
165
|
if (shown) {
|
|
@@ -163,6 +170,13 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
163
170
|
announce();
|
|
164
171
|
restore(options.scroll, url.hash);
|
|
165
172
|
}
|
|
173
|
+
// the file asked for early, waited for if it is on its way; none (null): the app is asked
|
|
174
|
+
const early = entry ? await entry.answer : null;
|
|
175
|
+
if (signal.aborted)
|
|
176
|
+
return;
|
|
177
|
+
// a Response is read once; a null stays, so the rests that follow ask nothing
|
|
178
|
+
if (early && prefetched.get(from) === entry)
|
|
179
|
+
prefetched.delete(from);
|
|
166
180
|
const response = early ??
|
|
167
181
|
(await fetch(from, {
|
|
168
182
|
method: post ? 'POST' : 'GET',
|
|
@@ -231,11 +245,12 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
231
245
|
// the page stays (reload(), Back's, a form's answer): what it asked for isn't taken away while it is asked again
|
|
232
246
|
if (!fresh && page.deferred)
|
|
233
247
|
for (const name of page.deferred)
|
|
234
|
-
if (
|
|
248
|
+
if (Object.hasOwn(on.props, name))
|
|
235
249
|
page.props[name] ??= on.props[name];
|
|
236
250
|
// its URL first, as the browser's own navigations: a page's render reads it (`location`, a URL's language);
|
|
237
|
-
// a redirect's, the fragment asked for if it is still this page's
|
|
238
|
-
|
|
251
|
+
// a redirect's, the fragment asked for if it is still this page's; a form's answer to the page it was sent
|
|
252
|
+
// from: its #fragment stays, a dialog the fragment opened with it
|
|
253
|
+
const target = page.url + (same ? location.hash : asked === page.url ? url.hash : '');
|
|
239
254
|
// a form's answer at its URL replaces the entry; Back's or reload()'s page redirected elsewhere too
|
|
240
255
|
const history = options.history === 'push' && same
|
|
241
256
|
? 'replace'
|
|
@@ -374,8 +389,12 @@ export function startNavigation({ initial, show, prerender, pending, preload, is
|
|
|
374
389
|
// Back and Forward: the entry's page, where it was scrolled
|
|
375
390
|
window.addEventListener('popstate', (event) => {
|
|
376
391
|
const popped = scroll.pop(event.state);
|
|
377
|
-
if (popped.fragment)
|
|
392
|
+
if (popped.fragment) {
|
|
393
|
+
// a #fragment that names no element (a dialog's `#settings/account`) leaves the page where it is, as the browser does
|
|
394
|
+
if (!popped.position && location.hash && !fragmentTarget(location.hash.slice(1)))
|
|
395
|
+
return;
|
|
378
396
|
return restore(popped.position, location.hash);
|
|
397
|
+
}
|
|
379
398
|
visit(location.href, {
|
|
380
399
|
history: 'none',
|
|
381
400
|
scroll: popped.position,
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TypedResponse } from 'hono';
|
|
2
|
+
import type { StatusCode } from 'hono/utils/http-status';
|
|
3
|
+
import type { answerOf, schemasOf } from './server/handler.js';
|
|
2
4
|
import type { Routes } from './types.js';
|
|
3
5
|
type Keys = keyof Routes & string;
|
|
4
6
|
type Actions = {
|
|
@@ -22,9 +24,10 @@ type HandlersOf<P extends string> = P extends `${infer K}?${infer A}` ? K extend
|
|
|
22
24
|
} ? G : never : never;
|
|
23
25
|
/** a query value: what its schema takes (`unknown`, as a coercing schema's input: any) */
|
|
24
26
|
type QueryValue = string | number | boolean | null | undefined;
|
|
25
|
-
|
|
27
|
+
/** the query a handler's schema takes (a `withValidator` handler; any query otherwise) */
|
|
28
|
+
type QueryOfHandler<H> = [H] extends [{
|
|
26
29
|
readonly [schemasOf]?: infer S;
|
|
27
|
-
} ? S extends {
|
|
30
|
+
}] ? S extends {
|
|
28
31
|
query: {
|
|
29
32
|
'~standard': {
|
|
30
33
|
types?: infer T;
|
|
@@ -35,17 +38,22 @@ type QueryOf<P extends string> = HandlersOf<P> extends {
|
|
|
35
38
|
} ? {
|
|
36
39
|
[K in keyof I]: unknown extends I[K] ? QueryValue : I[K];
|
|
37
40
|
} : Record<string, QueryValue> : Record<string, QueryValue> : Record<string, QueryValue>;
|
|
41
|
+
type QueryOf<P extends string> = QueryOfHandler<HandlersOf<P>>;
|
|
42
|
+
/** `query`, required when its schema requires a field */
|
|
43
|
+
type QueryArg<Q> = {} extends Q ? {
|
|
44
|
+
query?: Q;
|
|
45
|
+
} : {
|
|
46
|
+
query: Q;
|
|
47
|
+
};
|
|
38
48
|
/** `query` is required when its schema requires a field */
|
|
39
49
|
type Extra<P extends string> = {
|
|
40
50
|
hash?: string;
|
|
41
|
-
} &
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
query: QueryOf<P>;
|
|
45
|
-
});
|
|
46
|
-
type Args<P extends string> = [ParamNames<P>] extends [never] ? {} extends QueryOf<P> ? [options?: Extra<P>] : [options: Extra<P>] : [params: {
|
|
51
|
+
} & QueryArg<QueryOf<P>>;
|
|
52
|
+
/** the params of a URL pattern, as the options take them */
|
|
53
|
+
type ParamArgs<P extends string> = {
|
|
47
54
|
[K in ParamNames<P>]: string | number;
|
|
48
|
-
}
|
|
55
|
+
};
|
|
56
|
+
type Args<P extends string> = [ParamNames<P>] extends [never] ? {} extends QueryOf<P> ? [options?: Extra<P>] : [options: Extra<P>] : [params: ParamArgs<P> & Extra<P>];
|
|
49
57
|
/**
|
|
50
58
|
* Typed URLs from `app/routes/` (as React Router's `href`): `href('/users/:id', { id })` → `'/users/7'`;
|
|
51
59
|
* one of a file's `actions`: `href('/users/:id?delete', { id })`. Only patterns that exist; params are required.
|
|
@@ -57,3 +65,81 @@ export { usePage, useNavigation, reload, Deferred, __entry, renderPage, type Err
|
|
|
57
65
|
export { useForm } from './client/form.js';
|
|
58
66
|
export { loadDocument, navigate, type Page } from './client/navigation.js';
|
|
59
67
|
export type { ProgressOptions } from './client/progress.js';
|
|
68
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
69
|
+
/** no generated types (or none of routes): everything is loose, as `Href` is */
|
|
70
|
+
type Loose = string extends Keys ? true : [Keys] extends [never] ? true : false;
|
|
71
|
+
/** the HTTP methods a route file exports (a page has none) */
|
|
72
|
+
type MethodsOf<P extends string> = Loose extends true ? HttpMethod : P extends Keys ? keyof Routes[P] & HttpMethod : never;
|
|
73
|
+
/**
|
|
74
|
+
* The URL patterns of route files that export an HTTP method (`.ts` files). A page's pattern is not one: a page's
|
|
75
|
+
* data is its props (its loader, `defer()`), and `fetch()` is for effects and events.
|
|
76
|
+
*/
|
|
77
|
+
export type ApiPath = Loose extends true ? string : {
|
|
78
|
+
[P in Keys]: [MethodsOf<P>] extends [never] ? never : P;
|
|
79
|
+
}[Keys];
|
|
80
|
+
/** the handler of the route's method */
|
|
81
|
+
type HandlerOf<P extends string, M extends string> = Loose extends true ? unknown : P extends Keys ? Routes[P] extends {
|
|
82
|
+
[K in M]: infer H;
|
|
83
|
+
} ? H : never : never;
|
|
84
|
+
/** what the handler's `withValidator` body schema takes (any JSON when it has none) */
|
|
85
|
+
type BodyOfHandler<H> = [H] extends [{
|
|
86
|
+
readonly [schemasOf]?: infer S;
|
|
87
|
+
}] ? S extends {
|
|
88
|
+
body: {
|
|
89
|
+
'~standard': {
|
|
90
|
+
types?: infer T;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
} ? NonNullable<T> extends {
|
|
94
|
+
input: infer I;
|
|
95
|
+
} ? I : unknown : unknown : unknown;
|
|
96
|
+
type BodyArg<M extends string, B> = M extends 'GET' ? {
|
|
97
|
+
body?: never;
|
|
98
|
+
} : {} extends B ? {
|
|
99
|
+
body?: B;
|
|
100
|
+
} : undefined extends B ? {
|
|
101
|
+
body?: B;
|
|
102
|
+
} : {
|
|
103
|
+
body: B;
|
|
104
|
+
};
|
|
105
|
+
/** `method`: omitted is GET, so required when the route has no GET */
|
|
106
|
+
type MethodArg<P extends string, M extends string> = 'GET' extends MethodsOf<P> ? {
|
|
107
|
+
method?: M;
|
|
108
|
+
} : {
|
|
109
|
+
method: M;
|
|
110
|
+
};
|
|
111
|
+
type FetchOptions<P extends string, M extends HttpMethod> = ParamArgs<P> & MethodArg<P, M> & QueryArg<QueryOfHandler<HandlerOf<P, M>>> & BodyArg<M, BodyOfHandler<HandlerOf<P, M>>> & {
|
|
112
|
+
headers?: HeadersInit;
|
|
113
|
+
signal?: AbortSignal;
|
|
114
|
+
};
|
|
115
|
+
/** the options are optional when nothing is required: no params, no `body`, `query` or `method` to give */
|
|
116
|
+
export type FetchArgs<P extends string, M extends HttpMethod> = {} extends FetchOptions<P, M> ? [options?: FetchOptions<P, M>] : [options: FetchOptions<P, M>];
|
|
117
|
+
/** what a handler returns, in the type: a `defineHandler(...)` tuple, a `withValidator` handler, a function */
|
|
118
|
+
type ReturnedBy<H> = typeof answerOf extends keyof H ? NonNullable<H[typeof answerOf & keyof H]> : H extends readonly [...unknown[], infer Last] ? Returned<Last> : Returned<H>;
|
|
119
|
+
type Returned<F> = F extends (...args: never[]) => infer R ? Exclude<Awaited<R>, void> : unknown;
|
|
120
|
+
/**
|
|
121
|
+
* The answer as JSON delivers it: Hono's `c.json(data)` is a `TypedResponse` whose data is already what JSON makes of
|
|
122
|
+
* it (a `Date` is a string, `undefined` fields are dropped). Any other answer (text, a redirect, a body, or a
|
|
123
|
+
* branch that is not `c.json`) is `unknown`.
|
|
124
|
+
*/
|
|
125
|
+
type AnswerOfResponse<R> = [R] extends [never] ? unknown : [R] extends [TypedResponse<unknown, StatusCode, 'json'>] ? R extends TypedResponse<infer T, StatusCode, 'json'> ? T : never : unknown;
|
|
126
|
+
export type AnswerOf<P extends string, M extends string> = Loose extends true ? unknown : AnswerOfResponse<ReturnedBy<HandlerOf<P, M>>>;
|
|
127
|
+
/** a non-2xx answer: `status`, `data` (the parsed body: a 422's `{ errors }`) and the `response` */
|
|
128
|
+
export declare class FetchError extends Error {
|
|
129
|
+
readonly status: number;
|
|
130
|
+
readonly data: unknown;
|
|
131
|
+
readonly response: Response;
|
|
132
|
+
constructor(method: string, url: string, response: Response, data: unknown);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Calls a `.ts` route of the app from the browser (an effect, an event), typed by the route file: the params as
|
|
136
|
+
* `href()` takes them, `query` and `body` by the handler's `withValidator` schemas, `method` one the file exports
|
|
137
|
+
* (GET when left out), the answer by what the handler returns with `c.json()`, as JSON delivers it.
|
|
138
|
+
*
|
|
139
|
+
* const usage = await fetch('/api/usage')
|
|
140
|
+
* await fetch('/api/settings', { method: 'POST', body: { emails: false } })
|
|
141
|
+
*
|
|
142
|
+
* A non-2xx answer throws a `FetchError`. No retry, no cache. A page's data comes from its loader or `defer()`:
|
|
143
|
+
* called during the server render, it throws. (Follows Void's typed fetch.)
|
|
144
|
+
*/
|
|
145
|
+
export declare function fetch<P extends ApiPath, M extends MethodsOf<P> & HttpMethod = Extract<MethodsOf<P>, 'GET'>>(path: P, ...[options]: FetchArgs<P, M>): Promise<AnswerOf<P, M>>;
|
package/dist/client.js
CHANGED
|
@@ -7,6 +7,9 @@ import { fillPattern } from './paths.js';
|
|
|
7
7
|
*/
|
|
8
8
|
export function href(path, ...[options]) {
|
|
9
9
|
const { query, hash, ...params } = (options ?? {});
|
|
10
|
+
return buildUrl(path, params, query, hash);
|
|
11
|
+
}
|
|
12
|
+
function buildUrl(path, params, query, hash) {
|
|
10
13
|
let url = fillPattern(path, params);
|
|
11
14
|
const search = new URLSearchParams();
|
|
12
15
|
for (const [name, value] of Object.entries(query ?? {}))
|
|
@@ -23,3 +26,65 @@ export function href(path, ...[options]) {
|
|
|
23
26
|
export { usePage, useNavigation, reload, Deferred, __entry, renderPage, } from './client/app.js';
|
|
24
27
|
export { useForm } from './client/form.js';
|
|
25
28
|
export { loadDocument, navigate } from './client/navigation.js';
|
|
29
|
+
/** a non-2xx answer: `status`, `data` (the parsed body: a 422's `{ errors }`) and the `response` */
|
|
30
|
+
export class FetchError extends Error {
|
|
31
|
+
status;
|
|
32
|
+
data;
|
|
33
|
+
response;
|
|
34
|
+
constructor(method, url, response, data) {
|
|
35
|
+
super(`${method} ${url}: ${response.status}`);
|
|
36
|
+
this.name = 'FetchError';
|
|
37
|
+
this.status = response.status;
|
|
38
|
+
this.data = data;
|
|
39
|
+
this.response = response;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Calls a `.ts` route of the app from the browser (an effect, an event), typed by the route file: the params as
|
|
44
|
+
* `href()` takes them, `query` and `body` by the handler's `withValidator` schemas, `method` one the file exports
|
|
45
|
+
* (GET when left out), the answer by what the handler returns with `c.json()`, as JSON delivers it.
|
|
46
|
+
*
|
|
47
|
+
* const usage = await fetch('/api/usage')
|
|
48
|
+
* await fetch('/api/settings', { method: 'POST', body: { emails: false } })
|
|
49
|
+
*
|
|
50
|
+
* A non-2xx answer throws a `FetchError`. No retry, no cache. A page's data comes from its loader or `defer()`:
|
|
51
|
+
* called during the server render, it throws. (Follows Void's typed fetch.)
|
|
52
|
+
*/
|
|
53
|
+
export async function fetch(path, ...[options]) {
|
|
54
|
+
if (typeof document === 'undefined')
|
|
55
|
+
throw new Error("fondu: fetch() from fondu/client is for the browser (effects, events); a page's data comes from its loader or defer()");
|
|
56
|
+
const { method = 'GET', query, body, headers, signal, ...params } = (options ?? {});
|
|
57
|
+
const url = buildUrl(path, params, query, undefined);
|
|
58
|
+
// a Headers object: the call's headers replace ours whatever their case
|
|
59
|
+
const all = new Headers({ Accept: 'application/json' });
|
|
60
|
+
if (body !== undefined)
|
|
61
|
+
all.set('Content-Type', 'application/json');
|
|
62
|
+
new Headers(headers).forEach((value, name) => all.set(name, value));
|
|
63
|
+
const response = await globalThis.fetch(url, {
|
|
64
|
+
method,
|
|
65
|
+
headers: all,
|
|
66
|
+
body: JSON.stringify(body),
|
|
67
|
+
signal,
|
|
68
|
+
});
|
|
69
|
+
const data = await readAnswer(response);
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw new FetchError(method, url, response, data);
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
/** the answer's body: JSON when its content type says so, else text; none for 204 or an empty body */
|
|
75
|
+
async function readAnswer(response) {
|
|
76
|
+
const text = await response.text();
|
|
77
|
+
if (!text)
|
|
78
|
+
return undefined;
|
|
79
|
+
if (!/[/+]json\b/i.test(response.headers.get('content-type') ?? ''))
|
|
80
|
+
return text;
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(text);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// an error page that says it is JSON and isn't: its text is the data
|
|
86
|
+
if (!response.ok)
|
|
87
|
+
return text;
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/server/app.d.ts
CHANGED
|
@@ -28,8 +28,8 @@ export declare function checkExports(file: string, mod: Record<string, unknown>)
|
|
|
28
28
|
* - server code, `.ts`: HTTP handlers only, `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `OPTIONS` (not `loader`,
|
|
29
29
|
* `action`, `actions`: those are a page's)
|
|
30
30
|
* `_middleware.ts` applies to its directory and below (`routes/_middleware.ts`: every request, 404s included).
|
|
31
|
-
* Built in: CSRF protection, security headers, a request body limit, one URL per page (trailing slash), the error
|
|
32
|
-
* error handling: Hono's `app.notFound()` / `app.onError()` on the returned app.
|
|
31
|
+
* Built in: CSRF protection, security headers, a request body limit, one URL per page (trailing slash), the error
|
|
32
|
+
* page. Your own 404 / error handling: Hono's `app.notFound()` / `app.onError()` on the returned app.
|
|
33
33
|
*/
|
|
34
34
|
export declare function createApp(options?: CreateAppOptions): Hono;
|
|
35
35
|
export {};
|
package/dist/server/app.js
CHANGED
|
@@ -94,8 +94,8 @@ function actionName(url) {
|
|
|
94
94
|
* - server code, `.ts`: HTTP handlers only, `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `OPTIONS` (not `loader`,
|
|
95
95
|
* `action`, `actions`: those are a page's)
|
|
96
96
|
* `_middleware.ts` applies to its directory and below (`routes/_middleware.ts`: every request, 404s included).
|
|
97
|
-
* Built in: CSRF protection, security headers, a request body limit, one URL per page (trailing slash), the error
|
|
98
|
-
* error handling: Hono's `app.notFound()` / `app.onError()` on the returned app.
|
|
97
|
+
* Built in: CSRF protection, security headers, a request body limit, one URL per page (trailing slash), the error
|
|
98
|
+
* page. Your own 404 / error handling: Hono's `app.notFound()` / `app.onError()` on the returned app.
|
|
99
99
|
*/
|
|
100
100
|
export function createApp(options = {}) {
|
|
101
101
|
const app = new Hono();
|
package/dist/server/handler.d.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import type { Context, Handler, MiddlewareHandler } from 'hono';
|
|
2
2
|
import { type Input, type Schemas } from './validate.js';
|
|
3
|
-
type Final<S extends Schemas> = (c: Context, input: Input<S>) =>
|
|
3
|
+
type Final<S extends Schemas, R extends Response = Response> = (c: Context, input: Input<S>) => R | Promise<R>;
|
|
4
4
|
type Handlers = Array<Handler | MiddlewareHandler>;
|
|
5
5
|
/** @internal a type only, never a value: the schemas a `withValidator` handler checks, for `href()`'s `query` */
|
|
6
6
|
export declare const schemasOf: unique symbol;
|
|
7
|
-
/**
|
|
8
|
-
export
|
|
7
|
+
/** @internal a type only, never a value: the response the handler returns, for `fetch()`'s answer */
|
|
8
|
+
export declare const answerOf: unique symbol;
|
|
9
|
+
/**
|
|
10
|
+
* handlers of `withValidator`: with their schemas and the response they return, in the type only (`href()` types
|
|
11
|
+
* the query by the schemas, `fetch()` the answer by the response)
|
|
12
|
+
*/
|
|
13
|
+
export type Validated<S extends Schemas, R extends Response = Response> = Handlers & {
|
|
9
14
|
readonly [schemasOf]?: S;
|
|
15
|
+
readonly [answerOf]?: R;
|
|
10
16
|
};
|
|
11
17
|
/** overloads, as Hono's `createHandlers` has them: the last handler's parameters are typed reliably */
|
|
12
18
|
interface WithInput<S extends Schemas> {
|
|
13
|
-
(handler: Final<S>): Validated<S>;
|
|
14
|
-
(m1: MiddlewareHandler, handler: Final<S>): Validated<S>;
|
|
15
|
-
(m1: MiddlewareHandler, m2: MiddlewareHandler, handler: Final<S>): Validated<S>;
|
|
16
|
-
(m1: MiddlewareHandler, m2: MiddlewareHandler, m3: MiddlewareHandler, handler: Final<S>): Validated<S>;
|
|
19
|
+
<R extends Response>(handler: Final<S, R>): Validated<S, R>;
|
|
20
|
+
<R extends Response>(m1: MiddlewareHandler, handler: Final<S, R>): Validated<S, R>;
|
|
21
|
+
<R extends Response>(m1: MiddlewareHandler, m2: MiddlewareHandler, handler: Final<S, R>): Validated<S, R>;
|
|
22
|
+
<R extends Response>(m1: MiddlewareHandler, m2: MiddlewareHandler, m3: MiddlewareHandler, handler: Final<S, R>): Validated<S, R>;
|
|
17
23
|
}
|
|
18
24
|
/**
|
|
19
25
|
* Validated input for the handler, after the middleware (a guard runs before the request body is read):
|