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
@@ -1,11 +1,12 @@
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 { Render } from "../render/index.js";
6
6
  import { Route } from "../route/index.js";
7
- import { type Trie } from "../trie/index.js";
8
- import { Hash, Header } from "../util/index.js";
7
+ import { Form as FormSchema } from "../schema/index.js";
8
+ import type { Trie } from "../trie/index.js";
9
+ import { Checksum, Header, Method, Mime, type Util } from "../util/index.js";
9
10
 
10
11
  /** Properties to build the final `Response` with once middleware has run. */
11
12
  class PreparedResponse {
@@ -32,16 +33,38 @@ class PreparedResponse {
32
33
  }
33
34
 
34
35
  export namespace Context {
35
- // gives users access to Middleware.Context.Cookie.Options
36
- export type Cookie = InstanceType<typeof Cookie>;
36
+ /**
37
+ * Context.data return type.
38
+ *
39
+ * Routes without a schema return `null`.
40
+ *
41
+ * @template S Form shape
42
+ */
43
+ export type Result<S extends FormSchema.Shape> = [FormSchema.Shape] extends [
44
+ S,
45
+ ]
46
+ ? null
47
+ : Util.Prettify<
48
+ FormSchema.Parse.Result<
49
+ S,
50
+ {
51
+ /** URL with invalid field values encoded into a search parameter. */
52
+ readonly url: URL;
53
+ }
54
+ >
55
+ >;
37
56
  }
38
57
 
39
58
  /**
40
59
  * Request context.
41
60
  *
42
61
  * @template Params Parameters created from a route match
62
+ * @template Shape Parsed form data shape
43
63
  */
44
- export class Context<Params extends Trie.Params = Trie.Params> {
64
+ export class Context<
65
+ Params extends Trie.Params = Trie.Params,
66
+ Shape extends FormSchema.Shape = FormSchema.Shape,
67
+ > {
45
68
  /**
46
69
  * Incoming `Request` to the server.
47
70
  *
@@ -71,17 +94,15 @@ export class Context<Params extends Trie.Params = Trie.Params> {
71
94
  /** Forwarded app options */
72
95
  readonly #options: App.Options;
73
96
 
74
- // for reuse across methods
75
- static readonly #textHtml = "text/html";
76
- static readonly #utf8 = "charset=utf-8";
77
- static readonly #htmlType = `${Context.#textHtml}; ${Context.#utf8}`;
97
+ /** Cached result from `data()` to avoid re-reading request bodies. */
98
+ #parsed?: Promise<Context.Result<Shape>>;
78
99
 
79
100
  /**
80
101
  * Creates a new `Context` with the current `Request`.
81
102
  *
82
103
  * @param req Request
83
104
  */
84
- constructor(req: Request, options: App.Options = {}) {
105
+ constructor(req: Request, options: App.Options) {
85
106
  this.req = req;
86
107
  this.url = new URL(req.url);
87
108
  this.#options = options;
@@ -96,7 +117,7 @@ export class Context<Params extends Trie.Params = Trie.Params> {
96
117
  html(body: BodyInit | null, status?: number) {
97
118
  this.res.body = body;
98
119
  this.res.status = status;
99
- this.res.headers.set(Header.contentType, Context.#htmlType);
120
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
100
121
  }
101
122
 
102
123
  /**
@@ -105,22 +126,22 @@ export class Context<Params extends Trie.Params = Trie.Params> {
105
126
  * @param data passed into JSON.stringify to create the body
106
127
  * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
107
128
  */
108
- json(data: unknown, status?: number) {
129
+ json<D>(data: D extends bigint ? never : D, status?: number) {
109
130
  this.res.body = JSON.stringify(data);
110
131
  this.res.status = status;
111
- this.res.headers.set(Header.contentType, "application/json");
132
+ this.res.headers.set(Header.name.type, Mime.type.json);
112
133
  }
113
134
 
114
135
  /**
115
136
  * Creates a plain text response.
116
137
  *
117
- * @param body response body
138
+ * @param body text body
118
139
  * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
119
140
  */
120
- text(body: BodyInit, status?: number) {
141
+ text(body: BodyInit | null, status?: number) {
121
142
  this.res.body = body;
122
143
  this.res.status = status;
123
- this.res.headers.set(Header.contentType, `text/plain; ${Context.#utf8}`);
144
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.text));
124
145
  }
125
146
 
126
147
  /**
@@ -135,27 +156,33 @@ export class Context<Params extends Trie.Params = Trie.Params> {
135
156
  * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
136
157
  * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
137
158
  */
138
- redirect(location: string | URL, status: 301 | 302 | 303 | 307 | 308 = 302) {
159
+ redirect(
160
+ location: string | URL | Route,
161
+ status: 301 | 302 | 303 | 307 | 308 = 302,
162
+ ) {
139
163
  this.res.body = null;
140
164
  this.res.status = status;
141
- this.res.headers.set("location", String(location));
165
+ this.res.headers.set(
166
+ Header.name.loc,
167
+ location instanceof Route ? location.pathname() : String(location),
168
+ );
142
169
  }
143
170
 
144
171
  /**
145
- * Generates an etag from a hash of the string provided.
172
+ * Generates an etag from a checksum of the string provided.
146
173
  * If the etag matches, sets the response to not modified.
147
174
  *
148
175
  * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
149
176
  *
150
- * @param string string to hash
177
+ * @param string string to check
151
178
  * @returns `true` if the etag matches, `false` otherwise
152
179
  */
153
180
  etag(string: string) {
154
- const etag = `"${Hash.djb2(string)}"`;
181
+ const etag = `"${Checksum.djb2(string)}"`;
155
182
 
156
- this.res.headers.set("etag", etag);
183
+ this.res.headers.set(Header.name.etag, etag);
157
184
 
158
- if (this.req.headers.get("if-none-match") === etag) {
185
+ if (this.req.headers.get(Header.name.none) === etag) {
159
186
  this.res.body = null;
160
187
  this.res.status = 304;
161
188
 
@@ -166,10 +193,12 @@ export class Context<Params extends Trie.Params = Trie.Params> {
166
193
  }
167
194
 
168
195
  /**
169
- * Parse multipart requests.
196
+ * Parse a multipart request.
170
197
  *
171
- * @yields Multipart request `Part`(s)
198
+ * _Prefer_ `Context.data()` _when possible to parse
199
+ * with a type-safe schema._
172
200
  *
201
+ * @yields Multipart request `Part`(s)
173
202
  * @example
174
203
  *
175
204
  * ```ts
@@ -185,10 +214,55 @@ export class Context<Params extends Trie.Params = Trie.Params> {
185
214
  * ```
186
215
  */
187
216
  form(options?: Multipart.Options) {
188
- return new Multipart(
189
- this.req,
190
- Object.assign({}, this.#options.form, options),
191
- );
217
+ return new Multipart(this.req, { ...this.#options.form, ...options });
218
+ }
219
+
220
+ /**
221
+ * Parse route form data from search params or multipart requests.
222
+ *
223
+ * @param options Multipart options for POST requests
224
+ * @returns Parsed form data with possible invalid redirect URL state
225
+ */
226
+ async data(options?: Multipart.Options): Promise<Context.Result<Shape>> {
227
+ return (this.#parsed ??= (async () => {
228
+ const post = this.req.method === Method.post;
229
+ const form = this.route as Route.Post | undefined;
230
+
231
+ if (!form?.parse) return null as Context.Result<Shape>;
232
+
233
+ const result = await form.parse(
234
+ post
235
+ ? this.form({ parts: form.parts, ...options })
236
+ : this.url.searchParams,
237
+ );
238
+
239
+ if (result.issues) {
240
+ // create encoded URL state with invalid fields
241
+ let url = new URL(this.url);
242
+
243
+ if (post) {
244
+ const returnParam = url.searchParams.get(FormSchema.params[0]);
245
+
246
+ if (returnParam) {
247
+ try {
248
+ const returnUrl = new URL(returnParam, url);
249
+
250
+ if (returnUrl.origin === url.origin) url = returnUrl;
251
+ } catch {
252
+ // invalid redirect target
253
+ }
254
+ }
255
+ }
256
+
257
+ for (const param of FormSchema.params) url.searchParams.delete(param);
258
+
259
+ if (result.search) url.searchParams.set(...result.search);
260
+
261
+ return Object.assign(result, { url }) as Context.Result<Shape>;
262
+ }
263
+
264
+ return result as Context.Result<Shape>;
265
+ })());
192
266
  }
193
267
 
194
268
  /**
@@ -198,7 +272,7 @@ export class Context<Params extends Trie.Params = Trie.Params> {
198
272
  * @param i current middleware index (default `0`)
199
273
  * @returns return value of `middleware[i]`
200
274
  */
201
- async #run(middleware: Middleware<Params>[], i = 0) {
275
+ async #run(middleware: Middleware<Params, Shape>[], i = 0) {
202
276
  const mw = middleware[i];
203
277
 
204
278
  if (!mw) return; // end of stack
@@ -220,16 +294,16 @@ export class Context<Params extends Trie.Params = Trie.Params> {
220
294
  }
221
295
  } else if (r !== undefined) {
222
296
  // something to stream
223
- const type = this.res.headers.get(Header.contentType);
297
+ const [mime] = Header.shift(this.res.headers.get(Header.name.type));
224
298
 
225
299
  this.res.body = Render.stream(r, {
226
300
  // other defined types are safe
227
- safe: Boolean(type && !type.startsWith(Context.#textHtml)),
301
+ safe: Boolean(mime && !Mime.markup(mime)),
228
302
  });
229
303
 
230
- if (!type) {
304
+ if (!mime) {
231
305
  // default to HTML
232
- this.res.headers.set(Header.contentType, Context.#htmlType);
306
+ this.res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
233
307
  }
234
308
 
235
309
  // do not overwrite/remove status - that way user can set it before returning
@@ -246,17 +320,19 @@ export class Context<Params extends Trie.Params = Trie.Params> {
246
320
  static async compose(c: Context, middleware: Middleware[]) {
247
321
  await c.#run(middleware);
248
322
 
249
- if (c.req.method === "HEAD") {
323
+ if (c.req.method === Method.head) {
250
324
  if (c.res.body instanceof ReadableStream) {
251
325
  // cancel unused stream to prevent leaks
252
- await c.res.body.cancel("HEAD");
326
+ await c.res.body.cancel(Method.head);
253
327
  }
254
328
 
255
329
  c.res.body = null;
256
330
  }
257
331
 
258
- Object.freeze(c.res);
259
-
260
- return new Response(c.res.body, c.res);
332
+ return new Response(
333
+ c.res.body,
334
+ // prevents users from setting the response during the stream
335
+ Object.freeze(c.res),
336
+ );
261
337
  }
262
338
  }
@@ -92,7 +92,7 @@ export namespace Cookie {
92
92
 
93
93
  type InsecureOptions = BaseOptions & {
94
94
  /** Limits the scope of the cookie to a secure context (HTTPS). */
95
- readonly secure?: false;
95
+ readonly secure?: boolean;
96
96
 
97
97
  /**
98
98
  * Controls whether the cookie is sent with cross-site requests.
@@ -146,9 +146,9 @@ export class Cookie {
146
146
  * @returns Value of the cookie, or `undefined` if not found
147
147
  */
148
148
  get(name: string) {
149
- return (this.#parsed ??= Header.params(this.#c.req.headers.get("cookie")))[
150
- name
151
- ];
149
+ return (this.#parsed ??= Header.params(
150
+ this.#c.req.headers.get(Header.name.cookie),
151
+ ))[name];
152
152
  }
153
153
 
154
154
  /**
@@ -184,6 +184,6 @@ export class Cookie {
184
184
  if (options.expires)
185
185
  cookie.push(`Expires=${options.expires.toUTCString()}`);
186
186
 
187
- this.#c.res.headers.append("set-cookie", cookie.join("; "));
187
+ this.#c.res.headers.append(Header.name.set, cookie.join("; "));
188
188
  }
189
189
  }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
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";