ovr 6.0.1 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/LICENSE.md +2 -0
  2. package/dist/app/index.d.ts +1 -1
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +17 -11
  5. package/dist/context/index.d.ts +37 -13
  6. package/dist/context/index.d.ts.map +1 -1
  7. package/dist/context/index.js +70 -29
  8. package/dist/cookie/index.d.ts +1 -1
  9. package/dist/cookie/index.d.ts.map +1 -1
  10. package/dist/cookie/index.js +2 -2
  11. package/dist/index.d.ts +4 -3
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +3 -2
  14. package/dist/jsx/elements.d.ts +194 -2
  15. package/dist/jsx/elements.d.ts.map +1 -1
  16. package/dist/jsx/index.d.ts.map +1 -1
  17. package/dist/jsx/index.js +18 -8
  18. package/dist/middleware/index.d.ts +12 -3
  19. package/dist/middleware/index.d.ts.map +1 -1
  20. package/dist/multipart/index.d.ts +5 -0
  21. package/dist/multipart/index.d.ts.map +1 -1
  22. package/dist/multipart/index.js +22 -24
  23. package/dist/render/index.d.ts +1 -0
  24. package/dist/render/index.d.ts.map +1 -1
  25. package/dist/render/index.js +6 -4
  26. package/dist/route/index.d.ts +143 -42
  27. package/dist/route/index.d.ts.map +1 -1
  28. package/dist/route/index.js +95 -43
  29. package/dist/schema/index.d.ts +1286 -0
  30. package/dist/schema/index.d.ts.map +1 -0
  31. package/dist/schema/index.js +1321 -0
  32. package/dist/trie/index.d.ts.map +1 -1
  33. package/dist/trie/index.js +8 -2
  34. package/dist/util/index.d.ts +138 -9
  35. package/dist/util/index.d.ts.map +1 -1
  36. package/dist/util/index.js +152 -19
  37. package/package.json +4 -1
  38. package/src/app/index.ts +17 -15
  39. package/src/context/index.ts +116 -40
  40. package/src/cookie/index.ts +5 -5
  41. package/src/index.ts +4 -3
  42. package/src/jsx/elements.ts +320 -64
  43. package/src/jsx/index.ts +16 -8
  44. package/src/middleware/index.ts +19 -8
  45. package/src/multipart/index.ts +25 -27
  46. package/src/render/index.ts +7 -4
  47. package/src/route/index.ts +338 -94
  48. package/src/schema/index.ts +2794 -0
  49. package/src/trie/index.ts +12 -2
  50. package/src/util/index.ts +193 -19
package/dist/LICENSE.md CHANGED
@@ -24,3 +24,5 @@ Portions of this software have been adapted from the following projects, which a
24
24
  - Cookie handling - https://github.com/jshttp/cookie
25
25
  Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
26
26
  Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
27
+ - Schema API - https://github.com/colinhacks/zod
28
+ Copyright (c) 2025 Colin McDonnell
@@ -1,7 +1,7 @@
1
1
  import type { Middleware } from "../middleware/index.js";
2
2
  import type { Multipart } from "../multipart/index.js";
3
3
  import { Route } from "../route/index.js";
4
- import type { Util } from "../util/index.js";
4
+ import { type Util } from "../util/index.js";
5
5
  export declare namespace App {
6
6
  namespace Options {
7
7
  /** Trailing slash preference */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C,yBAAiB,GAAG,CAAC;IACpB,UAAiB,OAAO,CAAC;QACxB,gCAAgC;QAChC,KAAY,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;KAC1D;IAED,4BAA4B;IAC5B,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;WAYG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAExB;;;;;;;;WAQG;QACH,QAAQ,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B,qBAAa,GAAG;;IAUf;;;;OAIG;gBACS,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO;IAcjC;;;OAGG;IACH,GAAG,CACF,GAAG,MAAM,EAAE,IAAI,CAAC,SAAS,CACxB,KAAK,GAAG,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,UAAU,CAAC,CACvD,EAAE;IAiBJ;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAK,GACb,UAAU,WAAW,GAAG,GAAG,EAC3B,UAAU,WAAW,uBAgBpB;CAkCF"}
package/dist/app/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Context } from "../context/index.js";
2
2
  import { Route } from "../route/index.js";
3
3
  import { Trie } from "../trie/index.js";
4
+ import { Header, Method } from "../util/index.js";
4
5
  /** Web server application */
5
6
  export class App {
6
7
  /** Route trie */
@@ -15,8 +16,12 @@ export class App {
15
16
  * @param options configuration options
16
17
  */
17
18
  constructor(options) {
18
- this.#options = Object.assign({ csrf: true, trailingSlash: "never" }, options);
19
- if (this.#options.csrf === true)
19
+ this.#options = {
20
+ csrf: true,
21
+ trailingSlash: "never",
22
+ ...options,
23
+ };
24
+ if (this.#options.csrf)
20
25
  this.#global.push(App.#csrf);
21
26
  if (this.#options.trailingSlash !== "ignore") {
22
27
  this.#global.push(App.#createTrailingSlash(this.#options.trailingSlash));
@@ -31,7 +36,7 @@ export class App {
31
36
  if (route instanceof Route) {
32
37
  this.#trie.add(route);
33
38
  }
34
- else if (route instanceof Array) {
39
+ else if (Array.isArray(route)) {
35
40
  this.use(...route);
36
41
  }
37
42
  else if (typeof route === "function") {
@@ -58,14 +63,15 @@ export class App {
58
63
  // match
59
64
  this.#trie.find(
60
65
  // use GET for HEAD requests to find the correct route
61
- (c.req.method === "HEAD" ? "GET" : c.req.method) + c.url.pathname)), this.#global.concat(c.route?.middleware ?? []));
66
+ (c.req.method === Method.head ? Method.get : c.req.method) +
67
+ c.url.pathname)), this.#global.concat(c.route?.middleware ?? []));
62
68
  };
63
69
  /** Basic CSRF middleware */
64
70
  static #csrf(c, next) {
65
- if (c.req.method === "GET" ||
66
- c.req.method === "HEAD" ||
67
- c.req.headers.get("sec-fetch-site") === "same-origin" ||
68
- c.req.headers.get("origin") === c.url.origin) {
71
+ if (c.req.method === Method.get ||
72
+ c.req.method === Method.head ||
73
+ c.req.headers.get(Header.name.fetch) === "same-origin" ||
74
+ c.req.headers.get(Header.name.origin) === c.url.origin) {
69
75
  return next();
70
76
  }
71
77
  c.text("Forbidden", 403);
@@ -76,12 +82,12 @@ export class App {
76
82
  await next();
77
83
  if (c.res.status && c.res.status !== 404)
78
84
  return;
79
- const last = c.url.pathname.at(-1);
80
- if (mode === "always" && last !== "/") {
85
+ const slash = c.url.pathname.endsWith("/");
86
+ if (mode === "always" && !slash) {
81
87
  c.url.pathname += "/";
82
88
  c.redirect(c.url, 308);
83
89
  }
84
- else if (mode === "never" && c.url.pathname !== "/" && last === "/") {
90
+ else if (mode === "never" && c.url.pathname !== "/" && slash) {
85
91
  c.url.pathname = c.url.pathname.slice(0, -1);
86
92
  c.redirect(c.url, 308);
87
93
  }
@@ -1,9 +1,11 @@
1
1
  import type { App } from "../app/index.js";
2
2
  import { Cookie } from "../cookie/index.js";
3
- import { type Middleware } from "../middleware/index.js";
3
+ import type { Middleware } from "../middleware/index.js";
4
4
  import { Multipart } from "../multipart/index.js";
5
5
  import { Route } from "../route/index.js";
6
- import { type Trie } from "../trie/index.js";
6
+ import { Form as FormSchema } from "../schema/index.js";
7
+ import type { Trie } from "../trie/index.js";
8
+ import { type Util } from "../util/index.js";
7
9
  /** Properties to build the final `Response` with once middleware has run. */
8
10
  declare class PreparedResponse {
9
11
  /**
@@ -26,14 +28,27 @@ declare class PreparedResponse {
26
28
  headers: Headers;
27
29
  }
28
30
  export declare namespace Context {
29
- type Cookie = InstanceType<typeof Cookie>;
31
+ /**
32
+ * Context.data return type.
33
+ *
34
+ * Routes without a schema return `null`.
35
+ *
36
+ * @template S Form shape
37
+ */
38
+ type Result<S extends FormSchema.Shape> = [FormSchema.Shape] extends [
39
+ S
40
+ ] ? null : Util.Prettify<FormSchema.Parse.Result<S, {
41
+ /** URL with invalid field values encoded into a search parameter. */
42
+ readonly url: URL;
43
+ }>>;
30
44
  }
31
45
  /**
32
46
  * Request context.
33
47
  *
34
48
  * @template Params Parameters created from a route match
49
+ * @template Shape Parsed form data shape
35
50
  */
36
- export declare class Context<Params extends Trie.Params = Trie.Params> {
51
+ export declare class Context<Params extends Trie.Params = Trie.Params, Shape extends FormSchema.Shape = FormSchema.Shape> {
37
52
  #private;
38
53
  /**
39
54
  * Incoming `Request` to the server.
@@ -60,7 +75,7 @@ export declare class Context<Params extends Trie.Params = Trie.Params> {
60
75
  *
61
76
  * @param req Request
62
77
  */
63
- constructor(req: Request, options?: App.Options);
78
+ constructor(req: Request, options: App.Options);
64
79
  /**
65
80
  * Creates an HTML response.
66
81
  *
@@ -74,14 +89,14 @@ export declare class Context<Params extends Trie.Params = Trie.Params> {
74
89
  * @param data passed into JSON.stringify to create the body
75
90
  * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
76
91
  */
77
- json(data: unknown, status?: number): void;
92
+ json<D>(data: D extends bigint ? never : D, status?: number): void;
78
93
  /**
79
94
  * Creates a plain text response.
80
95
  *
81
- * @param body response body
96
+ * @param body text body
82
97
  * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
83
98
  */
84
- text(body: BodyInit, status?: number): void;
99
+ text(body: BodyInit | null, status?: number): void;
85
100
  /**
86
101
  * Creates a redirect response.
87
102
  *
@@ -94,22 +109,24 @@ export declare class Context<Params extends Trie.Params = Trie.Params> {
94
109
  * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
95
110
  * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
96
111
  */
97
- redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
112
+ redirect(location: string | URL | Route, status?: 301 | 302 | 303 | 307 | 308): void;
98
113
  /**
99
- * Generates an etag from a hash of the string provided.
114
+ * Generates an etag from a checksum of the string provided.
100
115
  * If the etag matches, sets the response to not modified.
101
116
  *
102
117
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
103
118
  *
104
- * @param string string to hash
119
+ * @param string string to check
105
120
  * @returns `true` if the etag matches, `false` otherwise
106
121
  */
107
122
  etag(string: string): boolean;
108
123
  /**
109
- * Parse multipart requests.
124
+ * Parse a multipart request.
110
125
  *
111
- * @yields Multipart request `Part`(s)
126
+ * _Prefer_ `Context.data()` _when possible to parse
127
+ * with a type-safe schema._
112
128
  *
129
+ * @yields Multipart request `Part`(s)
113
130
  * @example
114
131
  *
115
132
  * ```ts
@@ -125,6 +142,13 @@ export declare class Context<Params extends Trie.Params = Trie.Params> {
125
142
  * ```
126
143
  */
127
144
  form(options?: Multipart.Options): Multipart;
145
+ /**
146
+ * Parse route form data from search params or multipart requests.
147
+ *
148
+ * @param options Multipart options for POST requests
149
+ * @returns Parsed form data with possible invalid redirect URL state
150
+ */
151
+ data(options?: Multipart.Options): Promise<Context.Result<Shape>>;
128
152
  /**
129
153
  * Composes a stack of `middleware` into a `Response`.
130
154
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C,6EAA6E;A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I,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAuFvE;;;;;;OAMG;WACU,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE;CAkBzD"}
@@ -1,10 +1,9 @@
1
1
  import { Cookie } from "../cookie/index.js";
2
- import {} from "../middleware/index.js";
3
2
  import { Multipart } from "../multipart/index.js";
4
3
  import { Render } from "../render/index.js";
5
4
  import { Route } from "../route/index.js";
6
- import {} from "../trie/index.js";
7
- import { Hash, Header } from "../util/index.js";
5
+ import { Form as FormSchema } from "../schema/index.js";
6
+ import { Checksum, Header, Method, Mime } from "../util/index.js";
8
7
  /** Properties to build the final `Response` with once middleware has run. */
9
8
  class PreparedResponse {
10
9
  /**
@@ -30,6 +29,7 @@ class PreparedResponse {
30
29
  * Request context.
31
30
  *
32
31
  * @template Params Parameters created from a route match
32
+ * @template Shape Parsed form data shape
33
33
  */
34
34
  export class Context {
35
35
  /**
@@ -54,16 +54,14 @@ export class Context {
54
54
  cookie = new Cookie(this);
55
55
  /** Forwarded app options */
56
56
  #options;
57
- // for reuse across methods
58
- static #textHtml = "text/html";
59
- static #utf8 = "charset=utf-8";
60
- static #htmlType = `${Context.#textHtml}; ${Context.#utf8}`;
57
+ /** Cached result from `data()` to avoid re-reading request bodies. */
58
+ #parsed;
61
59
  /**
62
60
  * Creates a new `Context` with the current `Request`.
63
61
  *
64
62
  * @param req Request
65
63
  */
66
- constructor(req, options = {}) {
64
+ constructor(req, options) {
67
65
  this.req = req;
68
66
  this.url = new URL(req.url);
69
67
  this.#options = options;
@@ -77,7 +75,7 @@ export class Context {
77
75
  html(body, status) {
78
76
  this.res.body = body;
79
77
  this.res.status = status;
80
- this.res.headers.set(Header.contentType, Context.#htmlType);
78
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
81
79
  }
82
80
  /**
83
81
  * Creates a JSON response.
@@ -88,18 +86,18 @@ export class Context {
88
86
  json(data, status) {
89
87
  this.res.body = JSON.stringify(data);
90
88
  this.res.status = status;
91
- this.res.headers.set(Header.contentType, "application/json");
89
+ this.res.headers.set(Header.name.type, Mime.type.json);
92
90
  }
93
91
  /**
94
92
  * Creates a plain text response.
95
93
  *
96
- * @param body response body
94
+ * @param body text body
97
95
  * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
98
96
  */
99
97
  text(body, status) {
100
98
  this.res.body = body;
101
99
  this.res.status = status;
102
- this.res.headers.set(Header.contentType, `text/plain; ${Context.#utf8}`);
100
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.text));
103
101
  }
104
102
  /**
105
103
  * Creates a redirect response.
@@ -116,21 +114,21 @@ export class Context {
116
114
  redirect(location, status = 302) {
117
115
  this.res.body = null;
118
116
  this.res.status = status;
119
- this.res.headers.set("location", String(location));
117
+ this.res.headers.set(Header.name.loc, location instanceof Route ? location.pathname() : String(location));
120
118
  }
121
119
  /**
122
- * Generates an etag from a hash of the string provided.
120
+ * Generates an etag from a checksum of the string provided.
123
121
  * If the etag matches, sets the response to not modified.
124
122
  *
125
123
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
126
124
  *
127
- * @param string string to hash
125
+ * @param string string to check
128
126
  * @returns `true` if the etag matches, `false` otherwise
129
127
  */
130
128
  etag(string) {
131
- const etag = `"${Hash.djb2(string)}"`;
132
- this.res.headers.set("etag", etag);
133
- if (this.req.headers.get("if-none-match") === etag) {
129
+ const etag = `"${Checksum.djb2(string)}"`;
130
+ this.res.headers.set(Header.name.etag, etag);
131
+ if (this.req.headers.get(Header.name.none) === etag) {
134
132
  this.res.body = null;
135
133
  this.res.status = 304;
136
134
  return true;
@@ -138,10 +136,12 @@ export class Context {
138
136
  return false;
139
137
  }
140
138
  /**
141
- * Parse multipart requests.
139
+ * Parse a multipart request.
142
140
  *
143
- * @yields Multipart request `Part`(s)
141
+ * _Prefer_ `Context.data()` _when possible to parse
142
+ * with a type-safe schema._
144
143
  *
144
+ * @yields Multipart request `Part`(s)
145
145
  * @example
146
146
  *
147
147
  * ```ts
@@ -157,7 +157,47 @@ export class Context {
157
157
  * ```
158
158
  */
159
159
  form(options) {
160
- return new Multipart(this.req, Object.assign({}, this.#options.form, options));
160
+ return new Multipart(this.req, { ...this.#options.form, ...options });
161
+ }
162
+ /**
163
+ * Parse route form data from search params or multipart requests.
164
+ *
165
+ * @param options Multipart options for POST requests
166
+ * @returns Parsed form data with possible invalid redirect URL state
167
+ */
168
+ async data(options) {
169
+ return (this.#parsed ??= (async () => {
170
+ const post = this.req.method === Method.post;
171
+ const form = this.route;
172
+ if (!form?.parse)
173
+ return null;
174
+ const result = await form.parse(post
175
+ ? this.form({ parts: form.parts, ...options })
176
+ : this.url.searchParams);
177
+ if (result.issues) {
178
+ // create encoded URL state with invalid fields
179
+ let url = new URL(this.url);
180
+ if (post) {
181
+ const returnParam = url.searchParams.get(FormSchema.params[0]);
182
+ if (returnParam) {
183
+ try {
184
+ const returnUrl = new URL(returnParam, url);
185
+ if (returnUrl.origin === url.origin)
186
+ url = returnUrl;
187
+ }
188
+ catch {
189
+ // invalid redirect target
190
+ }
191
+ }
192
+ }
193
+ for (const param of FormSchema.params)
194
+ url.searchParams.delete(param);
195
+ if (result.search)
196
+ url.searchParams.set(...result.search);
197
+ return Object.assign(result, { url });
198
+ }
199
+ return result;
200
+ })());
161
201
  }
162
202
  /**
163
203
  * Dispatches the stack of `middleware` provided.
@@ -184,14 +224,14 @@ export class Context {
184
224
  }
185
225
  else if (r !== undefined) {
186
226
  // something to stream
187
- const type = this.res.headers.get(Header.contentType);
227
+ const [mime] = Header.shift(this.res.headers.get(Header.name.type));
188
228
  this.res.body = Render.stream(r, {
189
229
  // other defined types are safe
190
- safe: Boolean(type && !type.startsWith(Context.#textHtml)),
230
+ safe: Boolean(mime && !Mime.markup(mime)),
191
231
  });
192
- if (!type) {
232
+ if (!mime) {
193
233
  // default to HTML
194
- this.res.headers.set(Header.contentType, Context.#htmlType);
234
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
195
235
  }
196
236
  // do not overwrite/remove status - that way user can set it before returning
197
237
  }
@@ -205,14 +245,15 @@ export class Context {
205
245
  */
206
246
  static async compose(c, middleware) {
207
247
  await c.#run(middleware);
208
- if (c.req.method === "HEAD") {
248
+ if (c.req.method === Method.head) {
209
249
  if (c.res.body instanceof ReadableStream) {
210
250
  // cancel unused stream to prevent leaks
211
- await c.res.body.cancel("HEAD");
251
+ await c.res.body.cancel(Method.head);
212
252
  }
213
253
  c.res.body = null;
214
254
  }
215
- Object.freeze(c.res);
216
- return new Response(c.res.body, c.res);
255
+ return new Response(c.res.body,
256
+ // prevents users from setting the response during the stream
257
+ Object.freeze(c.res));
217
258
  }
218
259
  }
@@ -73,7 +73,7 @@ export declare namespace Cookie {
73
73
  };
74
74
  type InsecureOptions = BaseOptions & {
75
75
  /** Limits the scope of the cookie to a secure context (HTTPS). */
76
- readonly secure?: false;
76
+ readonly secure?: boolean;
77
77
  /**
78
78
  * Controls whether the cookie is sent with cross-site requests.
79
79
  * **Note:** `"none"` is not allowed unless `secure` is true.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cookie/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAGnD,yBAAiB,MAAM,CAAC;IACvB,KAAK,WAAW,GAAG;QAClB;;;WAGG;QACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAEzB;;;WAGG;QACH,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC;QAExB;;;WAGG;QACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;QAE5B;;;WAGG;QACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAEzB;;;;;WAKG;QACH,QAAQ,CAAC,IAA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,OAAO,CAAC;QAE1B;;;WAGG;QACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;QAErC;;;WAGG;QACH,QAAQ,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC;KAC7B,CAAC;IAEF;;;;OAIG;IACH,MAAM,MAAM,OAAO,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,IAC/C,IAAI,SAAS,KAAK,MAAM,GAAG,QAAQ,IAAI,MAAM,EAAE,GAC5C,KAAK,GACL,IAAI,SAAS,UAAU,MAAM,EAAE,GAC9B,iBAAiB,GACjB,IAAI,SAAS,YAAY,MAAM,EAAE,GAChC,mBAAmB,GACnB,aAAa,GAAG,eAAe,CAAC;;CACtC;AAED,0BAA0B;AAC1B,qBAAa,MAAM;;IAOlB;;;;OAIG;gBACS,CAAC,EAAE,OAAO;IAItB;;;;;OAKG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM;IAMhB;;;;;;OAMG;IACH,GAAG,CAAC,IAAI,SAAS,MAAM,EACtB,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,MAAM,EACb,GAAG,CACF,OAAO,EACP,EAAE,IAAI,SAAS,KAAK,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,IAAI,MAAM,EAAE,GACnE,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAC/B,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;CAqBpC"}
@@ -20,7 +20,7 @@ export class Cookie {
20
20
  * @returns Value of the cookie, or `undefined` if not found
21
21
  */
22
22
  get(name) {
23
- return (this.#parsed ??= Header.params(this.#c.req.headers.get("cookie")))[name];
23
+ return (this.#parsed ??= Header.params(this.#c.req.headers.get(Header.name.cookie)))[name];
24
24
  }
25
25
  /**
26
26
  * Sets a cookie on the response using the `Set-Cookie` header.
@@ -51,6 +51,6 @@ export class Cookie {
51
51
  cookie.push(`Max-Age=${options.maxAge}`);
52
52
  if (options.expires)
53
53
  cookie.push(`Expires=${options.expires.toUTCString()}`);
54
- this.#c.res.headers.append("set-cookie", cookie.join("; "));
54
+ this.#c.res.headers.append(Header.name.set, cookie.join("; "));
55
55
  }
56
56
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export { App } from "./app/index.js";
2
- export { Route } from "./route/index.js";
3
- export { Multipart } from "./multipart/index.js";
4
2
  export { JSX } from "./jsx/index.js";
5
- export { Render } from "./render/index.js";
6
3
  export type { Middleware } from "./middleware/index.js";
4
+ export { Multipart } from "./multipart/index.js";
5
+ export { Render } from "./render/index.js";
6
+ export { Route } from "./route/index.js";
7
+ export { Field, Form, Schema } from "./schema/index.js";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,YAAY,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,YAAY,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { App } from "./app/index.js";
2
- export { Route } from "./route/index.js";
3
- export { Multipart } from "./multipart/index.js";
4
2
  export { JSX } from "./jsx/index.js";
3
+ export { Multipart } from "./multipart/index.js";
5
4
  export { Render } from "./render/index.js";
5
+ export { Route } from "./route/index.js";
6
+ export { Field, Form, Schema } from "./schema/index.js";