ovr 6.1.0 → 6.2.1

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 (43) hide show
  1. package/dist/LICENSE.md +2 -0
  2. package/dist/app/index.d.ts.map +1 -1
  3. package/dist/app/index.js +13 -9
  4. package/dist/context/index.d.ts +33 -9
  5. package/dist/context/index.d.ts.map +1 -1
  6. package/dist/context/index.js +61 -17
  7. package/dist/cookie/index.d.ts +1 -1
  8. package/dist/cookie/index.d.ts.map +1 -1
  9. package/dist/cookie/index.js +2 -2
  10. package/dist/index.d.ts +4 -3
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -2
  13. package/dist/middleware/index.d.ts +12 -3
  14. package/dist/middleware/index.d.ts.map +1 -1
  15. package/dist/multipart/index.d.ts +5 -0
  16. package/dist/multipart/index.d.ts.map +1 -1
  17. package/dist/multipart/index.js +20 -22
  18. package/dist/render/index.d.ts +1 -0
  19. package/dist/render/index.d.ts.map +1 -1
  20. package/dist/render/index.js +6 -4
  21. package/dist/route/index.d.ts +143 -42
  22. package/dist/route/index.d.ts.map +1 -1
  23. package/dist/route/index.js +94 -42
  24. package/dist/schema/index.d.ts +1286 -0
  25. package/dist/schema/index.d.ts.map +1 -0
  26. package/dist/schema/index.js +1321 -0
  27. package/dist/trie/index.d.ts.map +1 -1
  28. package/dist/trie/index.js +8 -2
  29. package/dist/util/index.d.ts +113 -19
  30. package/dist/util/index.d.ts.map +1 -1
  31. package/dist/util/index.js +111 -35
  32. package/package.json +4 -1
  33. package/src/app/index.ts +13 -12
  34. package/src/context/index.ts +104 -25
  35. package/src/cookie/index.ts +3 -3
  36. package/src/index.ts +4 -3
  37. package/src/middleware/index.ts +19 -8
  38. package/src/multipart/index.ts +23 -24
  39. package/src/render/index.ts +7 -4
  40. package/src/route/index.ts +336 -95
  41. package/src/schema/index.ts +2798 -0
  42. package/src/trie/index.ts +12 -2
  43. package/src/util/index.ts +142 -36
@@ -1,9 +1,15 @@
1
1
  import { type JSX, jsx } from "../jsx/index.js";
2
2
  import type { Middleware } from "../middleware/index.js";
3
+ import { Form as FormSchema } from "../schema/index.js";
3
4
  import type { Trie } from "../trie/index.js";
4
- import { Hash, Method, Mime } from "../util/index.js";
5
+ import { Checksum, Method, Mime } from "../util/index.js";
6
+ import type { Util } from "../util/index.js";
5
7
 
6
- /** Helper type to extract the route params (`:slug`) into a record */
8
+ /**
9
+ * Helper type to extract the route params (`:slug`) into a record.
10
+ *
11
+ * @template Pattern Route pattern to extract params from
12
+ */
7
13
  export type ExtractParams<Pattern extends string = string> =
8
14
  Pattern extends `${infer _Start}:${infer Param}/${infer Rest}`
9
15
  ? { [k in Param | keyof ExtractParams<Rest>]: string }
@@ -13,7 +19,12 @@ export type ExtractParams<Pattern extends string = string> =
13
19
  ? { "*": string }
14
20
  : {};
15
21
 
16
- /** Helper type to insert a record of params into a resolved string */
22
+ /**
23
+ * Helper type to insert a record of params into a resolved string.
24
+ *
25
+ * @template Pattern Route pattern to resolve
26
+ * @template Params Params to insert into pattern
27
+ */
17
28
  export type InsertParams<
18
29
  Pattern extends string,
19
30
  Params extends Trie.Params,
@@ -45,6 +56,87 @@ export namespace Route {
45
56
  | "PATCH"
46
57
  | (string & {});
47
58
 
59
+ // these types are needed for proper JSDoc on `get` and `post` return types
60
+ /**
61
+ * Helper type for a route with a `<Button>` component.
62
+ *
63
+ * @template Pattern Route pattern
64
+ */
65
+ type WithButton<Pattern extends string = string> = {
66
+ /** `<button>` component with preset `formaction` and `formmethod` attributes */
67
+ readonly Button: Route.Button<Pattern>;
68
+ };
69
+
70
+ /**
71
+ * Helper type for a route with a `<Form>` component.
72
+ *
73
+ * @template Pattern Route pattern
74
+ */
75
+ type WithForm<Pattern extends string = string> = {
76
+ /** `<form>` component with preset `method` and `action` attributes */
77
+ readonly Form: Route.Form<Pattern>;
78
+ };
79
+
80
+ /**
81
+ * Helper type for a route with a `<Anchor>` component.
82
+ *
83
+ * @template Pattern Route pattern
84
+ */
85
+ type WithAnchor<Pattern extends string = string> = {
86
+ /** `<a>` component with preset `href` attribute */
87
+ readonly Anchor: Route.Anchor<Pattern>;
88
+ };
89
+
90
+ /**
91
+ * Form method subset attached to route helper types.
92
+ *
93
+ * Omits `component` so schema-specific `component` signatures are not widened when a
94
+ * concrete `Form<S>` is intersected onto the route type.
95
+ */
96
+ type FormMethods = Omit<Route.Schema<any>, "component">;
97
+
98
+ /**
99
+ * Optional form helper methods available when a route may have a schema.
100
+ *
101
+ * Function members are wrapped with bivariant parameters so routes can be
102
+ * structurally assigned across compatible generic instances.
103
+ */
104
+ type MaybeForm = {
105
+ // maybe schema assigned - makes route.Fields?.() possible
106
+ [K in keyof FormMethods]?: FormMethods[K] extends (...args: any[]) => any
107
+ ? Util.Bivariant<FormMethods[K]>
108
+ : FormMethods[K];
109
+ };
110
+
111
+ /**
112
+ * GET route type with route components and optional schema helpers.
113
+ *
114
+ * @template Pattern Route pattern
115
+ */
116
+ export type Get<Pattern extends string = string> = Route<Pattern> &
117
+ WithAnchor<Pattern> &
118
+ WithButton<Pattern> &
119
+ WithForm<Pattern> &
120
+ MaybeForm;
121
+
122
+ /**
123
+ * POST route type with route components and optional schema helpers.
124
+ *
125
+ * @template Pattern Route pattern
126
+ */
127
+ export type Post<Pattern extends string = string> = Route<Pattern> &
128
+ WithButton<Pattern> &
129
+ WithForm<Pattern> &
130
+ MaybeForm;
131
+
132
+ /**
133
+ * Schema helper type attached to routes that have a form schema.
134
+ *
135
+ * @template S Form field shape
136
+ */
137
+ export type Schema<S extends FormSchema.Shape = FormSchema.Shape> =
138
+ FormSchema<S>;
139
+
48
140
  /**
49
141
  * Options to construct a relative URL from the route.
50
142
  *
@@ -103,48 +195,22 @@ export namespace Route {
103
195
  * @template Pattern Route pattern
104
196
  */
105
197
  export type Form<Pattern extends string> = (
106
- props: JSX.IntrinsicElements["form"] & URLOptions<ExtractParams<Pattern>>,
198
+ props: JSX.IntrinsicElements["form"] &
199
+ URLOptions<ExtractParams<Pattern>> & { state?: URL },
107
200
  ) => JSX.Element;
108
201
 
109
- /** Extract params from a Route instance. */
110
- export type Params<R> = R extends { pattern: infer P }
202
+ /**
203
+ * Extract params from a Route instance.
204
+ *
205
+ * @template Route Route with pattern to extract params from
206
+ */
207
+ export type Params<Route> = Route extends { pattern: infer P }
111
208
  ? P extends string
112
209
  ? ExtractParams<P>
113
210
  : never
114
211
  : never;
115
212
  }
116
213
 
117
- // these types are needed for proper JSDoc on `get` and `post` return types
118
- /**
119
- * Helper type for a route with a `<Button>` component.
120
- *
121
- * @template Pattern Route pattern
122
- */
123
- type WithButton<Pattern extends string = string> = {
124
- /** `<button>` component with preset `formaction` and `formmethod` attributes */
125
- readonly Button: Route.Button<Pattern>;
126
- };
127
-
128
- /**
129
- * Helper type for a route with a `<Form>` component.
130
- *
131
- * @template Pattern Route pattern
132
- */
133
- type WithForm<Pattern extends string = string> = {
134
- /** `<form>` component with preset `method` and `action` attributes */
135
- readonly Form: Route.Form<Pattern>;
136
- };
137
-
138
- /**
139
- * Helper type for a route with a `<Anchor>` component.
140
- *
141
- * @template Pattern Route pattern
142
- */
143
- type WithAnchor<Pattern extends string = string> = {
144
- /** `<a>` component with preset `href` attribute */
145
- readonly Anchor: Route.Anchor<Pattern>;
146
- };
147
-
148
214
  /**
149
215
  * Route to use in the application.
150
216
  *
@@ -158,7 +224,7 @@ export class Route<Pattern extends string = string> {
158
224
  readonly method: Route.Method;
159
225
 
160
226
  /** Route middleware stack, runs after global middleware */
161
- readonly middleware: Middleware<any>[]; // any so you can use other middleware
227
+ readonly middleware: Middleware<any, any>[]; // any so you can use other middleware
162
228
 
163
229
  /** Pattern parts */
164
230
  #parts: string[];
@@ -173,7 +239,7 @@ export class Route<Pattern extends string = string> {
173
239
  constructor(
174
240
  method: Route.Method,
175
241
  pattern: Pattern,
176
- ...middleware: Middleware<ExtractParams<Pattern>>[]
242
+ ...middleware: Middleware<ExtractParams<Pattern>, any>[]
177
243
  ) {
178
244
  this.method = method;
179
245
  this.pattern = pattern;
@@ -192,30 +258,24 @@ export class Route<Pattern extends string = string> {
192
258
  ? [Route.URLOptions<ExtractParams<Pattern>>] | []
193
259
  : [Route.URLOptions<ExtractParams<Pattern>>]
194
260
  ) {
195
- const pathname = this.pathname(
196
- // @ts-expect-error - do not have to pass in {} if no params
197
- options?.params,
261
+ return (
262
+ this.pathname(
263
+ // @ts-expect-error - do not have to pass in {} if no params
264
+ options?.params,
265
+ ) +
266
+ (options?.search
267
+ ? // use the value as the init
268
+ // @ts-expect-error - see type above
269
+ "?" + new URLSearchParams(options.search)
270
+ : "") +
271
+ (options?.hash
272
+ ? // adding # prefix if not present matches the URL setter:
273
+ // https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
274
+ options.hash[0] === "#"
275
+ ? options.hash
276
+ : "#" + options.hash
277
+ : "")
198
278
  );
199
- let search = "";
200
- let hash = "";
201
-
202
- if (options?.search) {
203
- // use the value as the init
204
- // @ts-expect-error - see above
205
- search = "?" + new URLSearchParams(options.search);
206
- }
207
-
208
- if (options?.hash) {
209
- // adding # prefix if not present matches the URL setter:
210
- // https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
211
- if (options.hash.startsWith("#")) {
212
- hash = options.hash;
213
- } else {
214
- hash = "#" + options.hash;
215
- }
216
- }
217
-
218
- return pathname + search + hash;
219
279
  }
220
280
 
221
281
  /**
@@ -247,8 +307,7 @@ export class Route<Pattern extends string = string> {
247
307
  * @returns Route with added components
248
308
  */
249
309
  static #withComponents<Pattern extends string>(route: Route<Pattern>) {
250
- const enctype =
251
- route.method === Method.post ? Mime.multipartFormData : undefined;
310
+ const enctype = route.method === Method.post ? Mime.type.mp : undefined;
252
311
 
253
312
  return Object.assign(route, {
254
313
  Button: (({ params, search, hash, ...rest }) =>
@@ -260,15 +319,68 @@ export class Route<Pattern extends string = string> {
260
319
  formenctype: enctype,
261
320
  ...rest,
262
321
  })) as Route.Button<Pattern>,
263
- Form: (({ params, search, hash, ...rest }) =>
264
- jsx("form", {
265
- action: route.url({ params, search, hash } as Route.URLOptions<
266
- ExtractParams<Pattern>
267
- >),
322
+ Form: (({ params, search, hash, state, ...rest }) => {
323
+ let query: URLSearchParams | undefined;
324
+
325
+ if (route.method === Method.post && state) {
326
+ const target = new URL(state);
327
+
328
+ for (const param of FormSchema.params) {
329
+ target.searchParams.delete(param);
330
+ }
331
+
332
+ query = new URLSearchParams(
333
+ // @ts-expect-error - see type above
334
+ search,
335
+ );
336
+
337
+ query.set(
338
+ FormSchema.params[0],
339
+ target.pathname + target.search + target.hash,
340
+ );
341
+ }
342
+
343
+ return jsx("form", {
344
+ action: route.url({
345
+ params,
346
+ search: query ?? search,
347
+ hash,
348
+ } as Route.URLOptions<ExtractParams<Pattern>>),
268
349
  method: route.method,
269
350
  enctype,
270
351
  ...rest,
271
- })) as Route.Form<Pattern>,
352
+ });
353
+ }) as Route.Form<Pattern>,
354
+ });
355
+ }
356
+
357
+ /**
358
+ * @template Pattern Route pattern
359
+ * @template R Route type with Form component
360
+ * @param route Route to add schema behavior to
361
+ * @param schema Form schema
362
+ * @returns Route with schema behavior attached
363
+ */
364
+ static #withSchema<
365
+ Pattern extends string,
366
+ R extends { middleware: Middleware<any, any>[]; Form: Route.Form<Pattern> },
367
+ >(route: R, schema?: Route.Schema): R | (R & Route.Schema) {
368
+ if (!schema) return route;
369
+
370
+ // original form shell from normal route
371
+ const { Form } = route;
372
+
373
+ return Object.assign(route, schema, {
374
+ Form: (({ state, ...rest }) =>
375
+ Form({
376
+ // add in the default children (Fields) with state and submit button
377
+ children: [
378
+ schema.Fields({ state }),
379
+ jsx("button", { children: "Submit" }),
380
+ ],
381
+ state,
382
+ ...rest,
383
+ } as Parameters<typeof Form>[0])) as Route.Form<Pattern>,
272
384
  });
273
385
  }
274
386
 
@@ -281,30 +393,98 @@ export class Route<Pattern extends string = string> {
281
393
  static get<Pattern extends string>(
282
394
  pattern: Pattern,
283
395
  ...middleware: Middleware<ExtractParams<Pattern>>[]
284
- ): Route<Pattern> &
285
- WithButton<Pattern> &
286
- WithForm<Pattern> &
287
- WithAnchor<Pattern> {
396
+ ): Route.Get<Pattern>;
397
+ /**
398
+ * @template Pattern Route pattern
399
+ * @template S Form field shape
400
+ * @param pattern Route pattern
401
+ * @param fields Form field shape
402
+ * @param middleware GET middleware
403
+ * @returns GET `Route` with added components
404
+ */
405
+ static get<Pattern extends string, S extends FormSchema.Shape>(
406
+ pattern: Pattern,
407
+ fields: S,
408
+ ...middleware: Middleware<ExtractParams<Pattern>, S>[]
409
+ ): Route.Get<Pattern> & Route.Schema<S>;
410
+ /**
411
+ * @template Pattern Route pattern
412
+ * @template S Form field shape
413
+ * @param pattern Route pattern
414
+ * @param form Form schema
415
+ * @param middleware GET middleware
416
+ * @returns GET `Route` with added components
417
+ */
418
+ static get<Pattern extends string, S extends FormSchema.Shape>(
419
+ pattern: Pattern,
420
+ form: Route.Schema<S>,
421
+ ...middleware: Middleware<ExtractParams<Pattern>, S>[]
422
+ ): Route.Get<Pattern> & Route.Schema<S>;
423
+ static get<Pattern extends string>(
424
+ pattern: Pattern,
425
+ schemaOrMiddleware?:
426
+ | Route.Schema
427
+ | FormSchema.Shape
428
+ | Middleware<ExtractParams<Pattern>>
429
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>,
430
+ ...middleware: (
431
+ | Middleware<ExtractParams<Pattern>>
432
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>
433
+ )[]
434
+ ) {
435
+ let schema: Route.Schema | undefined;
436
+
437
+ if (schemaOrMiddleware) {
438
+ if (typeof schemaOrMiddleware === "function") {
439
+ middleware.unshift(schemaOrMiddleware);
440
+ } else {
441
+ schema = FormSchema.from(schemaOrMiddleware);
442
+ }
443
+ }
444
+
288
445
  const route = Route.#withComponents(
289
446
  new Route(Method.get, pattern, ...middleware),
290
447
  );
291
448
 
292
- return Object.assign(route, {
293
- Anchor: (({ params, search, hash, ...rest }) =>
294
- jsx("a", {
295
- href: route.url({ params, search, hash } as Route.URLOptions<
296
- ExtractParams<Pattern>
297
- >),
298
- ...rest,
299
- })) as Route.Anchor<Pattern>,
300
- });
449
+ return Route.#withSchema(
450
+ Object.assign(route, {
451
+ Anchor: (({ params, search, hash, ...rest }) =>
452
+ jsx("a", {
453
+ href: route.url({ params, search, hash } as Route.URLOptions<
454
+ ExtractParams<Pattern>
455
+ >),
456
+ ...rest,
457
+ })) as Route.Anchor<Pattern>,
458
+ }),
459
+ schema,
460
+ );
301
461
  }
302
462
 
303
463
  /**
304
464
  * @param middleware POST middleware
305
465
  * @returns POST `Route` with added components
306
466
  */
307
- static post(middleware: Middleware<{}>): Route & WithButton & WithForm;
467
+ static post(...middleware: Middleware<{}>[]): Route.Post;
468
+ /**
469
+ * @template S Form field shape
470
+ * @param fields Form field shape
471
+ * @param middleware POST middleware
472
+ * @returns POST `Route` with added components
473
+ */
474
+ static post<S extends FormSchema.Shape>(
475
+ fields: S,
476
+ ...middleware: Middleware<{}, S>[]
477
+ ): Route.Post & Route.Schema<S>;
478
+ /**
479
+ * @template S Form field shape
480
+ * @param form Form schema
481
+ * @param middleware POST middleware
482
+ * @returns POST `Route` with added components
483
+ */
484
+ static post<S extends FormSchema.Shape>(
485
+ form: Route.Schema<S>,
486
+ ...middleware: Middleware<{}, S>[]
487
+ ): Route.Post & Route.Schema<S>;
308
488
  /**
309
489
  * @template Pattern Route pattern
310
490
  * @param pattern Route pattern
@@ -314,22 +494,83 @@ export class Route<Pattern extends string = string> {
314
494
  static post<Pattern extends string>(
315
495
  pattern: Pattern,
316
496
  ...middleware: Middleware<ExtractParams<Pattern>>[]
317
- ): Route<Pattern> & WithButton<Pattern> & WithForm<Pattern>;
497
+ ): Route.Post<Pattern>;
498
+ /**
499
+ * @template Pattern Route pattern
500
+ * @template S Form field shape
501
+ * @param pattern Route pattern
502
+ * @param fields Form field shape
503
+ * @param middleware POST middleware
504
+ * @returns POST `Route` with added components
505
+ */
506
+ static post<Pattern extends string, S extends FormSchema.Shape>(
507
+ pattern: Pattern,
508
+ fields: S,
509
+ ...middleware: Middleware<ExtractParams<Pattern>, S>[]
510
+ ): Route.Post<Pattern> & Route.Schema<S>;
511
+ /**
512
+ * @template Pattern Route pattern
513
+ * @template S Form field shape
514
+ * @param pattern Route pattern
515
+ * @param form Form schema
516
+ * @param middleware POST middleware
517
+ * @returns POST `Route` with added components
518
+ */
519
+ static post<Pattern extends string, S extends FormSchema.Shape>(
520
+ pattern: Pattern,
521
+ form: Route.Schema<S>,
522
+ ...middleware: Middleware<ExtractParams<Pattern>, S>[]
523
+ ): Route.Post<Pattern> & Route.Schema<S>;
318
524
  static post<Pattern extends string>(
319
- patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
320
- ...middleware: Middleware<ExtractParams<Pattern>>[]
525
+ patternOrSchemaOrMiddleware:
526
+ | Pattern
527
+ | Route.Schema
528
+ | FormSchema.Shape
529
+ | Middleware<ExtractParams<Pattern>>
530
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>,
531
+ schemaOrMiddleware?:
532
+ | Route.Schema
533
+ | FormSchema.Shape
534
+ | Middleware<ExtractParams<Pattern>>
535
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>,
536
+ ...middleware: (
537
+ | Middleware<ExtractParams<Pattern>>
538
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>
539
+ )[]
321
540
  ) {
322
- let pattern: Pattern;
541
+ let pattern: Pattern | undefined;
542
+ let schema: Route.Schema | undefined;
543
+
544
+ const resolve = (
545
+ psm?:
546
+ | Pattern
547
+ | Route.Schema
548
+ | FormSchema.Shape
549
+ | Middleware<ExtractParams<Pattern>>
550
+ | Middleware<ExtractParams<Pattern>, FormSchema.Shape>,
551
+ ) => {
552
+ if (psm) {
553
+ if (typeof psm === "string") {
554
+ pattern = psm;
555
+ } else if (typeof psm !== "function") {
556
+ schema = FormSchema.from(psm);
557
+ } else {
558
+ middleware.unshift(psm);
559
+ }
560
+ }
561
+ };
562
+
563
+ resolve(patternOrSchemaOrMiddleware);
564
+ resolve(schemaOrMiddleware);
323
565
 
324
- if (typeof patternOrMiddleware === "string") {
325
- pattern = patternOrMiddleware;
326
- } else {
327
- middleware.unshift(patternOrMiddleware);
328
- pattern = `/_p/${Hash.djb2(middleware.join())}` as Pattern;
566
+ if (!pattern) {
567
+ // create pattern with checksum of all mw stringified
568
+ pattern = `/_p/${Checksum.djb2(middleware.join())}` as Pattern;
329
569
  }
330
570
 
331
- return Route.#withComponents(
332
- new Route(Method.post, pattern, ...middleware),
571
+ return Route.#withSchema(
572
+ Route.#withComponents(new Route(Method.post, pattern, ...middleware)),
573
+ schema,
333
574
  );
334
575
  }
335
576
  }