ovr 6.0.1 → 6.2.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/LICENSE.md +2 -0
- package/dist/app/index.d.ts +1 -1
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +17 -11
- package/dist/context/index.d.ts +37 -13
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +70 -29
- package/dist/cookie/index.d.ts +1 -1
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +2 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/jsx/elements.d.ts +194 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +18 -8
- package/dist/middleware/index.d.ts +12 -3
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +5 -0
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +22 -24
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +6 -4
- package/dist/route/index.d.ts +143 -42
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +95 -43
- package/dist/schema/index.d.ts +1286 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +1321 -0
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +8 -2
- package/dist/util/index.d.ts +138 -9
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +152 -19
- package/package.json +4 -1
- package/src/app/index.ts +17 -15
- package/src/context/index.ts +116 -40
- package/src/cookie/index.ts +5 -5
- package/src/index.ts +4 -3
- package/src/jsx/elements.ts +320 -64
- package/src/jsx/index.ts +16 -8
- package/src/middleware/index.ts +19 -8
- package/src/multipart/index.ts +25 -27
- package/src/render/index.ts +7 -4
- package/src/route/index.ts +338 -94
- package/src/schema/index.ts +2794 -0
- package/src/trie/index.ts +12 -2
- package/src/util/index.ts +193 -19
package/dist/route/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
2
|
import type { Middleware } from "../middleware/index.js";
|
|
3
|
+
import { Form as FormSchema } from "../schema/index.js";
|
|
3
4
|
import type { Trie } from "../trie/index.js";
|
|
4
|
-
|
|
5
|
+
import type { Util } from "../util/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Helper type to extract the route params (`:slug`) into a record.
|
|
8
|
+
*
|
|
9
|
+
* @template Pattern Route pattern to extract params from
|
|
10
|
+
*/
|
|
5
11
|
export type ExtractParams<Pattern extends string = string> = Pattern extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
6
12
|
[k in Param | keyof ExtractParams<Rest>]: string;
|
|
7
13
|
} : Pattern extends `${infer _Start}:${infer Param}` ? {
|
|
@@ -9,17 +15,83 @@ export type ExtractParams<Pattern extends string = string> = Pattern extends `${
|
|
|
9
15
|
} : Pattern extends `${infer _Rest}*` ? {
|
|
10
16
|
"*": string;
|
|
11
17
|
} : {};
|
|
12
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Helper type to insert a record of params into a resolved string.
|
|
20
|
+
*
|
|
21
|
+
* @template Pattern Route pattern to resolve
|
|
22
|
+
* @template Params Params to insert into pattern
|
|
23
|
+
*/
|
|
13
24
|
export type InsertParams<Pattern extends string, Params extends Trie.Params> = Pattern extends `${infer Start}:${infer Param}/${infer Rest}` ? Param extends keyof Params ? `${Start}${Params[Param]}/${InsertParams<Rest, Params>}` : Pattern : Pattern extends `${infer Start}:${infer Param}` ? Param extends keyof Params ? `${Start}${Params[Param]}` : Pattern : Pattern extends `${infer Start}*` ? "*" extends keyof Params ? `${Start}${Params["*"]}` : Pattern : Pattern;
|
|
14
25
|
export declare namespace Route {
|
|
15
26
|
/** HTTP Method */
|
|
16
|
-
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
27
|
+
export type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
28
|
+
/**
|
|
29
|
+
* Helper type for a route with a `<Button>` component.
|
|
30
|
+
*
|
|
31
|
+
* @template Pattern Route pattern
|
|
32
|
+
*/
|
|
33
|
+
type WithButton<Pattern extends string = string> = {
|
|
34
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes */
|
|
35
|
+
readonly Button: Route.Button<Pattern>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Helper type for a route with a `<Form>` component.
|
|
39
|
+
*
|
|
40
|
+
* @template Pattern Route pattern
|
|
41
|
+
*/
|
|
42
|
+
type WithForm<Pattern extends string = string> = {
|
|
43
|
+
/** `<form>` component with preset `method` and `action` attributes */
|
|
44
|
+
readonly Form: Route.Form<Pattern>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Helper type for a route with a `<Anchor>` component.
|
|
48
|
+
*
|
|
49
|
+
* @template Pattern Route pattern
|
|
50
|
+
*/
|
|
51
|
+
type WithAnchor<Pattern extends string = string> = {
|
|
52
|
+
/** `<a>` component with preset `href` attribute */
|
|
53
|
+
readonly Anchor: Route.Anchor<Pattern>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Form method subset attached to route helper types.
|
|
57
|
+
*
|
|
58
|
+
* Omits `component` so schema-specific `component` signatures are not widened when a
|
|
59
|
+
* concrete `Form<S>` is intersected onto the route type.
|
|
60
|
+
*/
|
|
61
|
+
type FormMethods = Omit<Route.Schema<any>, "component">;
|
|
62
|
+
/**
|
|
63
|
+
* Optional form helper methods available when a route may have a schema.
|
|
64
|
+
*
|
|
65
|
+
* Function members are wrapped with bivariant parameters so routes can be
|
|
66
|
+
* structurally assigned across compatible generic instances.
|
|
67
|
+
*/
|
|
68
|
+
type MaybeForm = {
|
|
69
|
+
[K in keyof FormMethods]?: FormMethods[K] extends (...args: any[]) => any ? Util.Bivariant<FormMethods[K]> : FormMethods[K];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* GET route type with route components and optional schema helpers.
|
|
73
|
+
*
|
|
74
|
+
* @template Pattern Route pattern
|
|
75
|
+
*/
|
|
76
|
+
export type Get<Pattern extends string = string> = Route<Pattern> & WithAnchor<Pattern> & WithButton<Pattern> & WithForm<Pattern> & MaybeForm;
|
|
77
|
+
/**
|
|
78
|
+
* POST route type with route components and optional schema helpers.
|
|
79
|
+
*
|
|
80
|
+
* @template Pattern Route pattern
|
|
81
|
+
*/
|
|
82
|
+
export type Post<Pattern extends string = string> = Route<Pattern> & WithButton<Pattern> & WithForm<Pattern> & MaybeForm;
|
|
83
|
+
/**
|
|
84
|
+
* Schema helper type attached to routes that have a form schema.
|
|
85
|
+
*
|
|
86
|
+
* @template S Form field shape
|
|
87
|
+
*/
|
|
88
|
+
export type Schema<S extends FormSchema.Shape = FormSchema.Shape> = FormSchema<S>;
|
|
17
89
|
/**
|
|
18
90
|
* Options to construct a relative URL from the route.
|
|
19
91
|
*
|
|
20
92
|
* @template Params Parameters created from a route match
|
|
21
93
|
*/
|
|
22
|
-
type URLOptions<Params extends Trie.Params> = {
|
|
94
|
+
export type URLOptions<Params extends Trie.Params> = {
|
|
23
95
|
/**
|
|
24
96
|
* Passed into `URLSearchParams` constructor to create new params.
|
|
25
97
|
*
|
|
@@ -44,51 +116,31 @@ export declare namespace Route {
|
|
|
44
116
|
*
|
|
45
117
|
* @template Pattern Route pattern
|
|
46
118
|
*/
|
|
47
|
-
type Anchor<Pattern extends string> = (props: JSX.IntrinsicElements["a"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
119
|
+
export type Anchor<Pattern extends string> = (props: JSX.IntrinsicElements["a"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
48
120
|
/**
|
|
49
121
|
* `<Button>` route component type
|
|
50
122
|
*
|
|
51
123
|
* @template Pattern Route pattern
|
|
52
124
|
*/
|
|
53
|
-
type Button<Pattern extends string> = (props: JSX.IntrinsicElements["button"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
125
|
+
export type Button<Pattern extends string> = (props: JSX.IntrinsicElements["button"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
54
126
|
/**
|
|
55
127
|
* `<Form>` route component type
|
|
56
128
|
*
|
|
57
129
|
* @template Pattern Route pattern
|
|
58
130
|
*/
|
|
59
|
-
type Form<Pattern extends string> = (props: JSX.IntrinsicElements["form"] & URLOptions<ExtractParams<Pattern>>
|
|
60
|
-
|
|
61
|
-
|
|
131
|
+
export type Form<Pattern extends string> = (props: JSX.IntrinsicElements["form"] & URLOptions<ExtractParams<Pattern>> & {
|
|
132
|
+
state?: URL;
|
|
133
|
+
}) => JSX.Element;
|
|
134
|
+
/**
|
|
135
|
+
* Extract params from a Route instance.
|
|
136
|
+
*
|
|
137
|
+
* @template Route Route with pattern to extract params from
|
|
138
|
+
*/
|
|
139
|
+
export type Params<Route> = Route extends {
|
|
62
140
|
pattern: infer P;
|
|
63
141
|
} ? P extends string ? ExtractParams<P> : never : never;
|
|
142
|
+
export {};
|
|
64
143
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Helper type for a route with a `<Button>` component.
|
|
67
|
-
*
|
|
68
|
-
* @template Pattern Route pattern
|
|
69
|
-
*/
|
|
70
|
-
type WithButton<Pattern extends string = string> = {
|
|
71
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes */
|
|
72
|
-
readonly Button: Route.Button<Pattern>;
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Helper type for a route with a `<Form>` component.
|
|
76
|
-
*
|
|
77
|
-
* @template Pattern Route pattern
|
|
78
|
-
*/
|
|
79
|
-
type WithForm<Pattern extends string = string> = {
|
|
80
|
-
/** `<form>` component with preset `method` and `action` attributes */
|
|
81
|
-
readonly Form: Route.Form<Pattern>;
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Helper type for a route with a `<Anchor>` component.
|
|
85
|
-
*
|
|
86
|
-
* @template Pattern Route pattern
|
|
87
|
-
*/
|
|
88
|
-
type WithAnchor<Pattern extends string = string> = {
|
|
89
|
-
/** `<a>` component with preset `href` attribute */
|
|
90
|
-
readonly Anchor: Route.Anchor<Pattern>;
|
|
91
|
-
};
|
|
92
144
|
/**
|
|
93
145
|
* Route to use in the application.
|
|
94
146
|
*
|
|
@@ -101,7 +153,7 @@ export declare class Route<Pattern extends string = string> {
|
|
|
101
153
|
/** HTTP method */
|
|
102
154
|
readonly method: Route.Method;
|
|
103
155
|
/** Route middleware stack, runs after global middleware */
|
|
104
|
-
readonly middleware: Middleware<any>[];
|
|
156
|
+
readonly middleware: Middleware<any, any>[];
|
|
105
157
|
/**
|
|
106
158
|
* Create a new route.
|
|
107
159
|
*
|
|
@@ -109,7 +161,7 @@ export declare class Route<Pattern extends string = string> {
|
|
|
109
161
|
* @param pattern Route pattern
|
|
110
162
|
* @param middleware Route middleware
|
|
111
163
|
*/
|
|
112
|
-
constructor(method: Route.Method, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern
|
|
164
|
+
constructor(method: Route.Method, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>, any>[]);
|
|
113
165
|
/**
|
|
114
166
|
* Constructs a _relative_ URL for the route.
|
|
115
167
|
*
|
|
@@ -129,19 +181,68 @@ export declare class Route<Pattern extends string = string> {
|
|
|
129
181
|
* @param middleware GET middleware
|
|
130
182
|
* @returns GET `Route` with added components
|
|
131
183
|
*/
|
|
132
|
-
static get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): Route<Pattern
|
|
184
|
+
static get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): Route.Get<Pattern>;
|
|
185
|
+
/**
|
|
186
|
+
* @template Pattern Route pattern
|
|
187
|
+
* @template S Form field shape
|
|
188
|
+
* @param pattern Route pattern
|
|
189
|
+
* @param fields Form field shape
|
|
190
|
+
* @param middleware GET middleware
|
|
191
|
+
* @returns GET `Route` with added components
|
|
192
|
+
*/
|
|
193
|
+
static get<Pattern extends string, S extends FormSchema.Shape>(pattern: Pattern, fields: S, ...middleware: Middleware<ExtractParams<Pattern>, S>[]): Route.Get<Pattern> & Route.Schema<S>;
|
|
194
|
+
/**
|
|
195
|
+
* @template Pattern Route pattern
|
|
196
|
+
* @template S Form field shape
|
|
197
|
+
* @param pattern Route pattern
|
|
198
|
+
* @param form Form schema
|
|
199
|
+
* @param middleware GET middleware
|
|
200
|
+
* @returns GET `Route` with added components
|
|
201
|
+
*/
|
|
202
|
+
static get<Pattern extends string, S extends FormSchema.Shape>(pattern: Pattern, form: Route.Schema<S>, ...middleware: Middleware<ExtractParams<Pattern>, S>[]): Route.Get<Pattern> & Route.Schema<S>;
|
|
133
203
|
/**
|
|
134
204
|
* @param middleware POST middleware
|
|
135
205
|
* @returns POST `Route` with added components
|
|
136
206
|
*/
|
|
137
|
-
static post(middleware: Middleware<{}>): Route
|
|
207
|
+
static post(...middleware: Middleware<{}>[]): Route.Post;
|
|
208
|
+
/**
|
|
209
|
+
* @template S Form field shape
|
|
210
|
+
* @param fields Form field shape
|
|
211
|
+
* @param middleware POST middleware
|
|
212
|
+
* @returns POST `Route` with added components
|
|
213
|
+
*/
|
|
214
|
+
static post<S extends FormSchema.Shape>(fields: S, ...middleware: Middleware<{}, S>[]): Route.Post & Route.Schema<S>;
|
|
215
|
+
/**
|
|
216
|
+
* @template S Form field shape
|
|
217
|
+
* @param form Form schema
|
|
218
|
+
* @param middleware POST middleware
|
|
219
|
+
* @returns POST `Route` with added components
|
|
220
|
+
*/
|
|
221
|
+
static post<S extends FormSchema.Shape>(form: Route.Schema<S>, ...middleware: Middleware<{}, S>[]): Route.Post & Route.Schema<S>;
|
|
222
|
+
/**
|
|
223
|
+
* @template Pattern Route pattern
|
|
224
|
+
* @param pattern Route pattern
|
|
225
|
+
* @param middleware POST middleware
|
|
226
|
+
* @returns POST `Route` with added components
|
|
227
|
+
*/
|
|
228
|
+
static post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): Route.Post<Pattern>;
|
|
229
|
+
/**
|
|
230
|
+
* @template Pattern Route pattern
|
|
231
|
+
* @template S Form field shape
|
|
232
|
+
* @param pattern Route pattern
|
|
233
|
+
* @param fields Form field shape
|
|
234
|
+
* @param middleware POST middleware
|
|
235
|
+
* @returns POST `Route` with added components
|
|
236
|
+
*/
|
|
237
|
+
static post<Pattern extends string, S extends FormSchema.Shape>(pattern: Pattern, fields: S, ...middleware: Middleware<ExtractParams<Pattern>, S>[]): Route.Post<Pattern> & Route.Schema<S>;
|
|
138
238
|
/**
|
|
139
239
|
* @template Pattern Route pattern
|
|
240
|
+
* @template S Form field shape
|
|
140
241
|
* @param pattern Route pattern
|
|
242
|
+
* @param form Form schema
|
|
141
243
|
* @param middleware POST middleware
|
|
142
244
|
* @returns POST `Route` with added components
|
|
143
245
|
*/
|
|
144
|
-
static post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern
|
|
246
|
+
static post<Pattern extends string, S extends FormSchema.Shape>(pattern: Pattern, form: Route.Schema<S>, ...middleware: Middleware<ExtractParams<Pattern>, S>[]): Route.Post<Pattern> & Route.Schema<S>;
|
|
145
247
|
}
|
|
146
|
-
export {};
|
|
147
248
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/route/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.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AOG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,SAAS,MAAM,EAAE,CAAC,SAAS,UAAU,CAAC,KAAK,EAC7D,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EACrB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,GACpD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;CAqDxC"}
|
package/dist/route/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx } from "../jsx/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Form as FormSchema } from "../schema/index.js";
|
|
3
|
+
import { Checksum, Method, Mime } from "../util/index.js";
|
|
3
4
|
/**
|
|
4
5
|
* Route to use in the application.
|
|
5
6
|
*
|
|
@@ -34,27 +35,21 @@ export class Route {
|
|
|
34
35
|
* @returns `pathname` + `search` + `hash`
|
|
35
36
|
*/
|
|
36
37
|
url(...[options]) {
|
|
37
|
-
|
|
38
|
+
return (this.pathname(
|
|
38
39
|
// @ts-expect-error - do not have to pass in {} if no params
|
|
39
|
-
options?.params)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
hash = "#" + options.hash;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return pathname + search + hash;
|
|
40
|
+
options?.params) +
|
|
41
|
+
(options?.search
|
|
42
|
+
? // use the value as the init
|
|
43
|
+
// @ts-expect-error - see type above
|
|
44
|
+
"?" + new URLSearchParams(options.search)
|
|
45
|
+
: "") +
|
|
46
|
+
(options?.hash
|
|
47
|
+
? // adding # prefix if not present matches the URL setter:
|
|
48
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
|
|
49
|
+
options.hash[0] === "#"
|
|
50
|
+
? options.hash
|
|
51
|
+
: "#" + options.hash
|
|
52
|
+
: ""));
|
|
58
53
|
}
|
|
59
54
|
/**
|
|
60
55
|
* @template Params Parameters to create the pathname with
|
|
@@ -81,7 +76,7 @@ export class Route {
|
|
|
81
76
|
* @returns Route with added components
|
|
82
77
|
*/
|
|
83
78
|
static #withComponents(route) {
|
|
84
|
-
const enctype = route.method ===
|
|
79
|
+
const enctype = route.method === Method.post ? Mime.type.mp : undefined;
|
|
85
80
|
return Object.assign(route, {
|
|
86
81
|
Button: (({ params, search, hash, ...rest }) => jsx("button", {
|
|
87
82
|
formaction: route.url({ params, search, hash }),
|
|
@@ -89,38 +84,95 @@ export class Route {
|
|
|
89
84
|
formenctype: enctype,
|
|
90
85
|
...rest,
|
|
91
86
|
})),
|
|
92
|
-
Form: (({ params, search, hash, ...rest }) =>
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
87
|
+
Form: (({ params, search, hash, state, ...rest }) => {
|
|
88
|
+
let query;
|
|
89
|
+
if (route.method === Method.post && state) {
|
|
90
|
+
const target = new URL(state);
|
|
91
|
+
for (const param of FormSchema.params) {
|
|
92
|
+
target.searchParams.delete(param);
|
|
93
|
+
}
|
|
94
|
+
query = new URLSearchParams(
|
|
95
|
+
// @ts-expect-error - see type above
|
|
96
|
+
search);
|
|
97
|
+
query.set(FormSchema.params[0], target.pathname + target.search + target.hash);
|
|
98
|
+
}
|
|
99
|
+
return jsx("form", {
|
|
100
|
+
action: route.url({
|
|
101
|
+
params,
|
|
102
|
+
search: query ?? search,
|
|
103
|
+
hash,
|
|
104
|
+
}),
|
|
105
|
+
method: route.method,
|
|
106
|
+
enctype,
|
|
107
|
+
...rest,
|
|
108
|
+
});
|
|
109
|
+
}),
|
|
98
110
|
});
|
|
99
111
|
}
|
|
100
112
|
/**
|
|
101
113
|
* @template Pattern Route pattern
|
|
102
|
-
* @
|
|
103
|
-
* @param
|
|
104
|
-
* @
|
|
114
|
+
* @template R Route type with Form component
|
|
115
|
+
* @param route Route to add schema behavior to
|
|
116
|
+
* @param schema Form schema
|
|
117
|
+
* @returns Route with schema behavior attached
|
|
105
118
|
*/
|
|
106
|
-
static
|
|
107
|
-
|
|
108
|
-
|
|
119
|
+
static #withSchema(route, schema) {
|
|
120
|
+
if (!schema)
|
|
121
|
+
return route;
|
|
122
|
+
// original form shell from normal route
|
|
123
|
+
const { Form } = route;
|
|
124
|
+
return Object.assign(route, schema, {
|
|
125
|
+
Form: (({ state, ...rest }) => Form({
|
|
126
|
+
// add in the default children (Fields) with state and submit button
|
|
127
|
+
children: [
|
|
128
|
+
schema.Fields({ state }),
|
|
129
|
+
jsx("button", { children: "Submit" }),
|
|
130
|
+
],
|
|
131
|
+
state,
|
|
132
|
+
...rest,
|
|
133
|
+
})),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
static get(pattern, schemaOrMiddleware, ...middleware) {
|
|
137
|
+
let schema;
|
|
138
|
+
if (schemaOrMiddleware) {
|
|
139
|
+
if (typeof schemaOrMiddleware === "function") {
|
|
140
|
+
middleware.unshift(schemaOrMiddleware);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
schema = FormSchema.from(schemaOrMiddleware);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const route = Route.#withComponents(new Route(Method.get, pattern, ...middleware));
|
|
147
|
+
return Route.#withSchema(Object.assign(route, {
|
|
109
148
|
Anchor: (({ params, search, hash, ...rest }) => jsx("a", {
|
|
110
149
|
href: route.url({ params, search, hash }),
|
|
111
150
|
...rest,
|
|
112
151
|
})),
|
|
113
|
-
});
|
|
152
|
+
}), schema);
|
|
114
153
|
}
|
|
115
|
-
static post(
|
|
154
|
+
static post(patternOrSchemaOrMiddleware, schemaOrMiddleware, ...middleware) {
|
|
116
155
|
let pattern;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
156
|
+
let schema;
|
|
157
|
+
const resolve = (psm) => {
|
|
158
|
+
if (psm) {
|
|
159
|
+
if (typeof psm === "string") {
|
|
160
|
+
pattern = psm;
|
|
161
|
+
}
|
|
162
|
+
else if (typeof psm !== "function") {
|
|
163
|
+
schema = FormSchema.from(psm);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
middleware.unshift(psm);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
resolve(patternOrSchemaOrMiddleware);
|
|
171
|
+
resolve(schemaOrMiddleware);
|
|
172
|
+
if (!pattern) {
|
|
173
|
+
// create pattern with checksum of all mw stringified
|
|
174
|
+
pattern = `/_p/${Checksum.djb2(middleware.join())}`;
|
|
123
175
|
}
|
|
124
|
-
return Route.#withComponents(new Route(
|
|
176
|
+
return Route.#withSchema(Route.#withComponents(new Route(Method.post, pattern, ...middleware)), schema);
|
|
125
177
|
}
|
|
126
178
|
}
|