ovr 4.1.0 → 4.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.
Files changed (47) hide show
  1. package/dist/app/context.d.ts +2 -2
  2. package/dist/app/context.d.ts.map +1 -1
  3. package/dist/app/context.js +2 -1
  4. package/dist/app/helper/get.d.ts +30 -0
  5. package/dist/app/helper/get.d.ts.map +1 -0
  6. package/dist/app/helper/get.js +38 -0
  7. package/dist/app/helper/index.d.ts +23 -0
  8. package/dist/app/helper/index.d.ts.map +1 -0
  9. package/dist/app/helper/index.js +63 -0
  10. package/dist/app/{post.d.ts → helper/post.d.ts} +4 -13
  11. package/dist/app/helper/post.d.ts.map +1 -0
  12. package/dist/app/helper/post.js +15 -0
  13. package/dist/app/index.d.ts +5 -5
  14. package/dist/app/index.d.ts.map +1 -1
  15. package/dist/app/index.js +12 -12
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +2 -2
  19. package/dist/jsx/chunk/index.js +5 -3
  20. package/dist/jsx/index.d.ts.map +1 -1
  21. package/dist/jsx/index.js +36 -28
  22. package/dist/jsx/merge-async-generators.d.ts +4 -1
  23. package/dist/jsx/merge-async-generators.d.ts.map +1 -1
  24. package/dist/types/index.d.ts +2 -1
  25. package/dist/types/index.d.ts.map +1 -1
  26. package/package.json +1 -1
  27. package/src/app/context.ts +4 -3
  28. package/src/app/helper/get.ts +48 -0
  29. package/src/app/helper/index.ts +98 -0
  30. package/src/app/helper/post.ts +54 -0
  31. package/src/app/index.ts +13 -15
  32. package/src/index.ts +3 -3
  33. package/src/jsx/chunk/index.ts +5 -5
  34. package/src/jsx/index.ts +36 -32
  35. package/src/jsx/merge-async-generators.ts +1 -1
  36. package/src/types/index.ts +18 -1
  37. package/dist/app/get.d.ts +0 -38
  38. package/dist/app/get.d.ts.map +0 -1
  39. package/dist/app/get.js +0 -59
  40. package/dist/app/post.d.ts.map +0 -1
  41. package/dist/app/post.js +0 -41
  42. package/dist/trie/insert-params.d.ts +0 -8
  43. package/dist/trie/insert-params.d.ts.map +0 -1
  44. package/dist/trie/insert-params.js +0 -24
  45. package/src/app/get.ts +0 -82
  46. package/src/app/post.ts +0 -89
  47. package/src/trie/insert-params.ts +0 -31
@@ -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 routes `Get` or `Post` to add to `App`
106
+ * @param helpers `Helper` to add to `App`
109
107
  * @returns `App` instance
110
108
  */
111
- add(...routes: DeepArray<Get | Post | Record<string, Get | Post>>[]) {
112
- for (const route of routes) {
113
- if (route instanceof Array) {
114
- this.add(...route);
115
- } else if (route instanceof Get) {
116
- this.get(route.pattern, ...route.middleware);
117
- } else if (route instanceof Post) {
118
- this.post(route.pattern, ...route.middleware);
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(route));
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, type Params } from "./app/index.js";
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";
@@ -46,20 +46,20 @@ export class Chunk {
46
46
  // tracks the position of the last successful match in the input string
47
47
  let start = 0;
48
48
  let result = "";
49
+ // index of the match
50
+ let i: number;
51
+ let match: string | undefined;
49
52
 
50
53
  // since `g` flag is used, regex maintains state with each loop,
51
54
  // checking from the lastIndex onward each time
52
55
  while (regex.test(s)) {
53
- // index of the match
54
- const i = regex.lastIndex - 1;
55
- const match = s[i];
56
-
56
+ i = regex.lastIndex - 1;
57
+ match = s[i];
57
58
  result +=
58
59
  // everything that didn't match during this test
59
60
  s.slice(start, i) +
60
61
  // replacement
61
62
  (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
62
-
63
63
  start = regex.lastIndex;
64
64
  }
65
65
 
package/src/jsx/index.ts CHANGED
@@ -72,28 +72,28 @@ export async function* jsx<P extends Props = Props>(
72
72
  // intrinsic element
73
73
  const { children, ...rest } = props;
74
74
 
75
- const attributes: string[] = [];
75
+ let attributes = "";
76
+ let key: string;
77
+ let value: JSX.Element;
76
78
 
77
- for (let key in rest) {
78
- const value = rest[key]; // needs to come before reassigning keys
79
+ for (key in rest) {
80
+ value = rest[key]; // needs to come before reassigning keys
79
81
 
80
82
  if (key === "className") key = "class";
81
83
  else if (key === "htmlFor") key = "for";
82
84
 
83
85
  if (value === true) {
84
86
  // just put the key without the value
85
- attributes.push(` ${key}`);
86
- } else if (
87
- typeof value === "string" ||
88
- typeof value === "number" ||
89
- typeof value === "bigint"
90
- ) {
91
- attributes.push(` ${key}="${Chunk.escape(String(value), true)}"`);
87
+ attributes += ` ${key}`;
88
+ } else if (typeof value === "string") {
89
+ attributes += ` ${key}="${Chunk.escape(value, true)}"`;
90
+ } else if (typeof value === "number" || typeof value === "bigint") {
91
+ attributes += ` ${key}="${value}"`;
92
92
  }
93
93
  // otherwise, don't include the attribute
94
94
  }
95
95
 
96
- yield new Chunk(`<${tag}${attributes.join("")}>`, true);
96
+ yield new Chunk(`<${tag}${attributes}>`, true);
97
97
 
98
98
  if (voidElements.has(tag)) return;
99
99
 
@@ -139,7 +139,9 @@ export async function* toGenerator(
139
139
  if (typeof element === "object") {
140
140
  if (Symbol.asyncIterator in element) {
141
141
  // async iterable - lazily resolve
142
- for await (const children of element) yield* toGenerator(children);
142
+ let children: JSX.Element;
143
+
144
+ for await (children of element) yield* toGenerator(children);
143
145
 
144
146
  return;
145
147
  }
@@ -155,11 +157,12 @@ export async function* toGenerator(
155
157
  ) {
156
158
  // sync generator
157
159
  // process lazily - avoids loading all in memory
158
- const yieldIterations = 50;
160
+ const yieldIterations = 150;
159
161
  let yieldCounter = yieldIterations;
162
+ let result: IteratorResult<JSX.Element>;
160
163
 
161
164
  while (true) {
162
- const result = iterator.next();
165
+ result = iterator.next();
163
166
 
164
167
  if (result.done) break;
165
168
  yield* toGenerator(result.value);
@@ -178,26 +181,24 @@ export async function* toGenerator(
178
181
  // other iterable - array, set, etc.
179
182
  // process children in parallel
180
183
  const generators: AsyncGenerator<Chunk, void, unknown>[] = [];
184
+ let result: IteratorResult<JSX.Element>;
181
185
 
182
186
  while (true) {
183
- const result = iterator.next();
187
+ result = iterator.next();
184
188
  if (result.done) break;
185
189
  generators.push(toGenerator(result.value));
186
190
  }
187
191
 
188
- const queue: (Chunk | null)[] = new Array(generators.length).fill(null);
189
-
190
- let current = 0;
192
+ const queue: (Chunk | null)[] = new Array(generators.length);
191
193
  const complete = new Set<number>();
194
+ let current = 0;
195
+ let m: { index: number; result: IteratorResult<Chunk, void> };
192
196
 
193
- for await (const {
194
- index,
195
- result: { done, value: chunk },
196
- } of merge(generators)) {
197
- if (done) {
198
- complete.add(index);
197
+ for await (m of merge(generators)) {
198
+ if (m.result.done) {
199
+ complete.add(m.index);
199
200
 
200
- if (index === current) {
201
+ if (m.index === current) {
201
202
  while (++current < generators.length) {
202
203
  if (queue[current]) {
203
204
  // yield whatever is in the next queue even if it hasn't completed yet
@@ -209,12 +210,12 @@ export async function* toGenerator(
209
210
  if (!complete.has(current)) break;
210
211
  }
211
212
  }
212
- } else if (index === current) {
213
- yield chunk; // stream the current value directly
213
+ } else if (m.index === current) {
214
+ yield m.result.value; // stream the current value directly
214
215
  } else {
215
216
  // queue the value for later
216
- if (queue[index]) queue[index].concat(chunk);
217
- else queue[index] = chunk;
217
+ if (queue[m.index]) queue[m.index]!.concat(m.result.value);
218
+ else queue[m.index] = m.result.value;
218
219
  }
219
220
  }
220
221
 
@@ -241,7 +242,10 @@ export async function* toGenerator(
241
242
  * @returns Concatenated HTML
242
243
  */
243
244
  export const toString = async (element: JSX.Element) => {
244
- const buffer: Chunk[] = [];
245
- for await (const chunk of toGenerator(element)) buffer.push(chunk);
246
- return buffer.join("");
245
+ let buffer = "";
246
+ let chunk: Chunk;
247
+
248
+ for await (chunk of toGenerator(element)) buffer += chunk.value;
249
+
250
+ return buffer;
247
251
  };
@@ -14,7 +14,7 @@ const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
14
14
  */
15
15
  export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
16
16
  const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
17
- const promises = new Map<number, Promise<any>>();
17
+ const promises = new Map<number, ReturnType<typeof next<T, void>>>();
18
18
 
19
19
  iterators.forEach((iterator, index) =>
20
20
  promises.set(index, next(iterator, index)),
@@ -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
@@ -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
- }
@@ -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
- };