@asterflow/router 1.0.2 → 1.0.4
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 +4 -92
- package/dist/cjs/index.cjs +34 -119
- package/dist/mjs/index.js +18 -104
- 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 +3 -4
- package/dist/types/types/mindleware.d.ts +1 -2
- package/dist/types/types/router.d.ts +4 -5
- package/package.json +5 -4
- package/dist/types/controllers/Response.d.ts +0 -37
- package/dist/types/types/response.d.ts +0 -12
package/README.md
CHANGED
|
@@ -156,100 +156,12 @@ 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
|
|
253
165
|
|
|
254
166
|
## 📄 License
|
|
255
167
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
for (var
|
|
8
|
-
|
|
9
|
-
},
|
|
2
|
+
var n = Object.defineProperty;
|
|
3
|
+
var y = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var M = Object.getOwnPropertyNames;
|
|
5
|
+
var u = Object.prototype.hasOwnProperty;
|
|
6
|
+
var w = (t, e) => {
|
|
7
|
+
for (var s in e)
|
|
8
|
+
n(t, s, { get: e[s], enumerable: !0 });
|
|
9
|
+
}, R = (t, e, s, d) => {
|
|
10
10
|
if (e && typeof e == "object" || typeof e == "function")
|
|
11
|
-
for (let
|
|
12
|
-
!
|
|
13
|
-
return
|
|
11
|
+
for (let r of M(e))
|
|
12
|
+
!u.call(t, r) && r !== s && n(t, r, { get: () => e[r], enumerable: !(d = y(e, r)) || d.enumerable });
|
|
13
|
+
return t;
|
|
14
14
|
};
|
|
15
|
-
var
|
|
15
|
+
var x = (t) => R(n({}, "__esModule", { value: !0 }), t);
|
|
16
16
|
|
|
17
17
|
// packages/router/src/index.ts
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
Method: () =>
|
|
21
|
-
MethodType: () =>
|
|
22
|
-
Middleware: () =>
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
var S = {};
|
|
19
|
+
w(S, {
|
|
20
|
+
Method: () => p,
|
|
21
|
+
MethodType: () => h,
|
|
22
|
+
Middleware: () => a,
|
|
23
|
+
Router: () => o,
|
|
24
|
+
createRouter: () => f
|
|
25
25
|
});
|
|
26
|
-
module.exports =
|
|
26
|
+
module.exports = x(S);
|
|
27
27
|
|
|
28
28
|
// packages/router/src/controllers/Router.ts
|
|
29
|
-
var
|
|
29
|
+
var m = require("url-ast"), o = class {
|
|
30
30
|
name;
|
|
31
31
|
path;
|
|
32
32
|
schema;
|
|
@@ -35,103 +35,13 @@ var c = require("url-ast"), a = class {
|
|
|
35
35
|
use;
|
|
36
36
|
url;
|
|
37
37
|
constructor(e) {
|
|
38
|
-
let { name:
|
|
39
|
-
this.name =
|
|
40
|
-
}
|
|
41
|
-
};
|
|
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);
|
|
38
|
+
let { name: s, path: d, schema: r, description: l, methods: c } = e;
|
|
39
|
+
this.name = s, this.path = d, this.schema = r, this.description = l, this.methods = c, this.use = e.use, this.url = new m.Analyze(this.path);
|
|
130
40
|
}
|
|
131
41
|
};
|
|
132
42
|
|
|
133
43
|
// packages/router/src/controllers/Middleware.ts
|
|
134
|
-
var
|
|
44
|
+
var a = class {
|
|
135
45
|
name;
|
|
136
46
|
onRun;
|
|
137
47
|
constructor(e) {
|
|
@@ -140,7 +50,7 @@ var p = class {
|
|
|
140
50
|
};
|
|
141
51
|
|
|
142
52
|
// packages/router/src/controllers/Method.ts
|
|
143
|
-
var
|
|
53
|
+
var i = require("url-ast"), p = class {
|
|
144
54
|
path;
|
|
145
55
|
url;
|
|
146
56
|
method;
|
|
@@ -149,17 +59,22 @@ var m = require("url-ast"), h = class {
|
|
|
149
59
|
use;
|
|
150
60
|
handler;
|
|
151
61
|
constructor(e) {
|
|
152
|
-
this.path = e.path, this.url = new
|
|
62
|
+
this.path = e.path, this.url = new i.Analyze(this.path), this.method = e.method, this.schema = e.schema, this.handler = e.handler, this.use = e.use;
|
|
153
63
|
}
|
|
154
64
|
};
|
|
155
65
|
|
|
66
|
+
// packages/router/src/controllers/createRouter.ts
|
|
67
|
+
function f() {
|
|
68
|
+
return (t) => new o(t);
|
|
69
|
+
}
|
|
70
|
+
|
|
156
71
|
// packages/router/src/types/method.ts
|
|
157
|
-
var
|
|
72
|
+
var h = /* @__PURE__ */ ((r) => (r.get = "get", r.post = "post", r.put = "put", r.delete = "delete", r))(h || {});
|
|
158
73
|
// Annotate the CommonJS export names for ESM import in node:
|
|
159
74
|
0 && (module.exports = {
|
|
160
75
|
Method,
|
|
161
76
|
MethodType,
|
|
162
77
|
Middleware,
|
|
163
|
-
|
|
164
|
-
|
|
78
|
+
Router,
|
|
79
|
+
createRouter
|
|
165
80
|
});
|
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 i } from "url-ast";
|
|
3
|
+
var s = class {
|
|
4
4
|
name;
|
|
5
5
|
path;
|
|
6
6
|
schema;
|
|
@@ -9,103 +9,13 @@ var d = class {
|
|
|
9
9
|
use;
|
|
10
10
|
url;
|
|
11
11
|
constructor(e) {
|
|
12
|
-
let { name:
|
|
13
|
-
this.name =
|
|
14
|
-
}
|
|
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);
|
|
12
|
+
let { name: n, path: a, schema: t, description: p, methods: m } = e;
|
|
13
|
+
this.name = n, this.path = a, this.schema = t, this.description = p, this.methods = m, this.use = e.use, this.url = new i(this.path);
|
|
104
14
|
}
|
|
105
15
|
};
|
|
106
16
|
|
|
107
17
|
// packages/router/src/controllers/Middleware.ts
|
|
108
|
-
var
|
|
18
|
+
var o = class {
|
|
109
19
|
name;
|
|
110
20
|
onRun;
|
|
111
21
|
constructor(e) {
|
|
@@ -114,8 +24,8 @@ var i = class {
|
|
|
114
24
|
};
|
|
115
25
|
|
|
116
26
|
// packages/router/src/controllers/Method.ts
|
|
117
|
-
import { Analyze as
|
|
118
|
-
var
|
|
27
|
+
import { Analyze as h } from "url-ast";
|
|
28
|
+
var d = class {
|
|
119
29
|
path;
|
|
120
30
|
url;
|
|
121
31
|
method;
|
|
@@ -124,17 +34,21 @@ var p = class {
|
|
|
124
34
|
use;
|
|
125
35
|
handler;
|
|
126
36
|
constructor(e) {
|
|
127
|
-
this.path = e.path, this.url = new
|
|
37
|
+
this.path = e.path, this.url = new h(this.path), this.method = e.method, this.schema = e.schema, this.handler = e.handler, this.use = e.use;
|
|
128
38
|
}
|
|
129
39
|
};
|
|
130
40
|
|
|
41
|
+
// packages/router/src/controllers/createRouter.ts
|
|
42
|
+
function x() {
|
|
43
|
+
return (r) => new s(r);
|
|
44
|
+
}
|
|
45
|
+
|
|
131
46
|
// 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 || {});
|
|
47
|
+
var l = /* @__PURE__ */ ((t) => (t.get = "get", t.post = "post", t.put = "put", t.delete = "delete", t))(l || {});
|
|
134
48
|
export {
|
|
135
|
-
|
|
49
|
+
d as Method,
|
|
136
50
|
l as MethodType,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
51
|
+
o as Middleware,
|
|
52
|
+
s as Router,
|
|
53
|
+
x as createRouter
|
|
140
54
|
};
|
|
@@ -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,10 +1,9 @@
|
|
|
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 {
|
|
9
8
|
get = "get",
|
|
10
9
|
post = "post",
|
|
@@ -18,7 +17,7 @@ export type AnyMethodHandler = {
|
|
|
18
17
|
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
18
|
request: Request<RequestType>;
|
|
20
19
|
response: Response<Responder>;
|
|
21
|
-
url: Analyze<Path, ParsePath<Path>, Analyze<
|
|
20
|
+
url: Analyze<Path, ParsePath<Path>, Analyze<Path>>;
|
|
22
21
|
schema: InferSchema<Schema>;
|
|
23
22
|
middleware: Context;
|
|
24
23
|
}) => 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<Responders, string, SchemaDynamic<MethodKeys>, AnyMiddleware[], MiddlewareOutput<AnyMiddleware[]>, AnyRouteHandler> | Method<Responders, string, MethodKeys, AnySchema, AnyMiddleware[], MiddlewareOutput<AnyMiddleware[]>, 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.4",
|
|
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,17 @@
|
|
|
18
18
|
"node": ">=20"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@asterflow/request": "1.0.
|
|
21
|
+
"@asterflow/request": "1.0.4",
|
|
22
|
+
"@asterflow/response": "1.0.1",
|
|
22
23
|
"@caeljs/tsh": "^1.1.3",
|
|
23
24
|
"@types/bun": "latest",
|
|
24
25
|
"fastify": "^5.3.3",
|
|
25
|
-
"zod": "^3.25.
|
|
26
|
+
"zod": "^3.25.62"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"typescript": "^5.8.3"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"url-ast": "^1.0.
|
|
32
|
+
"url-ast": "^1.0.3"
|
|
32
33
|
}
|
|
33
34
|
}
|
|
@@ -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
|
-
};
|