mikroserve 0.0.10 → 1.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.
- package/README.md +3 -3
- package/lib/MikroServe.d.mts +4 -0
- package/lib/MikroServe.d.ts +4 -0
- package/lib/MikroServe.js +58 -35
- package/lib/MikroServe.mjs +2 -2
- package/lib/Router.d.mts +22 -18
- package/lib/Router.d.ts +22 -18
- package/lib/Router.js +51 -35
- package/lib/Router.mjs +1 -1
- package/lib/{chunk-TQN6BEGA.mjs → chunk-N5ZQZGGT.mjs} +8 -1
- package/lib/{chunk-GUYBTPZH.mjs → chunk-YKRH6T5M.mjs} +51 -35
- package/lib/index.js +58 -35
- package/lib/index.mjs +2 -2
- package/package.json +1 -4
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
---
|
|
14
14
|
|
|
15
|
-
- Native Node.js [http](https://nodejs.org/api/http.html)/[https](https://nodejs.org/api/https.html) implementation, meaning maximum performance
|
|
15
|
+
- Native Node.js [http](https://nodejs.org/api/http.html)/[https](https://nodejs.org/api/https.html)/[http2](https://nodejs.org/api/http2.html) implementation, meaning maximum performance
|
|
16
16
|
- [Hono](https://hono.dev)-style API semantics for GET, POST, PATCH, PUT, DELETE operations
|
|
17
17
|
- Supports being exposed over HTTP, HTTPS, and HTTP2
|
|
18
18
|
- Supports custom middlewares
|
|
@@ -212,12 +212,12 @@ All of the settings already presented in the above examples can be provided in m
|
|
|
212
212
|
| --cert | `<string>` | sslCert | |
|
|
213
213
|
| --key | `<string>` | sslKey | |
|
|
214
214
|
| --ca | `<string>` | sslCa | |
|
|
215
|
-
| --debug | none (is flag) | debug | DEBUG |
|
|
216
215
|
| --ratelimit | none (is flag) | rateLimit.enabled | |
|
|
217
216
|
| --rps | `<number>` | rateLimit.requestsPerMinute | |
|
|
218
217
|
| --allowed | `<comma-separated strings>` | allowedDomains | |
|
|
218
|
+
| --debug | none (is flag) | debug | DEBUG |
|
|
219
219
|
|
|
220
|
-
### Order of
|
|
220
|
+
### Order of Application
|
|
221
221
|
|
|
222
222
|
As per [MikroConf](https://github.com/mikaelvesavuori/mikroconf) behavior, the configuration sources are applied in this order:
|
|
223
223
|
|
package/lib/MikroServe.d.mts
CHANGED
|
@@ -38,6 +38,10 @@ declare class MikroServe {
|
|
|
38
38
|
* @description Register a PATCH route.
|
|
39
39
|
*/
|
|
40
40
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
|
+
/**
|
|
42
|
+
* @description Register a route that responds to any HTTP method.
|
|
43
|
+
*/
|
|
44
|
+
any(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
45
|
/**
|
|
42
46
|
* @description Register an OPTIONS route.
|
|
43
47
|
*/
|
package/lib/MikroServe.d.ts
CHANGED
|
@@ -38,6 +38,10 @@ declare class MikroServe {
|
|
|
38
38
|
* @description Register a PATCH route.
|
|
39
39
|
*/
|
|
40
40
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
|
+
/**
|
|
42
|
+
* @description Register a route that responds to any HTTP method.
|
|
43
|
+
*/
|
|
44
|
+
any(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
45
|
/**
|
|
42
46
|
* @description Register an OPTIONS route.
|
|
43
47
|
*/
|
package/lib/MikroServe.js
CHANGED
|
@@ -92,64 +92,70 @@ var Router = class {
|
|
|
92
92
|
globalMiddlewares = [];
|
|
93
93
|
pathPatterns = /* @__PURE__ */ new Map();
|
|
94
94
|
/**
|
|
95
|
-
* Add a global middleware
|
|
95
|
+
* @description Add a global middleware.
|
|
96
96
|
*/
|
|
97
97
|
use(middleware) {
|
|
98
98
|
this.globalMiddlewares.push(middleware);
|
|
99
99
|
return this;
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
|
-
* Register a route
|
|
103
|
-
*/
|
|
104
|
-
register(method, path, handler, middlewares = []) {
|
|
105
|
-
this.routes.push({ method, path, handler, middlewares });
|
|
106
|
-
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Register a GET route
|
|
102
|
+
* @description Register a GET route.
|
|
111
103
|
*/
|
|
112
104
|
get(path, ...handlers) {
|
|
113
105
|
const handler = handlers.pop();
|
|
114
106
|
return this.register("GET", path, handler, handlers);
|
|
115
107
|
}
|
|
116
108
|
/**
|
|
117
|
-
* Register a POST route
|
|
109
|
+
* @description Register a POST route.
|
|
118
110
|
*/
|
|
119
111
|
post(path, ...handlers) {
|
|
120
112
|
const handler = handlers.pop();
|
|
121
113
|
return this.register("POST", path, handler, handlers);
|
|
122
114
|
}
|
|
123
115
|
/**
|
|
124
|
-
* Register a PUT route
|
|
116
|
+
* @description Register a PUT route.
|
|
125
117
|
*/
|
|
126
118
|
put(path, ...handlers) {
|
|
127
119
|
const handler = handlers.pop();
|
|
128
120
|
return this.register("PUT", path, handler, handlers);
|
|
129
121
|
}
|
|
130
122
|
/**
|
|
131
|
-
* Register a DELETE route
|
|
123
|
+
* @description Register a DELETE route.
|
|
132
124
|
*/
|
|
133
125
|
delete(path, ...handlers) {
|
|
134
126
|
const handler = handlers.pop();
|
|
135
127
|
return this.register("DELETE", path, handler, handlers);
|
|
136
128
|
}
|
|
137
129
|
/**
|
|
138
|
-
* Register a PATCH route
|
|
130
|
+
* @description Register a PATCH route.
|
|
139
131
|
*/
|
|
140
132
|
patch(path, ...handlers) {
|
|
141
133
|
const handler = handlers.pop();
|
|
142
134
|
return this.register("PATCH", path, handler, handlers);
|
|
143
135
|
}
|
|
144
136
|
/**
|
|
145
|
-
* Register
|
|
137
|
+
* @description Register a route for any HTTP method.
|
|
138
|
+
*/
|
|
139
|
+
any(path, ...handlers) {
|
|
140
|
+
const handler = handlers.pop();
|
|
141
|
+
const middlewares = handlers;
|
|
142
|
+
this.register("GET", path, handler, middlewares);
|
|
143
|
+
this.register("POST", path, handler, middlewares);
|
|
144
|
+
this.register("PUT", path, handler, middlewares);
|
|
145
|
+
this.register("DELETE", path, handler, middlewares);
|
|
146
|
+
this.register("PATCH", path, handler, middlewares);
|
|
147
|
+
this.register("OPTIONS", path, handler, middlewares);
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @description Register an OPTIONS route.
|
|
146
152
|
*/
|
|
147
153
|
options(path, ...handlers) {
|
|
148
154
|
const handler = handlers.pop();
|
|
149
155
|
return this.register("OPTIONS", path, handler, handlers);
|
|
150
156
|
}
|
|
151
157
|
/**
|
|
152
|
-
* Match a request to a route
|
|
158
|
+
* @description Match a request to a route.
|
|
153
159
|
*/
|
|
154
160
|
match(method, path) {
|
|
155
161
|
for (const route of this.routes) {
|
|
@@ -167,22 +173,7 @@ var Router = class {
|
|
|
167
173
|
return null;
|
|
168
174
|
}
|
|
169
175
|
/**
|
|
170
|
-
*
|
|
171
|
-
*/
|
|
172
|
-
createPathPattern(path) {
|
|
173
|
-
const paramNames = [];
|
|
174
|
-
const pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
175
|
-
const paramName = match.slice(2);
|
|
176
|
-
paramNames.push(paramName);
|
|
177
|
-
return "/([^/]+)";
|
|
178
|
-
}).replace(/\/$/, "/?");
|
|
179
|
-
return {
|
|
180
|
-
pattern: new RegExp(`^${pattern}$`),
|
|
181
|
-
paramNames
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Handle a request and find the matching route
|
|
176
|
+
* @description Handle a request and find the matching route.
|
|
186
177
|
*/
|
|
187
178
|
async handle(req, res) {
|
|
188
179
|
const method = req.method || "GET";
|
|
@@ -205,7 +196,6 @@ var Router = class {
|
|
|
205
196
|
headers: req.headers,
|
|
206
197
|
path,
|
|
207
198
|
state: {},
|
|
208
|
-
// Add the missing state property
|
|
209
199
|
raw: () => res,
|
|
210
200
|
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
211
201
|
statusCode: status,
|
|
@@ -269,7 +259,6 @@ var Router = class {
|
|
|
269
259
|
headers: { "Content-Type": "text/html" }
|
|
270
260
|
}),
|
|
271
261
|
form: (content) => ({
|
|
272
|
-
// Make sure form method is included here
|
|
273
262
|
statusCode: code,
|
|
274
263
|
body: content,
|
|
275
264
|
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
@@ -287,7 +276,34 @@ var Router = class {
|
|
|
287
276
|
return this.executeMiddlewareChain(context, middlewares, route.handler);
|
|
288
277
|
}
|
|
289
278
|
/**
|
|
290
|
-
*
|
|
279
|
+
* @description Register a route with specified method.
|
|
280
|
+
*/
|
|
281
|
+
register(method, path, handler, middlewares = []) {
|
|
282
|
+
this.routes.push({ method, path, handler, middlewares });
|
|
283
|
+
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* @description Create a regex pattern for path matching.
|
|
288
|
+
*/
|
|
289
|
+
createPathPattern(path) {
|
|
290
|
+
const paramNames = [];
|
|
291
|
+
let pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
292
|
+
const paramName = match.slice(2);
|
|
293
|
+
paramNames.push(paramName);
|
|
294
|
+
return "/([^/]+)";
|
|
295
|
+
});
|
|
296
|
+
if (pattern.endsWith("/*")) {
|
|
297
|
+
pattern = `${pattern.slice(0, -2)}(?:/(.*))?`;
|
|
298
|
+
paramNames.push("wildcard");
|
|
299
|
+
} else pattern = pattern.replace(/\/$/, "/?");
|
|
300
|
+
return {
|
|
301
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
302
|
+
paramNames
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* @description Execute middleware chain and final handler.
|
|
291
307
|
*/
|
|
292
308
|
async executeMiddlewareChain(context, middlewares, finalHandler) {
|
|
293
309
|
let currentIndex = 0;
|
|
@@ -422,6 +438,13 @@ var MikroServe = class {
|
|
|
422
438
|
this.router.patch(path, ...handlers);
|
|
423
439
|
return this;
|
|
424
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* @description Register a route that responds to any HTTP method.
|
|
443
|
+
*/
|
|
444
|
+
any(path, ...handlers) {
|
|
445
|
+
this.router.any(path, ...handlers);
|
|
446
|
+
return this;
|
|
447
|
+
}
|
|
425
448
|
/**
|
|
426
449
|
* @description Register an OPTIONS route.
|
|
427
450
|
*/
|
package/lib/MikroServe.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MikroServe
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-N5ZQZGGT.mjs";
|
|
4
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
5
|
-
import "./chunk-
|
|
5
|
+
import "./chunk-YKRH6T5M.mjs";
|
|
6
6
|
import "./chunk-YOHL3T54.mjs";
|
|
7
7
|
import "./chunk-JJX5XRNB.mjs";
|
|
8
8
|
export {
|
package/lib/Router.d.mts
CHANGED
|
@@ -4,61 +4,65 @@ import 'node:http2';
|
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Router class to manage routes and middleware
|
|
7
|
+
* @description Router class to manage routes and middleware.
|
|
8
8
|
*/
|
|
9
9
|
declare class Router {
|
|
10
10
|
private routes;
|
|
11
11
|
private globalMiddlewares;
|
|
12
12
|
private pathPatterns;
|
|
13
13
|
/**
|
|
14
|
-
* Add a global middleware
|
|
14
|
+
* @description Add a global middleware.
|
|
15
15
|
*/
|
|
16
16
|
use(middleware: Middleware): this;
|
|
17
17
|
/**
|
|
18
|
-
* Register a route
|
|
19
|
-
*/
|
|
20
|
-
private register;
|
|
21
|
-
/**
|
|
22
|
-
* Register a GET route
|
|
18
|
+
* @description Register a GET route.
|
|
23
19
|
*/
|
|
24
20
|
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
25
21
|
/**
|
|
26
|
-
* Register a POST route
|
|
22
|
+
* @description Register a POST route.
|
|
27
23
|
*/
|
|
28
24
|
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
29
25
|
/**
|
|
30
|
-
* Register a PUT route
|
|
26
|
+
* @description Register a PUT route.
|
|
31
27
|
*/
|
|
32
28
|
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
33
29
|
/**
|
|
34
|
-
* Register a DELETE route
|
|
30
|
+
* @description Register a DELETE route.
|
|
35
31
|
*/
|
|
36
32
|
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
37
33
|
/**
|
|
38
|
-
* Register a PATCH route
|
|
34
|
+
* @description Register a PATCH route.
|
|
39
35
|
*/
|
|
40
36
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
37
|
/**
|
|
42
|
-
* Register
|
|
38
|
+
* @description Register a route for any HTTP method.
|
|
39
|
+
*/
|
|
40
|
+
any(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
|
+
/**
|
|
42
|
+
* @description Register an OPTIONS route.
|
|
43
43
|
*/
|
|
44
44
|
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
45
45
|
/**
|
|
46
|
-
* Match a request to a route
|
|
46
|
+
* @description Match a request to a route.
|
|
47
47
|
*/
|
|
48
48
|
match(method: string, path: string): {
|
|
49
49
|
route: Route;
|
|
50
50
|
params: Record<string, string>;
|
|
51
51
|
} | null;
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* @description Handle a request and find the matching route.
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
handle(req: RequestType, res: ResponseType): Promise<HandlerResponse | null>;
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* @description Register a route with specified method.
|
|
58
58
|
*/
|
|
59
|
-
|
|
59
|
+
private register;
|
|
60
|
+
/**
|
|
61
|
+
* @description Create a regex pattern for path matching.
|
|
62
|
+
*/
|
|
63
|
+
private createPathPattern;
|
|
60
64
|
/**
|
|
61
|
-
* Execute middleware chain and final handler
|
|
65
|
+
* @description Execute middleware chain and final handler.
|
|
62
66
|
*/
|
|
63
67
|
private executeMiddlewareChain;
|
|
64
68
|
}
|
package/lib/Router.d.ts
CHANGED
|
@@ -4,61 +4,65 @@ import 'node:http2';
|
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Router class to manage routes and middleware
|
|
7
|
+
* @description Router class to manage routes and middleware.
|
|
8
8
|
*/
|
|
9
9
|
declare class Router {
|
|
10
10
|
private routes;
|
|
11
11
|
private globalMiddlewares;
|
|
12
12
|
private pathPatterns;
|
|
13
13
|
/**
|
|
14
|
-
* Add a global middleware
|
|
14
|
+
* @description Add a global middleware.
|
|
15
15
|
*/
|
|
16
16
|
use(middleware: Middleware): this;
|
|
17
17
|
/**
|
|
18
|
-
* Register a route
|
|
19
|
-
*/
|
|
20
|
-
private register;
|
|
21
|
-
/**
|
|
22
|
-
* Register a GET route
|
|
18
|
+
* @description Register a GET route.
|
|
23
19
|
*/
|
|
24
20
|
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
25
21
|
/**
|
|
26
|
-
* Register a POST route
|
|
22
|
+
* @description Register a POST route.
|
|
27
23
|
*/
|
|
28
24
|
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
29
25
|
/**
|
|
30
|
-
* Register a PUT route
|
|
26
|
+
* @description Register a PUT route.
|
|
31
27
|
*/
|
|
32
28
|
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
33
29
|
/**
|
|
34
|
-
* Register a DELETE route
|
|
30
|
+
* @description Register a DELETE route.
|
|
35
31
|
*/
|
|
36
32
|
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
37
33
|
/**
|
|
38
|
-
* Register a PATCH route
|
|
34
|
+
* @description Register a PATCH route.
|
|
39
35
|
*/
|
|
40
36
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
37
|
/**
|
|
42
|
-
* Register
|
|
38
|
+
* @description Register a route for any HTTP method.
|
|
39
|
+
*/
|
|
40
|
+
any(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
41
|
+
/**
|
|
42
|
+
* @description Register an OPTIONS route.
|
|
43
43
|
*/
|
|
44
44
|
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
45
45
|
/**
|
|
46
|
-
* Match a request to a route
|
|
46
|
+
* @description Match a request to a route.
|
|
47
47
|
*/
|
|
48
48
|
match(method: string, path: string): {
|
|
49
49
|
route: Route;
|
|
50
50
|
params: Record<string, string>;
|
|
51
51
|
} | null;
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* @description Handle a request and find the matching route.
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
handle(req: RequestType, res: ResponseType): Promise<HandlerResponse | null>;
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* @description Register a route with specified method.
|
|
58
58
|
*/
|
|
59
|
-
|
|
59
|
+
private register;
|
|
60
|
+
/**
|
|
61
|
+
* @description Create a regex pattern for path matching.
|
|
62
|
+
*/
|
|
63
|
+
private createPathPattern;
|
|
60
64
|
/**
|
|
61
|
-
* Execute middleware chain and final handler
|
|
65
|
+
* @description Execute middleware chain and final handler.
|
|
62
66
|
*/
|
|
63
67
|
private executeMiddlewareChain;
|
|
64
68
|
}
|
package/lib/Router.js
CHANGED
|
@@ -29,64 +29,70 @@ var Router = class {
|
|
|
29
29
|
globalMiddlewares = [];
|
|
30
30
|
pathPatterns = /* @__PURE__ */ new Map();
|
|
31
31
|
/**
|
|
32
|
-
* Add a global middleware
|
|
32
|
+
* @description Add a global middleware.
|
|
33
33
|
*/
|
|
34
34
|
use(middleware) {
|
|
35
35
|
this.globalMiddlewares.push(middleware);
|
|
36
36
|
return this;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* Register a route
|
|
40
|
-
*/
|
|
41
|
-
register(method, path, handler, middlewares = []) {
|
|
42
|
-
this.routes.push({ method, path, handler, middlewares });
|
|
43
|
-
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Register a GET route
|
|
39
|
+
* @description Register a GET route.
|
|
48
40
|
*/
|
|
49
41
|
get(path, ...handlers) {
|
|
50
42
|
const handler = handlers.pop();
|
|
51
43
|
return this.register("GET", path, handler, handlers);
|
|
52
44
|
}
|
|
53
45
|
/**
|
|
54
|
-
* Register a POST route
|
|
46
|
+
* @description Register a POST route.
|
|
55
47
|
*/
|
|
56
48
|
post(path, ...handlers) {
|
|
57
49
|
const handler = handlers.pop();
|
|
58
50
|
return this.register("POST", path, handler, handlers);
|
|
59
51
|
}
|
|
60
52
|
/**
|
|
61
|
-
* Register a PUT route
|
|
53
|
+
* @description Register a PUT route.
|
|
62
54
|
*/
|
|
63
55
|
put(path, ...handlers) {
|
|
64
56
|
const handler = handlers.pop();
|
|
65
57
|
return this.register("PUT", path, handler, handlers);
|
|
66
58
|
}
|
|
67
59
|
/**
|
|
68
|
-
* Register a DELETE route
|
|
60
|
+
* @description Register a DELETE route.
|
|
69
61
|
*/
|
|
70
62
|
delete(path, ...handlers) {
|
|
71
63
|
const handler = handlers.pop();
|
|
72
64
|
return this.register("DELETE", path, handler, handlers);
|
|
73
65
|
}
|
|
74
66
|
/**
|
|
75
|
-
* Register a PATCH route
|
|
67
|
+
* @description Register a PATCH route.
|
|
76
68
|
*/
|
|
77
69
|
patch(path, ...handlers) {
|
|
78
70
|
const handler = handlers.pop();
|
|
79
71
|
return this.register("PATCH", path, handler, handlers);
|
|
80
72
|
}
|
|
81
73
|
/**
|
|
82
|
-
* Register
|
|
74
|
+
* @description Register a route for any HTTP method.
|
|
75
|
+
*/
|
|
76
|
+
any(path, ...handlers) {
|
|
77
|
+
const handler = handlers.pop();
|
|
78
|
+
const middlewares = handlers;
|
|
79
|
+
this.register("GET", path, handler, middlewares);
|
|
80
|
+
this.register("POST", path, handler, middlewares);
|
|
81
|
+
this.register("PUT", path, handler, middlewares);
|
|
82
|
+
this.register("DELETE", path, handler, middlewares);
|
|
83
|
+
this.register("PATCH", path, handler, middlewares);
|
|
84
|
+
this.register("OPTIONS", path, handler, middlewares);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @description Register an OPTIONS route.
|
|
83
89
|
*/
|
|
84
90
|
options(path, ...handlers) {
|
|
85
91
|
const handler = handlers.pop();
|
|
86
92
|
return this.register("OPTIONS", path, handler, handlers);
|
|
87
93
|
}
|
|
88
94
|
/**
|
|
89
|
-
* Match a request to a route
|
|
95
|
+
* @description Match a request to a route.
|
|
90
96
|
*/
|
|
91
97
|
match(method, path) {
|
|
92
98
|
for (const route of this.routes) {
|
|
@@ -104,22 +110,7 @@ var Router = class {
|
|
|
104
110
|
return null;
|
|
105
111
|
}
|
|
106
112
|
/**
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
createPathPattern(path) {
|
|
110
|
-
const paramNames = [];
|
|
111
|
-
const pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
112
|
-
const paramName = match.slice(2);
|
|
113
|
-
paramNames.push(paramName);
|
|
114
|
-
return "/([^/]+)";
|
|
115
|
-
}).replace(/\/$/, "/?");
|
|
116
|
-
return {
|
|
117
|
-
pattern: new RegExp(`^${pattern}$`),
|
|
118
|
-
paramNames
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Handle a request and find the matching route
|
|
113
|
+
* @description Handle a request and find the matching route.
|
|
123
114
|
*/
|
|
124
115
|
async handle(req, res) {
|
|
125
116
|
const method = req.method || "GET";
|
|
@@ -142,7 +133,6 @@ var Router = class {
|
|
|
142
133
|
headers: req.headers,
|
|
143
134
|
path,
|
|
144
135
|
state: {},
|
|
145
|
-
// Add the missing state property
|
|
146
136
|
raw: () => res,
|
|
147
137
|
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
148
138
|
statusCode: status,
|
|
@@ -206,7 +196,6 @@ var Router = class {
|
|
|
206
196
|
headers: { "Content-Type": "text/html" }
|
|
207
197
|
}),
|
|
208
198
|
form: (content) => ({
|
|
209
|
-
// Make sure form method is included here
|
|
210
199
|
statusCode: code,
|
|
211
200
|
body: content,
|
|
212
201
|
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
@@ -224,7 +213,34 @@ var Router = class {
|
|
|
224
213
|
return this.executeMiddlewareChain(context, middlewares, route.handler);
|
|
225
214
|
}
|
|
226
215
|
/**
|
|
227
|
-
*
|
|
216
|
+
* @description Register a route with specified method.
|
|
217
|
+
*/
|
|
218
|
+
register(method, path, handler, middlewares = []) {
|
|
219
|
+
this.routes.push({ method, path, handler, middlewares });
|
|
220
|
+
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @description Create a regex pattern for path matching.
|
|
225
|
+
*/
|
|
226
|
+
createPathPattern(path) {
|
|
227
|
+
const paramNames = [];
|
|
228
|
+
let pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
229
|
+
const paramName = match.slice(2);
|
|
230
|
+
paramNames.push(paramName);
|
|
231
|
+
return "/([^/]+)";
|
|
232
|
+
});
|
|
233
|
+
if (pattern.endsWith("/*")) {
|
|
234
|
+
pattern = `${pattern.slice(0, -2)}(?:/(.*))?`;
|
|
235
|
+
paramNames.push("wildcard");
|
|
236
|
+
} else pattern = pattern.replace(/\/$/, "/?");
|
|
237
|
+
return {
|
|
238
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
239
|
+
paramNames
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* @description Execute middleware chain and final handler.
|
|
228
244
|
*/
|
|
229
245
|
async executeMiddlewareChain(context, middlewares, finalHandler) {
|
|
230
246
|
let currentIndex = 0;
|
package/lib/Router.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-ZFBBESGU.mjs";
|
|
4
4
|
import {
|
|
5
5
|
Router
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-YKRH6T5M.mjs";
|
|
7
7
|
import {
|
|
8
8
|
baseConfig
|
|
9
9
|
} from "./chunk-YOHL3T54.mjs";
|
|
@@ -75,6 +75,13 @@ var MikroServe = class {
|
|
|
75
75
|
this.router.patch(path, ...handlers);
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* @description Register a route that responds to any HTTP method.
|
|
80
|
+
*/
|
|
81
|
+
any(path, ...handlers) {
|
|
82
|
+
this.router.any(path, ...handlers);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
78
85
|
/**
|
|
79
86
|
* @description Register an OPTIONS route.
|
|
80
87
|
*/
|
|
@@ -5,64 +5,70 @@ var Router = class {
|
|
|
5
5
|
globalMiddlewares = [];
|
|
6
6
|
pathPatterns = /* @__PURE__ */ new Map();
|
|
7
7
|
/**
|
|
8
|
-
* Add a global middleware
|
|
8
|
+
* @description Add a global middleware.
|
|
9
9
|
*/
|
|
10
10
|
use(middleware) {
|
|
11
11
|
this.globalMiddlewares.push(middleware);
|
|
12
12
|
return this;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* Register a route
|
|
16
|
-
*/
|
|
17
|
-
register(method, path, handler, middlewares = []) {
|
|
18
|
-
this.routes.push({ method, path, handler, middlewares });
|
|
19
|
-
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Register a GET route
|
|
15
|
+
* @description Register a GET route.
|
|
24
16
|
*/
|
|
25
17
|
get(path, ...handlers) {
|
|
26
18
|
const handler = handlers.pop();
|
|
27
19
|
return this.register("GET", path, handler, handlers);
|
|
28
20
|
}
|
|
29
21
|
/**
|
|
30
|
-
* Register a POST route
|
|
22
|
+
* @description Register a POST route.
|
|
31
23
|
*/
|
|
32
24
|
post(path, ...handlers) {
|
|
33
25
|
const handler = handlers.pop();
|
|
34
26
|
return this.register("POST", path, handler, handlers);
|
|
35
27
|
}
|
|
36
28
|
/**
|
|
37
|
-
* Register a PUT route
|
|
29
|
+
* @description Register a PUT route.
|
|
38
30
|
*/
|
|
39
31
|
put(path, ...handlers) {
|
|
40
32
|
const handler = handlers.pop();
|
|
41
33
|
return this.register("PUT", path, handler, handlers);
|
|
42
34
|
}
|
|
43
35
|
/**
|
|
44
|
-
* Register a DELETE route
|
|
36
|
+
* @description Register a DELETE route.
|
|
45
37
|
*/
|
|
46
38
|
delete(path, ...handlers) {
|
|
47
39
|
const handler = handlers.pop();
|
|
48
40
|
return this.register("DELETE", path, handler, handlers);
|
|
49
41
|
}
|
|
50
42
|
/**
|
|
51
|
-
* Register a PATCH route
|
|
43
|
+
* @description Register a PATCH route.
|
|
52
44
|
*/
|
|
53
45
|
patch(path, ...handlers) {
|
|
54
46
|
const handler = handlers.pop();
|
|
55
47
|
return this.register("PATCH", path, handler, handlers);
|
|
56
48
|
}
|
|
57
49
|
/**
|
|
58
|
-
* Register
|
|
50
|
+
* @description Register a route for any HTTP method.
|
|
51
|
+
*/
|
|
52
|
+
any(path, ...handlers) {
|
|
53
|
+
const handler = handlers.pop();
|
|
54
|
+
const middlewares = handlers;
|
|
55
|
+
this.register("GET", path, handler, middlewares);
|
|
56
|
+
this.register("POST", path, handler, middlewares);
|
|
57
|
+
this.register("PUT", path, handler, middlewares);
|
|
58
|
+
this.register("DELETE", path, handler, middlewares);
|
|
59
|
+
this.register("PATCH", path, handler, middlewares);
|
|
60
|
+
this.register("OPTIONS", path, handler, middlewares);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @description Register an OPTIONS route.
|
|
59
65
|
*/
|
|
60
66
|
options(path, ...handlers) {
|
|
61
67
|
const handler = handlers.pop();
|
|
62
68
|
return this.register("OPTIONS", path, handler, handlers);
|
|
63
69
|
}
|
|
64
70
|
/**
|
|
65
|
-
* Match a request to a route
|
|
71
|
+
* @description Match a request to a route.
|
|
66
72
|
*/
|
|
67
73
|
match(method, path) {
|
|
68
74
|
for (const route of this.routes) {
|
|
@@ -80,22 +86,7 @@ var Router = class {
|
|
|
80
86
|
return null;
|
|
81
87
|
}
|
|
82
88
|
/**
|
|
83
|
-
*
|
|
84
|
-
*/
|
|
85
|
-
createPathPattern(path) {
|
|
86
|
-
const paramNames = [];
|
|
87
|
-
const pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
88
|
-
const paramName = match.slice(2);
|
|
89
|
-
paramNames.push(paramName);
|
|
90
|
-
return "/([^/]+)";
|
|
91
|
-
}).replace(/\/$/, "/?");
|
|
92
|
-
return {
|
|
93
|
-
pattern: new RegExp(`^${pattern}$`),
|
|
94
|
-
paramNames
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Handle a request and find the matching route
|
|
89
|
+
* @description Handle a request and find the matching route.
|
|
99
90
|
*/
|
|
100
91
|
async handle(req, res) {
|
|
101
92
|
const method = req.method || "GET";
|
|
@@ -118,7 +109,6 @@ var Router = class {
|
|
|
118
109
|
headers: req.headers,
|
|
119
110
|
path,
|
|
120
111
|
state: {},
|
|
121
|
-
// Add the missing state property
|
|
122
112
|
raw: () => res,
|
|
123
113
|
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
124
114
|
statusCode: status,
|
|
@@ -182,7 +172,6 @@ var Router = class {
|
|
|
182
172
|
headers: { "Content-Type": "text/html" }
|
|
183
173
|
}),
|
|
184
174
|
form: (content) => ({
|
|
185
|
-
// Make sure form method is included here
|
|
186
175
|
statusCode: code,
|
|
187
176
|
body: content,
|
|
188
177
|
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
@@ -200,7 +189,34 @@ var Router = class {
|
|
|
200
189
|
return this.executeMiddlewareChain(context, middlewares, route.handler);
|
|
201
190
|
}
|
|
202
191
|
/**
|
|
203
|
-
*
|
|
192
|
+
* @description Register a route with specified method.
|
|
193
|
+
*/
|
|
194
|
+
register(method, path, handler, middlewares = []) {
|
|
195
|
+
this.routes.push({ method, path, handler, middlewares });
|
|
196
|
+
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* @description Create a regex pattern for path matching.
|
|
201
|
+
*/
|
|
202
|
+
createPathPattern(path) {
|
|
203
|
+
const paramNames = [];
|
|
204
|
+
let pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
205
|
+
const paramName = match.slice(2);
|
|
206
|
+
paramNames.push(paramName);
|
|
207
|
+
return "/([^/]+)";
|
|
208
|
+
});
|
|
209
|
+
if (pattern.endsWith("/*")) {
|
|
210
|
+
pattern = `${pattern.slice(0, -2)}(?:/(.*))?`;
|
|
211
|
+
paramNames.push("wildcard");
|
|
212
|
+
} else pattern = pattern.replace(/\/$/, "/?");
|
|
213
|
+
return {
|
|
214
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
215
|
+
paramNames
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @description Execute middleware chain and final handler.
|
|
204
220
|
*/
|
|
205
221
|
async executeMiddlewareChain(context, middlewares, finalHandler) {
|
|
206
222
|
let currentIndex = 0;
|
package/lib/index.js
CHANGED
|
@@ -94,64 +94,70 @@ var Router = class {
|
|
|
94
94
|
globalMiddlewares = [];
|
|
95
95
|
pathPatterns = /* @__PURE__ */ new Map();
|
|
96
96
|
/**
|
|
97
|
-
* Add a global middleware
|
|
97
|
+
* @description Add a global middleware.
|
|
98
98
|
*/
|
|
99
99
|
use(middleware) {
|
|
100
100
|
this.globalMiddlewares.push(middleware);
|
|
101
101
|
return this;
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
104
|
-
* Register a route
|
|
105
|
-
*/
|
|
106
|
-
register(method, path, handler, middlewares = []) {
|
|
107
|
-
this.routes.push({ method, path, handler, middlewares });
|
|
108
|
-
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Register a GET route
|
|
104
|
+
* @description Register a GET route.
|
|
113
105
|
*/
|
|
114
106
|
get(path, ...handlers) {
|
|
115
107
|
const handler = handlers.pop();
|
|
116
108
|
return this.register("GET", path, handler, handlers);
|
|
117
109
|
}
|
|
118
110
|
/**
|
|
119
|
-
* Register a POST route
|
|
111
|
+
* @description Register a POST route.
|
|
120
112
|
*/
|
|
121
113
|
post(path, ...handlers) {
|
|
122
114
|
const handler = handlers.pop();
|
|
123
115
|
return this.register("POST", path, handler, handlers);
|
|
124
116
|
}
|
|
125
117
|
/**
|
|
126
|
-
* Register a PUT route
|
|
118
|
+
* @description Register a PUT route.
|
|
127
119
|
*/
|
|
128
120
|
put(path, ...handlers) {
|
|
129
121
|
const handler = handlers.pop();
|
|
130
122
|
return this.register("PUT", path, handler, handlers);
|
|
131
123
|
}
|
|
132
124
|
/**
|
|
133
|
-
* Register a DELETE route
|
|
125
|
+
* @description Register a DELETE route.
|
|
134
126
|
*/
|
|
135
127
|
delete(path, ...handlers) {
|
|
136
128
|
const handler = handlers.pop();
|
|
137
129
|
return this.register("DELETE", path, handler, handlers);
|
|
138
130
|
}
|
|
139
131
|
/**
|
|
140
|
-
* Register a PATCH route
|
|
132
|
+
* @description Register a PATCH route.
|
|
141
133
|
*/
|
|
142
134
|
patch(path, ...handlers) {
|
|
143
135
|
const handler = handlers.pop();
|
|
144
136
|
return this.register("PATCH", path, handler, handlers);
|
|
145
137
|
}
|
|
146
138
|
/**
|
|
147
|
-
* Register
|
|
139
|
+
* @description Register a route for any HTTP method.
|
|
140
|
+
*/
|
|
141
|
+
any(path, ...handlers) {
|
|
142
|
+
const handler = handlers.pop();
|
|
143
|
+
const middlewares = handlers;
|
|
144
|
+
this.register("GET", path, handler, middlewares);
|
|
145
|
+
this.register("POST", path, handler, middlewares);
|
|
146
|
+
this.register("PUT", path, handler, middlewares);
|
|
147
|
+
this.register("DELETE", path, handler, middlewares);
|
|
148
|
+
this.register("PATCH", path, handler, middlewares);
|
|
149
|
+
this.register("OPTIONS", path, handler, middlewares);
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @description Register an OPTIONS route.
|
|
148
154
|
*/
|
|
149
155
|
options(path, ...handlers) {
|
|
150
156
|
const handler = handlers.pop();
|
|
151
157
|
return this.register("OPTIONS", path, handler, handlers);
|
|
152
158
|
}
|
|
153
159
|
/**
|
|
154
|
-
* Match a request to a route
|
|
160
|
+
* @description Match a request to a route.
|
|
155
161
|
*/
|
|
156
162
|
match(method, path) {
|
|
157
163
|
for (const route of this.routes) {
|
|
@@ -169,22 +175,7 @@ var Router = class {
|
|
|
169
175
|
return null;
|
|
170
176
|
}
|
|
171
177
|
/**
|
|
172
|
-
*
|
|
173
|
-
*/
|
|
174
|
-
createPathPattern(path) {
|
|
175
|
-
const paramNames = [];
|
|
176
|
-
const pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
177
|
-
const paramName = match.slice(2);
|
|
178
|
-
paramNames.push(paramName);
|
|
179
|
-
return "/([^/]+)";
|
|
180
|
-
}).replace(/\/$/, "/?");
|
|
181
|
-
return {
|
|
182
|
-
pattern: new RegExp(`^${pattern}$`),
|
|
183
|
-
paramNames
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Handle a request and find the matching route
|
|
178
|
+
* @description Handle a request and find the matching route.
|
|
188
179
|
*/
|
|
189
180
|
async handle(req, res) {
|
|
190
181
|
const method = req.method || "GET";
|
|
@@ -207,7 +198,6 @@ var Router = class {
|
|
|
207
198
|
headers: req.headers,
|
|
208
199
|
path,
|
|
209
200
|
state: {},
|
|
210
|
-
// Add the missing state property
|
|
211
201
|
raw: () => res,
|
|
212
202
|
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
213
203
|
statusCode: status,
|
|
@@ -271,7 +261,6 @@ var Router = class {
|
|
|
271
261
|
headers: { "Content-Type": "text/html" }
|
|
272
262
|
}),
|
|
273
263
|
form: (content) => ({
|
|
274
|
-
// Make sure form method is included here
|
|
275
264
|
statusCode: code,
|
|
276
265
|
body: content,
|
|
277
266
|
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
@@ -289,7 +278,34 @@ var Router = class {
|
|
|
289
278
|
return this.executeMiddlewareChain(context, middlewares, route.handler);
|
|
290
279
|
}
|
|
291
280
|
/**
|
|
292
|
-
*
|
|
281
|
+
* @description Register a route with specified method.
|
|
282
|
+
*/
|
|
283
|
+
register(method, path, handler, middlewares = []) {
|
|
284
|
+
this.routes.push({ method, path, handler, middlewares });
|
|
285
|
+
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* @description Create a regex pattern for path matching.
|
|
290
|
+
*/
|
|
291
|
+
createPathPattern(path) {
|
|
292
|
+
const paramNames = [];
|
|
293
|
+
let pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
294
|
+
const paramName = match.slice(2);
|
|
295
|
+
paramNames.push(paramName);
|
|
296
|
+
return "/([^/]+)";
|
|
297
|
+
});
|
|
298
|
+
if (pattern.endsWith("/*")) {
|
|
299
|
+
pattern = `${pattern.slice(0, -2)}(?:/(.*))?`;
|
|
300
|
+
paramNames.push("wildcard");
|
|
301
|
+
} else pattern = pattern.replace(/\/$/, "/?");
|
|
302
|
+
return {
|
|
303
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
304
|
+
paramNames
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* @description Execute middleware chain and final handler.
|
|
293
309
|
*/
|
|
294
310
|
async executeMiddlewareChain(context, middlewares, finalHandler) {
|
|
295
311
|
let currentIndex = 0;
|
|
@@ -424,6 +440,13 @@ var MikroServe = class {
|
|
|
424
440
|
this.router.patch(path, ...handlers);
|
|
425
441
|
return this;
|
|
426
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* @description Register a route that responds to any HTTP method.
|
|
445
|
+
*/
|
|
446
|
+
any(path, ...handlers) {
|
|
447
|
+
this.router.any(path, ...handlers);
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
427
450
|
/**
|
|
428
451
|
* @description Register an OPTIONS route.
|
|
429
452
|
*/
|
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MikroServe
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-N5ZQZGGT.mjs";
|
|
4
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
5
|
-
import "./chunk-
|
|
5
|
+
import "./chunk-YKRH6T5M.mjs";
|
|
6
6
|
import "./chunk-YOHL3T54.mjs";
|
|
7
7
|
import "./chunk-JJX5XRNB.mjs";
|
|
8
8
|
export {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mikroserve",
|
|
3
3
|
"description": "Minimalistic, ready-to-use API, built on Node.js primitives.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"author": "Mikael Vesavuori",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"keywords": [
|
|
@@ -31,9 +31,6 @@
|
|
|
31
31
|
"!/lib/**/*.map",
|
|
32
32
|
"!/tests"
|
|
33
33
|
],
|
|
34
|
-
"bin": {
|
|
35
|
-
"mikroserve": "lib/index.js"
|
|
36
|
-
},
|
|
37
34
|
"scripts": {
|
|
38
35
|
"test": "npm run test:licenses && npm run test:types && npm run lint && npm run test:unit",
|
|
39
36
|
"test:types": "npx type-coverage --at-least 85 --strict --ignore-files \"tests/**/*.ts\" --ignore-files \"*.ts\" --ignore-files \"src/application/errors/*.ts\" --ignore-files \"testdata/*.ts\"",
|