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
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
- };