@asterflow/router 1.0.3 → 1.0.5
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/README.md +12 -99
- package/dist/cjs/index.cjs +26 -118
- package/dist/mjs/index.js +18 -108
- package/dist/types/controllers/Method.d.ts +1 -1
- package/dist/types/controllers/Middleware.d.ts +2 -2
- package/dist/types/controllers/Router.d.ts +14 -12
- package/dist/types/controllers/createRouter.d.ts +12 -0
- package/dist/types/index.d.ts +1 -2
- package/dist/types/types/method.d.ts +9 -8
- package/dist/types/types/mindleware.d.ts +1 -2
- package/dist/types/types/router.d.ts +4 -5
- package/package.json +5 -5
- package/dist/types/controllers/Response.d.ts +0 -37
- package/dist/types/types/response.d.ts +0 -12
package/README.md
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
# @asterflow/router
|
|
4
4
|
|
|
5
|
-

|
|
6
|
+

|
|
7
7
|
|
|
8
|
-

|
|
9
|
+

|
|
10
|
+

|
|
11
11
|
|
|
12
|
-

|
|
13
13
|

|
|
14
14
|
|
|
15
15
|
</div>
|
|
@@ -156,101 +156,14 @@ app.listen(server, { port: 3333 }, (err) => {
|
|
|
156
156
|
})
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
## 📚 API Reference
|
|
160
|
-
|
|
161
|
-
### Router Class
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
class Router<
|
|
165
|
-
Path extends string,
|
|
166
|
-
Method extends MethodKeys,
|
|
167
|
-
Schema extends SchemaDynamic<Method>,
|
|
168
|
-
Responder extends Responders,
|
|
169
|
-
Middlewares extends readonly Middleware[]
|
|
170
|
-
> {
|
|
171
|
-
constructor(options: {
|
|
172
|
-
path: Path
|
|
173
|
-
name?: string
|
|
174
|
-
description?: string
|
|
175
|
-
use?: Middlewares
|
|
176
|
-
schema?: Schema
|
|
177
|
-
methods: {
|
|
178
|
-
[M in MethodKeys]?: RouteHandler
|
|
179
|
-
}
|
|
180
|
-
})
|
|
181
|
-
}
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### Method Class
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
class Method<
|
|
188
|
-
Responder extends Responders,
|
|
189
|
-
Path extends string,
|
|
190
|
-
Method extends MethodKeys,
|
|
191
|
-
Schema extends AnySchema,
|
|
192
|
-
Middlewares extends readonly Middleware[]
|
|
193
|
-
> {
|
|
194
|
-
constructor(options: {
|
|
195
|
-
path: Path
|
|
196
|
-
method: Method
|
|
197
|
-
name?: string
|
|
198
|
-
schema?: Schema
|
|
199
|
-
use?: Middlewares
|
|
200
|
-
handler: MethodHandler
|
|
201
|
-
})
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
### Response Class
|
|
206
|
-
|
|
207
|
-
```typescript
|
|
208
|
-
class Response<Responder extends Responders> {
|
|
209
|
-
// Status Methods
|
|
210
|
-
success(data: Responder[200]): Response
|
|
211
|
-
created(data: Responder[201]): Response
|
|
212
|
-
noContent(data: Responder[204]): Response
|
|
213
|
-
badRequest(data: Responder[400]): Response
|
|
214
|
-
unauthorized(data: Responder[401]): Response
|
|
215
|
-
forbidden(data: Responder[403]): Response
|
|
216
|
-
notFound(data: Responder[404]): Response
|
|
217
|
-
|
|
218
|
-
// Headers and Cookies
|
|
219
|
-
setHeader(name: string, value: string): Response
|
|
220
|
-
setCookie(name: string, value: string): Response
|
|
221
|
-
|
|
222
|
-
// Conversion
|
|
223
|
-
toResponse(): globalThis.Response
|
|
224
|
-
toServerResponse(output: ServerResponse): void
|
|
225
|
-
}
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
### Middleware Class
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
class Middleware<
|
|
232
|
-
Responder extends Responders,
|
|
233
|
-
Schema extends AnySchema,
|
|
234
|
-
Name extends string,
|
|
235
|
-
Parameters extends Record<string, unknown>
|
|
236
|
-
> {
|
|
237
|
-
constructor(options: {
|
|
238
|
-
name: Name
|
|
239
|
-
onRun: (args: {
|
|
240
|
-
request: Request
|
|
241
|
-
response: Response<Responder>
|
|
242
|
-
schema: InferSchema<Schema>
|
|
243
|
-
next: (params: Parameters) => MiddlewareOptions
|
|
244
|
-
}) => MiddlewareOptions
|
|
245
|
-
})
|
|
246
|
-
}
|
|
247
|
-
```
|
|
248
|
-
|
|
249
159
|
## 🔗 Related Packages
|
|
250
160
|
|
|
251
|
-
- [@asterflow/core](https://
|
|
252
|
-
- [@asterflow/adapter](https://
|
|
161
|
+
- [@asterflow/core](https://www.npmjs.com/package/@asterflow/core) - Core framework
|
|
162
|
+
- [@asterflow/adapter](https://www.npmjs.com/package/@asterflow/adapter) - Adapters for different HTTP servers
|
|
163
|
+
- [@asterflow/response](https://www.npmjs.com/package/@asterflow/response) - Type-safe HTTP response system
|
|
164
|
+
- [@asterflow/request](https://www.npmjs.com/package/@asterflow/request) - Unified HTTP request system
|
|
165
|
+
- [@asterflow/plugin](https://www.npmjs.com/package/@asterflow/plugin) - A modular and typed plugin system
|
|
253
166
|
|
|
254
167
|
## 📄 License
|
|
255
168
|
|
|
256
|
-
MIT - See [LICENSE](https://github.com/
|
|
169
|
+
MIT - See [LICENSE](https://github.com/AsterFlow/AsterFlow/blob/main/LICENSE) for more details.
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
2
|
+
var a = Object.defineProperty;
|
|
3
|
+
var u = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var M = Object.getOwnPropertyNames;
|
|
5
5
|
var w = Object.prototype.hasOwnProperty;
|
|
6
|
-
var R = (
|
|
7
|
-
for (var
|
|
8
|
-
|
|
9
|
-
}, x = (
|
|
6
|
+
var R = (t, e) => {
|
|
7
|
+
for (var o in e)
|
|
8
|
+
a(t, o, { get: e[o], enumerable: !0 });
|
|
9
|
+
}, x = (t, e, o, d) => {
|
|
10
10
|
if (e && typeof e == "object" || typeof e == "function")
|
|
11
|
-
for (let s of
|
|
12
|
-
!w.call(
|
|
13
|
-
return
|
|
11
|
+
for (let s of M(e))
|
|
12
|
+
!w.call(t, s) && s !== o && a(t, s, { get: () => e[s], enumerable: !(d = u(e, s)) || d.enumerable });
|
|
13
|
+
return t;
|
|
14
14
|
};
|
|
15
|
-
var f = (
|
|
16
|
-
|
|
15
|
+
var f = (t) => x(a({}, "__esModule", { value: !0 }), t);
|
|
17
16
|
// packages/router/src/index.ts
|
|
18
17
|
var g = {};
|
|
19
18
|
R(g, {
|
|
20
|
-
Method: () =>
|
|
19
|
+
Method: () => m,
|
|
21
20
|
MethodType: () => l,
|
|
22
21
|
Middleware: () => p,
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
Router: () => n,
|
|
23
|
+
createRouter: () => S
|
|
25
24
|
});
|
|
26
25
|
module.exports = f(g);
|
|
27
|
-
|
|
28
26
|
// packages/router/src/controllers/Router.ts
|
|
29
|
-
var
|
|
27
|
+
var i = require("url-ast"), n = class {
|
|
30
28
|
name;
|
|
31
29
|
path;
|
|
32
30
|
schema;
|
|
@@ -35,101 +33,10 @@ var c = require("url-ast"), a = class {
|
|
|
35
33
|
use;
|
|
36
34
|
url;
|
|
37
35
|
constructor(e) {
|
|
38
|
-
let { name:
|
|
39
|
-
this.name =
|
|
36
|
+
let { name: o, path: d, schema: s, description: c, methods: y } = e;
|
|
37
|
+
this.name = o, this.path = d, this.schema = s, this.description = c, this.methods = y, this.use = e.use, this.url = new i.Analyze(this.path);
|
|
40
38
|
}
|
|
41
39
|
};
|
|
42
|
-
|
|
43
|
-
// packages/router/src/controllers/Response.ts
|
|
44
|
-
var i = class n {
|
|
45
|
-
_status;
|
|
46
|
-
body;
|
|
47
|
-
header;
|
|
48
|
-
cookies;
|
|
49
|
-
constructor(e) {
|
|
50
|
-
this._status = e?.code ?? 200, this.body = e?.data, this.header = e?.header ?? /* @__PURE__ */ new Map(), this.cookies = e?.cookies ?? /* @__PURE__ */ new Map();
|
|
51
|
-
}
|
|
52
|
-
clone(e) {
|
|
53
|
-
return new n({
|
|
54
|
-
code: e.code !== void 0 ? e.code : this._status,
|
|
55
|
-
data: e.data !== void 0 ? e.data : this.body,
|
|
56
|
-
header: e.header !== void 0 ? e.header : this.header,
|
|
57
|
-
cookies: e.cookies !== void 0 ? e.cookies : this.cookies
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
// --- Core Methods ---
|
|
61
|
-
status(e) {
|
|
62
|
-
return this.clone({ code: e });
|
|
63
|
-
}
|
|
64
|
-
getStatus() {
|
|
65
|
-
return this._status;
|
|
66
|
-
}
|
|
67
|
-
code(e) {
|
|
68
|
-
return this.status(e);
|
|
69
|
-
}
|
|
70
|
-
send(e) {
|
|
71
|
-
return this.clone({ data: e });
|
|
72
|
-
}
|
|
73
|
-
json(e) {
|
|
74
|
-
let t = new Map(this.header);
|
|
75
|
-
return t.set("Content-Type", "application/json"), this.clone({ data: e, header: t });
|
|
76
|
-
}
|
|
77
|
-
// --- Response Helpers ---
|
|
78
|
-
success(e) {
|
|
79
|
-
return this.clone({ code: 200, data: e });
|
|
80
|
-
}
|
|
81
|
-
created(e) {
|
|
82
|
-
return this.clone({ code: 201, data: e });
|
|
83
|
-
}
|
|
84
|
-
noContent(e) {
|
|
85
|
-
return this.clone({ code: 204, data: e });
|
|
86
|
-
}
|
|
87
|
-
badRequest(e) {
|
|
88
|
-
return this.clone({ code: 400, data: e });
|
|
89
|
-
}
|
|
90
|
-
zodError(e) {
|
|
91
|
-
return this.clone({ code: 422, data: e });
|
|
92
|
-
}
|
|
93
|
-
unauthorized(e) {
|
|
94
|
-
return this.clone({ code: 401, data: e });
|
|
95
|
-
}
|
|
96
|
-
forbidden(e) {
|
|
97
|
-
return this.clone({ code: 403, data: e });
|
|
98
|
-
}
|
|
99
|
-
notFound(e) {
|
|
100
|
-
return this.clone({ code: 404, data: e });
|
|
101
|
-
}
|
|
102
|
-
setHeader(e, t) {
|
|
103
|
-
let o = new Map(this.header);
|
|
104
|
-
return o.set(e, t), this.clone({ header: o });
|
|
105
|
-
}
|
|
106
|
-
setCookie(e, t) {
|
|
107
|
-
let o = new Map(this.cookies);
|
|
108
|
-
return o.set(e, t), this.clone({ cookies: o });
|
|
109
|
-
}
|
|
110
|
-
toResponse() {
|
|
111
|
-
let e = new Headers();
|
|
112
|
-
e.set("Content-Type", "text/plain");
|
|
113
|
-
for (let [o, s] of this.header) e.set(o, s);
|
|
114
|
-
(typeof this.body == "object" || Array.isArray(this.body)) && e.set("Content-Type", "application/json");
|
|
115
|
-
for (let [o, s] of this.cookies) e.append("Set-Cookie", `${o}=${s}`);
|
|
116
|
-
let t = e.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
117
|
-
return new globalThis.Response(t, {
|
|
118
|
-
status: this._status,
|
|
119
|
-
headers: e
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
toServerResponse(e) {
|
|
123
|
-
let t = new Headers();
|
|
124
|
-
t.set("Content-Type", "text/plain");
|
|
125
|
-
for (let [s, r] of this.header) t.set(s, r);
|
|
126
|
-
(typeof this.body == "object" || Array.isArray(this.body)) && t.set("Content-Type", "application/json");
|
|
127
|
-
for (let [s, r] of this.cookies) t.append("Set-Cookie", `${s}=${r}`);
|
|
128
|
-
let o = t.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
129
|
-
e.writeHead(this._status, Object.fromEntries(t)), e.end(o);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
40
|
// packages/router/src/controllers/Middleware.ts
|
|
134
41
|
var p = class {
|
|
135
42
|
name;
|
|
@@ -138,9 +45,8 @@ var p = class {
|
|
|
138
45
|
this.name = e.name, this.onRun = e.onRun;
|
|
139
46
|
}
|
|
140
47
|
};
|
|
141
|
-
|
|
142
48
|
// packages/router/src/controllers/Method.ts
|
|
143
|
-
var
|
|
49
|
+
var h = require("url-ast"), m = class {
|
|
144
50
|
path;
|
|
145
51
|
url;
|
|
146
52
|
method;
|
|
@@ -149,17 +55,19 @@ var m = require("url-ast"), h = class {
|
|
|
149
55
|
use;
|
|
150
56
|
handler;
|
|
151
57
|
constructor(e) {
|
|
152
|
-
this.path = e.path, this.url = new
|
|
58
|
+
this.path = e.path, this.url = new h.Analyze(this.path), this.method = e.method, this.schema = e.schema, this.handler = e.handler, this.use = e.use;
|
|
153
59
|
}
|
|
154
60
|
};
|
|
155
|
-
|
|
61
|
+
// packages/router/src/controllers/createRouter.ts
|
|
62
|
+
function S() {
|
|
63
|
+
return (t) => new n(t);
|
|
64
|
+
}
|
|
156
65
|
// packages/router/src/types/method.ts
|
|
157
|
-
var
|
|
158
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
var l = ((r) => (r.all = "all", r.get = "get", r.post = "post", r.put = "put", r.delete = "delete", r.options = "options", r.head = "head", r.patch = "patch", r))(l || {});
|
|
159
67
|
0 && (module.exports = {
|
|
160
68
|
Method,
|
|
161
69
|
MethodType,
|
|
162
70
|
Middleware,
|
|
163
|
-
|
|
164
|
-
|
|
71
|
+
Router,
|
|
72
|
+
createRouter
|
|
165
73
|
});
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/router/src/controllers/Router.ts
|
|
2
|
-
import { Analyze as
|
|
3
|
-
var
|
|
2
|
+
import { Analyze as h } from "url-ast";
|
|
3
|
+
var s = class {
|
|
4
4
|
name;
|
|
5
5
|
path;
|
|
6
6
|
schema;
|
|
@@ -9,113 +9,21 @@ var d = class {
|
|
|
9
9
|
use;
|
|
10
10
|
url;
|
|
11
11
|
constructor(e) {
|
|
12
|
-
let { name:
|
|
13
|
-
this.name =
|
|
12
|
+
let { name: d, path: a, schema: p, description: m, methods: i } = e;
|
|
13
|
+
this.name = d, this.path = a, this.schema = p, this.description = m, this.methods = i, this.use = e.use, this.url = new h(this.path);
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
|
-
|
|
17
|
-
// packages/router/src/controllers/Response.ts
|
|
18
|
-
var a = class r {
|
|
19
|
-
_status;
|
|
20
|
-
body;
|
|
21
|
-
header;
|
|
22
|
-
cookies;
|
|
23
|
-
constructor(e) {
|
|
24
|
-
this._status = e?.code ?? 200, this.body = e?.data, this.header = e?.header ?? /* @__PURE__ */ new Map(), this.cookies = e?.cookies ?? /* @__PURE__ */ new Map();
|
|
25
|
-
}
|
|
26
|
-
clone(e) {
|
|
27
|
-
return new r({
|
|
28
|
-
code: e.code !== void 0 ? e.code : this._status,
|
|
29
|
-
data: e.data !== void 0 ? e.data : this.body,
|
|
30
|
-
header: e.header !== void 0 ? e.header : this.header,
|
|
31
|
-
cookies: e.cookies !== void 0 ? e.cookies : this.cookies
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
// --- Core Methods ---
|
|
35
|
-
status(e) {
|
|
36
|
-
return this.clone({ code: e });
|
|
37
|
-
}
|
|
38
|
-
getStatus() {
|
|
39
|
-
return this._status;
|
|
40
|
-
}
|
|
41
|
-
code(e) {
|
|
42
|
-
return this.status(e);
|
|
43
|
-
}
|
|
44
|
-
send(e) {
|
|
45
|
-
return this.clone({ data: e });
|
|
46
|
-
}
|
|
47
|
-
json(e) {
|
|
48
|
-
let t = new Map(this.header);
|
|
49
|
-
return t.set("Content-Type", "application/json"), this.clone({ data: e, header: t });
|
|
50
|
-
}
|
|
51
|
-
// --- Response Helpers ---
|
|
52
|
-
success(e) {
|
|
53
|
-
return this.clone({ code: 200, data: e });
|
|
54
|
-
}
|
|
55
|
-
created(e) {
|
|
56
|
-
return this.clone({ code: 201, data: e });
|
|
57
|
-
}
|
|
58
|
-
noContent(e) {
|
|
59
|
-
return this.clone({ code: 204, data: e });
|
|
60
|
-
}
|
|
61
|
-
badRequest(e) {
|
|
62
|
-
return this.clone({ code: 400, data: e });
|
|
63
|
-
}
|
|
64
|
-
zodError(e) {
|
|
65
|
-
return this.clone({ code: 422, data: e });
|
|
66
|
-
}
|
|
67
|
-
unauthorized(e) {
|
|
68
|
-
return this.clone({ code: 401, data: e });
|
|
69
|
-
}
|
|
70
|
-
forbidden(e) {
|
|
71
|
-
return this.clone({ code: 403, data: e });
|
|
72
|
-
}
|
|
73
|
-
notFound(e) {
|
|
74
|
-
return this.clone({ code: 404, data: e });
|
|
75
|
-
}
|
|
76
|
-
setHeader(e, t) {
|
|
77
|
-
let o = new Map(this.header);
|
|
78
|
-
return o.set(e, t), this.clone({ header: o });
|
|
79
|
-
}
|
|
80
|
-
setCookie(e, t) {
|
|
81
|
-
let o = new Map(this.cookies);
|
|
82
|
-
return o.set(e, t), this.clone({ cookies: o });
|
|
83
|
-
}
|
|
84
|
-
toResponse() {
|
|
85
|
-
let e = new Headers();
|
|
86
|
-
e.set("Content-Type", "text/plain");
|
|
87
|
-
for (let [o, s] of this.header) e.set(o, s);
|
|
88
|
-
(typeof this.body == "object" || Array.isArray(this.body)) && e.set("Content-Type", "application/json");
|
|
89
|
-
for (let [o, s] of this.cookies) e.append("Set-Cookie", `${o}=${s}`);
|
|
90
|
-
let t = e.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
91
|
-
return new globalThis.Response(t, {
|
|
92
|
-
status: this._status,
|
|
93
|
-
headers: e
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
toServerResponse(e) {
|
|
97
|
-
let t = new Headers();
|
|
98
|
-
t.set("Content-Type", "text/plain");
|
|
99
|
-
for (let [s, n] of this.header) t.set(s, n);
|
|
100
|
-
(typeof this.body == "object" || Array.isArray(this.body)) && t.set("Content-Type", "application/json");
|
|
101
|
-
for (let [s, n] of this.cookies) t.append("Set-Cookie", `${s}=${n}`);
|
|
102
|
-
let o = t.get("Content-Type") === "application/json" ? JSON.stringify(this.body) : String(this.body);
|
|
103
|
-
e.writeHead(this._status, Object.fromEntries(t)), e.end(o);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
|
|
107
16
|
// packages/router/src/controllers/Middleware.ts
|
|
108
|
-
var
|
|
17
|
+
var o = class {
|
|
109
18
|
name;
|
|
110
19
|
onRun;
|
|
111
20
|
constructor(e) {
|
|
112
21
|
this.name = e.name, this.onRun = e.onRun;
|
|
113
22
|
}
|
|
114
23
|
};
|
|
115
|
-
|
|
116
24
|
// packages/router/src/controllers/Method.ts
|
|
117
|
-
import { Analyze as
|
|
118
|
-
var
|
|
25
|
+
import { Analyze as l } from "url-ast";
|
|
26
|
+
var n = class {
|
|
119
27
|
path;
|
|
120
28
|
url;
|
|
121
29
|
method;
|
|
@@ -124,17 +32,19 @@ var p = class {
|
|
|
124
32
|
use;
|
|
125
33
|
handler;
|
|
126
34
|
constructor(e) {
|
|
127
|
-
this.path = e.path, this.url = new
|
|
35
|
+
this.path = e.path, this.url = new l(this.path), this.method = e.method, this.schema = e.schema, this.handler = e.handler, this.use = e.use;
|
|
128
36
|
}
|
|
129
37
|
};
|
|
130
|
-
|
|
38
|
+
// packages/router/src/controllers/createRouter.ts
|
|
39
|
+
function f() {
|
|
40
|
+
return (r) => new s(r);
|
|
41
|
+
}
|
|
131
42
|
// packages/router/src/types/method.ts
|
|
132
|
-
|
|
133
|
-
var l = /* @__PURE__ */ ((s) => (s.get = "get", s.post = "post", s.put = "put", s.delete = "delete", s))(l || {});
|
|
43
|
+
var c = ((t) => (t.all = "all", t.get = "get", t.post = "post", t.put = "put", t.delete = "delete", t.options = "options", t.head = "head", t.patch = "patch", t))(c || {});
|
|
134
44
|
export {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
45
|
+
n as Method,
|
|
46
|
+
c as MethodType,
|
|
47
|
+
o as Middleware,
|
|
48
|
+
s as Router,
|
|
49
|
+
f as createRouter
|
|
140
50
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import type { Responders } from '@asterflow/response';
|
|
1
2
|
import { Analyze } from 'url-ast';
|
|
2
3
|
import type { MethodHandler, MethodKeys, MethodOptions } from '../types/method';
|
|
3
4
|
import type { MiddlewareOutput } from '../types/mindleware';
|
|
4
|
-
import type { Responders } from '../types/response';
|
|
5
5
|
import type { AnySchema } from '../types/schema';
|
|
6
6
|
import type { Middleware } from './Middleware';
|
|
7
7
|
export declare class Method<Responder extends Responders, const Path extends string = string, const Method extends MethodKeys = MethodKeys, const Schema extends AnySchema = AnySchema, const Middlewares extends readonly Middleware<Responder, Schema, string, Record<string, unknown>>[] = [], const Context extends MiddlewareOutput<Middlewares> = MiddlewareOutput<Middlewares>, const Handler extends MethodHandler<Path, Responder, Schema, Middlewares, Context> = MethodHandler<Path, Responder, Schema, Middlewares, Context>> {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { MiddlewareOptions } from '../types/mindleware';
|
|
2
|
-
import type { Responders } from '
|
|
2
|
+
import type { Responders } from '@asterflow/response';
|
|
3
3
|
import type { AnySchema } from '../types/schema';
|
|
4
4
|
export declare class Middleware<Responder extends Responders, Schema extends AnySchema, const Name extends string = string, const Parameters extends Record<string, unknown> = Record<string, unknown>> {
|
|
5
5
|
readonly name: Name;
|
|
6
6
|
readonly onRun: <RequestType>(args: {
|
|
7
|
-
response: import("
|
|
7
|
+
response: import("@asterflow/response").Response<Responder, import("@asterflow/response").BodyMap<Responder>, keyof Responder, Map<string, string>, Map<string, string>>;
|
|
8
8
|
request: import("@asterflow/request").Request<RequestType>;
|
|
9
9
|
schema: import("..").InferSchema<Schema>;
|
|
10
10
|
next: <Parameter extends Record<string, unknown>>(params: Parameter) => MiddlewareOptions<Responder, Schema, Name, Parameter>;
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
import type { Responders } from '@asterflow/response';
|
|
1
2
|
import { Analyze } from 'url-ast';
|
|
3
|
+
import type { MethodKeys } from '../types/method';
|
|
2
4
|
import type { MiddlewareOutput } from '../types/mindleware';
|
|
5
|
+
import type { RouteHandler, RouterOptions } from '../types/router';
|
|
3
6
|
import type { AnySchema, SchemaDynamic } from '../types/schema';
|
|
4
7
|
import type { Middleware } from './Middleware';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare class Router<Path extends string, Method extends MethodKeys, Schema extends SchemaDynamic<Method>, Responder extends Responders, const Routers extends {
|
|
8
|
+
export declare class Router<Responder extends Responders, const Path extends string = string, const Schema extends SchemaDynamic<MethodKeys> = SchemaDynamic<MethodKeys>, const Middlewares extends readonly Middleware<Responder, AnySchema, string, Record<string, unknown>>[] = [], const Context extends MiddlewareOutput<Middlewares> = MiddlewareOutput<Middlewares>, const Routers extends {
|
|
9
|
+
[Method in MethodKeys]?: RouteHandler<Path, Responder, Method, Schema, Middlewares, Context>;
|
|
10
|
+
} = {
|
|
9
11
|
[Method in MethodKeys]?: RouteHandler<Path, Responder, Method, Schema, Middlewares, Context>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
}> {
|
|
13
|
+
name?: string;
|
|
14
|
+
path: Path;
|
|
15
|
+
schema?: Schema;
|
|
16
|
+
description?: string;
|
|
17
|
+
methods: Routers;
|
|
18
|
+
use?: Middlewares;
|
|
17
19
|
url: Analyze<Path>;
|
|
18
|
-
constructor(options: RouterOptions<Path,
|
|
20
|
+
constructor(options: RouterOptions<Path, Schema, Responder, Middlewares, Context, Routers>);
|
|
19
21
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Responders } from '@asterflow/response';
|
|
2
|
+
import type { AnySchema, SchemaDynamic } from '../types/schema';
|
|
3
|
+
import type { Middleware } from './Middleware';
|
|
4
|
+
import type { MethodKeys } from '../types/method';
|
|
5
|
+
import type { MiddlewareOutput } from '../types/mindleware';
|
|
6
|
+
import type { RouteHandler, RouterOptions } from '../types/router';
|
|
7
|
+
import { Router } from './Router';
|
|
8
|
+
/**
|
|
9
|
+
* Starts building a Router, setting the expected response types.
|
|
10
|
+
* @returns An object with the `from` method to define the router options.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createRouter<Responder extends Responders>(): <const Path extends string, const Schema extends SchemaDynamic<MethodKeys>, const Middlewares extends readonly Middleware<Responder, AnySchema, string, Record<string, unknown>>[], const Context extends MiddlewareOutput<Middlewares>, const Routers extends { [Method in MethodKeys]?: RouteHandler<Path, Responder, Method, Schema, Middlewares, Context>; }>(options: RouterOptions<Path, Schema, Responder, Middlewares, Context, Routers>) => Router<Responder, Path, Schema, Middlewares, Context, Routers>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export * from './controllers/Router';
|
|
2
|
-
export * from './controllers/Response';
|
|
3
2
|
export * from './controllers/Middleware';
|
|
4
3
|
export * from './controllers/Method';
|
|
4
|
+
export * from './controllers/createRouter';
|
|
5
5
|
export * from './types/router';
|
|
6
6
|
export * from './types/schema';
|
|
7
|
-
export * from './types/response';
|
|
8
7
|
export * from './types/method';
|
|
9
8
|
export * from './types/mindleware';
|
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
import type { Request } from '@asterflow/request';
|
|
2
|
+
import type { Responders, Response } from '@asterflow/response';
|
|
3
|
+
import type { Analyze, ParsePath } from 'url-ast';
|
|
2
4
|
import type { Middleware } from '../controllers/Middleware';
|
|
3
|
-
import type { Response } from '../controllers/Response';
|
|
4
5
|
import type { AnyMiddleware, MiddlewareOutput } from './mindleware';
|
|
5
|
-
import type { Responders } from './response';
|
|
6
6
|
import type { AnySchema, InferSchema } from './schema';
|
|
7
|
-
import { Analyze, type ParsePath } from 'url-ast';
|
|
8
7
|
export declare enum MethodType {
|
|
8
|
+
all = "all",
|
|
9
9
|
get = "get",
|
|
10
10
|
post = "post",
|
|
11
11
|
put = "put",
|
|
12
|
-
delete = "delete"
|
|
12
|
+
delete = "delete",
|
|
13
|
+
options = "options",
|
|
14
|
+
head = "head",
|
|
15
|
+
patch = "patch"
|
|
13
16
|
}
|
|
17
|
+
export type AnyMethodHandler = MethodHandler<string, Responders, AnySchema, AnyMiddleware[], MiddlewareOutput<AnyMiddleware[]>>;
|
|
14
18
|
export type MethodKeys = keyof typeof MethodType;
|
|
15
|
-
export type AnyMethodHandler = {
|
|
16
|
-
[Method in MethodKeys]?: MethodHandler<string, Responders, AnySchema, AnyMiddleware[], MiddlewareOutput<AnyMiddleware[]>>;
|
|
17
|
-
};
|
|
18
19
|
export type MethodHandler<Path extends string, Responder extends Responders, Schema extends AnySchema, Middlewares extends readonly Middleware<Responder, Schema, string, Record<string, unknown>>[], Context extends MiddlewareOutput<Middlewares>> = <RequestType>(args: {
|
|
19
20
|
request: Request<RequestType>;
|
|
20
21
|
response: Response<Responder>;
|
|
21
|
-
url: Analyze<Path, ParsePath<Path>, Analyze<
|
|
22
|
+
url: Analyze<Path, ParsePath<Path>, Analyze<Path>>;
|
|
22
23
|
schema: InferSchema<Schema>;
|
|
23
24
|
middleware: Context;
|
|
24
25
|
}) => Promise<Response<Responder>> | Response<Responder>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Request } from '@asterflow/request';
|
|
2
2
|
import type { Middleware } from '../controllers/Middleware';
|
|
3
|
-
import type { Response } from '
|
|
4
|
-
import type { Responders } from './response';
|
|
3
|
+
import type { Response, Responders } from '@asterflow/response';
|
|
5
4
|
import type { AnySchema, InferSchema } from './schema';
|
|
6
5
|
export type AnyMiddleware = Middleware<any, any, any, any>;
|
|
7
6
|
/**
|
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
import type { Request } from '@asterflow/request';
|
|
2
|
+
import type { Responders, Response } from '@asterflow/response';
|
|
2
3
|
import type { Analyze, ParsePath } from 'url-ast';
|
|
3
4
|
import type { Method } from '../controllers/Method';
|
|
4
5
|
import type { Middleware } from '../controllers/Middleware';
|
|
5
|
-
import type { Response } from '../controllers/Response';
|
|
6
6
|
import type { Router } from '../controllers/Router';
|
|
7
7
|
import type { MethodKeys } from './method';
|
|
8
8
|
import type { AnyMiddleware, MiddlewareOutput } from './mindleware';
|
|
9
|
-
import type { Responders } from './response';
|
|
10
9
|
import type { AnySchema, InferredData, SchemaDynamic } from './schema';
|
|
11
10
|
export type AnyRouteHandler = {
|
|
12
11
|
[Method in MethodKeys]?: RouteHandler<string, Responders, Method, SchemaDynamic<Method>, AnyMiddleware[], MiddlewareOutput<AnyMiddleware[]>>;
|
|
13
12
|
};
|
|
14
|
-
export type AnyRouter = Router<
|
|
13
|
+
export type AnyRouter = Router<any, any, any, any, any, any> | Method<any, any, any, any, any, any, any>;
|
|
15
14
|
export type RouteHandler<Path extends string, Responder extends Responders, Method extends MethodKeys, Schema extends SchemaDynamic<Method>, Middlewares extends readonly Middleware<Responder, AnySchema, string, Record<string, unknown>>[], Context extends MiddlewareOutput<Middlewares>> = <RequestType>(args: {
|
|
16
15
|
request: Request<RequestType>;
|
|
17
16
|
response: Response<Responder>;
|
|
18
|
-
url: Analyze<Path, ParsePath<Path>, Analyze<
|
|
17
|
+
url: Analyze<Path, ParsePath<Path>, Analyze<Path>>;
|
|
19
18
|
schema: InferredData<Method, Schema>;
|
|
20
19
|
middleware: Context;
|
|
21
20
|
}) => Promise<Response> | Response;
|
|
22
|
-
export type RouterOptions<Path extends string,
|
|
21
|
+
export type RouterOptions<Path extends string, Schema extends SchemaDynamic<MethodKeys>, Responder extends Responders, Middlewares extends readonly Middleware<Responder, AnySchema, string, Record<string, unknown>>[], Context extends MiddlewareOutput<Middlewares>, Routers extends {
|
|
23
22
|
[Method in MethodKeys]?: RouteHandler<Path, Responder, Method, Schema, Middlewares, Context>;
|
|
24
23
|
}> = {
|
|
25
24
|
name?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asterflow/router",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"main": "dist/cjs/index.cjs",
|
|
5
5
|
"module": "dist/mjs/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -18,16 +18,16 @@
|
|
|
18
18
|
"node": ">=20"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@asterflow/request": "1.0.
|
|
21
|
+
"@asterflow/request": "1.0.5",
|
|
22
|
+
"@asterflow/response": "1.0.2",
|
|
22
23
|
"@caeljs/tsh": "^1.1.3",
|
|
23
|
-
"@types/bun": "latest",
|
|
24
24
|
"fastify": "^5.3.3",
|
|
25
|
-
"zod": "^3.25.
|
|
25
|
+
"zod": "^3.25.62"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"typescript": "^5.8.3"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"url-ast": "^1.0.
|
|
31
|
+
"url-ast": "^1.0.3"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { BodyMap, Responders, ResponseOptions } from '../types/response';
|
|
2
|
-
import { ServerResponse } from 'http';
|
|
3
|
-
export declare class Response<Responder extends Responders = Responders, BM extends BodyMap<Responder> = BodyMap<Responder>, Status extends keyof BM = keyof BM, Header extends Map<string, string> = Map<string, string>, Cookies extends Map<string, string> = Map<string, string>> {
|
|
4
|
-
protected readonly _status: Status;
|
|
5
|
-
readonly body?: BM[Status];
|
|
6
|
-
readonly header: Header;
|
|
7
|
-
readonly cookies: Cookies;
|
|
8
|
-
constructor(options?: ResponseOptions<Responder, BM, Status, Header, Cookies>);
|
|
9
|
-
protected clone<NewHeader extends Map<string, string> = Header, NewCookies extends Map<string, string> = Cookies>(overrides: {
|
|
10
|
-
data?: BM[Status];
|
|
11
|
-
header?: NewHeader;
|
|
12
|
-
cookies?: NewCookies;
|
|
13
|
-
}): Response<Responder, BM, Status, NewHeader, NewCookies>;
|
|
14
|
-
protected clone<NewStatus extends keyof BM, NewCookies extends Map<string, string> = Cookies>(overrides: {
|
|
15
|
-
code: NewStatus;
|
|
16
|
-
data?: BM[NewStatus];
|
|
17
|
-
header?: Header;
|
|
18
|
-
cookies?: NewCookies;
|
|
19
|
-
}): Response<Responder, BM, NewStatus, Header, NewCookies>;
|
|
20
|
-
status<NS extends keyof BM>(code: NS): Response<Responder, BM, NS, Header, Cookies>;
|
|
21
|
-
getStatus(): Status;
|
|
22
|
-
code<NS extends keyof BM>(code: NS): Response<Responder, BM, NS, Header, Cookies>;
|
|
23
|
-
send(data: BM[Status]): Response<Responder, BM, Status, Header, Cookies>;
|
|
24
|
-
json(data: BM[Status]): Response<Responder, BM, Status, Header & Map<"Content-Type", "application/json">, Cookies>;
|
|
25
|
-
success(data: BM[200]): Response<Responder, BM, 200, Header, Cookies>;
|
|
26
|
-
created(data: BM[201]): Response<Responder, BM, 201, Header, Cookies>;
|
|
27
|
-
noContent(data: BM[204]): Response<Responder, BM, 204, Header, Cookies>;
|
|
28
|
-
badRequest(data: BM[400]): Response<Responder, BM, 400, Header, Cookies>;
|
|
29
|
-
zodError(data: BM[422]): Response<Responder, BM, 422, Header, Cookies>;
|
|
30
|
-
unauthorized(data: BM[401]): Response<Responder, BM, 401, Header, Cookies>;
|
|
31
|
-
forbidden(data: BM[403]): Response<Responder, BM, 403, Header, Cookies>;
|
|
32
|
-
notFound(data: BM[404]): Response<Responder, BM, 404, Header, Cookies>;
|
|
33
|
-
setHeader<Name extends string, Value extends string>(name: Name, value: Value): Response<Responder, BM, Status, Header & Map<Name, Value>, Cookies>;
|
|
34
|
-
setCookie<Name extends string, Value extends string>(name: Name, value: Value): Response<Responder, BM, Status, Header, Cookies & Map<Name, Value>>;
|
|
35
|
-
toResponse(): globalThis.Response;
|
|
36
|
-
toServerResponse(output: ServerResponse): void;
|
|
37
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type Responders = {
|
|
2
|
-
[x in number]: unknown;
|
|
3
|
-
};
|
|
4
|
-
export type BodyMap<Responder extends Responders> = {
|
|
5
|
-
[S in keyof Responder]: Responder[S];
|
|
6
|
-
};
|
|
7
|
-
export type ResponseOptions<Responder extends Responders, BM extends BodyMap<Responder>, Status extends keyof BM, Header extends Map<string, string>, Cookies extends Map<string, string>> = {
|
|
8
|
-
data?: BM[Status];
|
|
9
|
-
code?: Status;
|
|
10
|
-
header?: Header;
|
|
11
|
-
cookies?: Cookies;
|
|
12
|
-
};
|