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.
Files changed (107) hide show
  1. package/dist/ClearRequest.cjs +23 -0
  2. package/dist/ClearRequest.d.cts +28 -0
  3. package/dist/ClearRequest.d.mts +28 -0
  4. package/dist/ClearRequest.mjs +22 -0
  5. package/dist/Contracts.d.cts +12 -0
  6. package/dist/Contracts.d.mts +12 -0
  7. package/dist/Controller.cjs +12 -0
  8. package/dist/Controller.d.cts +14 -0
  9. package/dist/Controller.d.mts +14 -0
  10. package/dist/Controller.mjs +11 -0
  11. package/dist/{Request-DKXwa_W0.d.mts → Request-BhTJDR_5.d.mts} +5 -5
  12. package/dist/Route.cjs +58 -0
  13. package/dist/Route.d.cts +35 -0
  14. package/dist/Route.d.mts +35 -0
  15. package/dist/Route.mjs +57 -0
  16. package/dist/core/Request.cjs +35 -0
  17. package/dist/core/Request.d.cts +25 -0
  18. package/dist/core/Request.d.mts +25 -0
  19. package/dist/core/Request.mjs +35 -0
  20. package/dist/core/Response.cjs +59 -0
  21. package/dist/core/Response.d.cts +24 -0
  22. package/dist/core/Response.d.mts +24 -0
  23. package/dist/core/Response.mjs +58 -0
  24. package/dist/{bindings-CLsZjOEy.cjs → core/bindings.cjs} +10 -160
  25. package/dist/{bindings-CNL7bpz5.d.mts → core/bindings.d.cts} +1 -1
  26. package/dist/{bindings-CxvtC8XS.d.cts → core/bindings.d.mts} +1 -1
  27. package/dist/{bindings-XLDXFpHZ.mjs → core/bindings.mjs} +3 -110
  28. package/dist/core/index.cjs +4 -20
  29. package/dist/core/index.d.cts +4 -1
  30. package/dist/core/index.d.mts +4 -1
  31. package/dist/core/index.mjs +4 -14
  32. package/dist/core/plugins.cjs +14 -0
  33. package/dist/core/plugins.d.cts +109 -0
  34. package/dist/core/plugins.d.mts +109 -0
  35. package/dist/core/plugins.mjs +13 -0
  36. package/dist/{responses-Bvnk0uvc.cjs → core/responses.cjs} +5 -20
  37. package/dist/{responses-BvETUeDL.mjs → core/responses.mjs} +2 -2
  38. package/dist/{router-C6W-k6sS.cjs → core/router.cjs} +11 -69
  39. package/dist/core/router.d.cts +274 -0
  40. package/dist/core/router.d.mts +274 -0
  41. package/dist/{router-Dc9w86Wn.mjs → core/router.mjs} +5 -58
  42. package/dist/decorators/index.cjs +1 -1
  43. package/dist/decorators/index.d.cts +1 -1
  44. package/dist/decorators/index.d.mts +1 -1
  45. package/dist/decorators/index.mjs +1 -1
  46. package/dist/decorators/setup.cjs +2 -2
  47. package/dist/decorators/setup.d.cts +1 -1
  48. package/dist/decorators/setup.d.mts +1 -1
  49. package/dist/decorators/setup.mjs +2 -2
  50. package/dist/express/index.cjs +2 -265
  51. package/dist/express/index.d.cts +1 -127
  52. package/dist/express/index.d.mts +1 -127
  53. package/dist/express/index.mjs +1 -264
  54. package/dist/express/router.cjs +265 -0
  55. package/dist/express/router.d.cts +131 -0
  56. package/dist/express/router.d.mts +131 -0
  57. package/dist/express/router.mjs +265 -0
  58. package/dist/fastify/index.cjs +2 -254
  59. package/dist/fastify/index.d.cts +1 -125
  60. package/dist/fastify/index.d.mts +1 -125
  61. package/dist/fastify/index.mjs +1 -253
  62. package/dist/fastify/router.cjs +254 -0
  63. package/dist/fastify/router.d.cts +129 -0
  64. package/dist/fastify/router.d.mts +129 -0
  65. package/dist/fastify/router.mjs +254 -0
  66. package/dist/h3/index.cjs +2 -260
  67. package/dist/h3/index.d.cts +1 -128
  68. package/dist/h3/index.d.mts +1 -128
  69. package/dist/h3/index.mjs +1 -259
  70. package/dist/h3/router.cjs +260 -0
  71. package/dist/h3/router.d.cts +132 -0
  72. package/dist/h3/router.d.mts +132 -0
  73. package/dist/h3/router.mjs +260 -0
  74. package/dist/hono/index.cjs +2 -251
  75. package/dist/hono/index.d.cts +1 -130
  76. package/dist/hono/index.d.mts +1 -130
  77. package/dist/hono/index.mjs +1 -250
  78. package/dist/hono/router.cjs +251 -0
  79. package/dist/hono/router.d.cts +134 -0
  80. package/dist/hono/router.d.mts +134 -0
  81. package/dist/hono/router.mjs +251 -0
  82. package/dist/index.cjs +16 -1097
  83. package/dist/index.d.cts +9 -563
  84. package/dist/index.d.mts +9 -563
  85. package/dist/index.mjs +8 -1089
  86. package/dist/koa/index.cjs +2 -261
  87. package/dist/koa/index.d.cts +1 -131
  88. package/dist/koa/index.d.mts +1 -131
  89. package/dist/koa/index.mjs +1 -260
  90. package/dist/koa/router.cjs +261 -0
  91. package/dist/koa/router.d.cts +135 -0
  92. package/dist/koa/router.d.mts +135 -0
  93. package/dist/koa/router.mjs +261 -0
  94. package/dist/types/basic.d.cts +43 -0
  95. package/dist/types/express.d.cts +44 -0
  96. package/dist/types/express.d.mts +4 -2
  97. package/dist/types/fastify.d.cts +23 -0
  98. package/dist/types/fastify.d.mts +4 -2
  99. package/dist/types/h3.d.cts +41 -0
  100. package/dist/types/h3.d.mts +4 -4
  101. package/dist/types/hono.d.cts +21 -0
  102. package/dist/types/hono.d.mts +4 -3
  103. package/dist/types/koa.d.cts +25 -0
  104. package/dist/types/koa.d.mts +4 -3
  105. package/package.json +1 -1
  106. package/dist/router-BEgAxp5A.d.cts +0 -649
  107. package/dist/router-DKKYx23P.d.mts +0 -649
@@ -1,262 +1,3 @@
1
- import "../bindings-XLDXFpHZ.mjs";
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
+ 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 Array.from(this.routes)) {
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,135 @@
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 { CoreRouter } from "../core/router.cjs";
5
+
6
+ //#region src/koa/router.d.ts
7
+ /**
8
+ * @class clear-router Koa Router
9
+ * @description Laravel-style routing system for Koa using @koa/router and shared clear-router core
10
+ * @author 3m1n3nc3
11
+ * @repository https://github.com/arkstack-tmp/clear-router
12
+ */
13
+ declare class Router extends CoreRouter {
14
+ protected static routerStateNamespace: string;
15
+ private static readonly bodyCache;
16
+ private static readBodyCached;
17
+ private static readBody;
18
+ private static sendReturnValue;
19
+ /**
20
+ * Adds a new route to the router.
21
+ *
22
+ * @param methods
23
+ * @param path
24
+ * @param handler
25
+ * @param middlewares
26
+ */
27
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
28
+ /**
29
+ * Define a resourceful API controller with standard CRUD routes
30
+ *
31
+ * @param basePath
32
+ * @param controller
33
+ * @param options
34
+ */
35
+ static apiResource(basePath: string, controller: any, options?: {
36
+ only?: ControllerAction[];
37
+ except?: ControllerAction[];
38
+ middlewares?: ApiResourceMiddleware<Middleware>;
39
+ }): void;
40
+ /**
41
+ * Define a GET route
42
+ *
43
+ * @param path
44
+ * @param handler
45
+ * @param middlewares
46
+ */
47
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
48
+ /**
49
+ * Define a POST route
50
+ *
51
+ * @param path
52
+ * @param handler
53
+ * @param middlewares
54
+ */
55
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
56
+ /**
57
+ * Define a PUT route
58
+ *
59
+ * @param path
60
+ * @param handler
61
+ * @param middlewares
62
+ */
63
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
64
+ /**
65
+ * Define a DELETE route
66
+ *
67
+ * @param path
68
+ * @param handler
69
+ * @param middlewares
70
+ */
71
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
72
+ /**
73
+ * Define a PATCH route
74
+ *
75
+ * @param path
76
+ * @param handler
77
+ * @param middlewares
78
+ */
79
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
80
+ /**
81
+ * Define an OPTIONS route
82
+ *
83
+ * @param path
84
+ * @param handler
85
+ * @param middlewares
86
+ */
87
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
88
+ /**
89
+ * Adds a new HEAD route to the router.
90
+ *
91
+ * @param this
92
+ * @param path
93
+ * @param handler
94
+ * @param middlewares
95
+ */
96
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
97
+ /**
98
+ * Defines a group of routes with a common prefix.
99
+ *
100
+ * @param prefix
101
+ * @param callback
102
+ * @param middlewares
103
+ */
104
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
105
+ /**
106
+ * Apply middlewares to a group of routes defined within the callback
107
+ *
108
+ * @param middlewares - Middleware or array of middlewares to apply
109
+ * @param callback - Function that defines the routes to which the middlewares will be applied
110
+ */
111
+ static middleware(middlewares: Middleware[], callback: () => void): void;
112
+ /**
113
+ * Get all defined routes, optionally organized by path or method
114
+ */
115
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
116
+ /**
117
+ * @param type - 'path' to get routes organized by path
118
+ */
119
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
120
+ /**
121
+ * @param type - 'method' to get routes organized by method
122
+ */
123
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
124
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
125
+ static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
126
+ /**
127
+ * Apply the defined routes to a @koa/router instance
128
+ *
129
+ * @param router @koa/router instance
130
+ * @returns The @koa/router instance with the applied routes
131
+ */
132
+ static apply(router: KoaRouterApp): KoaRouterApp;
133
+ }
134
+ //#endregion
135
+ export { Router };