clear-router 2.7.6 → 2.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ClearRequest.cjs +23 -0
- package/dist/ClearRequest.d.cts +28 -0
- package/dist/ClearRequest.d.mts +28 -0
- package/dist/ClearRequest.mjs +22 -0
- package/dist/Contracts.d.cts +12 -0
- package/dist/Contracts.d.mts +12 -0
- package/dist/Controller.cjs +12 -0
- package/dist/Controller.d.cts +14 -0
- package/dist/Controller.d.mts +14 -0
- package/dist/Controller.mjs +11 -0
- package/dist/{Request-DKXwa_W0.d.mts → Request-BhTJDR_5.d.mts} +5 -5
- package/dist/Route.cjs +58 -0
- package/dist/Route.d.cts +35 -0
- package/dist/Route.d.mts +35 -0
- package/dist/Route.mjs +57 -0
- package/dist/core/Request.cjs +35 -0
- package/dist/core/Request.d.cts +25 -0
- package/dist/core/Request.d.mts +25 -0
- package/dist/core/Request.mjs +35 -0
- package/dist/core/Response.cjs +59 -0
- package/dist/core/Response.d.cts +24 -0
- package/dist/core/Response.d.mts +24 -0
- package/dist/core/Response.mjs +58 -0
- package/dist/{bindings-CLsZjOEy.cjs → core/bindings.cjs} +10 -160
- package/dist/{bindings-CNL7bpz5.d.mts → core/bindings.d.cts} +1 -1
- package/dist/{bindings-CxvtC8XS.d.cts → core/bindings.d.mts} +1 -1
- package/dist/{bindings-XLDXFpHZ.mjs → core/bindings.mjs} +3 -110
- package/dist/core/index.cjs +4 -20
- package/dist/core/index.d.cts +4 -1
- package/dist/core/index.d.mts +4 -1
- package/dist/core/index.mjs +4 -14
- package/dist/core/plugins.cjs +14 -0
- package/dist/core/plugins.d.cts +109 -0
- package/dist/core/plugins.d.mts +109 -0
- package/dist/core/plugins.mjs +13 -0
- package/dist/{responses-Bvnk0uvc.cjs → core/responses.cjs} +5 -20
- package/dist/{responses-BvETUeDL.mjs → core/responses.mjs} +2 -2
- package/dist/{router-C6W-k6sS.cjs → core/router.cjs} +11 -69
- package/dist/core/router.d.cts +274 -0
- package/dist/core/router.d.mts +274 -0
- package/dist/{router-Dc9w86Wn.mjs → core/router.mjs} +5 -58
- package/dist/decorators/index.cjs +1 -1
- package/dist/decorators/index.d.cts +1 -1
- package/dist/decorators/index.d.mts +1 -1
- package/dist/decorators/index.mjs +1 -1
- package/dist/decorators/setup.cjs +2 -2
- package/dist/decorators/setup.d.cts +1 -1
- package/dist/decorators/setup.d.mts +1 -1
- package/dist/decorators/setup.mjs +2 -2
- package/dist/express/index.cjs +2 -265
- package/dist/express/index.d.cts +1 -127
- package/dist/express/index.d.mts +1 -127
- package/dist/express/index.mjs +1 -264
- package/dist/express/router.cjs +265 -0
- package/dist/express/router.d.cts +131 -0
- package/dist/express/router.d.mts +131 -0
- package/dist/express/router.mjs +265 -0
- package/dist/fastify/index.cjs +2 -254
- package/dist/fastify/index.d.cts +1 -125
- package/dist/fastify/index.d.mts +1 -125
- package/dist/fastify/index.mjs +1 -253
- package/dist/fastify/router.cjs +254 -0
- package/dist/fastify/router.d.cts +129 -0
- package/dist/fastify/router.d.mts +129 -0
- package/dist/fastify/router.mjs +254 -0
- package/dist/h3/index.cjs +2 -260
- package/dist/h3/index.d.cts +1 -128
- package/dist/h3/index.d.mts +1 -128
- package/dist/h3/index.mjs +1 -259
- package/dist/h3/router.cjs +260 -0
- package/dist/h3/router.d.cts +132 -0
- package/dist/h3/router.d.mts +132 -0
- package/dist/h3/router.mjs +260 -0
- package/dist/hono/index.cjs +2 -251
- package/dist/hono/index.d.cts +1 -130
- package/dist/hono/index.d.mts +1 -130
- package/dist/hono/index.mjs +1 -250
- package/dist/hono/router.cjs +251 -0
- package/dist/hono/router.d.cts +134 -0
- package/dist/hono/router.d.mts +134 -0
- package/dist/hono/router.mjs +251 -0
- package/dist/index.cjs +16 -1097
- package/dist/index.d.cts +9 -563
- package/dist/index.d.mts +9 -563
- package/dist/index.mjs +8 -1089
- package/dist/koa/index.cjs +2 -261
- package/dist/koa/index.d.cts +1 -131
- package/dist/koa/index.d.mts +1 -131
- package/dist/koa/index.mjs +1 -260
- package/dist/koa/router.cjs +261 -0
- package/dist/koa/router.d.cts +135 -0
- package/dist/koa/router.d.mts +135 -0
- package/dist/koa/router.mjs +261 -0
- package/dist/types/basic.d.cts +43 -0
- package/dist/types/express.d.cts +44 -0
- package/dist/types/express.d.mts +4 -2
- package/dist/types/fastify.d.cts +23 -0
- package/dist/types/fastify.d.mts +4 -2
- package/dist/types/h3.d.cts +41 -0
- package/dist/types/h3.d.mts +4 -4
- package/dist/types/hono.d.cts +21 -0
- package/dist/types/hono.d.mts +4 -3
- package/dist/types/koa.d.cts +25 -0
- package/dist/types/koa.d.mts +4 -3
- package/package.json +1 -1
- package/dist/router-BEgAxp5A.d.cts +0 -649
- package/dist/router-DKKYx23P.d.mts +0 -649
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { CoreRouter } from "../core/router.mjs";
|
|
2
|
+
import { isFetchResponse, resolveResponseMeta, responseWasSent } from "../core/responses.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/express/router.ts
|
|
5
|
+
/**
|
|
6
|
+
* @class clear-router Express Router
|
|
7
|
+
* @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
|
|
8
|
+
* @author Refkinscallv
|
|
9
|
+
* @author 3m1n3nc3
|
|
10
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
|
+
*/
|
|
12
|
+
var Router = class Router extends CoreRouter {
|
|
13
|
+
static routerStateNamespace = "clear-router:express";
|
|
14
|
+
static ensureRequestBodyAccessor(req) {
|
|
15
|
+
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
16
|
+
}
|
|
17
|
+
static async sendReturnValue(ctx, value, method, path) {
|
|
18
|
+
const { req, res } = ctx;
|
|
19
|
+
if (responseWasSent(res) || value === res || responseWasSent(value)) return;
|
|
20
|
+
const meta = resolveResponseMeta(value, {
|
|
21
|
+
headers: req.headers,
|
|
22
|
+
method,
|
|
23
|
+
path
|
|
24
|
+
});
|
|
25
|
+
if (!meta) return;
|
|
26
|
+
res.status(meta.status);
|
|
27
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
28
|
+
res.setHeader(key, headerValue);
|
|
29
|
+
});
|
|
30
|
+
if (isFetchResponse(meta.body)) {
|
|
31
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
32
|
+
res.setHeader(key, headerValue);
|
|
33
|
+
});
|
|
34
|
+
res.status(meta.body.status);
|
|
35
|
+
const body = Buffer.from(await meta.body.arrayBuffer());
|
|
36
|
+
res.send(body);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (meta.contentType && !res.getHeader("Content-Type")) res.setHeader("Content-Type", meta.contentType);
|
|
40
|
+
if (meta.isEmpty) {
|
|
41
|
+
res.sendStatus(meta.status);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
res.send(meta.body);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Adds a new route to the router.
|
|
48
|
+
*
|
|
49
|
+
* @param methods
|
|
50
|
+
* @param path
|
|
51
|
+
* @param handler
|
|
52
|
+
* @param middlewares
|
|
53
|
+
*/
|
|
54
|
+
static add(methods, path, handler, middlewares) {
|
|
55
|
+
return super.add(methods, path, handler, middlewares);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
59
|
+
*
|
|
60
|
+
* @param basePath
|
|
61
|
+
* @param controller
|
|
62
|
+
* @param options
|
|
63
|
+
*/
|
|
64
|
+
static apiResource(basePath, controller, options) {
|
|
65
|
+
super.apiResource(basePath, controller, options);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Adds a new GET route to the router.
|
|
69
|
+
*
|
|
70
|
+
* @param path
|
|
71
|
+
* @param handler
|
|
72
|
+
* @param middlewares
|
|
73
|
+
*/
|
|
74
|
+
static get(path, handler, middlewares) {
|
|
75
|
+
return super.get(path, handler, middlewares);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds a new POST route to the router.
|
|
79
|
+
*
|
|
80
|
+
* @param path
|
|
81
|
+
* @param handler
|
|
82
|
+
* @param middlewares
|
|
83
|
+
*/
|
|
84
|
+
static post(path, handler, middlewares) {
|
|
85
|
+
return super.post(path, handler, middlewares);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Adds a new PUT route to the router.
|
|
89
|
+
*
|
|
90
|
+
* @param path
|
|
91
|
+
* @param handler
|
|
92
|
+
* @param middlewares
|
|
93
|
+
*/
|
|
94
|
+
static put(path, handler, middlewares) {
|
|
95
|
+
return super.put(path, handler, middlewares);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Adds a new DELETE route to the router.
|
|
99
|
+
*
|
|
100
|
+
* @param path
|
|
101
|
+
* @param handler
|
|
102
|
+
* @param middlewares
|
|
103
|
+
*/
|
|
104
|
+
static delete(path, handler, middlewares) {
|
|
105
|
+
return super.delete(path, handler, middlewares);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Adds a new PATCH route to the router.
|
|
109
|
+
*
|
|
110
|
+
* @param path
|
|
111
|
+
* @param handler
|
|
112
|
+
* @param middlewares
|
|
113
|
+
*/
|
|
114
|
+
static patch(path, handler, middlewares) {
|
|
115
|
+
return super.patch(path, handler, middlewares);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds a new OPTIONS route to the router.
|
|
119
|
+
*
|
|
120
|
+
* @param path
|
|
121
|
+
* @param handler
|
|
122
|
+
* @param middlewares
|
|
123
|
+
*/
|
|
124
|
+
static options(path, handler, middlewares) {
|
|
125
|
+
return super.options(path, handler, middlewares);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Adds a new HEAD route to the router.
|
|
129
|
+
*
|
|
130
|
+
* @param path
|
|
131
|
+
* @param handler
|
|
132
|
+
* @param middlewares
|
|
133
|
+
*/
|
|
134
|
+
static head(path, handler, middlewares) {
|
|
135
|
+
return super.head(path, handler, middlewares);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Defines a group of routes with a common prefix.
|
|
139
|
+
*
|
|
140
|
+
* @param prefix
|
|
141
|
+
* @param callback
|
|
142
|
+
* @param middlewares
|
|
143
|
+
*/
|
|
144
|
+
static async group(prefix, callback, middlewares) {
|
|
145
|
+
await super.group(prefix, callback, middlewares);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Adds global middlewares to the router, which will be applied to all routes.
|
|
149
|
+
*
|
|
150
|
+
* @param middlewares
|
|
151
|
+
* @param callback
|
|
152
|
+
*/
|
|
153
|
+
static middleware(middlewares, callback) {
|
|
154
|
+
super.middleware(middlewares, callback);
|
|
155
|
+
}
|
|
156
|
+
static allRoutes(type) {
|
|
157
|
+
return super.allRoutes(type);
|
|
158
|
+
}
|
|
159
|
+
static route(name) {
|
|
160
|
+
return super.route(name);
|
|
161
|
+
}
|
|
162
|
+
static async apply(router) {
|
|
163
|
+
for (const route of Array.from(this.routes)) {
|
|
164
|
+
let handlerFunction = null;
|
|
165
|
+
let instance = null;
|
|
166
|
+
let bindingTarget;
|
|
167
|
+
let bindingMethod;
|
|
168
|
+
let bindingHandler;
|
|
169
|
+
let bindingMetadata;
|
|
170
|
+
try {
|
|
171
|
+
const resolved = this.resolveHandler(route);
|
|
172
|
+
handlerFunction = resolved.handlerFunction;
|
|
173
|
+
instance = resolved.instance;
|
|
174
|
+
bindingTarget = resolved.bindingTarget;
|
|
175
|
+
bindingMethod = resolved.bindingMethod;
|
|
176
|
+
bindingHandler = resolved.bindingHandler;
|
|
177
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
if (!handlerFunction) continue;
|
|
183
|
+
for (const method of route.methods) {
|
|
184
|
+
const allowedMethods = [
|
|
185
|
+
"get",
|
|
186
|
+
"post",
|
|
187
|
+
"put",
|
|
188
|
+
"delete",
|
|
189
|
+
"patch",
|
|
190
|
+
"options",
|
|
191
|
+
"head"
|
|
192
|
+
];
|
|
193
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
194
|
+
if (!allowedMethods.includes(method)) {
|
|
195
|
+
const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
196
|
+
console.error("[ROUTES]", error.message);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
for (const registrationPath of route.registrationPaths) router[method](registrationPath, (req, _res, next) => {
|
|
200
|
+
Router.ensureRequestBodyAccessor(req);
|
|
201
|
+
const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
|
|
202
|
+
if (method === "post" && override && override !== "post") return next("route");
|
|
203
|
+
return next();
|
|
204
|
+
}, ...route.middlewares || [], async (req, res, next) => {
|
|
205
|
+
try {
|
|
206
|
+
Router.ensureRequestBodyAccessor(req);
|
|
207
|
+
const ctx = {
|
|
208
|
+
req,
|
|
209
|
+
res,
|
|
210
|
+
next
|
|
211
|
+
};
|
|
212
|
+
const inst = instance ?? route;
|
|
213
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
214
|
+
body: ctx.req.getBody(),
|
|
215
|
+
query: ctx.req.query,
|
|
216
|
+
params: ctx.req.params,
|
|
217
|
+
method
|
|
218
|
+
});
|
|
219
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
220
|
+
const resolved = await Promise.resolve(result);
|
|
221
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
222
|
+
await Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
next(error);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
if ([
|
|
228
|
+
"put",
|
|
229
|
+
"patch",
|
|
230
|
+
"delete"
|
|
231
|
+
].includes(method)) for (const registrationPath of route.registrationPaths) router.post(registrationPath, (req, _res, next) => {
|
|
232
|
+
Router.ensureRequestBodyAccessor(req);
|
|
233
|
+
if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return next("route");
|
|
234
|
+
req.method = method.toUpperCase();
|
|
235
|
+
return next();
|
|
236
|
+
}, ...route.middlewares || [], async (req, res, next) => {
|
|
237
|
+
try {
|
|
238
|
+
Router.ensureRequestBodyAccessor(req);
|
|
239
|
+
const ctx = {
|
|
240
|
+
req,
|
|
241
|
+
res,
|
|
242
|
+
next
|
|
243
|
+
};
|
|
244
|
+
const inst = instance ?? route;
|
|
245
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
246
|
+
body: ctx.req.getBody(),
|
|
247
|
+
query: ctx.req.query,
|
|
248
|
+
params: ctx.req.params,
|
|
249
|
+
method
|
|
250
|
+
});
|
|
251
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
252
|
+
const resolved = await Promise.resolve(result);
|
|
253
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
254
|
+
await Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
next(error);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
//#endregion
|
|
265
|
+
export { Router as default };
|
package/dist/fastify/index.cjs
CHANGED
|
@@ -1,256 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
require('
|
|
3
|
-
const require_router = require('../router-C6W-k6sS.cjs');
|
|
4
|
-
const require_responses = require('../responses-Bvnk0uvc.cjs');
|
|
2
|
+
const require_router = require('./router.cjs');
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @class clear-router Fastify Router
|
|
9
|
-
* @description Laravel-style routing system for Fastify using shared clear-router core
|
|
10
|
-
* @author 3m1n3nc3
|
|
11
|
-
* @repository https://github.com/arkstack-tmp/clear-router
|
|
12
|
-
*/
|
|
13
|
-
var Router = class Router extends require_router.CoreRouter {
|
|
14
|
-
static routerStateNamespace = "clear-router:fastify";
|
|
15
|
-
static ensureRequestBodyAccessor(req) {
|
|
16
|
-
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
17
|
-
}
|
|
18
|
-
static async sendReturnValue(req, reply, value, method, path) {
|
|
19
|
-
if (require_responses.responseWasSent(reply) || value === reply || require_responses.responseWasSent(value)) return value;
|
|
20
|
-
const meta = require_responses.resolveResponseMeta(value, {
|
|
21
|
-
headers: req.headers,
|
|
22
|
-
method,
|
|
23
|
-
path
|
|
24
|
-
});
|
|
25
|
-
if (!meta) return void 0;
|
|
26
|
-
reply.code(meta.status);
|
|
27
|
-
meta.headers?.forEach((headerValue, key) => {
|
|
28
|
-
reply.header(key, headerValue);
|
|
29
|
-
});
|
|
30
|
-
if (require_responses.isFetchResponse(meta.body)) {
|
|
31
|
-
meta.body.headers.forEach((headerValue, key) => {
|
|
32
|
-
reply.header(key, headerValue);
|
|
33
|
-
});
|
|
34
|
-
reply.code(meta.body.status);
|
|
35
|
-
return reply.send(Buffer.from(await meta.body.arrayBuffer()));
|
|
36
|
-
}
|
|
37
|
-
if (meta.contentType) reply.type(meta.contentType);
|
|
38
|
-
if (meta.isEmpty) return reply.send();
|
|
39
|
-
return reply.send(meta.body);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Add a route to the router
|
|
43
|
-
*
|
|
44
|
-
* @param methods HTTP methods for the route
|
|
45
|
-
* @param path Route path
|
|
46
|
-
* @param handler Route handler function
|
|
47
|
-
* @param middlewares Optional middlewares for the route
|
|
48
|
-
*/
|
|
49
|
-
static add(methods, path, handler, middlewares) {
|
|
50
|
-
return super.add(methods, path, handler, middlewares);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Define a resourceful API controller with standard CRUD routes
|
|
54
|
-
*
|
|
55
|
-
* @param basePath Base path for the resource
|
|
56
|
-
* @param controller Controller class or instance
|
|
57
|
-
* @param options Optional configuration for the resource
|
|
58
|
-
*/
|
|
59
|
-
static apiResource(basePath, controller, options) {
|
|
60
|
-
super.apiResource(basePath, controller, options);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Define a GET route
|
|
64
|
-
*
|
|
65
|
-
* @param path
|
|
66
|
-
* @param handler
|
|
67
|
-
* @param middlewares
|
|
68
|
-
*/
|
|
69
|
-
static get(path, handler, middlewares) {
|
|
70
|
-
return super.get(path, handler, middlewares);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Define a POST route
|
|
74
|
-
*
|
|
75
|
-
* @param path
|
|
76
|
-
* @param handler
|
|
77
|
-
* @param middlewares
|
|
78
|
-
*/
|
|
79
|
-
static post(path, handler, middlewares) {
|
|
80
|
-
return super.post(path, handler, middlewares);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Define a PUT route
|
|
84
|
-
*
|
|
85
|
-
* @param path
|
|
86
|
-
* @param handler
|
|
87
|
-
* @param middlewares
|
|
88
|
-
*/
|
|
89
|
-
static put(path, handler, middlewares) {
|
|
90
|
-
return super.put(path, handler, middlewares);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Define a DELETE route
|
|
94
|
-
*
|
|
95
|
-
* @param path
|
|
96
|
-
* @param handler
|
|
97
|
-
* @param middlewares
|
|
98
|
-
*/
|
|
99
|
-
static delete(path, handler, middlewares) {
|
|
100
|
-
return super.delete(path, handler, middlewares);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Define a PATCH route
|
|
104
|
-
*
|
|
105
|
-
* @param path
|
|
106
|
-
* @param handler
|
|
107
|
-
* @param middlewares
|
|
108
|
-
*/
|
|
109
|
-
static patch(path, handler, middlewares) {
|
|
110
|
-
return super.patch(path, handler, middlewares);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Define an OPTIONS route
|
|
114
|
-
*
|
|
115
|
-
* @param path
|
|
116
|
-
* @param handler
|
|
117
|
-
* @param middlewares
|
|
118
|
-
*/
|
|
119
|
-
static options(path, handler, middlewares) {
|
|
120
|
-
return super.options(path, handler, middlewares);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Define a HEAD route
|
|
124
|
-
*
|
|
125
|
-
* @param path
|
|
126
|
-
* @param handler
|
|
127
|
-
* @param middlewares
|
|
128
|
-
*/
|
|
129
|
-
static head(path, handler, middlewares) {
|
|
130
|
-
return super.head(path, handler, middlewares);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Define a group of routes with a common prefix and optional middlewares
|
|
134
|
-
*
|
|
135
|
-
* @param prefix
|
|
136
|
-
* @param callback
|
|
137
|
-
* @param middlewares
|
|
138
|
-
*/
|
|
139
|
-
static async group(prefix, callback, middlewares) {
|
|
140
|
-
await super.group(prefix, callback, middlewares);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Apply middlewares to a group of routes defined within the callback
|
|
144
|
-
*
|
|
145
|
-
* @param middlewares
|
|
146
|
-
* @param callback
|
|
147
|
-
*/
|
|
148
|
-
static middleware(middlewares, callback) {
|
|
149
|
-
super.middleware(middlewares, callback);
|
|
150
|
-
}
|
|
151
|
-
static allRoutes(type) {
|
|
152
|
-
return super.allRoutes(type);
|
|
153
|
-
}
|
|
154
|
-
static route(name) {
|
|
155
|
-
return super.route(name);
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Apply the defined routes to a Fastify application instance
|
|
159
|
-
*
|
|
160
|
-
* @param app - The Fastify application instance
|
|
161
|
-
* @returns The Fastify application instance with the applied routes
|
|
162
|
-
*/
|
|
163
|
-
static apply(app) {
|
|
164
|
-
for (const route of Array.from(this.routes)) {
|
|
165
|
-
let handlerFunction = null;
|
|
166
|
-
let instance = null;
|
|
167
|
-
let bindingTarget;
|
|
168
|
-
let bindingMethod;
|
|
169
|
-
let bindingHandler;
|
|
170
|
-
let bindingMetadata;
|
|
171
|
-
try {
|
|
172
|
-
const resolved = this.resolveHandler(route);
|
|
173
|
-
handlerFunction = resolved.handlerFunction;
|
|
174
|
-
instance = resolved.instance;
|
|
175
|
-
bindingTarget = resolved.bindingTarget;
|
|
176
|
-
bindingMethod = resolved.bindingMethod;
|
|
177
|
-
bindingHandler = resolved.bindingHandler;
|
|
178
|
-
bindingMetadata = resolved.bindingMetadata;
|
|
179
|
-
} catch (error) {
|
|
180
|
-
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
181
|
-
throw error;
|
|
182
|
-
}
|
|
183
|
-
if (!handlerFunction) continue;
|
|
184
|
-
for (const method of route.methods) {
|
|
185
|
-
const allowedMethods = [
|
|
186
|
-
"get",
|
|
187
|
-
"post",
|
|
188
|
-
"put",
|
|
189
|
-
"delete",
|
|
190
|
-
"patch",
|
|
191
|
-
"options",
|
|
192
|
-
"head"
|
|
193
|
-
];
|
|
194
|
-
if (method === "options" && route.methods.length > 1) continue;
|
|
195
|
-
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
196
|
-
for (const registrationPath of route.registrationPaths) app.route({
|
|
197
|
-
method: method.toUpperCase(),
|
|
198
|
-
url: registrationPath,
|
|
199
|
-
preHandler: route.middlewares,
|
|
200
|
-
handler: async (req, reply) => {
|
|
201
|
-
Router.ensureRequestBodyAccessor(req);
|
|
202
|
-
const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
|
|
203
|
-
if (method === "post" && override && override !== "post") return reply.code(404).send();
|
|
204
|
-
const ctx = {
|
|
205
|
-
req,
|
|
206
|
-
reply
|
|
207
|
-
};
|
|
208
|
-
const inst = instance ?? route;
|
|
209
|
-
Router.bindRequestToInstance(ctx, inst, route, {
|
|
210
|
-
body: ctx.req.getBody(),
|
|
211
|
-
query: ctx.req.query ?? {},
|
|
212
|
-
params: ctx.req.params ?? {},
|
|
213
|
-
method
|
|
214
|
-
});
|
|
215
|
-
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
216
|
-
const resolved = await Promise.resolve(result);
|
|
217
|
-
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
218
|
-
return Router.sendReturnValue(req, reply, outgoing, method, route.path);
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
if ([
|
|
222
|
-
"put",
|
|
223
|
-
"patch",
|
|
224
|
-
"delete"
|
|
225
|
-
].includes(method)) for (const registrationPath of route.registrationPaths) app.route({
|
|
226
|
-
method: "POST",
|
|
227
|
-
url: registrationPath,
|
|
228
|
-
preHandler: route.middlewares,
|
|
229
|
-
handler: async (req, reply) => {
|
|
230
|
-
Router.ensureRequestBodyAccessor(req);
|
|
231
|
-
if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return reply.code(404).send();
|
|
232
|
-
const ctx = {
|
|
233
|
-
req,
|
|
234
|
-
reply
|
|
235
|
-
};
|
|
236
|
-
const inst = instance ?? route;
|
|
237
|
-
Router.bindRequestToInstance(ctx, inst, route, {
|
|
238
|
-
body: ctx.req.getBody(),
|
|
239
|
-
query: ctx.req.query ?? {},
|
|
240
|
-
params: ctx.req.params ?? {},
|
|
241
|
-
method
|
|
242
|
-
});
|
|
243
|
-
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
244
|
-
const resolved = await Promise.resolve(result);
|
|
245
|
-
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
246
|
-
return Router.sendReturnValue(req, reply, outgoing, method, route.path);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return app;
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
//#endregion
|
|
256
|
-
exports.Router = Router;
|
|
4
|
+
exports.Router = require_router;
|
package/dist/fastify/index.d.cts
CHANGED
|
@@ -1,126 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/fastify/router.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* @class clear-router Fastify Router
|
|
6
|
-
* @description Laravel-style routing system for Fastify using shared clear-router core
|
|
7
|
-
* @author 3m1n3nc3
|
|
8
|
-
* @repository https://github.com/arkstack-tmp/clear-router
|
|
9
|
-
*/
|
|
10
|
-
declare class Router extends CoreRouter {
|
|
11
|
-
protected static routerStateNamespace: string;
|
|
12
|
-
private static ensureRequestBodyAccessor;
|
|
13
|
-
private static sendReturnValue;
|
|
14
|
-
/**
|
|
15
|
-
* Add a route to the router
|
|
16
|
-
*
|
|
17
|
-
* @param methods HTTP methods for the route
|
|
18
|
-
* @param path Route path
|
|
19
|
-
* @param handler Route handler function
|
|
20
|
-
* @param middlewares Optional middlewares for the route
|
|
21
|
-
*/
|
|
22
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
23
|
-
/**
|
|
24
|
-
* Define a resourceful API controller with standard CRUD routes
|
|
25
|
-
*
|
|
26
|
-
* @param basePath Base path for the resource
|
|
27
|
-
* @param controller Controller class or instance
|
|
28
|
-
* @param options Optional configuration for the resource
|
|
29
|
-
*/
|
|
30
|
-
static apiResource(basePath: string, controller: any, options?: {
|
|
31
|
-
only?: ControllerAction[];
|
|
32
|
-
except?: ControllerAction[];
|
|
33
|
-
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
34
|
-
}): void;
|
|
35
|
-
/**
|
|
36
|
-
* Define a GET route
|
|
37
|
-
*
|
|
38
|
-
* @param path
|
|
39
|
-
* @param handler
|
|
40
|
-
* @param middlewares
|
|
41
|
-
*/
|
|
42
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
43
|
-
/**
|
|
44
|
-
* Define a POST route
|
|
45
|
-
*
|
|
46
|
-
* @param path
|
|
47
|
-
* @param handler
|
|
48
|
-
* @param middlewares
|
|
49
|
-
*/
|
|
50
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
51
|
-
/**
|
|
52
|
-
* Define a PUT route
|
|
53
|
-
*
|
|
54
|
-
* @param path
|
|
55
|
-
* @param handler
|
|
56
|
-
* @param middlewares
|
|
57
|
-
*/
|
|
58
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
59
|
-
/**
|
|
60
|
-
* Define a DELETE route
|
|
61
|
-
*
|
|
62
|
-
* @param path
|
|
63
|
-
* @param handler
|
|
64
|
-
* @param middlewares
|
|
65
|
-
*/
|
|
66
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
67
|
-
/**
|
|
68
|
-
* Define a PATCH route
|
|
69
|
-
*
|
|
70
|
-
* @param path
|
|
71
|
-
* @param handler
|
|
72
|
-
* @param middlewares
|
|
73
|
-
*/
|
|
74
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
75
|
-
/**
|
|
76
|
-
* Define an OPTIONS route
|
|
77
|
-
*
|
|
78
|
-
* @param path
|
|
79
|
-
* @param handler
|
|
80
|
-
* @param middlewares
|
|
81
|
-
*/
|
|
82
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
83
|
-
/**
|
|
84
|
-
* Define a HEAD route
|
|
85
|
-
*
|
|
86
|
-
* @param path
|
|
87
|
-
* @param handler
|
|
88
|
-
* @param middlewares
|
|
89
|
-
*/
|
|
90
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
91
|
-
/**
|
|
92
|
-
* Define a group of routes with a common prefix and optional middlewares
|
|
93
|
-
*
|
|
94
|
-
* @param prefix
|
|
95
|
-
* @param callback
|
|
96
|
-
* @param middlewares
|
|
97
|
-
*/
|
|
98
|
-
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
99
|
-
/**
|
|
100
|
-
* Apply middlewares to a group of routes defined within the callback
|
|
101
|
-
*
|
|
102
|
-
* @param middlewares
|
|
103
|
-
* @param callback
|
|
104
|
-
*/
|
|
105
|
-
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
106
|
-
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
107
|
-
/**
|
|
108
|
-
* @param type - 'path' to get routes organized by path
|
|
109
|
-
*/
|
|
110
|
-
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
111
|
-
/**
|
|
112
|
-
* @param type - 'method' to get routes organized by method
|
|
113
|
-
*/
|
|
114
|
-
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
115
|
-
static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
116
|
-
static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
|
|
117
|
-
/**
|
|
118
|
-
* Apply the defined routes to a Fastify application instance
|
|
119
|
-
*
|
|
120
|
-
* @param app - The Fastify application instance
|
|
121
|
-
* @returns The Fastify application instance with the applied routes
|
|
122
|
-
*/
|
|
123
|
-
static apply(app: FastifyApp): FastifyApp;
|
|
124
|
-
}
|
|
125
|
-
//#endregion
|
|
1
|
+
import { Router } from "./router.cjs";
|
|
126
2
|
export { Router };
|