@devbro/neko-router 0.1.6 → 0.1.8
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/CompiledRoute.mjs +26 -49
- package/dist/CompiledRoute.mjs.map +1 -1
- package/dist/Controller.mjs +14 -35
- package/dist/Controller.mjs.map +1 -1
- package/dist/Middleware.mjs.map +1 -1
- package/dist/MiddlewareFactory.mjs +1 -1
- package/dist/MiddlewareFactory.mjs.map +1 -1
- package/dist/Route.mjs +2 -1
- package/dist/Route.mjs.map +1 -1
- package/dist/Router.mjs +7 -29
- package/dist/Router.mjs.map +1 -1
- package/dist/helpers.mjs +16 -40
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.js +463 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -3
- package/dist/CompiledRoute.d.ts +0 -22
- package/dist/CompiledRoute.js +0 -161
- package/dist/CompiledRoute.js.map +0 -1
- package/dist/Controller.d.ts +0 -42
- package/dist/Controller.js +0 -154
- package/dist/Controller.js.map +0 -1
- package/dist/Middleware-C8dxjlFD.d.ts +0 -30
- package/dist/Middleware.d.ts +0 -2
- package/dist/Middleware.js +0 -35
- package/dist/Middleware.js.map +0 -1
- package/dist/MiddlewareFactory.d.ts +0 -8
- package/dist/MiddlewareFactory.js +0 -42
- package/dist/MiddlewareFactory.js.map +0 -1
- package/dist/Route.d.ts +0 -40
- package/dist/Route.js +0 -138
- package/dist/Route.js.map +0 -1
- package/dist/Router.d.ts +0 -19
- package/dist/Router.js +0 -118
- package/dist/Router.js.map +0 -1
- package/dist/helpers.d.ts +0 -6
- package/dist/helpers.js +0 -70
- package/dist/helpers.js.map +0 -1
- package/dist/index.d.ts +0 -8
- package/dist/types.d.ts +0 -2
- package/dist/types.js +0 -17
- package/dist/types.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
6
12
|
var __copyProps = (to, from, except, desc) => {
|
|
7
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
14
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,27 +17,467 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
17
|
}
|
|
12
18
|
return to;
|
|
13
19
|
};
|
|
14
|
-
var
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
15
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
16
31
|
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
BaseController: () => BaseController,
|
|
34
|
+
CompiledRoute: () => CompiledRoute,
|
|
35
|
+
Controller: () => Controller,
|
|
36
|
+
Delete: () => Delete,
|
|
37
|
+
Get: () => Get,
|
|
38
|
+
Middleware: () => Middleware,
|
|
39
|
+
MiddlewareFactory: () => MiddlewareFactory,
|
|
40
|
+
Options: () => Options,
|
|
41
|
+
Patch: () => Patch,
|
|
42
|
+
Post: () => Post,
|
|
43
|
+
Put: () => Put,
|
|
44
|
+
Route: () => Route,
|
|
45
|
+
Router: () => Router,
|
|
46
|
+
createParamDecorator: () => createParamDecorator,
|
|
47
|
+
runNext: () => runNext
|
|
48
|
+
});
|
|
17
49
|
module.exports = __toCommonJS(index_exports);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
50
|
+
|
|
51
|
+
// src/Middleware.mts
|
|
52
|
+
var Middleware = class {
|
|
53
|
+
constructor(params = {}) {
|
|
54
|
+
}
|
|
55
|
+
static getInstance(params) {
|
|
56
|
+
throw new Error("Method not implemented. Please implement a static getInstance method.");
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/MiddlewareFactory.mts
|
|
61
|
+
var MiddlewareFactory = class {
|
|
62
|
+
static create(func) {
|
|
63
|
+
const cls = class extends Middleware {
|
|
64
|
+
call(req, res, next) {
|
|
65
|
+
return func(req, res, next);
|
|
66
|
+
}
|
|
67
|
+
constructor(params = {}) {
|
|
68
|
+
super(params);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
return new cls();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/CompiledRoute.mts
|
|
76
|
+
var CompiledRoute = class {
|
|
77
|
+
constructor(route, request, response, globalMiddlewares = []) {
|
|
78
|
+
this.route = route;
|
|
79
|
+
this.request = request;
|
|
80
|
+
this.response = response;
|
|
81
|
+
this.globalMiddlewares = globalMiddlewares;
|
|
82
|
+
this.prepareMiddlewares();
|
|
83
|
+
}
|
|
84
|
+
middlewares = [];
|
|
85
|
+
getMiddlewares() {
|
|
86
|
+
return this.middlewares;
|
|
87
|
+
}
|
|
88
|
+
prepareMiddlewares() {
|
|
89
|
+
this.middlewares = [];
|
|
90
|
+
for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {
|
|
91
|
+
if (middleware instanceof Middleware) {
|
|
92
|
+
this.middlewares.push(middleware);
|
|
93
|
+
} else if (this.isClass(middleware)) {
|
|
94
|
+
this.middlewares.push(middleware.getInstance({}));
|
|
95
|
+
} else if (typeof middleware === "function") {
|
|
96
|
+
this.middlewares.push(MiddlewareFactory.create(middleware));
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error("Invalid middleware type");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
isClass(func) {
|
|
103
|
+
return typeof func === "function" && /^class\s/.test(Function.prototype.toString.call(func));
|
|
104
|
+
}
|
|
105
|
+
async run() {
|
|
106
|
+
return await this.runMiddlewares(this.middlewares, this.request, this.response);
|
|
107
|
+
}
|
|
108
|
+
prepareOutputJsonFormat(obj) {
|
|
109
|
+
function traverse(value) {
|
|
110
|
+
if (!value || typeof value !== "object") {
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
if (typeof value.toJson === "function") {
|
|
114
|
+
return traverse(value.toJson());
|
|
115
|
+
}
|
|
116
|
+
if (typeof value.toJSON === "function") {
|
|
117
|
+
return traverse(value.toJSON());
|
|
118
|
+
}
|
|
119
|
+
if (Array.isArray(value)) {
|
|
120
|
+
return value.map(traverse);
|
|
121
|
+
}
|
|
122
|
+
const result = {};
|
|
123
|
+
for (const key in value) {
|
|
124
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
125
|
+
result[key] = traverse(value[key]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
return traverse(obj);
|
|
131
|
+
}
|
|
132
|
+
convertToString(obj) {
|
|
133
|
+
if (typeof obj === "string") {
|
|
134
|
+
return obj;
|
|
135
|
+
} else if (obj instanceof Buffer) {
|
|
136
|
+
return obj.toString();
|
|
137
|
+
} else if (typeof obj === "object") {
|
|
138
|
+
return JSON.stringify(this.prepareOutputJsonFormat(obj));
|
|
139
|
+
}
|
|
140
|
+
return String(obj);
|
|
141
|
+
}
|
|
142
|
+
processResponseBody(res, controller_rc) {
|
|
143
|
+
if (controller_rc && res.writableEnded) {
|
|
144
|
+
throw new Error("cannot write to response, response has already ended");
|
|
145
|
+
}
|
|
146
|
+
if (res.writableEnded) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (controller_rc) {
|
|
150
|
+
const header_content_type = res.getHeader("Content-Type");
|
|
151
|
+
if (!header_content_type && typeof controller_rc === "object") {
|
|
152
|
+
res.setHeader("Content-Type", "application/json");
|
|
153
|
+
} else if (!header_content_type) {
|
|
154
|
+
res.setHeader("Content-Type", "text/plain");
|
|
155
|
+
}
|
|
156
|
+
res.end(this.convertToString(controller_rc));
|
|
157
|
+
return;
|
|
158
|
+
} else {
|
|
159
|
+
res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;
|
|
160
|
+
res.end();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async runMiddlewares(middlewares, req, res) {
|
|
164
|
+
let index = 0;
|
|
165
|
+
const me = this;
|
|
166
|
+
async function next() {
|
|
167
|
+
if (index >= middlewares.length) {
|
|
168
|
+
const controller_rc = await me.route.callHanlder(req, res);
|
|
169
|
+
await me.processResponseBody(res, controller_rc);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const middleware = middlewares[index++];
|
|
173
|
+
if (middleware instanceof Middleware) {
|
|
174
|
+
await middleware.call(req, res, next);
|
|
175
|
+
} else if (typeof middleware === "function") {
|
|
176
|
+
await middleware(req, res, next);
|
|
177
|
+
} else {
|
|
178
|
+
throw new Error("does not know how to run middleware");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
await next();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/Controller.mts
|
|
186
|
+
var BaseController = class {
|
|
187
|
+
static basePath = "";
|
|
188
|
+
static baseMiddlewares;
|
|
189
|
+
static getInstance() {
|
|
190
|
+
return new this();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
function Controller(path2, options = {}) {
|
|
194
|
+
return function(target) {
|
|
195
|
+
target.routes = target.routes || [];
|
|
196
|
+
target.basePath = path2;
|
|
197
|
+
target.baseMiddlewares = options.middlewares || [];
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function createHttpDecorator(data) {
|
|
201
|
+
return function(target, propertyKey, descriptor) {
|
|
202
|
+
const ctor = target.constructor;
|
|
203
|
+
if (!ctor.routes) ctor.routes = [];
|
|
204
|
+
ctor.routes.push({
|
|
205
|
+
methods: data.methods,
|
|
206
|
+
path: data.path,
|
|
207
|
+
handler: propertyKey,
|
|
208
|
+
middlewares: data.middlewares || []
|
|
209
|
+
});
|
|
210
|
+
const originalMethod = descriptor.value;
|
|
211
|
+
const paramKeys = Reflect.ownKeys(target.constructor);
|
|
212
|
+
const methodName = propertyKey.toString();
|
|
213
|
+
descriptor.value = async function(...args) {
|
|
214
|
+
const paramCustomKeys = paramKeys.filter(
|
|
215
|
+
(key) => typeof key === "string" && key.startsWith(`${methodName}:`) && key.endsWith(":custom")
|
|
216
|
+
);
|
|
217
|
+
for (const paramKey of paramCustomKeys) {
|
|
218
|
+
const paramIndex = parseInt(paramKey.split(":")[1]);
|
|
219
|
+
let method = Reflect.get(target.constructor, paramKey.toString());
|
|
220
|
+
if (typeof paramIndex === "number" && typeof method === "function") {
|
|
221
|
+
args[paramIndex] = await method();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return originalMethod.apply(this, args);
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function Get(data = {}) {
|
|
229
|
+
return createHttpDecorator({
|
|
230
|
+
methods: ["GET", "HEAD"],
|
|
231
|
+
path: data.path || "/",
|
|
232
|
+
middlewares: data.middlewares || []
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function Post(data = {}) {
|
|
236
|
+
return createHttpDecorator({
|
|
237
|
+
methods: ["POST"],
|
|
238
|
+
path: data.path || "/",
|
|
239
|
+
middlewares: data.middlewares || []
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
function Put(data = {}) {
|
|
243
|
+
return createHttpDecorator({
|
|
244
|
+
methods: ["PUT"],
|
|
245
|
+
path: data.path || "/",
|
|
246
|
+
middlewares: data.middlewares || []
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function Patch(data = {}) {
|
|
250
|
+
return createHttpDecorator({
|
|
251
|
+
methods: ["PATCH"],
|
|
252
|
+
path: data.path || "/",
|
|
253
|
+
middlewares: data.middlewares || []
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function Delete(data = {}) {
|
|
257
|
+
return createHttpDecorator({
|
|
258
|
+
methods: ["DELETE"],
|
|
259
|
+
path: data.path || "/",
|
|
260
|
+
middlewares: data.middlewares || []
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function Options(data = {}) {
|
|
264
|
+
return createHttpDecorator({
|
|
265
|
+
methods: ["OPTIONS"],
|
|
266
|
+
path: data.path || "/",
|
|
267
|
+
middlewares: data.middlewares || []
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function createParamDecorator(func) {
|
|
271
|
+
return function MyParamDecorator(target, propertyKey, parameterIndex) {
|
|
272
|
+
Reflect.set(target.constructor, `${propertyKey?.toString()}:${parameterIndex}:custom`, func);
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/Route.mts
|
|
277
|
+
var Route = class {
|
|
278
|
+
constructor(methods, path2, handler) {
|
|
279
|
+
this.methods = methods;
|
|
280
|
+
this.path = path2;
|
|
281
|
+
this.handler = handler;
|
|
282
|
+
this.urlRegex = this.pathToRegex(path2);
|
|
283
|
+
}
|
|
284
|
+
middlewares = [];
|
|
285
|
+
urlRegex;
|
|
286
|
+
pathToRegex(path2) {
|
|
287
|
+
const lex = this.lexUrlPath(path2);
|
|
288
|
+
return this.tokensToRegex(lex);
|
|
289
|
+
}
|
|
290
|
+
lexUrlPath(path2) {
|
|
291
|
+
const tokens = [];
|
|
292
|
+
let i = 0;
|
|
293
|
+
while (i < path2.length) {
|
|
294
|
+
const char = path2[i];
|
|
295
|
+
if (char === "/") {
|
|
296
|
+
tokens.push({ type: "SLASH", value: "/" });
|
|
297
|
+
i++;
|
|
298
|
+
} else if (char === ":") {
|
|
299
|
+
let start = i + 1;
|
|
300
|
+
while (start < path2.length && /[a-zA-Z0-9_]/.test(path2[start])) {
|
|
301
|
+
start++;
|
|
302
|
+
}
|
|
303
|
+
tokens.push({ type: "PARAM", value: path2.slice(i + 1, start) });
|
|
304
|
+
i = start;
|
|
305
|
+
} else if (char === "*") {
|
|
306
|
+
let start = i + 1;
|
|
307
|
+
while (start < path2.length && /[a-zA-Z0-9_]/.test(path2[start])) {
|
|
308
|
+
start++;
|
|
309
|
+
}
|
|
310
|
+
tokens.push({ type: "WILDCARD", value: path2.slice(i + 1, start) });
|
|
311
|
+
i = start;
|
|
312
|
+
} else {
|
|
313
|
+
let start = i;
|
|
314
|
+
while (start < path2.length && !["/", ":", "*"].includes(path2[start])) {
|
|
315
|
+
start++;
|
|
316
|
+
}
|
|
317
|
+
tokens.push({ type: "TEXT", value: path2.slice(i, start) });
|
|
318
|
+
i = start;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return tokens;
|
|
322
|
+
}
|
|
323
|
+
tokensToRegex(tokens) {
|
|
324
|
+
const regexParts = [];
|
|
325
|
+
for (const token of tokens) {
|
|
326
|
+
if (token.type === "SLASH") {
|
|
327
|
+
regexParts.push("\\/");
|
|
328
|
+
} else if (token.type === "PARAM") {
|
|
329
|
+
regexParts.push(`(?<${token.value}>[^\\/]+)`);
|
|
330
|
+
} else if (token.type === "WILDCARD") {
|
|
331
|
+
regexParts.push("(.+)");
|
|
332
|
+
} else if (token.type === "TEXT") {
|
|
333
|
+
regexParts.push(token.value.replace(/[-\/\\^$.*+?()[\]{}|]/g, "\\$&"));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (regexParts.length > 0 && regexParts[regexParts.length - 1] === "\\/") {
|
|
337
|
+
regexParts[regexParts.length - 1] = "\\/?";
|
|
338
|
+
} else {
|
|
339
|
+
regexParts.push("\\/?");
|
|
340
|
+
}
|
|
341
|
+
return new RegExp(`^${regexParts.join("")}$`);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* to evaludate if request is a match for this route
|
|
345
|
+
* @param request http request
|
|
346
|
+
* @returns return true if route is a match for this request
|
|
347
|
+
*/
|
|
348
|
+
test(request) {
|
|
349
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
353
|
+
return this.testPath(url.pathname);
|
|
354
|
+
}
|
|
355
|
+
testPath(pathname) {
|
|
356
|
+
return this.urlRegex.test(pathname);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* returns details of the match, otherwise it returns false
|
|
360
|
+
* @param request the request to match
|
|
361
|
+
* @returns object cotaining details of match including matched params
|
|
362
|
+
*/
|
|
363
|
+
match(request) {
|
|
364
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
368
|
+
const r = this.urlRegex.exec(url.pathname);
|
|
369
|
+
if (!r) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
url,
|
|
374
|
+
params: r.groups || {}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
addMiddleware(middlewares) {
|
|
378
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
getMiddlewares() {
|
|
382
|
+
return this.middlewares;
|
|
383
|
+
}
|
|
384
|
+
callHanlder(request, response) {
|
|
385
|
+
return this.handler(request, response);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// src/Router.mts
|
|
390
|
+
var import_path = __toESM(require("path"), 1);
|
|
391
|
+
var Router = class {
|
|
392
|
+
middlewares = [];
|
|
393
|
+
routes = [];
|
|
394
|
+
addRoute(methods, path2, handler) {
|
|
395
|
+
const route = new Route(methods, path2, handler);
|
|
396
|
+
this.routes.push(route);
|
|
397
|
+
return route;
|
|
398
|
+
}
|
|
399
|
+
getMiddlewares() {
|
|
400
|
+
return [...this.middlewares];
|
|
401
|
+
}
|
|
402
|
+
addController(controller) {
|
|
403
|
+
const basePath = controller.basePath || "";
|
|
404
|
+
for (const route of controller.routes) {
|
|
405
|
+
const urlPath = import_path.default.join(basePath, route.path);
|
|
406
|
+
this.addRoute(route.methods, urlPath, async (req, res) => {
|
|
407
|
+
const controllerInstance = controller.getInstance();
|
|
408
|
+
return await controllerInstance[route.handler]();
|
|
409
|
+
}).addMiddleware([...controller.baseMiddlewares, ...route.middlewares]);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
addGlobalMiddleware(middlewares) {
|
|
413
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
414
|
+
}
|
|
415
|
+
resolve(request) {
|
|
416
|
+
for (const route of this.routes) {
|
|
417
|
+
if (route.test(request)) {
|
|
418
|
+
return route;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return void 0;
|
|
422
|
+
}
|
|
423
|
+
resolveMultiple(request) {
|
|
424
|
+
const rc = [];
|
|
425
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
426
|
+
for (const route of this.routes) {
|
|
427
|
+
if (route.testPath(url.pathname)) {
|
|
428
|
+
rc.push(route);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return rc;
|
|
432
|
+
}
|
|
433
|
+
getCompiledRoute(request, response) {
|
|
434
|
+
const route = this.resolve(request);
|
|
435
|
+
if (!route) {
|
|
436
|
+
return void 0;
|
|
437
|
+
}
|
|
438
|
+
const match = route.match(request);
|
|
439
|
+
if (!match) {
|
|
440
|
+
return void 0;
|
|
441
|
+
}
|
|
442
|
+
request.params = match.params;
|
|
443
|
+
return new CompiledRoute(route, request, response, this.middlewares);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// src/helpers.mts
|
|
448
|
+
async function runNext(middlewares, req, res, final) {
|
|
449
|
+
let index = 0;
|
|
450
|
+
async function next() {
|
|
451
|
+
if (index >= middlewares.length) {
|
|
452
|
+
return await final(req, res);
|
|
453
|
+
}
|
|
454
|
+
const middleware = middlewares[index++];
|
|
455
|
+
if (middleware instanceof Middleware) {
|
|
456
|
+
await middleware.call(req, res, next);
|
|
457
|
+
} else if (typeof middleware === "function") {
|
|
458
|
+
await middleware(req, res, next);
|
|
459
|
+
} else {
|
|
460
|
+
throw new Error("does not know how to run middleware");
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
await next();
|
|
464
|
+
}
|
|
26
465
|
// Annotate the CommonJS export names for ESM import in node:
|
|
27
466
|
0 && (module.exports = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
467
|
+
BaseController,
|
|
468
|
+
CompiledRoute,
|
|
469
|
+
Controller,
|
|
470
|
+
Delete,
|
|
471
|
+
Get,
|
|
472
|
+
Middleware,
|
|
473
|
+
MiddlewareFactory,
|
|
474
|
+
Options,
|
|
475
|
+
Patch,
|
|
476
|
+
Post,
|
|
477
|
+
Put,
|
|
478
|
+
Route,
|
|
479
|
+
Router,
|
|
480
|
+
createParamDecorator,
|
|
481
|
+
runNext
|
|
36
482
|
});
|
|
37
483
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './types';\nexport * from './CompiledRoute';\nexport * from './Controller';\nexport * from './Route';\nexport * from './Router';\nexport * from './Middleware';\nexport * from './MiddlewareFactory';\nexport * from './helpers';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,oBAAd;AACA,0BAAc,4BADd;AAEA,0BAAc,yBAFd;AAGA,0BAAc,oBAHd;AAIA,0BAAc,qBAJd;AAKA,0BAAc,yBALd;AAMA,0BAAc,gCANd;AAOA,0BAAc,sBAPd;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/Middleware.mts","../src/MiddlewareFactory.mts","../src/CompiledRoute.mts","../src/Controller.mts","../src/Route.mts","../src/Router.mts","../src/helpers.mts"],"sourcesContent":["export * from './types.mjs';\nexport * from './CompiledRoute.mjs';\nexport * from './Controller.mjs';\nexport * from './Route.mjs';\nexport * from './Router.mjs';\nexport * from './Middleware.mjs';\nexport * from './MiddlewareFactory.mjs';\nexport * from './helpers.mjs';\n","import { LexerToken, Request, Response } from './types.mjs';\n\nexport abstract class Middleware {\n protected constructor(params: any = {}) {}\n static getInstance(params: any): Middleware {\n throw new Error('Method not implemented. Please implement a static getInstance method.');\n }\n\n abstract call(req: Request, res: Response, next: () => Promise<void>): Promise<void>;\n}\n","import { Middleware } from './Middleware.mjs';\nimport { HandlerType, LexerToken, Request, Response } from './types.mjs';\n\nexport class MiddlewareFactory {\n public static create(func: HandlerType): Middleware {\n const cls = class extends Middleware {\n call(req: Request, res: Response, next: () => Promise<void>): Promise<void> {\n return func(req, res, next);\n }\n constructor(params: any = {}) {\n super(params);\n }\n };\n\n return new cls();\n }\n}\n","import { Middleware } from './Middleware.mjs';\nimport { MiddlewareFactory } from './MiddlewareFactory.mjs';\nimport { Route } from './Route.mjs';\nimport { HandlerType, MiddlewareProvider } from './types.mjs';\nimport { LexerToken, Request, Response } from './types.mjs';\n\nexport class CompiledRoute {\n constructor(\n public route: Route,\n public request: Request,\n public response: Response,\n public globalMiddlewares: MiddlewareProvider[] = []\n ) {\n this.prepareMiddlewares();\n }\n\n private middlewares: Middleware[] = [];\n\n getMiddlewares() {\n return this.middlewares;\n }\n\n private prepareMiddlewares() {\n this.middlewares = [];\n for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {\n if (middleware instanceof Middleware) {\n this.middlewares.push(middleware);\n } else if (this.isClass(middleware)) {\n this.middlewares.push((middleware as any).getInstance({}));\n } else if (typeof middleware === 'function') {\n this.middlewares.push(MiddlewareFactory.create(middleware as HandlerType));\n } else {\n throw new Error('Invalid middleware type');\n }\n }\n }\n\n isClass(func: any) {\n return typeof func === 'function' && /^class\\s/.test(Function.prototype.toString.call(func));\n }\n\n async run() {\n return await this.runMiddlewares(this.middlewares, this.request, this.response);\n }\n\n prepareOutputJsonFormat<T>(obj: object | Array<any>): T {\n function traverse(value: any): any {\n if (!value || typeof value !== 'object') {\n return value;\n }\n\n if (typeof value.toJson === 'function') {\n return traverse(value.toJson());\n }\n\n // to handle Date and other built-in types\n if (typeof value.toJSON === 'function') {\n return traverse(value.toJSON());\n }\n\n if (Array.isArray(value)) {\n return value.map(traverse);\n }\n\n const result: Record<string, any> = {};\n for (const key in value) {\n if (Object.prototype.hasOwnProperty.call(value, key)) {\n result[key] = traverse(value[key]);\n }\n }\n return result;\n }\n\n return traverse(obj);\n }\n\n convertToString(obj: any) {\n if (typeof obj === 'string') {\n return obj;\n } else if (obj instanceof Buffer) {\n return obj.toString();\n } else if (typeof obj === 'object') {\n return JSON.stringify(this.prepareOutputJsonFormat(obj));\n }\n return String(obj);\n }\n\n processResponseBody(res: Response, controller_rc: any) {\n if (controller_rc && res.writableEnded) {\n throw new Error('cannot write to response, response has already ended');\n }\n\n if (res.writableEnded) {\n return;\n }\n\n if (controller_rc) {\n const header_content_type = res.getHeader('Content-Type');\n if (!header_content_type && typeof controller_rc === 'object') {\n res.setHeader('Content-Type', 'application/json');\n } else if (!header_content_type) {\n res.setHeader('Content-Type', 'text/plain');\n }\n\n res.end(this.convertToString(controller_rc));\n return;\n } else {\n res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;\n res.end();\n }\n }\n\n async runMiddlewares(middlewares: Middleware[], req: Request, res: Response) {\n let index = 0;\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const me = this;\n\n async function next() {\n if (index >= middlewares.length) {\n const controller_rc = await me.route.callHanlder(req, res);\n await me.processResponseBody(res, controller_rc);\n return;\n }\n\n const middleware: Middleware | any = middlewares[index++];\n\n if (middleware instanceof Middleware) {\n await middleware.call(req, res, next);\n } else if (typeof middleware === 'function') {\n await middleware(req, res, next);\n } else {\n throw new Error('does not know how to run middleware');\n }\n }\n\n await next();\n }\n}\n","import { ControllerDecoratorOptions, HttpMethod, MiddlewareProvider } from './types.mjs';\nimport { Middleware } from './Middleware.mjs';\n\nexport class BaseController {\n declare static routes: {\n methods: HttpMethod[];\n path: string;\n handler: string;\n middlewares: MiddlewareProvider[];\n }[];\n static basePath: string = '';\n static baseMiddlewares: MiddlewareProvider[];\n\n static getInstance() {\n return new this();\n }\n}\n\nexport function Controller(path: string, options: ControllerDecoratorOptions = {}): ClassDecorator {\n return function (target: any) {\n (target as any).routes = (target as any).routes || [];\n (target as any).basePath = path;\n (target as any).baseMiddlewares = options.middlewares || [];\n };\n}\n\nfunction createHttpDecorator(data: {\n methods: HttpMethod[];\n path: string;\n middlewares: MiddlewareProvider[];\n}): MethodDecorator {\n return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {\n const ctor = target.constructor;\n if (!ctor.routes) ctor.routes = [];\n ctor.routes.push({\n methods: data.methods,\n path: data.path,\n handler: propertyKey,\n middlewares: data.middlewares || [],\n });\n\n const originalMethod = descriptor.value!;\n const paramKeys = Reflect.ownKeys(target.constructor);\n const methodName = propertyKey.toString();\n\n descriptor.value = async function (...args: any[]) {\n const paramCustomKeys = paramKeys.filter(\n (key) =>\n typeof key === 'string' && key.startsWith(`${methodName}:`) && key.endsWith(':custom')\n );\n for (const paramKey of paramCustomKeys) {\n const paramIndex = parseInt((paramKey as string).split(':')[1]);\n let method = Reflect.get(target.constructor, paramKey.toString());\n if (typeof paramIndex === 'number' && typeof method === 'function') {\n args[paramIndex] = await method();\n }\n }\n\n return originalMethod.apply(this, args);\n };\n };\n}\n\nexport function Get(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['GET', 'HEAD'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Post(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['POST'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Put(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['PUT'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Patch(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['PATCH'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Delete(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['DELETE'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Options(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['OPTIONS'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function createParamDecorator(func: () => Promise<any> | (() => any)): ParameterDecorator {\n return function MyParamDecorator(\n target: Object,\n propertyKey: string | symbol | undefined,\n parameterIndex: number\n ) {\n Reflect.set(target.constructor, `${propertyKey?.toString()}:${parameterIndex}:custom`, func);\n };\n}\n","import { Middleware } from './Middleware.mjs';\nimport { MiddlewareFactory } from './MiddlewareFactory.mjs';\nimport { HandlerType, HttpMethod, MiddlewareProvider } from './types.mjs';\nimport { LexerToken, Request, Response } from './types.mjs';\n\nexport class Route {\n private middlewares: MiddlewareProvider[] = [];\n private urlRegex: RegExp;\n constructor(\n public methods: HttpMethod[],\n public path: string,\n public handler: HandlerType\n ) {\n this.urlRegex = this.pathToRegex(path);\n }\n pathToRegex(path: string): RegExp {\n const lex = this.lexUrlPath(path);\n return this.tokensToRegex(lex);\n }\n\n lexUrlPath(path: string) {\n const tokens = [];\n let i = 0;\n\n while (i < path.length) {\n const char = path[i];\n\n if (char === '/') {\n tokens.push({ type: 'SLASH', value: '/' });\n i++;\n } else if (char === ':') {\n let start = i + 1;\n while (start < path.length && /[a-zA-Z0-9_]/.test(path[start])) {\n start++;\n }\n tokens.push({ type: 'PARAM', value: path.slice(i + 1, start) });\n i = start;\n } else if (char === '*') {\n let start = i + 1;\n while (start < path.length && /[a-zA-Z0-9_]/.test(path[start])) {\n start++;\n }\n tokens.push({ type: 'WILDCARD', value: path.slice(i + 1, start) });\n i = start;\n } else {\n let start = i;\n while (start < path.length && !['/', ':', '*'].includes(path[start])) {\n start++;\n }\n tokens.push({ type: 'TEXT', value: path.slice(i, start) });\n i = start;\n }\n }\n\n return tokens;\n }\n tokensToRegex(tokens: LexerToken[]) {\n const regexParts = [];\n\n for (const token of tokens) {\n if (token.type === 'SLASH') {\n regexParts.push('\\\\/');\n } else if (token.type === 'PARAM') {\n regexParts.push(`(?<${token.value}>[^\\\\/]+)`);\n } else if (token.type === 'WILDCARD') {\n regexParts.push('(.+)');\n } else if (token.type === 'TEXT') {\n regexParts.push(token.value.replace(/[-\\/\\\\^$.*+?()[\\]{}|]/g, '\\\\$&'));\n }\n }\n\n if (regexParts.length > 0 && regexParts[regexParts.length - 1] === '\\\\/') {\n regexParts[regexParts.length - 1] = '\\\\/?';\n } else {\n regexParts.push('\\\\/?');\n }\n\n return new RegExp(`^${regexParts.join('')}$`);\n }\n\n /**\n * to evaludate if request is a match for this route\n * @param request http request\n * @returns return true if route is a match for this request\n */\n test(request: Request) {\n if (this.methods.indexOf(request.method as HttpMethod) === -1) {\n return false;\n }\n const url = new URL(request.url || '/', 'http://localhost');\n return this.testPath(url.pathname);\n }\n\n testPath(pathname: string) {\n return this.urlRegex.test(pathname);\n }\n\n /**\n * returns details of the match, otherwise it returns false\n * @param request the request to match\n * @returns object cotaining details of match including matched params\n */\n match(request: Request) {\n if (this.methods.indexOf(request.method as HttpMethod) === -1) {\n return false;\n }\n\n const url = new URL(request.url || '/', 'http://localhost');\n\n const r = this.urlRegex.exec(url.pathname);\n if (!r) {\n return false;\n }\n\n return {\n url,\n params: r.groups || {},\n };\n }\n\n addMiddleware(middlewares: MiddlewareProvider | MiddlewareProvider[]) {\n this.middlewares = this.middlewares.concat(middlewares);\n return this;\n }\n\n getMiddlewares() {\n return this.middlewares;\n }\n\n callHanlder(request: Request, response: Response) {\n return this.handler(request, response);\n }\n}\n","import { CompiledRoute } from './CompiledRoute.mjs';\nimport { BaseController } from './Controller.mjs';\nimport { Middleware } from './Middleware.mjs';\nimport { MiddlewareFactory } from './MiddlewareFactory.mjs';\nimport { Route } from './Route.mjs';\nimport { HandlerType, HttpMethod, MiddlewareProvider } from './types.mjs';\nimport { LexerToken, Request, Response } from './types.mjs';\nimport path from 'path';\n\nexport class Router {\n private middlewares: MiddlewareProvider[] = [];\n routes: Route[] = [];\n addRoute(methods: HttpMethod[], path: string, handler: HandlerType) {\n const route: Route = new Route(methods, path, handler);\n this.routes.push(route);\n return route;\n }\n\n getMiddlewares() {\n return [...this.middlewares];\n }\n\n addController(controller: typeof BaseController) {\n const basePath = controller.basePath || '';\n for (const route of controller.routes) {\n const urlPath = path.join(basePath, route.path);\n this.addRoute(route.methods, urlPath, async (req: Request, res: Response) => {\n const controllerInstance = controller.getInstance();\n // @ts-ignore\n return await controllerInstance[route.handler]();\n }).addMiddleware([...controller.baseMiddlewares, ...route.middlewares]);\n }\n }\n\n addGlobalMiddleware(middlewares: MiddlewareProvider | MiddlewareProvider[]) {\n this.middlewares = this.middlewares.concat(middlewares);\n }\n\n resolve(request: Request): Route | undefined {\n for (const route of this.routes) {\n if (route.test(request)) {\n return route;\n }\n }\n return undefined;\n }\n\n resolveMultiple(request: Request): Route[] {\n const rc: Route[] = [];\n const url = new URL(request.url || '/', 'http://localhost');\n for (const route of this.routes) {\n if (route.testPath(url.pathname)) {\n rc.push(route);\n }\n }\n return rc;\n }\n\n getCompiledRoute(request: Request, response: Response) {\n const route = this.resolve(request);\n if (!route) {\n return undefined;\n }\n const match = route.match(request);\n if (!match) {\n return undefined;\n }\n\n request.params = match.params;\n return new CompiledRoute(route, request, response, this.middlewares);\n }\n}\n","import { Middleware } from './Middleware.mjs';\nimport { Request, Response } from './types.mjs';\n\nexport async function runNext(\n middlewares: Middleware[],\n req: Request,\n res: Response,\n final: (request: Request, response: Response) => Promise<void>\n) {\n let index = 0;\n\n async function next() {\n if (index >= middlewares.length) {\n return await final(req, res);\n }\n\n const middleware: Middleware | any = middlewares[index++];\n\n if (middleware instanceof Middleware) {\n await middleware.call(req, res, next);\n } else if (typeof middleware === 'function') {\n await middleware(req, res, next);\n } else {\n throw new Error('does not know how to run middleware');\n }\n }\n\n await next();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAe,aAAf,MAA0B;AAAA,EACrB,YAAY,SAAc,CAAC,GAAG;AAAA,EAAC;AAAA,EACzC,OAAO,YAAY,QAAyB;AAC1C,UAAM,IAAI,MAAM,uEAAuE;AAAA,EACzF;AAGF;;;ACNO,IAAM,oBAAN,MAAwB;AAAA,EAC7B,OAAc,OAAO,MAA+B;AAClD,UAAM,MAAM,cAAc,WAAW;AAAA,MACnC,KAAK,KAAc,KAAe,MAA0C;AAC1E,eAAO,KAAK,KAAK,KAAK,IAAI;AAAA,MAC5B;AAAA,MACA,YAAY,SAAc,CAAC,GA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pC,YAAM,WAAW,KAAK,KAAK,KAAK,IAAI;AAAA,IACtC,WAAW,OAAO,eAAe,YAAY;AAC3C,YAAM,WAAW,KAAK,KAAK,IAAI;AAAA,IACjC,OAAO;AACL,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAAA,EACF;AAEA,QAAM,KAAK;AACb;","names":["path","path","path"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export * from "./types";
|
|
2
|
-
export * from "./CompiledRoute";
|
|
3
|
-
export * from "./Controller";
|
|
4
|
-
export * from "./Route";
|
|
5
|
-
export * from "./Router";
|
|
6
|
-
export * from "./Middleware";
|
|
7
|
-
export * from "./MiddlewareFactory";
|
|
8
|
-
export * from "./helpers";
|
|
1
|
+
export * from "./types.mjs";
|
|
2
|
+
export * from "./CompiledRoute.mjs";
|
|
3
|
+
export * from "./Controller.mjs";
|
|
4
|
+
export * from "./Route.mjs";
|
|
5
|
+
export * from "./Router.mjs";
|
|
6
|
+
export * from "./Middleware.mjs";
|
|
7
|
+
export * from "./MiddlewareFactory.mjs";
|
|
8
|
+
export * from "./helpers.mjs";
|
|
9
9
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './types';\nexport * from './CompiledRoute';\nexport * from './Controller';\nexport * from './Route';\nexport * from './Router';\nexport * from './Middleware';\nexport * from './MiddlewareFactory';\nexport * from './helpers';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './types.mjs';\nexport * from './CompiledRoute.mjs';\nexport * from './Controller.mjs';\nexport * from './Route.mjs';\nexport * from './Router.mjs';\nexport * from './Middleware.mjs';\nexport * from './MiddlewareFactory.mjs';\nexport * from './helpers.mjs';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devbro/neko-router",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "general purpose router for URI to controller selection",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsup",
|
|
20
|
-
"test": "
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"test:watch": "vitest watch",
|
|
22
|
+
"test:coverage": "vitest run --coverage",
|
|
21
23
|
"format": "eslint . --fix --ext ts,tsx --report-unused-disable-directives --max-warnings 0 ",
|
|
22
24
|
"prepare": "husky",
|
|
23
25
|
"prettier": "prettier --write .",
|
|
@@ -38,7 +40,8 @@
|
|
|
38
40
|
"ts-jest": "^29.1.2",
|
|
39
41
|
"ts-node": "^10.9.2",
|
|
40
42
|
"tsup": "^8.0.2",
|
|
41
|
-
"typescript": "^5.3.3"
|
|
43
|
+
"typescript": "^5.3.3",
|
|
44
|
+
"vitest": "^3.2.4"
|
|
42
45
|
},
|
|
43
46
|
"dependencies": {},
|
|
44
47
|
"directories": {
|
package/dist/CompiledRoute.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { R as Request, a as Response, M as MiddlewareProvider, c as Middleware } from './Middleware-C8dxjlFD.js';
|
|
2
|
-
import { Route } from './Route.js';
|
|
3
|
-
import 'http';
|
|
4
|
-
|
|
5
|
-
declare class CompiledRoute {
|
|
6
|
-
route: Route;
|
|
7
|
-
request: Request;
|
|
8
|
-
response: Response;
|
|
9
|
-
globalMiddlewares: MiddlewareProvider[];
|
|
10
|
-
constructor(route: Route, request: Request, response: Response, globalMiddlewares?: MiddlewareProvider[]);
|
|
11
|
-
private middlewares;
|
|
12
|
-
getMiddlewares(): Middleware[];
|
|
13
|
-
private prepareMiddlewares;
|
|
14
|
-
isClass(func: any): boolean;
|
|
15
|
-
run(): Promise<void>;
|
|
16
|
-
prepareOutputJsonFormat<T>(obj: object | Array<any>): T;
|
|
17
|
-
convertToString(obj: any): string;
|
|
18
|
-
processResponseBody(res: Response, controller_rc: any): void;
|
|
19
|
-
runMiddlewares(middlewares: Middleware[], req: Request, res: Response): Promise<void>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export { CompiledRoute };
|