ovr 4.1.1 → 4.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.
- package/dist/app/context.d.ts +2 -2
- package/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +12 -10
- package/dist/app/helper/get.d.ts +30 -0
- package/dist/app/helper/get.d.ts.map +1 -0
- package/dist/app/helper/get.js +38 -0
- package/dist/app/helper/index.d.ts +23 -0
- package/dist/app/helper/index.d.ts.map +1 -0
- package/dist/app/helper/index.js +63 -0
- package/dist/app/{post.d.ts → helper/post.d.ts} +4 -13
- package/dist/app/helper/post.d.ts.map +1 -0
- package/dist/app/helper/post.js +15 -0
- package/dist/app/index.d.ts +5 -5
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +12 -12
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/app/context.ts +25 -17
- package/src/app/helper/get.ts +48 -0
- package/src/app/helper/index.ts +98 -0
- package/src/app/helper/post.ts +54 -0
- package/src/app/index.ts +13 -15
- package/src/index.ts +3 -3
- package/src/types/index.ts +18 -1
- package/dist/app/get.d.ts +0 -38
- package/dist/app/get.d.ts.map +0 -1
- package/dist/app/get.js +0 -59
- package/dist/app/post.d.ts.map +0 -1
- package/dist/app/post.js +0 -41
- package/dist/trie/insert-params.d.ts +0 -8
- package/dist/trie/insert-params.d.ts.map +0 -1
- package/dist/trie/insert-params.js +0 -24
- package/src/app/get.ts +0 -82
- package/src/app/post.ts +0 -89
- package/src/trie/insert-params.ts +0 -31
package/dist/app/context.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type { Route } from "../trie/index.js";
|
|
3
|
-
import { type ErrorHandler, type Middleware, type NotFoundHandler, type
|
|
2
|
+
import type { Params, Route } from "../trie/index.js";
|
|
3
|
+
import { type ErrorHandler, type Middleware, type NotFoundHandler, type TrailingSlash } from "./index.js";
|
|
4
4
|
type Layout = (props: {
|
|
5
5
|
children: JSX.Element;
|
|
6
6
|
}) => JSX.Element;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAe,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAe,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;AAShE,qBAAa,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IAC7C,gFAAgF;IAChF,GAAG,EAAE,OAAO,CAAC;IAEb,4EAA4E;IAC5E,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;OAOG;IACH,MAAM,EAAG,CAAC,CAAC;IAEX,oCAAoC;IACpC,KAAK,EAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE/B,+FAA+F;IAC/F,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAQ;IAE7B,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gFAAgF;IAChF,OAAO,UAAiB;IAQ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QG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IAed;;OAEG;IACH,KAAK;IA+BL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
|
package/dist/app/context.js
CHANGED
|
@@ -42,6 +42,7 @@ export class Context {
|
|
|
42
42
|
notFound;
|
|
43
43
|
#memo = new Memo();
|
|
44
44
|
memo = this.#memo.use;
|
|
45
|
+
static #encoder = new TextEncoder();
|
|
45
46
|
constructor(req, url, trailingSlash, base, error, notFound) {
|
|
46
47
|
this.req = req;
|
|
47
48
|
this.url = url;
|
|
@@ -170,24 +171,25 @@ export class Context {
|
|
|
170
171
|
// HTML partial - just use the layouts + page
|
|
171
172
|
gen = toGenerator(Page);
|
|
172
173
|
}
|
|
174
|
+
const close = (c) => {
|
|
175
|
+
c?.close();
|
|
176
|
+
gen.return();
|
|
177
|
+
};
|
|
173
178
|
let result;
|
|
174
179
|
this.html(new ReadableStream({
|
|
175
180
|
pull: async (c) => {
|
|
176
|
-
if (this.req.signal.aborted)
|
|
177
|
-
|
|
178
|
-
gen.return();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
+
if (this.req.signal.aborted)
|
|
182
|
+
return close(c);
|
|
181
183
|
result = await gen.next();
|
|
182
184
|
if (result.done)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
185
|
+
return close(c);
|
|
186
|
+
// need to encode for Node JS (ex: during prerendering)
|
|
187
|
+
c.enqueue(Context.#encoder.encode(result.value.value));
|
|
186
188
|
},
|
|
187
189
|
cancel() {
|
|
188
|
-
|
|
190
|
+
close();
|
|
189
191
|
},
|
|
190
|
-
}).
|
|
192
|
+
}, { highWaterMark: 16_000, size: (chunk) => chunk.length }), status);
|
|
191
193
|
}
|
|
192
194
|
/**
|
|
193
195
|
* Generates an etag from a hash of the string provided.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type JSX } from "../../jsx/index.js";
|
|
2
|
+
import type { AnchorProps, ExtractParams } from "../../types/index.js";
|
|
3
|
+
import type { Middleware } from "../index.js";
|
|
4
|
+
import { Helper } from "./index.js";
|
|
5
|
+
export declare class Get<Pattern extends string = string> extends Helper<Pattern> {
|
|
6
|
+
/** `<a>` component with preset `href` attribute. */
|
|
7
|
+
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
8
|
+
/**
|
|
9
|
+
* @param pattern Route pattern
|
|
10
|
+
* @param middleware GET middleware
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { Get } from "ovr";
|
|
16
|
+
*
|
|
17
|
+
* const page = new Get("/", () => {
|
|
18
|
+
* return <p>Hello world</p>
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const Nav = () => {
|
|
22
|
+
* return <page.Anchor>Home</page.Anchor>
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* app.add(page); register
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=get.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get.d.ts","sourceRoot":"","sources":["../../../src/app/helper/get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,qBAAa,GAAG,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IACxE,oDAAoD;IACpD,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE;;;;;;;;;;;;;;;;;;;OAmBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAgBpD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { jsx } from "../../jsx/index.js";
|
|
2
|
+
import { Helper } from "./index.js";
|
|
3
|
+
export class Get extends Helper {
|
|
4
|
+
/** `<a>` component with preset `href` attribute. */
|
|
5
|
+
Anchor;
|
|
6
|
+
/**
|
|
7
|
+
* @param pattern Route pattern
|
|
8
|
+
* @param middleware GET middleware
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { Get } from "ovr";
|
|
14
|
+
*
|
|
15
|
+
* const page = new Get("/", () => {
|
|
16
|
+
* return <p>Hello world</p>
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const Nav = () => {
|
|
20
|
+
* return <page.Anchor>Home</page.Anchor>
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* app.add(page); register
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
constructor(pattern, ...middleware) {
|
|
27
|
+
super("GET", pattern, ...middleware);
|
|
28
|
+
this.Anchor = (props) => {
|
|
29
|
+
const { params, ...rest } = props;
|
|
30
|
+
return jsx("a", {
|
|
31
|
+
href: this.pathname(
|
|
32
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
33
|
+
params),
|
|
34
|
+
...rest,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type JSX } from "../../jsx/index.js";
|
|
2
|
+
import type { ButtonProps, ExtractParams, FormProps, InsertParams } from "../../types/index.js";
|
|
3
|
+
import type { Middleware } from "../index.js";
|
|
4
|
+
export declare class Helper<Pattern extends string> {
|
|
5
|
+
#private;
|
|
6
|
+
/** Extracted parameters type for the pattern */
|
|
7
|
+
readonly Params: ExtractParams<Pattern>;
|
|
8
|
+
/** Route pattern */
|
|
9
|
+
pattern: Pattern;
|
|
10
|
+
/** GET middleware */
|
|
11
|
+
middleware: Middleware<any>[];
|
|
12
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
13
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
14
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
15
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
16
|
+
constructor(method: string, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
17
|
+
/**
|
|
18
|
+
* @param [params] Parameters to insert
|
|
19
|
+
* @returns Resolved `pathname` with params
|
|
20
|
+
*/
|
|
21
|
+
pathname<Params extends ExtractParams<Pattern>>(...[params]: keyof Params extends never ? [] : [Params]): InsertParams<Pattern, Params>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/helper/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EACX,WAAW,EACX,aAAa,EACb,SAAS,EACT,YAAY,EACZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,qBAAa,MAAM,CAAC,OAAO,SAAS,MAAM;;IACzC,gDAAgD;IAChD,SAAiB,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAEhD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,qBAAqB;IACrB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,iFAAiF;IACjF,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;gBAM/D,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;IAiCpD;;;OAGG;IACH,QAAQ,CAAC,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,EAC7C,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,SAAS,KAAK,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,GACrD,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC;CA2BhC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jsx } from "../../jsx/index.js";
|
|
2
|
+
export class Helper {
|
|
3
|
+
/** Route pattern */
|
|
4
|
+
pattern;
|
|
5
|
+
/** GET middleware */
|
|
6
|
+
middleware; // any so you can use other middleware
|
|
7
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
8
|
+
Button;
|
|
9
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
10
|
+
Form;
|
|
11
|
+
/** Pattern parts */
|
|
12
|
+
#parts;
|
|
13
|
+
constructor(method, pattern, ...middleware) {
|
|
14
|
+
this.pattern = pattern;
|
|
15
|
+
this.middleware = middleware;
|
|
16
|
+
this.#parts = pattern.split("/");
|
|
17
|
+
this.Button = (props) => {
|
|
18
|
+
const { params, ...rest } = props;
|
|
19
|
+
return jsx("button", {
|
|
20
|
+
formaction: this.pathname(
|
|
21
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
22
|
+
params),
|
|
23
|
+
formmethod: method,
|
|
24
|
+
...rest,
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
this.Form = (props) => {
|
|
28
|
+
const { params, ...rest } = props;
|
|
29
|
+
return jsx("form", {
|
|
30
|
+
action: this.pathname(
|
|
31
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
32
|
+
params),
|
|
33
|
+
method,
|
|
34
|
+
...rest,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @param [params] Parameters to insert
|
|
40
|
+
* @returns Resolved `pathname` with params
|
|
41
|
+
*/
|
|
42
|
+
pathname(...[params]) {
|
|
43
|
+
if (!params)
|
|
44
|
+
return this.pattern;
|
|
45
|
+
const wild = "*";
|
|
46
|
+
return this.#parts
|
|
47
|
+
.map((part) => {
|
|
48
|
+
if (part.startsWith(":")) {
|
|
49
|
+
const param = part.slice(1);
|
|
50
|
+
if (!(param in params))
|
|
51
|
+
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
52
|
+
return params[param];
|
|
53
|
+
}
|
|
54
|
+
if (part === wild) {
|
|
55
|
+
if (!(wild in params))
|
|
56
|
+
throw new Error("No wildcard parameter found.");
|
|
57
|
+
return params[wild];
|
|
58
|
+
}
|
|
59
|
+
return part;
|
|
60
|
+
})
|
|
61
|
+
.join("/");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
export declare class Post<Pattern extends string = string> {
|
|
5
|
-
#private;
|
|
6
|
-
/** Route pattern */
|
|
7
|
-
pattern: Pattern;
|
|
8
|
-
/** POST middleware */
|
|
9
|
-
middleware: Middleware<any>[];
|
|
10
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
11
|
-
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
12
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
13
|
-
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
1
|
+
import type { ExtractParams } from "../../types/index.js";
|
|
2
|
+
import type { Middleware } from "../index.js";
|
|
3
|
+
import { Helper } from "./index.js";
|
|
4
|
+
export declare class Post<Pattern extends string = string> extends Helper<Pattern> {
|
|
14
5
|
/**
|
|
15
6
|
* @param middleware POST middleware
|
|
16
7
|
*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../src/app/helper/post.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,qBAAa,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IACzE;;;;;;;;;;;;;;;;;;;;;;OAsBG;gBACS,GAAG,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,EAAE;IAC3C;;;OAGG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAiBpD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { hash } from "../../util/hash.js";
|
|
2
|
+
import { Helper } from "./index.js";
|
|
3
|
+
export class Post extends Helper {
|
|
4
|
+
constructor(patternOrMiddleware, ...middleware) {
|
|
5
|
+
let pattern;
|
|
6
|
+
if (typeof patternOrMiddleware === "string") {
|
|
7
|
+
pattern = patternOrMiddleware;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
middleware.unshift(patternOrMiddleware);
|
|
11
|
+
pattern = `/_p/${hash(middleware.join())}`;
|
|
12
|
+
}
|
|
13
|
+
super("POST", pattern, ...middleware);
|
|
14
|
+
}
|
|
15
|
+
}
|
package/dist/app/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { type Params } from "../trie/index.js";
|
|
1
2
|
import type { DeepArray, ExtractMultiParams, ExtractParams } from "../types/index.js";
|
|
2
3
|
import { Context } from "./context.js";
|
|
3
|
-
import { Get } from "./get.js";
|
|
4
|
-
import { Post } from "./post.js";
|
|
4
|
+
import { Get } from "./helper/get.js";
|
|
5
|
+
import { Post } from "./helper/post.js";
|
|
5
6
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
-
export type Params = Record<string, string>;
|
|
7
7
|
export type Middleware<P extends Params = Params> = (context: Context<P>, next: () => Promise<void>) => any;
|
|
8
8
|
export type TrailingSlash = "always" | "never" | "ignore";
|
|
9
9
|
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
@@ -57,10 +57,10 @@ export declare class App {
|
|
|
57
57
|
/** Stores context per request. */
|
|
58
58
|
static storage: AsyncLocalStorage<Context<Params>>;
|
|
59
59
|
/**
|
|
60
|
-
* @param
|
|
60
|
+
* @param helpers `Helper` to add to `App`
|
|
61
61
|
* @returns `App` instance
|
|
62
62
|
*/
|
|
63
|
-
add(...
|
|
63
|
+
add(...helpers: DeepArray<Get | Post | Record<string, Get | Post>>[]): this;
|
|
64
64
|
/**
|
|
65
65
|
* Add global middleware.
|
|
66
66
|
*
|
package/dist/app/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAe,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,MAAM,mBAAmB,CAAC;AAC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,IAAI,CAAC,OAAO,SAAS,MAAM,EAC1B,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,IAAI;IACP;;;;OAIG;IACH,IAAI,CAAC,QAAQ,SAAS,MAAM,EAAE,EAC7B,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,EACvB,GAAG,UAAU,EAAE,UAAU,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,EAAE,GACvD,IAAI;IAQP;;;OAGG;IACH,KAAK,GAAI,KAAK,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC,CA8DvC;CACF"}
|
package/dist/app/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Route, Trie } from "../trie/index.js";
|
|
2
2
|
import { Context } from "./context.js";
|
|
3
|
-
import { Get } from "./get.js";
|
|
4
|
-
import { Post } from "./post.js";
|
|
3
|
+
import { Get } from "./helper/get.js";
|
|
4
|
+
import { Post } from "./helper/post.js";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
export class App {
|
|
7
7
|
/** Built tries per HTTP method. */
|
|
@@ -54,22 +54,22 @@ export class App {
|
|
|
54
54
|
/** Stores context per request. */
|
|
55
55
|
static storage = new AsyncLocalStorage();
|
|
56
56
|
/**
|
|
57
|
-
* @param
|
|
57
|
+
* @param helpers `Helper` to add to `App`
|
|
58
58
|
* @returns `App` instance
|
|
59
59
|
*/
|
|
60
|
-
add(...
|
|
61
|
-
for (const
|
|
62
|
-
if (
|
|
63
|
-
this.add(...
|
|
60
|
+
add(...helpers) {
|
|
61
|
+
for (const helper of helpers) {
|
|
62
|
+
if (helper instanceof Array) {
|
|
63
|
+
this.add(...helper);
|
|
64
64
|
}
|
|
65
|
-
else if (
|
|
66
|
-
this.get(
|
|
65
|
+
else if (helper instanceof Get) {
|
|
66
|
+
this.get(helper.pattern, ...helper.middleware);
|
|
67
67
|
}
|
|
68
|
-
else if (
|
|
69
|
-
this.post(
|
|
68
|
+
else if (helper instanceof Post) {
|
|
69
|
+
this.post(helper.pattern, ...helper.middleware);
|
|
70
70
|
}
|
|
71
71
|
else {
|
|
72
|
-
this.add(...Object.values(
|
|
72
|
+
this.add(...Object.values(helper));
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
return this;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { jsx, toGenerator, toString, type JSX } from "./jsx/index.js";
|
|
2
2
|
export { Chunk } from "./jsx/chunk/index.js";
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
|
-
export { App, type Middleware
|
|
4
|
+
export { App, type Middleware } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export { Get } from "./app/get.js";
|
|
7
|
-
export { Post } from "./app/post.js";
|
|
6
|
+
export { Get } from "./app/helper/get.js";
|
|
7
|
+
export { Post } from "./app/helper/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,6 @@ export { Chunk } from "./jsx/chunk/index.js";
|
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { App } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export { Get } from "./app/get.js";
|
|
7
|
-
export { Post } from "./app/post.js";
|
|
6
|
+
export { Get } from "./app/helper/get.js";
|
|
7
|
+
export { Post } from "./app/helper/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Params } from "../app/index.js";
|
|
2
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
|
+
import type { Params } from "../trie/index.js";
|
|
3
3
|
export type MaybePromise<T> = T | Promise<T>;
|
|
4
4
|
export type MaybeFunction<T> = T | (() => T);
|
|
5
5
|
export type DeepArray<T> = T | DeepArray<T>[];
|
|
@@ -14,6 +14,7 @@ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
|
14
14
|
infer First extends string,
|
|
15
15
|
...infer Rest extends string[]
|
|
16
16
|
] ? Rest["length"] extends 0 ? ExtractParams<First> : ExtractParams<First> | ExtractMultiParams<Rest> : never;
|
|
17
|
+
export type InsertParams<Pattern extends string, P extends Params> = Pattern extends `${infer Start}:${infer Param}/${infer Rest}` ? Param extends keyof P ? `${Start}${P[Param]}/${InsertParams<Rest, P>}` : Pattern : Pattern extends `${infer Start}:${infer Param}` ? Param extends keyof P ? `${Start}${P[Param]}` : Pattern : Pattern extends `${infer Start}*` ? "*" extends keyof P ? `${Start}${P["*"]}` : Pattern : Pattern;
|
|
17
18
|
export type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] & (keyof P extends never ? {
|
|
18
19
|
params?: never;
|
|
19
20
|
} : {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAE7C,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;AAE9C,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,IACxD,OAAO,SAAS,GAAG,MAAM,MAAM,IAAI,MAAM,KAAK,IAAI,MAAM,IAAI,EAAE,GAC3D;KAAG,CAAC,IAAI,KAAK,GAAG,MAAM,aAAa,CAAC,IAAI,CAAC,GAAG,MAAM;CAAE,GACpD,OAAO,SAAS,GAAG,MAAM,MAAM,IAAI,MAAM,KAAK,EAAE,GAC/C;KAAG,CAAC,IAAI,KAAK,GAAG,MAAM;CAAE,GACxB,O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C;AAE9D,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,iBAAiB,CAAC,MAAM,CAAC,GACtE,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG;IAAE,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC;AAE9D,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,GAC1E,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG;IAAE,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/app/context.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { Chunk } from "../jsx/chunk/index.js";
|
|
2
2
|
import { type JSX, toGenerator } from "../jsx/index.js";
|
|
3
|
-
import type { Route } from "../trie/index.js";
|
|
3
|
+
import type { Params, Route } from "../trie/index.js";
|
|
4
4
|
import { hash } from "../util/hash.js";
|
|
5
5
|
import {
|
|
6
6
|
App,
|
|
7
7
|
type ErrorHandler,
|
|
8
8
|
type Middleware,
|
|
9
9
|
type NotFoundHandler,
|
|
10
|
-
type Params,
|
|
11
10
|
type TrailingSlash,
|
|
12
11
|
} from "./index.js";
|
|
13
12
|
import { Memo } from "./memo/index.js";
|
|
@@ -69,6 +68,8 @@ export class Context<P extends Params = Params> {
|
|
|
69
68
|
|
|
70
69
|
memo = this.#memo.use;
|
|
71
70
|
|
|
71
|
+
static #encoder = new TextEncoder();
|
|
72
|
+
|
|
72
73
|
constructor(
|
|
73
74
|
req: Request,
|
|
74
75
|
url: URL,
|
|
@@ -233,26 +234,33 @@ export class Context<P extends Params = Params> {
|
|
|
233
234
|
gen = toGenerator(Page);
|
|
234
235
|
}
|
|
235
236
|
|
|
237
|
+
const close = (c?: ReadableStreamDefaultController) => {
|
|
238
|
+
c?.close();
|
|
239
|
+
gen.return();
|
|
240
|
+
};
|
|
241
|
+
|
|
236
242
|
let result: IteratorResult<Chunk>;
|
|
237
243
|
|
|
238
244
|
this.html(
|
|
239
|
-
new ReadableStream<
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
245
|
+
new ReadableStream<Uint8Array>(
|
|
246
|
+
{
|
|
247
|
+
pull: async (c) => {
|
|
248
|
+
if (this.req.signal.aborted) return close(c);
|
|
249
|
+
|
|
250
|
+
result = await gen.next();
|
|
251
|
+
|
|
252
|
+
if (result.done) return close(c);
|
|
253
|
+
|
|
254
|
+
// need to encode for Node JS (ex: during prerendering)
|
|
255
|
+
c.enqueue(Context.#encoder.encode(result.value.value));
|
|
256
|
+
},
|
|
251
257
|
|
|
252
|
-
|
|
253
|
-
|
|
258
|
+
cancel() {
|
|
259
|
+
close();
|
|
260
|
+
},
|
|
254
261
|
},
|
|
255
|
-
|
|
262
|
+
{ highWaterMark: 16_000, size: (chunk) => chunk.length },
|
|
263
|
+
),
|
|
256
264
|
status,
|
|
257
265
|
);
|
|
258
266
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../../jsx/index.js";
|
|
2
|
+
import type { AnchorProps, ExtractParams } from "../../types/index.js";
|
|
3
|
+
import type { Middleware } from "../index.js";
|
|
4
|
+
import { Helper } from "./index.js";
|
|
5
|
+
|
|
6
|
+
export class Get<Pattern extends string = string> extends Helper<Pattern> {
|
|
7
|
+
/** `<a>` component with preset `href` attribute. */
|
|
8
|
+
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param pattern Route pattern
|
|
12
|
+
* @param middleware GET middleware
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```tsx
|
|
17
|
+
* import { Get } from "ovr";
|
|
18
|
+
*
|
|
19
|
+
* const page = new Get("/", () => {
|
|
20
|
+
* return <p>Hello world</p>
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const Nav = () => {
|
|
24
|
+
* return <page.Anchor>Home</page.Anchor>
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* app.add(page); register
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
constructor(
|
|
31
|
+
pattern: Pattern,
|
|
32
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
33
|
+
) {
|
|
34
|
+
super("GET", pattern, ...middleware);
|
|
35
|
+
|
|
36
|
+
this.Anchor = (props) => {
|
|
37
|
+
const { params, ...rest } = props;
|
|
38
|
+
|
|
39
|
+
return jsx("a", {
|
|
40
|
+
href: this.pathname(
|
|
41
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
42
|
+
params,
|
|
43
|
+
),
|
|
44
|
+
...rest,
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../../jsx/index.js";
|
|
2
|
+
import type {
|
|
3
|
+
ButtonProps,
|
|
4
|
+
ExtractParams,
|
|
5
|
+
FormProps,
|
|
6
|
+
InsertParams,
|
|
7
|
+
} from "../../types/index.js";
|
|
8
|
+
import type { Middleware } from "../index.js";
|
|
9
|
+
|
|
10
|
+
export class Helper<Pattern extends string> {
|
|
11
|
+
/** Extracted parameters type for the pattern */
|
|
12
|
+
declare readonly Params: ExtractParams<Pattern>;
|
|
13
|
+
|
|
14
|
+
/** Route pattern */
|
|
15
|
+
pattern: Pattern;
|
|
16
|
+
|
|
17
|
+
/** GET middleware */
|
|
18
|
+
middleware: Middleware<any>[]; // any so you can use other middleware
|
|
19
|
+
|
|
20
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
21
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
22
|
+
|
|
23
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
24
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
25
|
+
|
|
26
|
+
/** Pattern parts */
|
|
27
|
+
#parts: string[];
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
method: string,
|
|
31
|
+
pattern: Pattern,
|
|
32
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
33
|
+
) {
|
|
34
|
+
this.pattern = pattern;
|
|
35
|
+
this.middleware = middleware;
|
|
36
|
+
this.#parts = pattern.split("/");
|
|
37
|
+
|
|
38
|
+
this.Button = (props) => {
|
|
39
|
+
const { params, ...rest } = props;
|
|
40
|
+
|
|
41
|
+
return jsx("button", {
|
|
42
|
+
formaction: this.pathname(
|
|
43
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
44
|
+
params,
|
|
45
|
+
),
|
|
46
|
+
formmethod: method,
|
|
47
|
+
...rest,
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
this.Form = (props) => {
|
|
52
|
+
const { params, ...rest } = props;
|
|
53
|
+
|
|
54
|
+
return jsx("form", {
|
|
55
|
+
action: this.pathname(
|
|
56
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
57
|
+
params,
|
|
58
|
+
),
|
|
59
|
+
method,
|
|
60
|
+
...rest,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param [params] Parameters to insert
|
|
67
|
+
* @returns Resolved `pathname` with params
|
|
68
|
+
*/
|
|
69
|
+
pathname<Params extends ExtractParams<Pattern>>(
|
|
70
|
+
...[params]: keyof Params extends never ? [] : [Params]
|
|
71
|
+
): InsertParams<Pattern, Params> {
|
|
72
|
+
if (!params) return this.pattern as InsertParams<Pattern, Params>;
|
|
73
|
+
|
|
74
|
+
const wild = "*";
|
|
75
|
+
|
|
76
|
+
return this.#parts
|
|
77
|
+
.map((part) => {
|
|
78
|
+
if (part.startsWith(":")) {
|
|
79
|
+
const param = part.slice(1);
|
|
80
|
+
|
|
81
|
+
if (!(param in params))
|
|
82
|
+
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
83
|
+
|
|
84
|
+
return params[param as keyof typeof params];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (part === wild) {
|
|
88
|
+
if (!(wild in params))
|
|
89
|
+
throw new Error("No wildcard parameter found.");
|
|
90
|
+
|
|
91
|
+
return params[wild];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return part;
|
|
95
|
+
})
|
|
96
|
+
.join("/") as InsertParams<Pattern, Params>;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ExtractParams } from "../../types/index.js";
|
|
2
|
+
import { hash } from "../../util/hash.js";
|
|
3
|
+
import type { Middleware } from "../index.js";
|
|
4
|
+
import { Helper } from "./index.js";
|
|
5
|
+
|
|
6
|
+
export class Post<Pattern extends string = string> extends Helper<Pattern> {
|
|
7
|
+
/**
|
|
8
|
+
* @param middleware POST middleware
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { Post } from "ovr";
|
|
14
|
+
*
|
|
15
|
+
* const post = new Post((c) => {
|
|
16
|
+
* console.log("posted");
|
|
17
|
+
* c.redirect("/", 303);
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const page = new Get("/", () => (
|
|
21
|
+
* <post.Form>
|
|
22
|
+
* <input type="text" name="name" />
|
|
23
|
+
* <button>Submit</button>
|
|
24
|
+
* </post.Form>
|
|
25
|
+
* ));
|
|
26
|
+
*
|
|
27
|
+
* app.add(page, post); register
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
constructor(...middleware: Middleware<{}>[]);
|
|
31
|
+
/**
|
|
32
|
+
* @param pattern Route pattern
|
|
33
|
+
* @param middleware POST middleware
|
|
34
|
+
*/
|
|
35
|
+
constructor(
|
|
36
|
+
pattern: Pattern,
|
|
37
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
38
|
+
);
|
|
39
|
+
constructor(
|
|
40
|
+
patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
|
|
41
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
42
|
+
) {
|
|
43
|
+
let pattern: Pattern;
|
|
44
|
+
|
|
45
|
+
if (typeof patternOrMiddleware === "string") {
|
|
46
|
+
pattern = patternOrMiddleware;
|
|
47
|
+
} else {
|
|
48
|
+
middleware.unshift(patternOrMiddleware);
|
|
49
|
+
pattern = `/_p/${hash(middleware.join())}` as Pattern;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
super("POST", pattern, ...middleware);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/app/index.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import { Route, Trie } from "../trie/index.js";
|
|
1
|
+
import { type Params, Route, Trie } from "../trie/index.js";
|
|
2
2
|
import type {
|
|
3
3
|
DeepArray,
|
|
4
4
|
ExtractMultiParams,
|
|
5
5
|
ExtractParams,
|
|
6
6
|
} from "../types/index.js";
|
|
7
7
|
import { Context } from "./context.js";
|
|
8
|
-
import { Get } from "./get.js";
|
|
9
|
-
import { Post } from "./post.js";
|
|
8
|
+
import { Get } from "./helper/get.js";
|
|
9
|
+
import { Post } from "./helper/post.js";
|
|
10
10
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
11
|
|
|
12
|
-
export type Params = Record<string, string>;
|
|
13
|
-
|
|
14
12
|
export type Middleware<P extends Params = Params> = (
|
|
15
13
|
context: Context<P>,
|
|
16
14
|
next: () => Promise<void>,
|
|
@@ -105,19 +103,19 @@ export class App {
|
|
|
105
103
|
static storage = new AsyncLocalStorage<Context>();
|
|
106
104
|
|
|
107
105
|
/**
|
|
108
|
-
* @param
|
|
106
|
+
* @param helpers `Helper` to add to `App`
|
|
109
107
|
* @returns `App` instance
|
|
110
108
|
*/
|
|
111
|
-
add(...
|
|
112
|
-
for (const
|
|
113
|
-
if (
|
|
114
|
-
this.add(...
|
|
115
|
-
} else if (
|
|
116
|
-
this.get(
|
|
117
|
-
} else if (
|
|
118
|
-
this.post(
|
|
109
|
+
add(...helpers: DeepArray<Get | Post | Record<string, Get | Post>>[]) {
|
|
110
|
+
for (const helper of helpers) {
|
|
111
|
+
if (helper instanceof Array) {
|
|
112
|
+
this.add(...helper);
|
|
113
|
+
} else if (helper instanceof Get) {
|
|
114
|
+
this.get(helper.pattern, ...helper.middleware);
|
|
115
|
+
} else if (helper instanceof Post) {
|
|
116
|
+
this.post(helper.pattern, ...helper.middleware);
|
|
119
117
|
} else {
|
|
120
|
-
this.add(...Object.values(
|
|
118
|
+
this.add(...Object.values(helper));
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { jsx, toGenerator, toString, type JSX } from "./jsx/index.js";
|
|
2
2
|
export { Chunk } from "./jsx/chunk/index.js";
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
|
-
export { App, type Middleware
|
|
4
|
+
export { App, type Middleware } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export { Get } from "./app/get.js";
|
|
7
|
-
export { Post } from "./app/post.js";
|
|
6
|
+
export { Get } from "./app/helper/get.js";
|
|
7
|
+
export { Post } from "./app/helper/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
package/src/types/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Params } from "../app/index.js";
|
|
2
1
|
import type { JSX } from "../jsx/index.js";
|
|
2
|
+
import type { Params } from "../trie/index.js";
|
|
3
3
|
|
|
4
4
|
export type MaybePromise<T> = T | Promise<T>;
|
|
5
5
|
|
|
@@ -25,6 +25,23 @@ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
|
25
25
|
: ExtractParams<First> | ExtractMultiParams<Rest>
|
|
26
26
|
: never;
|
|
27
27
|
|
|
28
|
+
export type InsertParams<
|
|
29
|
+
Pattern extends string,
|
|
30
|
+
P extends Params,
|
|
31
|
+
> = Pattern extends `${infer Start}:${infer Param}/${infer Rest}`
|
|
32
|
+
? Param extends keyof P
|
|
33
|
+
? `${Start}${P[Param]}/${InsertParams<Rest, P>}`
|
|
34
|
+
: Pattern
|
|
35
|
+
: Pattern extends `${infer Start}:${infer Param}`
|
|
36
|
+
? Param extends keyof P
|
|
37
|
+
? `${Start}${P[Param]}`
|
|
38
|
+
: Pattern
|
|
39
|
+
: Pattern extends `${infer Start}*`
|
|
40
|
+
? "*" extends keyof P
|
|
41
|
+
? `${Start}${P["*"]}`
|
|
42
|
+
: Pattern
|
|
43
|
+
: Pattern;
|
|
44
|
+
|
|
28
45
|
export type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] &
|
|
29
46
|
(keyof P extends never ? { params?: never } : { params: P });
|
|
30
47
|
|
package/dist/app/get.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type { AnchorProps, ButtonProps, ExtractParams, FormProps } from "../types/index.js";
|
|
3
|
-
import type { Middleware } from "./index.js";
|
|
4
|
-
export declare class Get<Pattern extends string = string> {
|
|
5
|
-
#private;
|
|
6
|
-
/** Route pattern */
|
|
7
|
-
pattern: Pattern;
|
|
8
|
-
/** GET middleware */
|
|
9
|
-
middleware: Middleware<any>[];
|
|
10
|
-
/** `<a>` component with preset `href` attribute. */
|
|
11
|
-
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
12
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
13
|
-
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
14
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
15
|
-
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
16
|
-
/**
|
|
17
|
-
* @param pattern Route pattern
|
|
18
|
-
* @param middleware GET middleware
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
*
|
|
22
|
-
* ```tsx
|
|
23
|
-
* import { Get } from "ovr";
|
|
24
|
-
*
|
|
25
|
-
* const page = new Get("/", () => {
|
|
26
|
-
* return <p>Hello world</p>
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* const Nav = () => {
|
|
30
|
-
* return <page.Anchor>Home</page.Anchor>
|
|
31
|
-
* }
|
|
32
|
-
*
|
|
33
|
-
* app.add(page); register
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
37
|
-
}
|
|
38
|
-
//# sourceMappingURL=get.d.ts.map
|
package/dist/app/get.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"get.d.ts","sourceRoot":"","sources":["../../src/app/get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EACX,WAAW,EACX,WAAW,EACX,aAAa,EACb,SAAS,EACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,qBAAa,GAAG,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAC/C,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,qBAAqB;IACrB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,oDAAoD;IACpD,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,iFAAiF;IACjF,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIhE;;;;;;;;;;;;;;;;;;;OAmBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CA+BpD"}
|
package/dist/app/get.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { jsx } from "../jsx/index.js";
|
|
2
|
-
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
export class Get {
|
|
4
|
-
/** Route pattern */
|
|
5
|
-
pattern;
|
|
6
|
-
/** GET middleware */
|
|
7
|
-
middleware;
|
|
8
|
-
/** `<a>` component with preset `href` attribute. */
|
|
9
|
-
Anchor;
|
|
10
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
11
|
-
Button;
|
|
12
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
13
|
-
Form;
|
|
14
|
-
#parts;
|
|
15
|
-
/**
|
|
16
|
-
* @param pattern Route pattern
|
|
17
|
-
* @param middleware GET middleware
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
*
|
|
21
|
-
* ```tsx
|
|
22
|
-
* import { Get } from "ovr";
|
|
23
|
-
*
|
|
24
|
-
* const page = new Get("/", () => {
|
|
25
|
-
* return <p>Hello world</p>
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* const Nav = () => {
|
|
29
|
-
* return <page.Anchor>Home</page.Anchor>
|
|
30
|
-
* }
|
|
31
|
-
*
|
|
32
|
-
* app.add(page); register
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
constructor(pattern, ...middleware) {
|
|
36
|
-
this.pattern = pattern;
|
|
37
|
-
this.middleware = middleware;
|
|
38
|
-
this.#parts = pattern.split("/");
|
|
39
|
-
this.Anchor = (props) => {
|
|
40
|
-
const { params = {}, ...rest } = props;
|
|
41
|
-
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
42
|
-
};
|
|
43
|
-
this.Button = (props) => {
|
|
44
|
-
const { params = {}, ...rest } = props;
|
|
45
|
-
return jsx("button", {
|
|
46
|
-
formaction: insertParams(this.#parts, params),
|
|
47
|
-
formmethod: "get",
|
|
48
|
-
...rest,
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
this.Form = (props) => {
|
|
52
|
-
const { params = {}, ...rest } = props;
|
|
53
|
-
return jsx("form", {
|
|
54
|
-
action: insertParams(this.#parts, params),
|
|
55
|
-
...rest,
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
package/dist/app/post.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../src/app/post.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE/E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,qBAAa,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAChD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,sBAAsB;IACtB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,iFAAiF;IACjF,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIhE;;;;;;;;;;;;;;;;;;;;;;OAsBG;gBACS,GAAG,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,EAAE;IAC3C;;;OAGG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAqCpD"}
|
package/dist/app/post.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { jsx } from "../jsx/index.js";
|
|
2
|
-
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
import { hash } from "../util/hash.js";
|
|
4
|
-
export class Post {
|
|
5
|
-
/** Route pattern */
|
|
6
|
-
pattern;
|
|
7
|
-
/** POST middleware */
|
|
8
|
-
middleware;
|
|
9
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
10
|
-
Button;
|
|
11
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
12
|
-
Form;
|
|
13
|
-
#parts = [];
|
|
14
|
-
constructor(patternOrMiddleware, ...middleware) {
|
|
15
|
-
this.middleware = middleware;
|
|
16
|
-
if (typeof patternOrMiddleware === "string") {
|
|
17
|
-
this.pattern = patternOrMiddleware;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
this.middleware.unshift(patternOrMiddleware);
|
|
21
|
-
this.pattern = `/_p/${hash(this.middleware.join())}`;
|
|
22
|
-
}
|
|
23
|
-
this.#parts = this.pattern.split("/");
|
|
24
|
-
this.Button = (props) => {
|
|
25
|
-
const { params = {}, ...rest } = props;
|
|
26
|
-
return jsx("button", {
|
|
27
|
-
formaction: insertParams(this.#parts, params),
|
|
28
|
-
formmethod: "post",
|
|
29
|
-
...rest,
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
this.Form = (props) => {
|
|
33
|
-
const { params = {}, ...rest } = props;
|
|
34
|
-
return jsx("form", {
|
|
35
|
-
action: insertParams(this.#parts, params),
|
|
36
|
-
method: "post",
|
|
37
|
-
...rest,
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Params } from "./index.js";
|
|
2
|
-
/**
|
|
3
|
-
* @param parts Pattern parts
|
|
4
|
-
* @param params Parameters to insert
|
|
5
|
-
* @returns Resolved pathname with params
|
|
6
|
-
*/
|
|
7
|
-
export declare const insertParams: (parts: string[], params: Params) => string;
|
|
8
|
-
//# sourceMappingURL=insert-params.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"insert-params.d.ts","sourceRoot":"","sources":["../../src/trie/insert-params.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,EAAE,EAAE,QAAQ,MAAM,WAuB3D,CAAC"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param parts Pattern parts
|
|
3
|
-
* @param params Parameters to insert
|
|
4
|
-
* @returns Resolved pathname with params
|
|
5
|
-
*/
|
|
6
|
-
export const insertParams = (parts, params) => {
|
|
7
|
-
const wild = "*";
|
|
8
|
-
return parts
|
|
9
|
-
.map((part) => {
|
|
10
|
-
if (part.startsWith(":")) {
|
|
11
|
-
const param = part.slice(1);
|
|
12
|
-
if (!(param in params))
|
|
13
|
-
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
14
|
-
return params[param];
|
|
15
|
-
}
|
|
16
|
-
if (part === wild) {
|
|
17
|
-
if (!(wild in params))
|
|
18
|
-
throw new Error("No wildcard parameter found.");
|
|
19
|
-
return params[wild];
|
|
20
|
-
}
|
|
21
|
-
return part;
|
|
22
|
-
})
|
|
23
|
-
.join("/");
|
|
24
|
-
};
|
package/src/app/get.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
-
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
import type {
|
|
4
|
-
AnchorProps,
|
|
5
|
-
ButtonProps,
|
|
6
|
-
ExtractParams,
|
|
7
|
-
FormProps,
|
|
8
|
-
} from "../types/index.js";
|
|
9
|
-
import type { Middleware } from "./index.js";
|
|
10
|
-
|
|
11
|
-
export class Get<Pattern extends string = string> {
|
|
12
|
-
/** Route pattern */
|
|
13
|
-
pattern: Pattern;
|
|
14
|
-
|
|
15
|
-
/** GET middleware */
|
|
16
|
-
middleware: Middleware<any>[];
|
|
17
|
-
|
|
18
|
-
/** `<a>` component with preset `href` attribute. */
|
|
19
|
-
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
20
|
-
|
|
21
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
22
|
-
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
23
|
-
|
|
24
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
25
|
-
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
26
|
-
|
|
27
|
-
#parts: string[];
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param pattern Route pattern
|
|
31
|
-
* @param middleware GET middleware
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
*
|
|
35
|
-
* ```tsx
|
|
36
|
-
* import { Get } from "ovr";
|
|
37
|
-
*
|
|
38
|
-
* const page = new Get("/", () => {
|
|
39
|
-
* return <p>Hello world</p>
|
|
40
|
-
* });
|
|
41
|
-
*
|
|
42
|
-
* const Nav = () => {
|
|
43
|
-
* return <page.Anchor>Home</page.Anchor>
|
|
44
|
-
* }
|
|
45
|
-
*
|
|
46
|
-
* app.add(page); register
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
constructor(
|
|
50
|
-
pattern: Pattern,
|
|
51
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
52
|
-
) {
|
|
53
|
-
this.pattern = pattern;
|
|
54
|
-
this.middleware = middleware;
|
|
55
|
-
this.#parts = pattern.split("/");
|
|
56
|
-
|
|
57
|
-
this.Anchor = (props) => {
|
|
58
|
-
const { params = {}, ...rest } = props;
|
|
59
|
-
|
|
60
|
-
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
this.Button = (props) => {
|
|
64
|
-
const { params = {}, ...rest } = props;
|
|
65
|
-
|
|
66
|
-
return jsx("button", {
|
|
67
|
-
formaction: insertParams(this.#parts, params),
|
|
68
|
-
formmethod: "get",
|
|
69
|
-
...rest,
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
this.Form = (props) => {
|
|
74
|
-
const { params = {}, ...rest } = props;
|
|
75
|
-
|
|
76
|
-
return jsx("form", {
|
|
77
|
-
action: insertParams(this.#parts, params),
|
|
78
|
-
...rest,
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
package/src/app/post.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
-
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
import type { ButtonProps, ExtractParams, FormProps } from "../types/index.js";
|
|
4
|
-
import { hash } from "../util/hash.js";
|
|
5
|
-
import type { Middleware } from "./index.js";
|
|
6
|
-
|
|
7
|
-
export class Post<Pattern extends string = string> {
|
|
8
|
-
/** Route pattern */
|
|
9
|
-
pattern: Pattern;
|
|
10
|
-
|
|
11
|
-
/** POST middleware */
|
|
12
|
-
middleware: Middleware<any>[];
|
|
13
|
-
|
|
14
|
-
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
15
|
-
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
16
|
-
|
|
17
|
-
/** `<form>` component with preset `method` and `action` attributes. */
|
|
18
|
-
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
19
|
-
|
|
20
|
-
#parts: string[] = [];
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @param middleware POST middleware
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
*
|
|
27
|
-
* ```tsx
|
|
28
|
-
* import { Post } from "ovr";
|
|
29
|
-
*
|
|
30
|
-
* const post = new Post((c) => {
|
|
31
|
-
* console.log("posted");
|
|
32
|
-
* c.redirect("/", 303);
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* const page = new Get("/", () => (
|
|
36
|
-
* <post.Form>
|
|
37
|
-
* <input type="text" name="name" />
|
|
38
|
-
* <button>Submit</button>
|
|
39
|
-
* </post.Form>
|
|
40
|
-
* ));
|
|
41
|
-
*
|
|
42
|
-
* app.add(page, post); register
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
constructor(...middleware: Middleware<{}>[]);
|
|
46
|
-
/**
|
|
47
|
-
* @param pattern Route pattern
|
|
48
|
-
* @param middleware POST middleware
|
|
49
|
-
*/
|
|
50
|
-
constructor(
|
|
51
|
-
pattern: Pattern,
|
|
52
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
53
|
-
);
|
|
54
|
-
constructor(
|
|
55
|
-
patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
|
|
56
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
57
|
-
) {
|
|
58
|
-
this.middleware = middleware;
|
|
59
|
-
|
|
60
|
-
if (typeof patternOrMiddleware === "string") {
|
|
61
|
-
this.pattern = patternOrMiddleware;
|
|
62
|
-
} else {
|
|
63
|
-
this.middleware.unshift(patternOrMiddleware);
|
|
64
|
-
this.pattern = `/_p/${hash(this.middleware.join())}` as Pattern;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
this.#parts = this.pattern.split("/");
|
|
68
|
-
|
|
69
|
-
this.Button = (props) => {
|
|
70
|
-
const { params = {}, ...rest } = props;
|
|
71
|
-
|
|
72
|
-
return jsx("button", {
|
|
73
|
-
formaction: insertParams(this.#parts, params),
|
|
74
|
-
formmethod: "post",
|
|
75
|
-
...rest,
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
this.Form = (props) => {
|
|
80
|
-
const { params = {}, ...rest } = props;
|
|
81
|
-
|
|
82
|
-
return jsx("form", {
|
|
83
|
-
action: insertParams(this.#parts, params),
|
|
84
|
-
method: "post",
|
|
85
|
-
...rest,
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { Params } from "./index.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param parts Pattern parts
|
|
5
|
-
* @param params Parameters to insert
|
|
6
|
-
* @returns Resolved pathname with params
|
|
7
|
-
*/
|
|
8
|
-
export const insertParams = (parts: string[], params: Params) => {
|
|
9
|
-
const wild = "*";
|
|
10
|
-
|
|
11
|
-
return parts
|
|
12
|
-
.map((part) => {
|
|
13
|
-
if (part.startsWith(":")) {
|
|
14
|
-
const param = part.slice(1);
|
|
15
|
-
|
|
16
|
-
if (!(param in params))
|
|
17
|
-
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
18
|
-
|
|
19
|
-
return params[param as keyof typeof params];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (part === wild) {
|
|
23
|
-
if (!(wild in params)) throw new Error("No wildcard parameter found.");
|
|
24
|
-
|
|
25
|
-
return params[wild];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return part;
|
|
29
|
-
})
|
|
30
|
-
.join("/");
|
|
31
|
-
};
|