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