ovr 5.0.0 → 6.0.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 (86) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +49 -83
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +40 -92
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -6
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +37 -52
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -200
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +341 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +30 -30
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +67 -82
  33. package/dist/util/index.d.ts +38 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +88 -0
  36. package/package.json +3 -3
  37. package/src/app/index.ts +99 -224
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -7
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -223
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +486 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +87 -111
  48. package/src/util/index.ts +112 -0
  49. package/dist/app/context.d.ts +0 -149
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -246
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -51
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -82
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/jsx/chunk/index.d.ts +0 -38
  65. package/dist/jsx/chunk/index.d.ts.map +0 -1
  66. package/dist/jsx/chunk/index.js +0 -69
  67. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  68. package/dist/jsx/merge-async-generators.d.ts +0 -14
  69. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  70. package/dist/jsx/merge-async-generators.js +0 -42
  71. package/dist/types/index.d.ts +0 -17
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/util/hash.d.ts +0 -11
  74. package/dist/util/hash.d.ts.map +0 -1
  75. package/dist/util/hash.js +0 -16
  76. package/src/app/context.ts +0 -303
  77. package/src/app/helper/get.ts +0 -46
  78. package/src/app/helper/index.ts +0 -156
  79. package/src/app/helper/post.ts +0 -54
  80. package/src/app/memo/index.ts +0 -40
  81. package/src/jsx/chunk/index.ts +0 -79
  82. package/src/jsx/merge-async-generators.ts +0 -47
  83. package/src/types/index.ts +0 -42
  84. package/src/util/hash.ts +0 -17
  85. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  86. /package/dist/{types → middleware}/index.js +0 -0
@@ -1,246 +0,0 @@
1
- import { Chunk } from "../jsx/chunk/index.js";
2
- import { toStream } from "../jsx/index.js";
3
- import { Route } from "../trie/index.js";
4
- import { hash } from "../util/hash.js";
5
- import {} from "./index.js";
6
- import { Memo } from "./memo/index.js";
7
- class TagNotFound extends Error {
8
- name = "TagNotFound";
9
- constructor(tag) {
10
- super(`No closing ${tag} tag found`);
11
- }
12
- }
13
- export class Context {
14
- /**
15
- * Incoming `Request` to the server.
16
- *
17
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
18
- */
19
- req;
20
- /**
21
- * Parsed `URL` created from `req.url`.
22
- *
23
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
24
- */
25
- url;
26
- /**
27
- * Route pattern parameters
28
- *
29
- * Given the route pattern `/posts/:slug` is added, a request made to
30
- * `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
31
- *
32
- * @example { slug: "my-post" }
33
- */
34
- params = {}; // set after match
35
- /** Matched `Route` instance. */
36
- route = null;
37
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
38
- body = null;
39
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
40
- status;
41
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
42
- headers = new Headers();
43
- /** `layouts` for the `Page` to be passed into before being injected into the `<body>` */
44
- layouts = [];
45
- /** `JSX.Element`(s) to add to the head */
46
- head = [];
47
- /**
48
- * Middleware to run when no `body` or `status` has been set on the `context`.
49
- * Set to a new function to override the default.
50
- *
51
- * @default
52
- *
53
- * ```ts
54
- * (c) => {
55
- * c.text("Not found", 404);
56
- * c.headers.set("cache-control", "no-cache");
57
- * }
58
- * ```
59
- */
60
- notFound = (c) => {
61
- c.text("Not found", 404);
62
- c.headers.set("cache-control", "no-cache");
63
- };
64
- /**
65
- * Base HTML to inject the `head` and `page` elements into.
66
- *
67
- * If left empty, components will be returned as partials.
68
- *
69
- * @default ""
70
- */
71
- base = "";
72
- /** `Memo` unique to the current `Request` context */
73
- memo = new Memo();
74
- static #contentType = "content-type";
75
- static #headClose = "</head>";
76
- static #bodyClose = "</body>";
77
- /**
78
- * Creates a new `Context` with the current `Request`.
79
- *
80
- * @param req Request
81
- */
82
- constructor(req) {
83
- this.req = req;
84
- this.url = new URL(req.url);
85
- }
86
- /**
87
- * Mirrors `new Response()` constructor, set values with one function.
88
- *
89
- * @param body Response BodyInit
90
- * @param init ResponseInit
91
- */
92
- res(body, init) {
93
- this.body = body;
94
- this.status = init?.status ?? 200;
95
- if (init?.headers) {
96
- for (const [name, value] of new Headers(init.headers)) {
97
- this.headers.set(name, value);
98
- }
99
- }
100
- }
101
- /**
102
- * Creates an HTML response.
103
- *
104
- * @param body HTML body
105
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
106
- */
107
- html(body, status) {
108
- this.res(body, {
109
- status,
110
- headers: { [Context.#contentType]: "text/html; charset=utf-8" },
111
- });
112
- }
113
- /**
114
- * Creates a JSON response.
115
- *
116
- * @param data passed into JSON.stringify to create the body
117
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
118
- */
119
- json(data, status) {
120
- this.res(JSON.stringify(data), {
121
- status,
122
- headers: { [Context.#contentType]: "application/json" },
123
- });
124
- }
125
- /**
126
- * Creates a plain text response.
127
- *
128
- * @param body response body
129
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
130
- */
131
- text(body, status) {
132
- this.res(body, {
133
- status,
134
- headers: { [Context.#contentType]: "text/plain; charset=utf-8" },
135
- });
136
- }
137
- /**
138
- * Creates a redirect response.
139
- *
140
- * @param location redirect `Location` header
141
- * @param status HTTP status code
142
- *
143
- * - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
144
- * - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
145
- * - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
146
- * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
147
- * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
148
- */
149
- redirect(location, status = 302) {
150
- this.res(null, { status, headers: { location: location.toString() } });
151
- }
152
- /**
153
- * Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
154
- *
155
- * @param Page `JSX.Element` to inject into the `<body>`
156
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
157
- */
158
- page(Page, status) {
159
- for (let i = this.layouts.length - 1; i >= 0; i--) {
160
- // add layouts around the page in reverse order (1st is the root layout)
161
- Page = this.layouts[i]({ children: Page });
162
- }
163
- if (this.base) {
164
- // inject into base
165
- const elements = this.base.split(Context.#headClose);
166
- if (!elements[1])
167
- throw new TagNotFound(Context.#headClose);
168
- elements.splice(1, 0, this.head, Context.#headClose);
169
- const bodyParts = elements[3].split(Context.#bodyClose);
170
- if (!bodyParts[1])
171
- throw new TagNotFound(Context.#bodyClose);
172
- const userAgent = this.req.headers.get("user-agent");
173
- if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
174
- // https://bugs.webkit.org/show_bug.cgi?id=252413
175
- // https://github.com/sveltejs/kit/issues/10315
176
- // https://github.com/remix-run/remix/issues/5804
177
- bodyParts[0] += `<div aria-hidden=true style=position:absolute;width:0;height:0;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0>${"\u200b".repeat(200)}</div>`;
178
- }
179
- elements[3] = bodyParts[0];
180
- elements.push(Page, Context.#bodyClose + bodyParts[1]);
181
- return this.html(toStream(elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el))), status);
182
- }
183
- // HTML partial - just use the layouts + page
184
- // head elements are ignored if no base is set
185
- return this.html(toStream(Page), status);
186
- }
187
- /**
188
- * Generates an etag from a hash of the string provided.
189
- * If the etag matches, sets the response to not modified.
190
- *
191
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
192
- *
193
- * @param s string to hash
194
- * @returns `true` if the etag matches, `false` otherwise
195
- */
196
- etag(s) {
197
- const etag = `"${hash(s)}"`;
198
- this.headers.set("etag", etag);
199
- if (this.req.headers.get("if-none-match") === etag) {
200
- this.body = null;
201
- this.status = 304;
202
- return true;
203
- }
204
- return false;
205
- }
206
- /**
207
- * Modifies the context based on the the return value of middleware.
208
- *
209
- * @param value value returned from middleware
210
- */
211
- #resolveReturn(value) {
212
- if (value instanceof Response) {
213
- this.res(value.body, value);
214
- }
215
- else if (value instanceof ReadableStream) {
216
- this.body = value;
217
- }
218
- else if (value != null) {
219
- // nullish are not used so `void` will not render empty page
220
- this.page(value);
221
- }
222
- }
223
- /**
224
- * Dispatches the stack of `middleware` provided.
225
- *
226
- * @param middleware stack to run
227
- * @param i current middleware index (default `0`)
228
- */
229
- async #run(middleware, i = 0) {
230
- if (!middleware[i])
231
- return;
232
- this.#resolveReturn(await middleware[i](this, () => this.#run(middleware, i + 1)));
233
- }
234
- /**
235
- * Composes a stack of `middleware` into a `Response`.
236
- *
237
- * @param middleware stack to compose
238
- * @returns constructed `Response`
239
- */
240
- async build(middleware) {
241
- await this.#run(middleware);
242
- if (!this.body && !this.status)
243
- this.notFound(this, Promise.resolve);
244
- return new Response(this.body, this);
245
- }
246
- }
@@ -1,30 +0,0 @@
1
- import { type JSX } from "../../jsx/index.js";
2
- import type { ExtractParams } from "../../types/index.js";
3
- import type { Middleware } from "../index.js";
4
- import { Helper, type UrlOptions } 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: JSX.IntrinsicElements["a"] & UrlOptions<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
@@ -1 +0,0 @@
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,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAErD,qBAAa,GAAG,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IACxE,oDAAoD;IACpD,MAAM,EAAE,CACP,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAClE,GAAG,CAAC,OAAO,CAAC;IAEjB;;;;;;;;;;;;;;;;;;;OAmBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAYpD"}
@@ -1,33 +0,0 @@
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 = ({ params, search, hash, ...rest }) => jsx("a", {
29
- href: this.url({ params, search, hash }),
30
- ...rest,
31
- });
32
- }
33
- }
@@ -1,51 +0,0 @@
1
- import { type JSX } from "../../jsx/index.js";
2
- import type { Params } from "../../trie/index.js";
3
- import type { ExtractParams, InsertParams } from "../../types/index.js";
4
- import type { Middleware } from "../index.js";
5
- export type UrlOptions<P extends Params> = {
6
- /**
7
- * Passed into `URLSearchParams` constructor to create new params.
8
- *
9
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
10
- */
11
- search?: string | Iterable<[string, string]> | Record<string, string>;
12
- /**
13
- * Hash (fragment) of the URL. `"#"` prefix is added if not present.
14
- *
15
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash)
16
- */
17
- hash?: string;
18
- } & (keyof P extends never ? {
19
- /** Route pattern does not contain parameters */
20
- params?: never;
21
- } : {
22
- /** Route pattern parameters */
23
- params: P;
24
- });
25
- export declare class Helper<Pattern extends string> {
26
- #private;
27
- /** Extracted parameters type for the pattern */
28
- readonly Params: ExtractParams<Pattern>;
29
- /** Route pattern */
30
- pattern: Pattern;
31
- /** GET middleware */
32
- middleware: Middleware<any>[];
33
- /** `<button>` component with preset `formaction` and `formmethod` attributes. */
34
- Button: (props: JSX.IntrinsicElements["button"] & UrlOptions<ExtractParams<Pattern>>) => JSX.Element;
35
- /** `<form>` component with preset `method` and `action` attributes. */
36
- Form: (props: JSX.IntrinsicElements["form"] & UrlOptions<ExtractParams<Pattern>>) => JSX.Element;
37
- constructor(method: string, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
38
- /**
39
- * Constructs a _relative_ URL for the route.
40
- *
41
- * @param [options] Options with type safe parameters
42
- * @returns `pathname` + `search` + `hash`
43
- */
44
- url(...[options]: keyof ExtractParams<Pattern> extends never ? [UrlOptions<ExtractParams<Pattern>>] | [] : [UrlOptions<ExtractParams<Pattern>>]): string;
45
- /**
46
- * @param [params] Parameters to insert
47
- * @returns Resolved `pathname` with params
48
- */
49
- pathname<Params extends ExtractParams<Pattern>>(...[params]: keyof Params extends never ? [] : [Params]): InsertParams<Pattern, Params>;
50
- }
51
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
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,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,IAAI;IAC1C;;;;OAIG;IACH,MAAM,CAAC,EACJ,MAAM,GAGN,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC1B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,GAAG,CAAC,MAAM,CAAC,SAAS,KAAK,GACvB;IACA,gDAAgD;IAChD,MAAM,CAAC,EAAE,KAAK,CAAC;CACf,GACA;IACA,+BAA+B;IAC/B,MAAM,EAAE,CAAC,CAAC;CACV,CAAC,CAAC;AAEL,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,CACP,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KACvE,GAAG,CAAC,OAAO,CAAC;IAEjB,uEAAuE;IACvE,IAAI,EAAE,CACL,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KACrE,GAAG,CAAC,OAAO,CAAC;gBAMhB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;IAyBpD;;;;;OAKG;IACH,GAAG,CACF,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,aAAa,CAAC,OAAO,CAAC,SAAS,KAAK,GACrD,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,GACzC,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;IA4BxC;;;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"}
@@ -1,82 +0,0 @@
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 = ({ params, search, hash, ...rest }) => jsx("button", {
18
- formaction: this.url({ params, search, hash }),
19
- formmethod: method,
20
- ...rest,
21
- });
22
- this.Form = ({ params, search, hash, ...rest }) => jsx("form", {
23
- action: this.url({ params, search, hash }),
24
- method,
25
- ...rest,
26
- });
27
- }
28
- /**
29
- * Constructs a _relative_ URL for the route.
30
- *
31
- * @param [options] Options with type safe parameters
32
- * @returns `pathname` + `search` + `hash`
33
- */
34
- url(...[options]) {
35
- const pathname = this.pathname(
36
- // @ts-expect-error - do not have to pass in {} if no params
37
- options?.params);
38
- let search = "";
39
- let hash = "";
40
- if (options?.search) {
41
- // use the value as the init
42
- // @ts-expect-error - see above
43
- search = "?" + new URLSearchParams(options.search);
44
- }
45
- if (options?.hash) {
46
- // adding # prefix if not present matches the URL setter:
47
- // https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
48
- if (options.hash.startsWith("#")) {
49
- hash = options.hash;
50
- }
51
- else {
52
- hash = "#" + options.hash;
53
- }
54
- }
55
- return pathname + search + hash;
56
- }
57
- /**
58
- * @param [params] Parameters to insert
59
- * @returns Resolved `pathname` with params
60
- */
61
- pathname(...[params]) {
62
- if (!params)
63
- return this.pattern;
64
- const wild = "*";
65
- return this.#parts
66
- .map((part) => {
67
- if (part.startsWith(":")) {
68
- const param = part.slice(1);
69
- if (!(param in params))
70
- throw new Error(`Parameter "${param}" did not match pattern.`);
71
- return params[param];
72
- }
73
- if (part === wild) {
74
- if (!(wild in params))
75
- throw new Error("No wildcard parameter found.");
76
- return params[wild];
77
- }
78
- return part;
79
- })
80
- .join("/");
81
- }
82
- }
@@ -1,35 +0,0 @@
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> {
5
- /**
6
- * @param middleware POST middleware
7
- *
8
- * @example
9
- *
10
- * ```tsx
11
- * import { Post } from "ovr";
12
- *
13
- * const post = new Post((c) => {
14
- * console.log("posted");
15
- * c.redirect("/", 303);
16
- * });
17
- *
18
- * const page = new Get("/", () => (
19
- * <post.Form>
20
- * <input type="text" name="name" />
21
- * <button>Submit</button>
22
- * </post.Form>
23
- * ));
24
- *
25
- * app.add(page, post); // register
26
- * ```
27
- */
28
- constructor(...middleware: Middleware<{}>[]);
29
- /**
30
- * @param pattern Route pattern
31
- * @param middleware POST middleware
32
- */
33
- constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
34
- }
35
- //# sourceMappingURL=post.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,15 +0,0 @@
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
- }
@@ -1,12 +0,0 @@
1
- export declare class Memo {
2
- #private;
3
- /**
4
- * Calls to the returned function with the same arguments will
5
- * return a cached result.
6
- *
7
- * @param fn Function to memoize.
8
- * @returns The memoized function.
9
- */
10
- use<A extends any[], R>(fn: (...args: A) => R): (...args: A) => R;
11
- }
12
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/memo/index.ts"],"names":[],"mappings":"AAAA,qBAAa,IAAI;;IAOhB;;;;;;OAMG;IACH,GAAG,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,aAAP,CAAC,KAAK,CAAC;CAyB7C"}
@@ -1,34 +0,0 @@
1
- export class Memo {
2
- /** Already memoized functions. */
3
- #memoized = new WeakMap();
4
- /** Symbol to store the result of each memoized function. */
5
- static #RESULT = Symbol();
6
- /**
7
- * Calls to the returned function with the same arguments will
8
- * return a cached result.
9
- *
10
- * @param fn Function to memoize.
11
- * @returns The memoized function.
12
- */
13
- use(fn) {
14
- if (this.#memoized.has(fn))
15
- return this.#memoized.get(fn);
16
- // closure will GC automatically
17
- const trie = new Map();
18
- const memo = (...args) => {
19
- let node = trie;
20
- for (const arg of args) {
21
- // get/create a node for each arg
22
- if (!node.has(arg))
23
- node.set(arg, new Map());
24
- node = node.get(arg);
25
- }
26
- // set the final result if not there
27
- if (!node.has(Memo.#RESULT))
28
- node.set(Memo.#RESULT, fn(...args));
29
- return node.get(Memo.#RESULT);
30
- };
31
- this.#memoized.set(fn, memo);
32
- return memo;
33
- }
34
- }
@@ -1,38 +0,0 @@
1
- export declare class Chunk {
2
- #private;
3
- /** Safe value to render */
4
- value: string;
5
- /**
6
- * `Chunk` to send in an HTML stream.
7
- *
8
- * @param html string of HTML to escape
9
- * @param safe Set to `true` if the HTML is safe and should not be escaped
10
- */
11
- constructor(html: unknown, safe?: boolean);
12
- /**
13
- * @returns `value`
14
- */
15
- toString(): string;
16
- /**
17
- * @param chunks Chunks to append to the end of the chunk
18
- */
19
- concat(...chunks: Chunk[]): void;
20
- /**
21
- * Wraps `new Chunk("html", true)`.
22
- *
23
- * Use to create a new `Chunk` and bypass escaping.
24
- *
25
- * @param html Safe string of HTML
26
- * @returns New _safe_ `Chunk`
27
- */
28
- static safe(html: unknown): Chunk;
29
- /**
30
- * Escapes strings of HTML.
31
- *
32
- * @param html String to escape
33
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
34
- * @returns Escaped string of HTML
35
- */
36
- static escape(html: string, attr?: boolean): string;
37
- }
38
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKzC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;;OAOG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA2B1C"}
@@ -1,69 +0,0 @@
1
- export class Chunk {
2
- static #attr = /[&"<]/g;
3
- static #content = /[&<]/g;
4
- /** Safe value to render */
5
- value;
6
- /**
7
- * `Chunk` to send in an HTML stream.
8
- *
9
- * @param html string of HTML to escape
10
- * @param safe Set to `true` if the HTML is safe and should not be escaped
11
- */
12
- constructor(html, safe) {
13
- const value = String(html ?? "");
14
- this.value = safe ? value : Chunk.escape(value);
15
- }
16
- /**
17
- * @returns `value`
18
- */
19
- toString() {
20
- return this.value;
21
- }
22
- /**
23
- * @param chunks Chunks to append to the end of the chunk
24
- */
25
- concat(...chunks) {
26
- this.value += chunks.join("");
27
- }
28
- /**
29
- * Wraps `new Chunk("html", true)`.
30
- *
31
- * Use to create a new `Chunk` and bypass escaping.
32
- *
33
- * @param html Safe string of HTML
34
- * @returns New _safe_ `Chunk`
35
- */
36
- static safe(html) {
37
- return new Chunk(html, true);
38
- }
39
- /**
40
- * Escapes strings of HTML.
41
- *
42
- * @param html String to escape
43
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
44
- * @returns Escaped string of HTML
45
- */
46
- static escape(html, attr) {
47
- // adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
48
- const regex = attr ? Chunk.#attr : Chunk.#content;
49
- // search starts at beginning of the string
50
- regex.lastIndex = 0;
51
- // tracks the position of the last successful match in the input string
52
- let start = 0;
53
- let result = "";
54
- // since `g` flag is used, regex maintains state with each loop,
55
- // checking from the lastIndex onward each time
56
- while (regex.test(html)) {
57
- // index of the match
58
- const i = regex.lastIndex - 1;
59
- const match = html[i];
60
- result +=
61
- // everything that didn't match during this test
62
- html.slice(start, i) +
63
- // replacement
64
- (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
65
- start = regex.lastIndex;
66
- }
67
- return result + html.slice(start);
68
- }
69
- }