ovr 4.6.0 → 5.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.
- package/dist/app/context.d.ts +58 -56
- package/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +97 -134
- package/dist/app/helper/index.d.ts +2 -6
- package/dist/app/helper/index.d.ts.map +1 -1
- package/dist/app/helper/index.js +5 -19
- package/dist/app/index.d.ts +31 -45
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +68 -94
- package/dist/app/memo/index.d.ts +1 -1
- package/dist/app/memo/index.d.ts.map +1 -1
- package/dist/app/memo/index.js +2 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +18 -15
- package/dist/trie/index.d.ts +5 -0
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +8 -2
- package/package.json +2 -2
- package/src/app/context.ts +101 -163
- package/src/app/helper/index.ts +6 -24
- package/src/app/index.ts +111 -123
- package/src/app/memo/index.ts +2 -2
- package/src/index.ts +1 -2
- package/src/jsx/index.ts +37 -36
- package/src/trie/index.ts +9 -2
- package/src/util/hash.ts +1 -1
- package/dist/app/mw/csrf.d.ts +0 -23
- package/dist/app/mw/csrf.d.ts.map +0 -1
- package/dist/app/mw/csrf.js +0 -30
- package/src/app/mw/csrf.ts +0 -32
package/dist/app/context.d.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type
|
|
3
|
-
import { type
|
|
2
|
+
import { type Params, Route } from "../trie/index.js";
|
|
3
|
+
import { type Middleware } from "./index.js";
|
|
4
|
+
import { Memo } from "./memo/index.js";
|
|
4
5
|
type Layout = (props: {
|
|
5
6
|
children: JSX.Element;
|
|
6
7
|
}) => JSX.Element;
|
|
7
8
|
export declare class Context<P extends Params = Params> {
|
|
8
9
|
#private;
|
|
9
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Incoming `Request` to the server.
|
|
12
|
+
*
|
|
13
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
14
|
+
*/
|
|
10
15
|
req: Request;
|
|
11
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Parsed `URL` created from `req.url`.
|
|
18
|
+
*
|
|
19
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
20
|
+
*/
|
|
12
21
|
url: URL;
|
|
13
22
|
/**
|
|
14
23
|
* Route pattern parameters
|
|
@@ -19,30 +28,53 @@ export declare class Context<P extends Params = Params> {
|
|
|
19
28
|
* @example { slug: "my-post" }
|
|
20
29
|
*/
|
|
21
30
|
params: P;
|
|
22
|
-
/**
|
|
23
|
-
route: Route<Middleware<P>[]
|
|
31
|
+
/** Matched `Route` instance. */
|
|
32
|
+
route: Route<Middleware<P>[]> | null;
|
|
33
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
34
|
+
body: BodyInit | null;
|
|
35
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
36
|
+
status?: number;
|
|
24
37
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
25
38
|
headers: Headers;
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
/** `layouts` for the `Page` to be passed into before being injected into the `<body>` */
|
|
40
|
+
layouts: Layout[];
|
|
41
|
+
/** `JSX.Element`(s) to add to the head */
|
|
42
|
+
head: JSX.Element[];
|
|
43
|
+
/**
|
|
44
|
+
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
45
|
+
* Set to a new function to override the default.
|
|
46
|
+
*
|
|
47
|
+
* @default
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* (c) => {
|
|
51
|
+
* c.text("Not found", 404);
|
|
52
|
+
* c.headers.set("cache-control", "no-cache");
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
notFound: Middleware<P>;
|
|
57
|
+
/**
|
|
58
|
+
* Base HTML to inject the `head` and `page` elements into.
|
|
59
|
+
*
|
|
60
|
+
* If left empty, components will be returned as partials.
|
|
61
|
+
*
|
|
62
|
+
* @default ""
|
|
63
|
+
*/
|
|
64
|
+
base: string;
|
|
65
|
+
/** `Memo` unique to the current `Request` context */
|
|
66
|
+
memo: Memo;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new `Context` with the current `Request`.
|
|
69
|
+
*
|
|
70
|
+
* @param req Request
|
|
71
|
+
*/
|
|
72
|
+
constructor(req: Request);
|
|
41
73
|
/**
|
|
42
74
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
43
75
|
*
|
|
44
76
|
* @param body Response BodyInit
|
|
45
|
-
* @param init
|
|
77
|
+
* @param init ResponseInit
|
|
46
78
|
*/
|
|
47
79
|
res(body: BodyInit | null, init?: {
|
|
48
80
|
/**
|
|
@@ -88,14 +120,6 @@ export declare class Context<P extends Params = Params> {
|
|
|
88
120
|
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
89
121
|
*/
|
|
90
122
|
redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
|
|
91
|
-
/**
|
|
92
|
-
* @param Element `JSX.Element` to add to the head
|
|
93
|
-
*/
|
|
94
|
-
head(...Element: JSX.Element[]): void;
|
|
95
|
-
/**
|
|
96
|
-
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
97
|
-
*/
|
|
98
|
-
layout(...Layout: Layout[]): void;
|
|
99
123
|
/**
|
|
100
124
|
* Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
|
|
101
125
|
*
|
|
@@ -114,34 +138,12 @@ export declare class Context<P extends Params = Params> {
|
|
|
114
138
|
*/
|
|
115
139
|
etag(s: string): boolean;
|
|
116
140
|
/**
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
build(): Response;
|
|
120
|
-
/**
|
|
121
|
-
* Call within the scope of a handler to get the current context.
|
|
141
|
+
* Composes a stack of `middleware` into a `Response`.
|
|
122
142
|
*
|
|
123
|
-
* @
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
*
|
|
127
|
-
* ```ts
|
|
128
|
-
* import { Context } from "ovr";
|
|
129
|
-
*
|
|
130
|
-
* const app = new Router();
|
|
131
|
-
*
|
|
132
|
-
* const fn = () => {
|
|
133
|
-
* const c = Context.get();
|
|
134
|
-
* // ...
|
|
135
|
-
* }
|
|
136
|
-
*
|
|
137
|
-
* app.get("/", () => {
|
|
138
|
-
* fn(); // OK
|
|
139
|
-
* });
|
|
140
|
-
*
|
|
141
|
-
* fn() // ReferenceError - outside AsyncLocalStorage scope
|
|
142
|
-
* ```
|
|
143
|
+
* @param middleware stack to compose
|
|
144
|
+
* @returns constructed `Response`
|
|
143
145
|
*/
|
|
144
|
-
|
|
146
|
+
build(middleware: Middleware<P>[]): Promise<Response>;
|
|
145
147
|
}
|
|
146
148
|
export {};
|
|
147
149
|
//# sourceMappingURL=context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAY,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAY,MAAM,iBAAiB,CAAC;AACrD,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IAYF;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IAO3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM;IAO/B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAOpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAI1E;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAwCvC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IA6Cd;;;;;OAKG;IACG,KAAK,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE;CAOvC"}
|
package/dist/app/context.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Chunk } from "../jsx/chunk/index.js";
|
|
2
2
|
import { toStream } from "../jsx/index.js";
|
|
3
|
+
import { Route } from "../trie/index.js";
|
|
3
4
|
import { hash } from "../util/hash.js";
|
|
4
|
-
import {
|
|
5
|
+
import {} from "./index.js";
|
|
5
6
|
import { Memo } from "./memo/index.js";
|
|
6
7
|
class TagNotFound extends Error {
|
|
7
8
|
name = "TagNotFound";
|
|
@@ -9,18 +10,18 @@ class TagNotFound extends Error {
|
|
|
9
10
|
super(`No closing ${tag} tag found`);
|
|
10
11
|
}
|
|
11
12
|
}
|
|
12
|
-
class ResponseFinalized extends Error {
|
|
13
|
-
name = "ResponseFinalized";
|
|
14
|
-
constructor(property) {
|
|
15
|
-
super(`Cannot set \`${property}\` after the \`Response\` has been finalized. ` +
|
|
16
|
-
`This can occur if \`${property}\` is set within a component since ` +
|
|
17
|
-
"components are evaluated during streaming.");
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
13
|
export class Context {
|
|
21
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Incoming `Request` to the server.
|
|
16
|
+
*
|
|
17
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
18
|
+
*/
|
|
22
19
|
req;
|
|
23
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Parsed `URL` created from `req.url`.
|
|
22
|
+
*
|
|
23
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
24
|
+
*/
|
|
24
25
|
url;
|
|
25
26
|
/**
|
|
26
27
|
* Route pattern parameters
|
|
@@ -30,69 +31,63 @@ export class Context {
|
|
|
30
31
|
*
|
|
31
32
|
* @example { slug: "my-post" }
|
|
32
33
|
*/
|
|
33
|
-
params; // set after match
|
|
34
|
-
/**
|
|
35
|
-
route
|
|
36
|
-
#body
|
|
37
|
-
|
|
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;
|
|
38
41
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
39
42
|
headers = new Headers();
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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 = "";
|
|
48
72
|
/** `Memo` unique to the current `Request` context */
|
|
49
|
-
|
|
50
|
-
memo = this.#memo.use;
|
|
51
|
-
/** Check if the `Response` has been built already */
|
|
52
|
-
#finalized = false;
|
|
73
|
+
memo = new Memo();
|
|
53
74
|
static #contentType = "content-type";
|
|
54
75
|
static #headClose = "</head>";
|
|
55
76
|
static #bodyClose = "</body>";
|
|
56
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new `Context` with the current `Request`.
|
|
79
|
+
*
|
|
80
|
+
* @param req Request
|
|
81
|
+
*/
|
|
82
|
+
constructor(req) {
|
|
57
83
|
this.req = req;
|
|
58
|
-
this.url = url;
|
|
59
|
-
this.#trailingSlash = trailingSlash;
|
|
60
|
-
this.#base = base;
|
|
61
|
-
this.error = error;
|
|
62
|
-
this.notFound = notFound;
|
|
63
|
-
}
|
|
64
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
65
|
-
get status() {
|
|
66
|
-
return this.#status;
|
|
67
|
-
}
|
|
68
|
-
set status(status) {
|
|
69
|
-
if (this.#finalized)
|
|
70
|
-
throw new ResponseFinalized("status");
|
|
71
|
-
this.#status = status;
|
|
72
|
-
}
|
|
73
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
74
|
-
get body() {
|
|
75
|
-
return this.#body;
|
|
76
|
-
}
|
|
77
|
-
set body(body) {
|
|
78
|
-
if (this.#finalized)
|
|
79
|
-
throw new ResponseFinalized("body");
|
|
80
|
-
this.#body = body;
|
|
81
|
-
}
|
|
82
|
-
/** Passed from `app.base` */
|
|
83
|
-
get base() {
|
|
84
|
-
return this.#base;
|
|
85
|
-
}
|
|
86
|
-
set base(base) {
|
|
87
|
-
if (this.#finalized)
|
|
88
|
-
throw new ResponseFinalized("base");
|
|
89
|
-
this.#base = base;
|
|
84
|
+
this.url = new URL(req.url);
|
|
90
85
|
}
|
|
91
86
|
/**
|
|
92
87
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
93
88
|
*
|
|
94
89
|
* @param body Response BodyInit
|
|
95
|
-
* @param init
|
|
90
|
+
* @param init ResponseInit
|
|
96
91
|
*/
|
|
97
92
|
res(body, init) {
|
|
98
93
|
this.body = body;
|
|
@@ -152,24 +147,7 @@ export class Context {
|
|
|
152
147
|
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
153
148
|
*/
|
|
154
149
|
redirect(location, status = 302) {
|
|
155
|
-
this.
|
|
156
|
-
this.status = status;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* @param Element `JSX.Element` to add to the head
|
|
160
|
-
*/
|
|
161
|
-
head(...Element) {
|
|
162
|
-
if (this.#finalized)
|
|
163
|
-
throw new ResponseFinalized("head");
|
|
164
|
-
this.#headElements.push(...Element);
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
168
|
-
*/
|
|
169
|
-
layout(...Layout) {
|
|
170
|
-
if (this.#finalized)
|
|
171
|
-
throw new ResponseFinalized("layout");
|
|
172
|
-
this.#layouts.push(...Layout);
|
|
150
|
+
this.res(null, { status, headers: { location: location.toString() } });
|
|
173
151
|
}
|
|
174
152
|
/**
|
|
175
153
|
* Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
|
|
@@ -178,16 +156,16 @@ export class Context {
|
|
|
178
156
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
179
157
|
*/
|
|
180
158
|
page(Page, status) {
|
|
181
|
-
for (let i = this
|
|
182
|
-
|
|
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 });
|
|
183
162
|
}
|
|
184
|
-
let stream;
|
|
185
163
|
if (this.base) {
|
|
186
164
|
// inject into base
|
|
187
165
|
const elements = this.base.split(Context.#headClose);
|
|
188
166
|
if (!elements[1])
|
|
189
167
|
throw new TagNotFound(Context.#headClose);
|
|
190
|
-
elements.splice(1, 0, this
|
|
168
|
+
elements.splice(1, 0, this.head, Context.#headClose);
|
|
191
169
|
const bodyParts = elements[3].split(Context.#bodyClose);
|
|
192
170
|
if (!bodyParts[1])
|
|
193
171
|
throw new TagNotFound(Context.#bodyClose);
|
|
@@ -200,13 +178,11 @@ export class Context {
|
|
|
200
178
|
}
|
|
201
179
|
elements[3] = bodyParts[0];
|
|
202
180
|
elements.push(Page, Context.#bodyClose + bodyParts[1]);
|
|
203
|
-
|
|
181
|
+
return this.html(toStream(elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el))), status);
|
|
204
182
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
this.html(stream, status);
|
|
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);
|
|
210
186
|
}
|
|
211
187
|
/**
|
|
212
188
|
* Generates an etag from a hash of the string provided.
|
|
@@ -228,56 +204,43 @@ export class Context {
|
|
|
228
204
|
return false;
|
|
229
205
|
}
|
|
230
206
|
/**
|
|
231
|
-
*
|
|
207
|
+
* Modifies the context based on the the return value of middleware.
|
|
208
|
+
*
|
|
209
|
+
* @param value value returned from middleware
|
|
232
210
|
*/
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
this
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
last === "/") {
|
|
244
|
-
this.url.pathname = this.url.pathname.slice(0, -1);
|
|
245
|
-
this.redirect(this.url, 308);
|
|
246
|
-
}
|
|
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);
|
|
247
221
|
}
|
|
248
|
-
if (!this.body && !this.status)
|
|
249
|
-
this.notFound(this);
|
|
250
|
-
this.#finalized = true;
|
|
251
|
-
return new Response(this.body, this);
|
|
252
222
|
}
|
|
253
223
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* @returns Request context
|
|
257
|
-
*
|
|
258
|
-
* @example
|
|
259
|
-
*
|
|
260
|
-
* ```ts
|
|
261
|
-
* import { Context } from "ovr";
|
|
262
|
-
*
|
|
263
|
-
* const app = new Router();
|
|
264
|
-
*
|
|
265
|
-
* const fn = () => {
|
|
266
|
-
* const c = Context.get();
|
|
267
|
-
* // ...
|
|
268
|
-
* }
|
|
224
|
+
* Dispatches the stack of `middleware` provided.
|
|
269
225
|
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
|
|
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`.
|
|
273
236
|
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
237
|
+
* @param middleware stack to compose
|
|
238
|
+
* @returns constructed `Response`
|
|
276
239
|
*/
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (!
|
|
280
|
-
|
|
281
|
-
return
|
|
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);
|
|
282
245
|
}
|
|
283
246
|
}
|
|
@@ -4,15 +4,11 @@ import type { ExtractParams, InsertParams } from "../../types/index.js";
|
|
|
4
4
|
import type { Middleware } from "../index.js";
|
|
5
5
|
export type UrlOptions<P extends Params> = {
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* can only be used in the context of a request.
|
|
9
|
-
*
|
|
10
|
-
* Otherwise the value is passed into `URLSearchParams` constructor to
|
|
11
|
-
* create new params.
|
|
7
|
+
* Passed into `URLSearchParams` constructor to create new params.
|
|
12
8
|
*
|
|
13
9
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
14
10
|
*/
|
|
15
|
-
search?:
|
|
11
|
+
search?: string | Iterable<[string, string]> | Record<string, string>;
|
|
16
12
|
/**
|
|
17
13
|
* Hash (fragment) of the URL. `"#"` prefix is added if not present.
|
|
18
14
|
*
|
|
@@ -1 +1 @@
|
|
|
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,qBA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uE;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"}
|
package/dist/app/helper/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { jsx } from "../../jsx/index.js";
|
|
2
|
-
import { Context } from "../context.js";
|
|
3
2
|
export class Helper {
|
|
4
3
|
/** Route pattern */
|
|
5
4
|
pattern;
|
|
@@ -36,23 +35,13 @@ export class Helper {
|
|
|
36
35
|
const pathname = this.pathname(
|
|
37
36
|
// @ts-expect-error - do not have to pass in {} if no params
|
|
38
37
|
options?.params);
|
|
39
|
-
let search;
|
|
38
|
+
let search = "";
|
|
39
|
+
let hash = "";
|
|
40
40
|
if (options?.search) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
// use the value as the init
|
|
47
|
-
search =
|
|
48
|
-
"?" +
|
|
49
|
-
new URLSearchParams(options.search);
|
|
50
|
-
}
|
|
41
|
+
// use the value as the init
|
|
42
|
+
// @ts-expect-error - see above
|
|
43
|
+
search = "?" + new URLSearchParams(options.search);
|
|
51
44
|
}
|
|
52
|
-
else {
|
|
53
|
-
search = "";
|
|
54
|
-
}
|
|
55
|
-
let hash;
|
|
56
45
|
if (options?.hash) {
|
|
57
46
|
// adding # prefix if not present matches the URL setter:
|
|
58
47
|
// https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
|
|
@@ -63,9 +52,6 @@ export class Helper {
|
|
|
63
52
|
hash = "#" + options.hash;
|
|
64
53
|
}
|
|
65
54
|
}
|
|
66
|
-
else {
|
|
67
|
-
hash = "";
|
|
68
|
-
}
|
|
69
55
|
return pathname + search + hash;
|
|
70
56
|
}
|
|
71
57
|
/**
|
package/dist/app/index.d.ts
CHANGED
|
@@ -3,16 +3,26 @@ import type { DeepArray, ExtractMultiParams, ExtractParams } from "../types/inde
|
|
|
3
3
|
import { Context } from "./context.js";
|
|
4
4
|
import { Get } from "./helper/get.js";
|
|
5
5
|
import { Post } from "./helper/post.js";
|
|
6
|
-
|
|
7
|
-
export type Middleware<P extends Params = Params> = (context: Context<P>, next:
|
|
6
|
+
export type Next = () => Promise<void>;
|
|
7
|
+
export type Middleware<P extends Params = Params> = (context: Context<P>, next: Next) => any;
|
|
8
8
|
export type TrailingSlash = "always" | "never" | "ignore";
|
|
9
9
|
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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;
|
|
16
26
|
/**
|
|
17
27
|
* - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
|
|
18
28
|
* - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
|
|
@@ -22,40 +32,11 @@ export declare class App {
|
|
|
22
32
|
*
|
|
23
33
|
* @default "never"
|
|
24
34
|
*/
|
|
25
|
-
trailingSlash
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
* ```html
|
|
32
|
-
* <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
base: string;
|
|
36
|
-
/**
|
|
37
|
-
* Assign a handler to run when an `Error` is thrown.
|
|
38
|
-
*
|
|
39
|
-
* If not set, `Error` will be thrown. This might be desired
|
|
40
|
-
* if your server already includes error handling. Set in `config.start`
|
|
41
|
-
* to handle errors globally.
|
|
42
|
-
*
|
|
43
|
-
* @default null
|
|
44
|
-
*/
|
|
45
|
-
error: ErrorHandler;
|
|
46
|
-
/**
|
|
47
|
-
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
48
|
-
* Set to a new function to override the default.
|
|
49
|
-
*
|
|
50
|
-
* @default
|
|
51
|
-
*
|
|
52
|
-
* ```ts
|
|
53
|
-
* (c) => c.html("Not found", 404)
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
notFound: NotFoundHandler;
|
|
57
|
-
/** Stores context per request. */
|
|
58
|
-
static storage: AsyncLocalStorage<Context<Params>>;
|
|
35
|
+
trailingSlash?: TrailingSlash;
|
|
36
|
+
};
|
|
37
|
+
export declare class App {
|
|
38
|
+
#private;
|
|
39
|
+
constructor(options?: AppOptions);
|
|
59
40
|
/**
|
|
60
41
|
* @param helpers `Helper` to add to `App`
|
|
61
42
|
* @returns `App` instance
|
|
@@ -107,10 +88,15 @@ export declare class App {
|
|
|
107
88
|
*/
|
|
108
89
|
post<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
|
|
109
90
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
91
|
+
* Request a resource from the application.
|
|
92
|
+
*
|
|
93
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch)
|
|
94
|
+
*
|
|
95
|
+
* @param resource [Resource](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#resource) to fetch
|
|
96
|
+
* @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
|
|
97
|
+
* @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
|
|
112
98
|
*/
|
|
113
|
-
fetch: (
|
|
99
|
+
fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
|
|
114
100
|
}
|
|
115
101
|
export {};
|
|
116
102
|
//# sourceMappingURL=index.d.ts.map
|