ovr 5.0.0 → 6.0.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 (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 +40 -53
  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 +340 -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 +36 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +87 -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 -221
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +485 -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 +109 -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
@@ -0,0 +1,26 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ross Robino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ ---
12
+
13
+ Portions of this software have been adapted from the following projects, which are also licensed under the MIT License:
14
+
15
+ - Middleware composition - https://github.com/koajs/compose
16
+ Copyright (c) 2013 TJ Holowaychuk <tj@apex.sh>
17
+ - djb2 hash util - https://github.com/sveltejs/kit
18
+ Copyright (c) 2020 SvelteKit contributors
19
+ - Buffer search and HTML escape replacer - https://github.com/remix-run/remix
20
+ Copyright (c) 2025 Shopify Inc.
21
+ - Trie router - https://github.com/SaltyAom/memoirist
22
+ Copyright (c) 2023 saltyAom
23
+ Copyright (c) 2019 Medley
24
+ - Cookie handling - https://github.com/jshttp/cookie
25
+ Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
26
+ Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
@@ -1,92 +1,59 @@
1
- import { type Params } from "../trie/index.js";
2
- import type { DeepArray, ExtractMultiParams, ExtractParams } from "../types/index.js";
3
- import { Context } from "./context.js";
4
- import { Get } from "./helper/get.js";
5
- import { Post } from "./helper/post.js";
6
- export type Next = () => Promise<void>;
7
- export type Middleware<P extends Params = Params> = (context: Context<P>, next: Next) => any;
8
- export type TrailingSlash = "always" | "never" | "ignore";
9
- type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
10
- /** `App` configuration options */
11
- type AppOptions = {
12
- /**
13
- * Basic
14
- * [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
15
- * protection that checks the request's `method`, and its
16
- * [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
17
- * and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
18
- * headers.
19
- *
20
- * More robust protection requires a stateful server or a database to store
21
- * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
22
- *
23
- * @default true
24
- */
25
- csrf?: boolean;
26
- /**
27
- * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
28
- * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
29
- * - `"ignore"` - no redirects (not recommended, bad for SEO)
30
- *
31
- * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
32
- *
33
- * @default "never"
34
- */
35
- trailingSlash?: TrailingSlash;
36
- };
1
+ import type { Middleware } from "../middleware/index.js";
2
+ import type { Multipart } from "../multipart/index.js";
3
+ import { Route } from "../route/index.js";
4
+ import type { Util } from "../util/index.js";
5
+ export declare namespace App {
6
+ namespace Options {
7
+ /** Trailing slash preference */
8
+ type TrailingSlash = "always" | "never" | "ignore";
9
+ }
10
+ /** Configuration options */
11
+ type Options = {
12
+ /**
13
+ * Basic
14
+ * [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
15
+ * protection that checks the request's `method`, and its
16
+ * [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
17
+ * and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
18
+ * headers.
19
+ *
20
+ * More robust protection requires a stateful server or a database to store
21
+ * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
22
+ *
23
+ * @default true
24
+ */
25
+ readonly csrf?: boolean;
26
+ /**
27
+ * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
28
+ * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
29
+ * - `"ignore"` - no redirects (not recommended, bad for SEO)
30
+ *
31
+ * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
32
+ *
33
+ * @default "never"
34
+ */
35
+ readonly trailingSlash?: Options.TrailingSlash;
36
+ /**
37
+ * Multipart form data options
38
+ * (overridden by options passed into `Context.form(options)`)
39
+ */
40
+ readonly form?: Multipart.Options;
41
+ };
42
+ }
43
+ /** Web server application */
37
44
  export declare class App {
38
45
  #private;
39
- constructor(options?: AppOptions);
40
- /**
41
- * @param helpers `Helper` to add to `App`
42
- * @returns `App` instance
43
- */
44
- add(...helpers: DeepArray<Get | Post | Record<string, Get | Post>>[]): this;
45
46
  /**
46
- * Add global middleware.
47
+ * Create a new application.
47
48
  *
48
- * @param middleware
49
- * @returns `App` instance
49
+ * @param options configuration options
50
50
  */
51
- use(...middleware: Middleware[]): this;
51
+ constructor(options?: App.Options);
52
52
  /**
53
- * @param method HTTP method
54
- * @param pattern Route pattern
55
- * @param middleware
53
+ * @param routes Route or middleware to use
56
54
  * @returns `App` instance
57
55
  */
58
- on<Pattern extends string>(method: Method | Method[], pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
59
- /**
60
- * @param method HTTP method
61
- * @param patterns Array of route patterns
62
- * @param middleware
63
- * @returns `App` instance
64
- */
65
- on<Patterns extends string[]>(method: Method | Method[], patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
66
- /**
67
- * @param pattern Route pattern
68
- * @param middleware
69
- * @returns `App` instance
70
- */
71
- get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
72
- /**
73
- * @param patterns Array of route patterns
74
- * @param middleware
75
- * @returns `App` instance
76
- */
77
- get<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
78
- /**
79
- * @param pattern Route pattern
80
- * @param middleware
81
- * @returns the router instance
82
- */
83
- post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
84
- /**
85
- * @param patterns Array of route patterns
86
- * @param middleware
87
- * @returns the router instance
88
- */
89
- post<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
56
+ use(...routes: Util.DeepArray<Route | Middleware | Record<string, Route | Middleware>>[]): this;
90
57
  /**
91
58
  * Request a resource from the application.
92
59
  *
@@ -96,7 +63,6 @@ export declare class App {
96
63
  * @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
97
64
  * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
98
65
  */
99
- fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
66
+ readonly fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
100
67
  }
101
- export {};
102
68
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AAAA,OAAO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/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,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC;QAE/C;;;WAGG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC;KAClC,CAAC;CACF;AAED,6BAA6B;AAC7B,qBAAa,GAAG;;IAUf;;;;OAIG;gBACS,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO;IAajC;;;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,uBAepB;CAkCF"}
package/dist/app/index.js CHANGED
@@ -1,83 +1,48 @@
1
- import { Route, Trie } from "../trie/index.js";
2
- import { Context } from "./context.js";
3
- import { Get } from "./helper/get.js";
4
- import { Post } from "./helper/post.js";
1
+ import { Context } from "../context/index.js";
2
+ import { Route } from "../route/index.js";
3
+ import { Trie } from "../trie/index.js";
4
+ /** Web server application */
5
5
  export class App {
6
+ /** Route trie */
7
+ #trie = new Trie();
8
+ /** Global middleware */
9
+ #global = [];
10
+ /** App options */
11
+ #options;
12
+ /**
13
+ * Create a new application.
14
+ *
15
+ * @param options configuration options
16
+ */
6
17
  constructor(options) {
7
- const resolved = {
8
- csrf: true,
9
- trailingSlash: "never",
10
- };
11
- Object.assign(resolved, options);
12
- if (resolved.csrf === true)
13
- this.#use.push(App.#csrf);
14
- if (resolved.trailingSlash !== "ignore") {
15
- this.#use.push(App.#trailingSlash(resolved.trailingSlash));
18
+ this.#options = Object.assign({ csrf: true, trailingSlash: "never" }, options);
19
+ if (this.#options.csrf === true)
20
+ this.#global.push(App.#csrf);
21
+ if (this.#options.trailingSlash !== "ignore") {
22
+ this.#global.push(App.#createTrailingSlash(this.#options.trailingSlash));
16
23
  }
17
24
  }
18
- /** Built tries per HTTP method. */
19
- #trieMap = new Map();
20
- /** Added routes per HTTP method. */
21
- #routesMap = new Map();
22
- /** Global middleware. */
23
- #use = [];
24
25
  /**
25
- * @param helpers `Helper` to add to `App`
26
+ * @param routes Route or middleware to use
26
27
  * @returns `App` instance
27
28
  */
28
- add(...helpers) {
29
- for (const helper of helpers) {
30
- if (helper instanceof Array) {
31
- this.add(...helper);
29
+ use(...routes) {
30
+ for (const route of routes) {
31
+ if (route instanceof Route) {
32
+ this.#trie.add(route);
32
33
  }
33
- else if (helper instanceof Get) {
34
- this.get(helper.pattern, ...helper.middleware);
34
+ else if (route instanceof Array) {
35
+ this.use(...route);
35
36
  }
36
- else if (helper instanceof Post) {
37
- this.post(helper.pattern, ...helper.middleware);
37
+ else if (typeof route === "function") {
38
+ this.#global.push(route);
38
39
  }
39
40
  else {
40
- this.add(...Object.values(helper));
41
- }
42
- }
43
- return this;
44
- }
45
- /**
46
- * Add global middleware.
47
- *
48
- * @param middleware
49
- * @returns `App` instance
50
- */
51
- use(...middleware) {
52
- this.#use.push(...middleware);
53
- return this;
54
- }
55
- on(method, pattern, ...middleware) {
56
- if (!Array.isArray(method))
57
- method = [method];
58
- let patterns;
59
- if (!Array.isArray(pattern))
60
- patterns = [pattern];
61
- else
62
- patterns = pattern;
63
- for (const p of patterns) {
64
- const route = new Route(p, middleware);
65
- for (const m of method) {
66
- const routes = this.#routesMap.get(m);
67
- if (routes)
68
- routes.push(route);
69
- else
70
- this.#routesMap.set(m, [route]);
41
+ this.use(...Object.values(route));
71
42
  }
72
43
  }
73
44
  return this;
74
45
  }
75
- get(patternOrPatterns, ...middleware) {
76
- return this.on("GET", patternOrPatterns, ...middleware);
77
- }
78
- post(patternOrPatterns, ...middleware) {
79
- return this.on("POST", patternOrPatterns, ...middleware);
80
- }
81
46
  /**
82
47
  * Request a resource from the application.
83
48
  *
@@ -88,32 +53,15 @@ export class App {
88
53
  * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
89
54
  */
90
55
  fetch = async (resource, options) => {
91
- const c = new Context(new Request(resource, options));
92
- // see if the method is already built
93
- let trie = this.#trieMap.get(c.req.method);
94
- if (!trie) {
95
- // check if there are any routes with the method
96
- const routes = this.#routesMap.get(c.req.method);
97
- if (routes) {
98
- // build trie
99
- trie = new Trie();
100
- for (const route of routes)
101
- trie.add(route);
102
- this.#trieMap.set(c.req.method, trie);
103
- }
104
- }
105
- if (trie) {
106
- const match = trie.find(c.url.pathname);
107
- if (match) {
108
- Object.assign(c, match);
109
- return c.build(this.#use.concat(match.route.store));
110
- }
111
- }
112
- // no match, just run global middleware
113
- return c.build(this.#use);
56
+ const c = new Context(new Request(resource, options), this.#options);
57
+ return Context.compose(Object.assign(c,
58
+ // match
59
+ this.#trie.find(
60
+ // 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 ?? []));
114
62
  };
115
- /** Basic CSRF middleware. */
116
- static async #csrf(c, next) {
63
+ /** Basic CSRF middleware */
64
+ static #csrf(c, next) {
117
65
  if (c.req.method === "GET" ||
118
66
  c.req.method === "HEAD" ||
119
67
  c.req.headers.get("sec-fetch-site") === "same-origin" ||
@@ -123,10 +71,10 @@ export class App {
123
71
  c.text("Forbidden", 403);
124
72
  }
125
73
  /** @returns Trailing slash middleware */
126
- static #trailingSlash(mode) {
74
+ static #createTrailingSlash(mode) {
127
75
  return async (c, next) => {
128
76
  await next();
129
- if (c.status && c.status !== 404)
77
+ if (c.res.status && c.res.status !== 404)
130
78
  return;
131
79
  const last = c.url.pathname.at(-1);
132
80
  if (mode === "always" && last !== "/") {
@@ -0,0 +1,138 @@
1
+ import type { App } from "../app/index.js";
2
+ import { Cookie } from "../cookie/index.js";
3
+ import { type Middleware } from "../middleware/index.js";
4
+ import { Multipart } from "../multipart/index.js";
5
+ import { Route } from "../route/index.js";
6
+ import { type Trie } from "../trie/index.js";
7
+ /** Properties to build the final `Response` with once middleware has run. */
8
+ declare class PreparedResponse {
9
+ /**
10
+ * `body` used to create the `Response`.
11
+ *
12
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body)
13
+ */
14
+ body?: BodyInit | null;
15
+ /**
16
+ * `status` used to create the `Response`.
17
+ *
18
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
19
+ */
20
+ status?: number;
21
+ /**
22
+ * `Headers` used to create the `Response`.
23
+ *
24
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
25
+ */
26
+ headers: Headers;
27
+ }
28
+ export declare namespace Context {
29
+ type Cookie = InstanceType<typeof Cookie>;
30
+ }
31
+ /**
32
+ * Request context.
33
+ *
34
+ * @template Params Parameters created from a route match
35
+ */
36
+ export declare class Context<Params extends Trie.Params = Trie.Params> {
37
+ #private;
38
+ /**
39
+ * Incoming `Request` to the server.
40
+ *
41
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
42
+ */
43
+ readonly req: Request;
44
+ /**
45
+ * Parsed `URL` created from `req.url`.
46
+ *
47
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
48
+ */
49
+ readonly url: URL;
50
+ /** Route pattern parameters */
51
+ readonly params: Params;
52
+ /** Matched `Route` instance */
53
+ readonly route?: Route;
54
+ /** Contains the arguments to used create the final `Response` */
55
+ readonly res: PreparedResponse;
56
+ /** Get, set, and delete cookies. */
57
+ readonly cookie: Cookie;
58
+ /**
59
+ * Creates a new `Context` with the current `Request`.
60
+ *
61
+ * @param req Request
62
+ */
63
+ constructor(req: Request, options?: App.Options);
64
+ /**
65
+ * Creates an HTML response.
66
+ *
67
+ * @param body HTML body
68
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
69
+ */
70
+ html(body: BodyInit | null, status?: number): void;
71
+ /**
72
+ * Creates a JSON response.
73
+ *
74
+ * @param data passed into JSON.stringify to create the body
75
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
76
+ */
77
+ json(data: unknown, status?: number): void;
78
+ /**
79
+ * Creates a plain text response.
80
+ *
81
+ * @param body response body
82
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
83
+ */
84
+ text(body: BodyInit, status?: number): void;
85
+ /**
86
+ * Creates a redirect response.
87
+ *
88
+ * @param location redirect `Location` header
89
+ * @param status HTTP status code
90
+ *
91
+ * - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
92
+ * - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
93
+ * - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
94
+ * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
95
+ * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
96
+ */
97
+ redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
98
+ /**
99
+ * Generates an etag from a hash of the string provided.
100
+ * If the etag matches, sets the response to not modified.
101
+ *
102
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
103
+ *
104
+ * @param string string to hash
105
+ * @returns `true` if the etag matches, `false` otherwise
106
+ */
107
+ etag(string: string): boolean;
108
+ /**
109
+ * Parse multipart requests.
110
+ *
111
+ * @yields Multipart request `Part`(s)
112
+ *
113
+ * @example
114
+ *
115
+ * ```ts
116
+ * import { Route } from "ovr";
117
+ *
118
+ * const post = Route.post(async (c) => {
119
+ * for await (const part of c.form()) {
120
+ * if (part.name === "email") {
121
+ * // ...
122
+ * }
123
+ * }
124
+ * })
125
+ * ```
126
+ */
127
+ form(options?: Multipart.Options): Multipart;
128
+ /**
129
+ * Composes a stack of `middleware` into a `Response`.
130
+ *
131
+ * @param c Context
132
+ * @param middleware stack to compose
133
+ * @returns constructed `Response`
134
+ */
135
+ static compose(c: Context, middleware: Middleware[]): Promise<Response>;
136
+ }
137
+ export {};
138
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;AAC7E,cAAM,gBAAgB;IACrB;;;;OAIG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;IAEvB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;IACH,OAAO,U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