clear-router 2.4.0 → 2.5.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 +10 -4
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.d.cts +1 -1
- package/dist/core/index.d.mts +1 -1
- package/dist/core/index.mjs +1 -1
- package/dist/express/index.cjs +11 -13
- package/dist/express/index.d.cts +11 -13
- package/dist/express/index.d.mts +11 -13
- package/dist/express/index.mjs +11 -13
- package/dist/fastify/index.cjs +1 -1
- package/dist/fastify/index.d.cts +1 -1
- package/dist/fastify/index.d.mts +1 -1
- package/dist/fastify/index.mjs +1 -1
- package/dist/h3/index.cjs +5 -7
- package/dist/h3/index.d.cts +5 -7
- package/dist/h3/index.d.mts +5 -7
- package/dist/h3/index.mjs +5 -7
- package/dist/hono/index.cjs +3 -3
- package/dist/hono/index.d.cts +3 -3
- package/dist/hono/index.d.mts +3 -3
- package/dist/hono/index.mjs +3 -3
- package/dist/index.cjs +15 -12
- package/dist/index.d.cts +10 -12
- package/dist/index.d.mts +10 -12
- package/dist/index.mjs +15 -12
- package/dist/koa/index.cjs +244 -0
- package/dist/koa/index.d.cts +147 -0
- package/dist/koa/index.d.mts +147 -0
- package/dist/koa/index.mjs +243 -0
- package/dist/{router-CHaZi7NS.mjs → router-BReOXz-F.mjs} +15 -12
- package/dist/{router-C-c43ybe.cjs → router-CHg0pZUO.cjs} +15 -12
- package/dist/{router-A-9JuZPi.d.cts → router-CS_2XQ7I.d.cts} +10 -12
- package/dist/{router-CVeqd7Ro.d.mts → router-_w2VzMQF.d.mts} +10 -12
- package/dist/types/koa.d.mts +22 -0
- package/dist/types/koa.mjs +1 -0
- package/package.json +21 -5
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { t as CoreRouter } from "../router-BReOXz-F.mjs";
|
|
2
|
+
import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-HOePPAl8.mjs";
|
|
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/toneflix/clear-router
|
|
10
|
+
*/
|
|
11
|
+
var Router = class Router extends 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 = 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
|
+
if (isFetchResponse(meta.body)) {
|
|
47
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
48
|
+
ctx.set(key, headerValue);
|
|
49
|
+
});
|
|
50
|
+
ctx.status = meta.body.status;
|
|
51
|
+
ctx.body = Buffer.from(await meta.body.arrayBuffer());
|
|
52
|
+
return ctx.body;
|
|
53
|
+
}
|
|
54
|
+
if (meta.contentType) ctx.type = meta.contentType;
|
|
55
|
+
ctx.body = meta.isEmpty ? null : meta.body;
|
|
56
|
+
return ctx.body;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Adds a new route to the router.
|
|
60
|
+
*
|
|
61
|
+
* @param methods
|
|
62
|
+
* @param path
|
|
63
|
+
* @param handler
|
|
64
|
+
* @param middlewares
|
|
65
|
+
*/
|
|
66
|
+
static add(methods, path, handler, middlewares) {
|
|
67
|
+
super.add(methods, path, handler, middlewares);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
71
|
+
*
|
|
72
|
+
* @param basePath
|
|
73
|
+
* @param controller
|
|
74
|
+
* @param options
|
|
75
|
+
*/
|
|
76
|
+
static apiResource(basePath, controller, options) {
|
|
77
|
+
super.apiResource(basePath, controller, options);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Define a GET route
|
|
81
|
+
*
|
|
82
|
+
* @param path
|
|
83
|
+
* @param handler
|
|
84
|
+
* @param middlewares
|
|
85
|
+
*/
|
|
86
|
+
static get(path, handler, middlewares) {
|
|
87
|
+
super.get(path, handler, middlewares);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Define a POST route
|
|
91
|
+
*
|
|
92
|
+
* @param path
|
|
93
|
+
* @param handler
|
|
94
|
+
* @param middlewares
|
|
95
|
+
*/
|
|
96
|
+
static post(path, handler, middlewares) {
|
|
97
|
+
super.post(path, handler, middlewares);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Define a PUT route
|
|
101
|
+
*
|
|
102
|
+
* @param path
|
|
103
|
+
* @param handler
|
|
104
|
+
* @param middlewares
|
|
105
|
+
*/
|
|
106
|
+
static put(path, handler, middlewares) {
|
|
107
|
+
super.put(path, handler, middlewares);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Define a DELETE route
|
|
111
|
+
*
|
|
112
|
+
* @param path
|
|
113
|
+
* @param handler
|
|
114
|
+
* @param middlewares
|
|
115
|
+
*/
|
|
116
|
+
static delete(path, handler, middlewares) {
|
|
117
|
+
super.delete(path, handler, middlewares);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Define a PATCH route
|
|
121
|
+
*
|
|
122
|
+
* @param path
|
|
123
|
+
* @param handler
|
|
124
|
+
* @param middlewares
|
|
125
|
+
*/
|
|
126
|
+
static patch(path, handler, middlewares) {
|
|
127
|
+
super.patch(path, handler, middlewares);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Define an OPTIONS route
|
|
131
|
+
*
|
|
132
|
+
* @param path
|
|
133
|
+
* @param handler
|
|
134
|
+
* @param middlewares
|
|
135
|
+
*/
|
|
136
|
+
static options(path, handler, middlewares) {
|
|
137
|
+
super.options(path, handler, middlewares);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Adds a new HEAD route to the router.
|
|
141
|
+
*
|
|
142
|
+
* @param this
|
|
143
|
+
* @param path
|
|
144
|
+
* @param handler
|
|
145
|
+
* @param middlewares
|
|
146
|
+
*/
|
|
147
|
+
static head(path, handler, middlewares) {
|
|
148
|
+
super.head(path, handler, middlewares);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Defines a group of routes with a common prefix.
|
|
152
|
+
*
|
|
153
|
+
* @param prefix
|
|
154
|
+
* @param callback
|
|
155
|
+
* @param middlewares
|
|
156
|
+
*/
|
|
157
|
+
static async group(prefix, callback, middlewares) {
|
|
158
|
+
await super.group(prefix, callback, middlewares);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
162
|
+
*
|
|
163
|
+
* @param middlewares - Middleware or array of middlewares to apply
|
|
164
|
+
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
165
|
+
*/
|
|
166
|
+
static middleware(middlewares, callback) {
|
|
167
|
+
super.middleware(middlewares, callback);
|
|
168
|
+
}
|
|
169
|
+
static allRoutes(type) {
|
|
170
|
+
return super.allRoutes(type);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Apply the defined routes to a @koa/router instance
|
|
174
|
+
*
|
|
175
|
+
* @param router @koa/router instance
|
|
176
|
+
* @returns The @koa/router instance with the applied routes
|
|
177
|
+
*/
|
|
178
|
+
static apply(router) {
|
|
179
|
+
for (const route of this.routes) {
|
|
180
|
+
let handlerFunction = null;
|
|
181
|
+
let instance = null;
|
|
182
|
+
try {
|
|
183
|
+
const resolved = this.resolveHandler(route);
|
|
184
|
+
handlerFunction = resolved.handlerFunction;
|
|
185
|
+
instance = resolved.instance;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
if (!handlerFunction) continue;
|
|
191
|
+
for (const method of route.methods) {
|
|
192
|
+
const allowedMethods = [
|
|
193
|
+
"get",
|
|
194
|
+
"post",
|
|
195
|
+
"put",
|
|
196
|
+
"delete",
|
|
197
|
+
"patch",
|
|
198
|
+
"options",
|
|
199
|
+
"head"
|
|
200
|
+
];
|
|
201
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
202
|
+
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
203
|
+
router[method](route.path, ...route.middlewares || [], async (context, next) => {
|
|
204
|
+
const ctx = context;
|
|
205
|
+
const reqBody = await Router.readBodyCached(ctx);
|
|
206
|
+
const override = Router.resolveMethodOverride(ctx.method, ctx.headers, reqBody);
|
|
207
|
+
if (method === "post" && override && override !== "post") return next();
|
|
208
|
+
const inst = instance ?? route;
|
|
209
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
210
|
+
body: reqBody,
|
|
211
|
+
query: ctx.query,
|
|
212
|
+
params: ctx.params ?? {}
|
|
213
|
+
});
|
|
214
|
+
const result = handlerFunction(ctx, inst.clearRequest);
|
|
215
|
+
const resolved = await Promise.resolve(result);
|
|
216
|
+
return Router.sendReturnValue(ctx, resolved, method, route.path);
|
|
217
|
+
});
|
|
218
|
+
if ([
|
|
219
|
+
"put",
|
|
220
|
+
"patch",
|
|
221
|
+
"delete"
|
|
222
|
+
].includes(method)) router.post(route.path, ...route.middlewares || [], async (context, next) => {
|
|
223
|
+
const ctx = context;
|
|
224
|
+
const reqBody = await Router.readBodyCached(ctx);
|
|
225
|
+
if (Router.resolveMethodOverride(ctx.method, ctx.headers, reqBody) !== method) return next();
|
|
226
|
+
const inst = instance ?? route;
|
|
227
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
228
|
+
body: reqBody,
|
|
229
|
+
query: ctx.query,
|
|
230
|
+
params: ctx.params ?? {}
|
|
231
|
+
});
|
|
232
|
+
const result = handlerFunction(ctx, inst.clearRequest);
|
|
233
|
+
const resolved = await Promise.resolve(result);
|
|
234
|
+
return Router.sendReturnValue(ctx, resolved, method, route.path);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return router;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
//#endregion
|
|
243
|
+
export { Router };
|
|
@@ -144,6 +144,11 @@ var CoreRouter = class {
|
|
|
144
144
|
ctx.reply.code(204).send();
|
|
145
145
|
return;
|
|
146
146
|
}
|
|
147
|
+
if (ctx?.set && "status" in ctx) {
|
|
148
|
+
ctx.set("Allow", allow);
|
|
149
|
+
ctx.status = 204;
|
|
150
|
+
ctx.body = null;
|
|
151
|
+
}
|
|
147
152
|
};
|
|
148
153
|
}
|
|
149
154
|
static config = { methodOverride: {
|
|
@@ -239,7 +244,7 @@ var CoreRouter = class {
|
|
|
239
244
|
return null;
|
|
240
245
|
}
|
|
241
246
|
/**
|
|
242
|
-
* Adds a new route to the router
|
|
247
|
+
* Adds a new route to the router.
|
|
243
248
|
*
|
|
244
249
|
* @param this
|
|
245
250
|
* @param methods
|
|
@@ -269,8 +274,7 @@ var CoreRouter = class {
|
|
|
269
274
|
}
|
|
270
275
|
}
|
|
271
276
|
/**
|
|
272
|
-
*
|
|
273
|
-
* options to include/exclude specific actions and apply middlewares.
|
|
277
|
+
* Define a resourceful API controller with standard CRUD routes.
|
|
274
278
|
*
|
|
275
279
|
* @param this
|
|
276
280
|
* @param basePath
|
|
@@ -313,7 +317,7 @@ var CoreRouter = class {
|
|
|
313
317
|
}
|
|
314
318
|
}
|
|
315
319
|
/**
|
|
316
|
-
* Adds a new GET route to the router
|
|
320
|
+
* Adds a new GET route to the router.
|
|
317
321
|
*
|
|
318
322
|
* @param this The router instance.
|
|
319
323
|
* @param path The path for the GET route.
|
|
@@ -324,7 +328,7 @@ var CoreRouter = class {
|
|
|
324
328
|
this.add("get", path, handler, middlewares);
|
|
325
329
|
}
|
|
326
330
|
/**
|
|
327
|
-
* Adds a new POST route to the router
|
|
331
|
+
* Adds a new POST route to the router.
|
|
328
332
|
*
|
|
329
333
|
* @param this
|
|
330
334
|
* @param path
|
|
@@ -335,7 +339,7 @@ var CoreRouter = class {
|
|
|
335
339
|
this.add("post", path, handler, middlewares);
|
|
336
340
|
}
|
|
337
341
|
/**
|
|
338
|
-
* Adds a new PUT route to the router
|
|
342
|
+
* Adds a new PUT route to the router.
|
|
339
343
|
*
|
|
340
344
|
* @param this
|
|
341
345
|
* @param path
|
|
@@ -346,7 +350,7 @@ var CoreRouter = class {
|
|
|
346
350
|
this.add("put", path, handler, middlewares);
|
|
347
351
|
}
|
|
348
352
|
/**
|
|
349
|
-
* Adds a new DELETE route to the router
|
|
353
|
+
* Adds a new DELETE route to the router.
|
|
350
354
|
*
|
|
351
355
|
* @param this
|
|
352
356
|
* @param path
|
|
@@ -357,7 +361,7 @@ var CoreRouter = class {
|
|
|
357
361
|
this.add("delete", path, handler, middlewares);
|
|
358
362
|
}
|
|
359
363
|
/**
|
|
360
|
-
* Adds a new PATCH route to the router
|
|
364
|
+
* Adds a new PATCH route to the router.
|
|
361
365
|
*
|
|
362
366
|
* @param this
|
|
363
367
|
* @param path
|
|
@@ -368,7 +372,7 @@ var CoreRouter = class {
|
|
|
368
372
|
this.add("patch", path, handler, middlewares);
|
|
369
373
|
}
|
|
370
374
|
/**
|
|
371
|
-
* Adds a new OPTIONS route to the router
|
|
375
|
+
* Adds a new OPTIONS route to the router.
|
|
372
376
|
*
|
|
373
377
|
* @param this
|
|
374
378
|
* @param path
|
|
@@ -379,7 +383,7 @@ var CoreRouter = class {
|
|
|
379
383
|
this.add("options", path, handler, middlewares);
|
|
380
384
|
}
|
|
381
385
|
/**
|
|
382
|
-
* Adds a new HEAD route to the router
|
|
386
|
+
* Adds a new HEAD route to the router.
|
|
383
387
|
*
|
|
384
388
|
* @param this
|
|
385
389
|
* @param path
|
|
@@ -390,8 +394,7 @@ var CoreRouter = class {
|
|
|
390
394
|
this.add("head", path, handler, middlewares);
|
|
391
395
|
}
|
|
392
396
|
/**
|
|
393
|
-
* Defines a group of routes with a common prefix
|
|
394
|
-
* organization and reuse of route configurations.
|
|
397
|
+
* Defines a group of routes with a common prefix.
|
|
395
398
|
*
|
|
396
399
|
* @param this
|
|
397
400
|
* @param prefix
|
|
@@ -144,6 +144,11 @@ var CoreRouter = class {
|
|
|
144
144
|
ctx.reply.code(204).send();
|
|
145
145
|
return;
|
|
146
146
|
}
|
|
147
|
+
if (ctx?.set && "status" in ctx) {
|
|
148
|
+
ctx.set("Allow", allow);
|
|
149
|
+
ctx.status = 204;
|
|
150
|
+
ctx.body = null;
|
|
151
|
+
}
|
|
147
152
|
};
|
|
148
153
|
}
|
|
149
154
|
static config = { methodOverride: {
|
|
@@ -239,7 +244,7 @@ var CoreRouter = class {
|
|
|
239
244
|
return null;
|
|
240
245
|
}
|
|
241
246
|
/**
|
|
242
|
-
* Adds a new route to the router
|
|
247
|
+
* Adds a new route to the router.
|
|
243
248
|
*
|
|
244
249
|
* @param this
|
|
245
250
|
* @param methods
|
|
@@ -269,8 +274,7 @@ var CoreRouter = class {
|
|
|
269
274
|
}
|
|
270
275
|
}
|
|
271
276
|
/**
|
|
272
|
-
*
|
|
273
|
-
* options to include/exclude specific actions and apply middlewares.
|
|
277
|
+
* Define a resourceful API controller with standard CRUD routes.
|
|
274
278
|
*
|
|
275
279
|
* @param this
|
|
276
280
|
* @param basePath
|
|
@@ -313,7 +317,7 @@ var CoreRouter = class {
|
|
|
313
317
|
}
|
|
314
318
|
}
|
|
315
319
|
/**
|
|
316
|
-
* Adds a new GET route to the router
|
|
320
|
+
* Adds a new GET route to the router.
|
|
317
321
|
*
|
|
318
322
|
* @param this The router instance.
|
|
319
323
|
* @param path The path for the GET route.
|
|
@@ -324,7 +328,7 @@ var CoreRouter = class {
|
|
|
324
328
|
this.add("get", path, handler, middlewares);
|
|
325
329
|
}
|
|
326
330
|
/**
|
|
327
|
-
* Adds a new POST route to the router
|
|
331
|
+
* Adds a new POST route to the router.
|
|
328
332
|
*
|
|
329
333
|
* @param this
|
|
330
334
|
* @param path
|
|
@@ -335,7 +339,7 @@ var CoreRouter = class {
|
|
|
335
339
|
this.add("post", path, handler, middlewares);
|
|
336
340
|
}
|
|
337
341
|
/**
|
|
338
|
-
* Adds a new PUT route to the router
|
|
342
|
+
* Adds a new PUT route to the router.
|
|
339
343
|
*
|
|
340
344
|
* @param this
|
|
341
345
|
* @param path
|
|
@@ -346,7 +350,7 @@ var CoreRouter = class {
|
|
|
346
350
|
this.add("put", path, handler, middlewares);
|
|
347
351
|
}
|
|
348
352
|
/**
|
|
349
|
-
* Adds a new DELETE route to the router
|
|
353
|
+
* Adds a new DELETE route to the router.
|
|
350
354
|
*
|
|
351
355
|
* @param this
|
|
352
356
|
* @param path
|
|
@@ -357,7 +361,7 @@ var CoreRouter = class {
|
|
|
357
361
|
this.add("delete", path, handler, middlewares);
|
|
358
362
|
}
|
|
359
363
|
/**
|
|
360
|
-
* Adds a new PATCH route to the router
|
|
364
|
+
* Adds a new PATCH route to the router.
|
|
361
365
|
*
|
|
362
366
|
* @param this
|
|
363
367
|
* @param path
|
|
@@ -368,7 +372,7 @@ var CoreRouter = class {
|
|
|
368
372
|
this.add("patch", path, handler, middlewares);
|
|
369
373
|
}
|
|
370
374
|
/**
|
|
371
|
-
* Adds a new OPTIONS route to the router
|
|
375
|
+
* Adds a new OPTIONS route to the router.
|
|
372
376
|
*
|
|
373
377
|
* @param this
|
|
374
378
|
* @param path
|
|
@@ -379,7 +383,7 @@ var CoreRouter = class {
|
|
|
379
383
|
this.add("options", path, handler, middlewares);
|
|
380
384
|
}
|
|
381
385
|
/**
|
|
382
|
-
* Adds a new HEAD route to the router
|
|
386
|
+
* Adds a new HEAD route to the router.
|
|
383
387
|
*
|
|
384
388
|
* @param this
|
|
385
389
|
* @param path
|
|
@@ -390,8 +394,7 @@ var CoreRouter = class {
|
|
|
390
394
|
this.add("head", path, handler, middlewares);
|
|
391
395
|
}
|
|
392
396
|
/**
|
|
393
|
-
* Defines a group of routes with a common prefix
|
|
394
|
-
* organization and reuse of route configurations.
|
|
397
|
+
* Defines a group of routes with a common prefix.
|
|
395
398
|
*
|
|
396
399
|
* @param this
|
|
397
400
|
* @param prefix
|
|
@@ -211,7 +211,7 @@ declare abstract class CoreRouter {
|
|
|
211
211
|
static configure(this: any, options?: RouterConfig): void;
|
|
212
212
|
protected static resolveMethodOverride(this: any, method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
|
|
213
213
|
/**
|
|
214
|
-
* Adds a new route to the router
|
|
214
|
+
* Adds a new route to the router.
|
|
215
215
|
*
|
|
216
216
|
* @param this
|
|
217
217
|
* @param methods
|
|
@@ -221,8 +221,7 @@ declare abstract class CoreRouter {
|
|
|
221
221
|
*/
|
|
222
222
|
static add(this: any, methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
|
|
223
223
|
/**
|
|
224
|
-
*
|
|
225
|
-
* options to include/exclude specific actions and apply middlewares.
|
|
224
|
+
* Define a resourceful API controller with standard CRUD routes.
|
|
226
225
|
*
|
|
227
226
|
* @param this
|
|
228
227
|
* @param basePath
|
|
@@ -235,7 +234,7 @@ declare abstract class CoreRouter {
|
|
|
235
234
|
middlewares?: ApiResourceMiddleware<any>;
|
|
236
235
|
}): void;
|
|
237
236
|
/**
|
|
238
|
-
* Adds a new GET route to the router
|
|
237
|
+
* Adds a new GET route to the router.
|
|
239
238
|
*
|
|
240
239
|
* @param this The router instance.
|
|
241
240
|
* @param path The path for the GET route.
|
|
@@ -244,7 +243,7 @@ declare abstract class CoreRouter {
|
|
|
244
243
|
*/
|
|
245
244
|
static get(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
246
245
|
/**
|
|
247
|
-
* Adds a new POST route to the router
|
|
246
|
+
* Adds a new POST route to the router.
|
|
248
247
|
*
|
|
249
248
|
* @param this
|
|
250
249
|
* @param path
|
|
@@ -253,7 +252,7 @@ declare abstract class CoreRouter {
|
|
|
253
252
|
*/
|
|
254
253
|
static post(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
255
254
|
/**
|
|
256
|
-
* Adds a new PUT route to the router
|
|
255
|
+
* Adds a new PUT route to the router.
|
|
257
256
|
*
|
|
258
257
|
* @param this
|
|
259
258
|
* @param path
|
|
@@ -262,7 +261,7 @@ declare abstract class CoreRouter {
|
|
|
262
261
|
*/
|
|
263
262
|
static put(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
264
263
|
/**
|
|
265
|
-
* Adds a new DELETE route to the router
|
|
264
|
+
* Adds a new DELETE route to the router.
|
|
266
265
|
*
|
|
267
266
|
* @param this
|
|
268
267
|
* @param path
|
|
@@ -271,7 +270,7 @@ declare abstract class CoreRouter {
|
|
|
271
270
|
*/
|
|
272
271
|
static delete(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
273
272
|
/**
|
|
274
|
-
* Adds a new PATCH route to the router
|
|
273
|
+
* Adds a new PATCH route to the router.
|
|
275
274
|
*
|
|
276
275
|
* @param this
|
|
277
276
|
* @param path
|
|
@@ -280,7 +279,7 @@ declare abstract class CoreRouter {
|
|
|
280
279
|
*/
|
|
281
280
|
static patch(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
282
281
|
/**
|
|
283
|
-
* Adds a new OPTIONS route to the router
|
|
282
|
+
* Adds a new OPTIONS route to the router.
|
|
284
283
|
*
|
|
285
284
|
* @param this
|
|
286
285
|
* @param path
|
|
@@ -289,7 +288,7 @@ declare abstract class CoreRouter {
|
|
|
289
288
|
*/
|
|
290
289
|
static options(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
291
290
|
/**
|
|
292
|
-
* Adds a new HEAD route to the router
|
|
291
|
+
* Adds a new HEAD route to the router.
|
|
293
292
|
*
|
|
294
293
|
* @param this
|
|
295
294
|
* @param path
|
|
@@ -298,8 +297,7 @@ declare abstract class CoreRouter {
|
|
|
298
297
|
*/
|
|
299
298
|
static head(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
300
299
|
/**
|
|
301
|
-
* Defines a group of routes with a common prefix
|
|
302
|
-
* organization and reuse of route configurations.
|
|
300
|
+
* Defines a group of routes with a common prefix.
|
|
303
301
|
*
|
|
304
302
|
* @param this
|
|
305
303
|
* @param prefix
|
|
@@ -211,7 +211,7 @@ declare abstract class CoreRouter {
|
|
|
211
211
|
static configure(this: any, options?: RouterConfig): void;
|
|
212
212
|
protected static resolveMethodOverride(this: any, method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
|
|
213
213
|
/**
|
|
214
|
-
* Adds a new route to the router
|
|
214
|
+
* Adds a new route to the router.
|
|
215
215
|
*
|
|
216
216
|
* @param this
|
|
217
217
|
* @param methods
|
|
@@ -221,8 +221,7 @@ declare abstract class CoreRouter {
|
|
|
221
221
|
*/
|
|
222
222
|
static add(this: any, methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
|
|
223
223
|
/**
|
|
224
|
-
*
|
|
225
|
-
* options to include/exclude specific actions and apply middlewares.
|
|
224
|
+
* Define a resourceful API controller with standard CRUD routes.
|
|
226
225
|
*
|
|
227
226
|
* @param this
|
|
228
227
|
* @param basePath
|
|
@@ -235,7 +234,7 @@ declare abstract class CoreRouter {
|
|
|
235
234
|
middlewares?: ApiResourceMiddleware<any>;
|
|
236
235
|
}): void;
|
|
237
236
|
/**
|
|
238
|
-
* Adds a new GET route to the router
|
|
237
|
+
* Adds a new GET route to the router.
|
|
239
238
|
*
|
|
240
239
|
* @param this The router instance.
|
|
241
240
|
* @param path The path for the GET route.
|
|
@@ -244,7 +243,7 @@ declare abstract class CoreRouter {
|
|
|
244
243
|
*/
|
|
245
244
|
static get(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
246
245
|
/**
|
|
247
|
-
* Adds a new POST route to the router
|
|
246
|
+
* Adds a new POST route to the router.
|
|
248
247
|
*
|
|
249
248
|
* @param this
|
|
250
249
|
* @param path
|
|
@@ -253,7 +252,7 @@ declare abstract class CoreRouter {
|
|
|
253
252
|
*/
|
|
254
253
|
static post(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
255
254
|
/**
|
|
256
|
-
* Adds a new PUT route to the router
|
|
255
|
+
* Adds a new PUT route to the router.
|
|
257
256
|
*
|
|
258
257
|
* @param this
|
|
259
258
|
* @param path
|
|
@@ -262,7 +261,7 @@ declare abstract class CoreRouter {
|
|
|
262
261
|
*/
|
|
263
262
|
static put(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
264
263
|
/**
|
|
265
|
-
* Adds a new DELETE route to the router
|
|
264
|
+
* Adds a new DELETE route to the router.
|
|
266
265
|
*
|
|
267
266
|
* @param this
|
|
268
267
|
* @param path
|
|
@@ -271,7 +270,7 @@ declare abstract class CoreRouter {
|
|
|
271
270
|
*/
|
|
272
271
|
static delete(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
273
272
|
/**
|
|
274
|
-
* Adds a new PATCH route to the router
|
|
273
|
+
* Adds a new PATCH route to the router.
|
|
275
274
|
*
|
|
276
275
|
* @param this
|
|
277
276
|
* @param path
|
|
@@ -280,7 +279,7 @@ declare abstract class CoreRouter {
|
|
|
280
279
|
*/
|
|
281
280
|
static patch(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
282
281
|
/**
|
|
283
|
-
* Adds a new OPTIONS route to the router
|
|
282
|
+
* Adds a new OPTIONS route to the router.
|
|
284
283
|
*
|
|
285
284
|
* @param this
|
|
286
285
|
* @param path
|
|
@@ -289,7 +288,7 @@ declare abstract class CoreRouter {
|
|
|
289
288
|
*/
|
|
290
289
|
static options(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
291
290
|
/**
|
|
292
|
-
* Adds a new HEAD route to the router
|
|
291
|
+
* Adds a new HEAD route to the router.
|
|
293
292
|
*
|
|
294
293
|
* @param this
|
|
295
294
|
* @param path
|
|
@@ -298,8 +297,7 @@ declare abstract class CoreRouter {
|
|
|
298
297
|
*/
|
|
299
298
|
static head(this: any, path: string, handler: any, middlewares?: any[] | any): void;
|
|
300
299
|
/**
|
|
301
|
-
* Defines a group of routes with a common prefix
|
|
302
|
-
* organization and reuse of route configurations.
|
|
300
|
+
* Defines a group of routes with a common prefix.
|
|
303
301
|
*
|
|
304
302
|
* @param this
|
|
305
303
|
* @param prefix
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.mjs";
|
|
2
|
+
import { ClearRequest } from "./ClearRequest.mjs";
|
|
3
|
+
import Koa from "koa";
|
|
4
|
+
import Router from "@koa/router";
|
|
5
|
+
|
|
6
|
+
//#region types/koa.d.ts
|
|
7
|
+
interface RequestWithGetBody extends Koa.Request {
|
|
8
|
+
getBody: () => Record<string, any>;
|
|
9
|
+
body?: any;
|
|
10
|
+
}
|
|
11
|
+
interface HttpContext extends Koa.Context {
|
|
12
|
+
request: RequestWithGetBody;
|
|
13
|
+
params: Record<string, any>;
|
|
14
|
+
query: Record<string, any>;
|
|
15
|
+
}
|
|
16
|
+
type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
|
|
17
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
18
|
+
type NextFunction = Koa.Next;
|
|
19
|
+
type Middleware = Koa.Middleware<any, any>;
|
|
20
|
+
type KoaRouterApp = Router<any, any>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { Handler, HttpContext, KoaRouterApp, Middleware, NextFunction, RequestWithGetBody, RouteHandler };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|