@abejarano/ts-express-server 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +14 -2
  2. package/dist/BootstrapServer.d.ts +13 -6
  3. package/dist/BootstrapServer.js +30 -23
  4. package/dist/BootstrapStandardServer.d.ts +2 -1
  5. package/dist/BootstrapStandardServer.js +6 -4
  6. package/dist/abstract/ServerModule.d.ts +2 -2
  7. package/dist/abstract/ServerService.d.ts +2 -2
  8. package/dist/abstract/ServerTypes.d.ts +55 -0
  9. package/dist/abstract/ServerTypes.js +8 -0
  10. package/dist/abstract/index.d.ts +1 -0
  11. package/dist/abstract/index.js +1 -0
  12. package/dist/adapters/BunAdapter.d.ts +8 -0
  13. package/dist/adapters/BunAdapter.js +418 -0
  14. package/dist/adapters/ExpressAdapter.d.ts +8 -0
  15. package/dist/adapters/ExpressAdapter.js +40 -0
  16. package/dist/adapters/index.d.ts +2 -0
  17. package/dist/adapters/index.js +18 -0
  18. package/dist/createRouter.d.ts +2 -0
  19. package/dist/createRouter.js +12 -0
  20. package/dist/decorators/Use.d.ts +2 -2
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.js +2 -0
  23. package/dist/modules/ControllersModule.d.ts +2 -2
  24. package/dist/modules/ControllersModule.js +14 -8
  25. package/dist/modules/CorsModule.d.ts +2 -2
  26. package/dist/modules/CorsModule.js +66 -2
  27. package/dist/modules/FileUploadModule.d.ts +4 -4
  28. package/dist/modules/FileUploadModule.js +43 -6
  29. package/dist/modules/RateLimitModule.d.ts +4 -4
  30. package/dist/modules/RateLimitModule.js +48 -7
  31. package/dist/modules/RequestContextModule.d.ts +2 -9
  32. package/dist/modules/RequestContextModule.js +53 -4
  33. package/dist/modules/RoutesModule.d.ts +4 -4
  34. package/dist/modules/RoutesModule.js +12 -3
  35. package/dist/modules/SecurityModule.d.ts +4 -4
  36. package/dist/modules/SecurityModule.js +43 -6
  37. package/dist/testing/createDecoratedTestApp.d.ts +3 -3
  38. package/dist/testing/createDecoratedTestApp.js +8 -4
  39. package/package.json +6 -7
@@ -0,0 +1,418 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BunAdapter = void 0;
4
+ const ServerTypes_1 = require("../abstract/ServerTypes");
5
+ class BunResponse {
6
+ constructor() {
7
+ this.statusCode = 200;
8
+ this.headers = new Headers();
9
+ this.body = null;
10
+ this.ended = false;
11
+ this.endPromise = new Promise((resolve) => {
12
+ this.resolveEnd = resolve;
13
+ });
14
+ }
15
+ status(code) {
16
+ this.statusCode = code;
17
+ return this;
18
+ }
19
+ set(name, value) {
20
+ this.headers.set(name, value);
21
+ return this;
22
+ }
23
+ header(name, value) {
24
+ return this.set(name, value);
25
+ }
26
+ json(body) {
27
+ if (!this.headers.has("content-type")) {
28
+ this.headers.set("content-type", "application/json");
29
+ }
30
+ this.body = JSON.stringify(body ?? null);
31
+ this.ended = true;
32
+ this.resolveEnd?.();
33
+ }
34
+ send(body) {
35
+ if (body instanceof Response) {
36
+ this.rawResponse = body;
37
+ this.ended = true;
38
+ this.resolveEnd?.();
39
+ return;
40
+ }
41
+ if (typeof body === "string" || body instanceof Uint8Array) {
42
+ this.body = body;
43
+ this.ended = true;
44
+ this.resolveEnd?.();
45
+ return;
46
+ }
47
+ if (body === undefined) {
48
+ this.body = null;
49
+ this.ended = true;
50
+ this.resolveEnd?.();
51
+ return;
52
+ }
53
+ if (!this.headers.has("content-type")) {
54
+ this.headers.set("content-type", "application/json");
55
+ }
56
+ this.body = JSON.stringify(body);
57
+ this.ended = true;
58
+ this.resolveEnd?.();
59
+ }
60
+ end(body) {
61
+ if (body !== undefined) {
62
+ this.send(body);
63
+ return;
64
+ }
65
+ this.ended = true;
66
+ this.resolveEnd?.();
67
+ }
68
+ isEnded() {
69
+ return this.ended;
70
+ }
71
+ waitForEnd() {
72
+ return this.endPromise;
73
+ }
74
+ toResponse() {
75
+ if (this.rawResponse) {
76
+ return this.rawResponse;
77
+ }
78
+ return new Response(this.body, {
79
+ status: this.statusCode,
80
+ headers: this.headers,
81
+ });
82
+ }
83
+ }
84
+ class BunRouter {
85
+ constructor() {
86
+ this.middlewares = [];
87
+ this.routes = [];
88
+ }
89
+ use(pathOrHandler, ...handlers) {
90
+ if (typeof pathOrHandler === "string") {
91
+ const path = pathOrHandler;
92
+ const resolvedHandlers = normalizeHandlers(handlers).map((handler) => handler instanceof BunRouter
93
+ ? this.wrapRouter(handler, path)
94
+ : handler);
95
+ this.middlewares.push({ path, handlers: resolvedHandlers });
96
+ return;
97
+ }
98
+ const resolvedHandlers = normalizeHandlers([pathOrHandler, ...handlers]);
99
+ const wrappedHandlers = resolvedHandlers.map((handler) => handler instanceof BunRouter
100
+ ? this.wrapRouter(handler)
101
+ : handler);
102
+ this.middlewares.push({ handlers: wrappedHandlers });
103
+ }
104
+ get(path, ...handlers) {
105
+ this.routes.push({ method: "GET", path, handlers });
106
+ }
107
+ post(path, ...handlers) {
108
+ this.routes.push({ method: "POST", path, handlers });
109
+ }
110
+ put(path, ...handlers) {
111
+ this.routes.push({ method: "PUT", path, handlers });
112
+ }
113
+ delete(path, ...handlers) {
114
+ this.routes.push({ method: "DELETE", path, handlers });
115
+ }
116
+ patch(path, ...handlers) {
117
+ this.routes.push({ method: "PATCH", path, handlers });
118
+ }
119
+ async handle(req, res, done, pathOverride, suppressNotFound) {
120
+ const path = normalizePath(pathOverride ?? req.path);
121
+ const middlewares = this.collectMiddlewares(path);
122
+ const routeMatch = this.matchRoute(req.method, path);
123
+ if (routeMatch) {
124
+ req.params = routeMatch.params;
125
+ }
126
+ const handlers = [
127
+ ...middlewares.flatMap((mw) => mw.handlers),
128
+ ...(routeMatch ? routeMatch.route.handlers : []),
129
+ ];
130
+ let chainCompleted = false;
131
+ try {
132
+ chainCompleted = await runHandlers(handlers, req, res);
133
+ }
134
+ catch (error) {
135
+ res.status(500).json({ message: "Internal server error" });
136
+ done(error);
137
+ return;
138
+ }
139
+ if (!routeMatch) {
140
+ if (chainCompleted && !res.isEnded() && !suppressNotFound) {
141
+ res.status(404).json({ message: "Not found" });
142
+ }
143
+ if (chainCompleted) {
144
+ done();
145
+ }
146
+ return;
147
+ }
148
+ if (chainCompleted) {
149
+ done();
150
+ }
151
+ }
152
+ collectMiddlewares(path) {
153
+ return this.middlewares.filter((layer) => {
154
+ if (!layer.path) {
155
+ return true;
156
+ }
157
+ const normalized = normalizePath(layer.path);
158
+ if (normalized === "/") {
159
+ return true;
160
+ }
161
+ return path === normalized || path.startsWith(`${normalized}/`);
162
+ });
163
+ }
164
+ matchRoute(method, path) {
165
+ for (const route of this.routes) {
166
+ if (route.method !== method.toUpperCase()) {
167
+ continue;
168
+ }
169
+ const match = matchPath(route.path, path);
170
+ if (match) {
171
+ return { route, params: match };
172
+ }
173
+ }
174
+ return null;
175
+ }
176
+ wrapRouter(router, basePath) {
177
+ return async (req, res, next) => {
178
+ if (!basePath) {
179
+ await router.handle(req, res, next, undefined, true);
180
+ return;
181
+ }
182
+ const normalizedBase = normalizePath(basePath);
183
+ const currentPath = normalizePath(req.path);
184
+ if (normalizedBase === "/") {
185
+ await router.handle(req, res, next, currentPath, true);
186
+ return;
187
+ }
188
+ if (currentPath !== normalizedBase &&
189
+ !currentPath.startsWith(`${normalizedBase}/`)) {
190
+ return next();
191
+ }
192
+ const nestedPath = stripPrefix(currentPath, normalizedBase);
193
+ await router.handle(req, res, next, nestedPath, true);
194
+ };
195
+ }
196
+ }
197
+ class BunApp extends BunRouter {
198
+ createFetchHandler() {
199
+ return async (request, server) => {
200
+ const client = server?.requestIP?.(request);
201
+ const req = createRequest(request, client?.address);
202
+ const res = new BunResponse();
203
+ await this.handle(req, res, () => undefined);
204
+ if (!res.isEnded()) {
205
+ res.status(204).end();
206
+ }
207
+ return res.toResponse();
208
+ };
209
+ }
210
+ }
211
+ class BunAdapter {
212
+ constructor() {
213
+ this.runtime = ServerTypes_1.ServerRuntime.Bun;
214
+ }
215
+ createApp() {
216
+ return new BunApp();
217
+ }
218
+ createRouter() {
219
+ return new BunRouter();
220
+ }
221
+ configure(app, _port) {
222
+ const bunApp = app;
223
+ bunApp.use(parseJsonBody);
224
+ bunApp.use(parseUrlEncodedBody);
225
+ }
226
+ listen(app, port, onListen) {
227
+ const bunApp = app;
228
+ const server = Bun.serve({
229
+ port,
230
+ fetch: bunApp.createFetchHandler(),
231
+ });
232
+ onListen();
233
+ return {
234
+ close: (callback) => {
235
+ server.stop(true);
236
+ callback?.();
237
+ },
238
+ };
239
+ }
240
+ }
241
+ exports.BunAdapter = BunAdapter;
242
+ const parseJsonBody = async (req, _res, next) => {
243
+ if (!req.raw || req.body !== undefined) {
244
+ return next();
245
+ }
246
+ const contentType = String(req.headers["content-type"] || "");
247
+ if (!contentType.includes("application/json")) {
248
+ return next();
249
+ }
250
+ try {
251
+ req.body = await req.raw.json();
252
+ }
253
+ catch {
254
+ req.body = undefined;
255
+ }
256
+ next();
257
+ };
258
+ const parseUrlEncodedBody = async (req, _res, next) => {
259
+ if (!req.raw || req.body !== undefined) {
260
+ return next();
261
+ }
262
+ const contentType = String(req.headers["content-type"] || "");
263
+ if (!contentType.includes("application/x-www-form-urlencoded")) {
264
+ return next();
265
+ }
266
+ const text = await req.raw.text();
267
+ req.body = Object.fromEntries(new URLSearchParams(text));
268
+ next();
269
+ };
270
+ function createRequest(request, ipOverride) {
271
+ const url = new URL(request.url);
272
+ const headers = toHeaderRecord(request.headers);
273
+ const query = toQueryRecord(url.searchParams);
274
+ return {
275
+ method: request.method.toUpperCase(),
276
+ path: normalizePath(url.pathname),
277
+ originalUrl: url.pathname + url.search,
278
+ params: {},
279
+ query,
280
+ headers,
281
+ cookies: parseCookies(headers.cookie),
282
+ ip: ipOverride || extractIp(headers),
283
+ raw: request,
284
+ };
285
+ }
286
+ function toHeaderRecord(headers) {
287
+ const record = {};
288
+ headers.forEach((value, key) => {
289
+ record[key.toLowerCase()] = value;
290
+ });
291
+ return record;
292
+ }
293
+ function toQueryRecord(search) {
294
+ const record = {};
295
+ for (const [key, value] of search.entries()) {
296
+ const existing = record[key];
297
+ if (existing === undefined) {
298
+ record[key] = value;
299
+ }
300
+ else if (Array.isArray(existing)) {
301
+ existing.push(value);
302
+ }
303
+ else {
304
+ record[key] = [existing, value];
305
+ }
306
+ }
307
+ return record;
308
+ }
309
+ function parseCookies(cookieHeader) {
310
+ if (!cookieHeader) {
311
+ return undefined;
312
+ }
313
+ const cookies = {};
314
+ cookieHeader.split(";").forEach((entry) => {
315
+ const [name, ...rest] = entry.trim().split("=");
316
+ if (!name) {
317
+ return;
318
+ }
319
+ cookies[name] = decodeURIComponent(rest.join("="));
320
+ });
321
+ return cookies;
322
+ }
323
+ function extractIp(headers) {
324
+ const forwarded = headers["x-forwarded-for"];
325
+ if (forwarded) {
326
+ return forwarded.split(",")[0]?.trim();
327
+ }
328
+ return headers["x-real-ip"];
329
+ }
330
+ function normalizePath(path) {
331
+ if (!path.startsWith("/")) {
332
+ path = `/${path}`;
333
+ }
334
+ if (path.length > 1 && path.endsWith("/")) {
335
+ return path.slice(0, -1);
336
+ }
337
+ return path;
338
+ }
339
+ function stripPrefix(path, prefix) {
340
+ if (!path.startsWith(prefix)) {
341
+ return path;
342
+ }
343
+ const stripped = path.slice(prefix.length);
344
+ return normalizePath(stripped || "/");
345
+ }
346
+ function matchPath(routePath, requestPath) {
347
+ const normalizedRoute = normalizePath(routePath);
348
+ const normalizedRequest = normalizePath(requestPath);
349
+ if (normalizedRoute === normalizedRequest) {
350
+ return {};
351
+ }
352
+ const routeParts = normalizedRoute.split("/").filter(Boolean);
353
+ const requestParts = normalizedRequest.split("/").filter(Boolean);
354
+ if (routeParts.length !== requestParts.length) {
355
+ return null;
356
+ }
357
+ const params = {};
358
+ for (let index = 0; index < routeParts.length; index += 1) {
359
+ const routePart = routeParts[index];
360
+ const requestPart = requestParts[index];
361
+ if (routePart.startsWith(":")) {
362
+ params[routePart.slice(1)] = decodeURIComponent(requestPart);
363
+ continue;
364
+ }
365
+ if (routePart !== requestPart) {
366
+ return null;
367
+ }
368
+ }
369
+ return params;
370
+ }
371
+ function normalizeHandlers(inputs) {
372
+ const handlers = [];
373
+ for (const input of inputs) {
374
+ if (Array.isArray(input)) {
375
+ handlers.push(...input);
376
+ continue;
377
+ }
378
+ handlers.push(input);
379
+ }
380
+ return handlers;
381
+ }
382
+ async function runHandlers(handlers, req, res) {
383
+ let index = 0;
384
+ const dispatch = async () => {
385
+ const handler = handlers[index];
386
+ index += 1;
387
+ if (!handler) {
388
+ return true;
389
+ }
390
+ let nextCalled = false;
391
+ let nextPromise;
392
+ let resolveNext;
393
+ const nextSignal = new Promise((resolve) => {
394
+ resolveNext = resolve;
395
+ });
396
+ const wrappedNext = (nextErr) => {
397
+ if (nextErr) {
398
+ throw nextErr;
399
+ }
400
+ nextCalled = true;
401
+ resolveNext?.();
402
+ nextPromise = dispatch();
403
+ return nextPromise;
404
+ };
405
+ await handler(req, res, wrappedNext);
406
+ if (!nextCalled) {
407
+ if (res.isEnded()) {
408
+ return false;
409
+ }
410
+ await Promise.race([nextSignal, res.waitForEnd()]);
411
+ if (!nextCalled || res.isEnded()) {
412
+ return false;
413
+ }
414
+ }
415
+ return nextPromise ? await nextPromise : true;
416
+ };
417
+ return dispatch();
418
+ }
@@ -0,0 +1,8 @@
1
+ import { ServerAdapter, ServerApp, ServerInstance, ServerRouter, ServerRuntime } from "../abstract/ServerTypes";
2
+ export declare class ExpressAdapter implements ServerAdapter {
3
+ runtime: ServerRuntime;
4
+ createApp(): ServerApp;
5
+ createRouter(): ServerRouter;
6
+ configure(app: ServerApp, port: number): void;
7
+ listen(app: ServerApp, port: number, onListen: () => void): ServerInstance;
8
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExpressAdapter = void 0;
4
+ const ServerTypes_1 = require("../abstract/ServerTypes");
5
+ let expressModule = null;
6
+ const getExpressModule = () => {
7
+ if (!expressModule) {
8
+ expressModule = require("express");
9
+ }
10
+ return expressModule;
11
+ };
12
+ class ExpressAdapter {
13
+ constructor() {
14
+ this.runtime = ServerTypes_1.ServerRuntime.Express;
15
+ }
16
+ createApp() {
17
+ const express = getExpressModule();
18
+ return express();
19
+ }
20
+ createRouter() {
21
+ const express = getExpressModule();
22
+ return express.Router();
23
+ }
24
+ configure(app, port) {
25
+ const expressApp = app;
26
+ const express = getExpressModule();
27
+ const bodyParser = require("body-parser");
28
+ const cookieParser = require("cookie-parser");
29
+ expressApp.use(express.json());
30
+ expressApp.use(bodyParser.urlencoded({ extended: true }));
31
+ expressApp.use(cookieParser());
32
+ expressApp.set("port", port);
33
+ expressApp.set("trust proxy", 1);
34
+ }
35
+ listen(app, port, onListen) {
36
+ const expressApp = app;
37
+ return expressApp.listen(port, onListen);
38
+ }
39
+ }
40
+ exports.ExpressAdapter = ExpressAdapter;
@@ -0,0 +1,2 @@
1
+ export * from "./BunAdapter";
2
+ export * from "./ExpressAdapter";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./BunAdapter"), exports);
18
+ __exportStar(require("./ExpressAdapter"), exports);
@@ -0,0 +1,2 @@
1
+ import { ServerRuntime, ServerRouter } from "./abstract";
2
+ export declare const createRouter: (runtime?: ServerRuntime) => ServerRouter;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRouter = void 0;
4
+ const abstract_1 = require("./abstract");
5
+ const adapters_1 = require("./adapters");
6
+ const createRouter = (runtime = abstract_1.ServerRuntime.Express) => {
7
+ if (runtime === abstract_1.ServerRuntime.Bun) {
8
+ return new adapters_1.BunAdapter().createRouter();
9
+ }
10
+ return new adapters_1.ExpressAdapter().createRouter();
11
+ };
12
+ exports.createRouter = createRouter;
@@ -1,3 +1,3 @@
1
- import { RequestHandler } from "express";
1
+ import { ServerHandler } from "../abstract";
2
2
  import "reflect-metadata";
3
- export declare function Use(middleware: RequestHandler | RequestHandler[]): (target: any, key?: string, descriptor?: PropertyDescriptor) => void;
3
+ export declare function Use(middleware: ServerHandler | ServerHandler[]): (target: any, key?: string, descriptor?: PropertyDescriptor) => void;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import "reflect-metadata";
2
2
  export { BootstrapServer } from "./BootstrapServer";
3
+ export type { BootstrapServerOptions } from "./BootstrapServer";
3
4
  export { BootstrapStandardServer } from "./BootstrapStandardServer";
4
5
  export type { BootstrapStandardServerOptions } from "./BootstrapStandardServer";
6
+ export * from "./adapters";
7
+ export * from "./createRouter";
5
8
  export * from "./modules";
6
9
  export * from "./abstract";
7
10
  export * from "./decorators";
package/dist/index.js CHANGED
@@ -20,6 +20,8 @@ var BootstrapServer_1 = require("./BootstrapServer");
20
20
  Object.defineProperty(exports, "BootstrapServer", { enumerable: true, get: function () { return BootstrapServer_1.BootstrapServer; } });
21
21
  var BootstrapStandardServer_1 = require("./BootstrapStandardServer");
22
22
  Object.defineProperty(exports, "BootstrapStandardServer", { enumerable: true, get: function () { return BootstrapStandardServer_1.BootstrapStandardServer; } });
23
+ __exportStar(require("./adapters"), exports);
24
+ __exportStar(require("./createRouter"), exports);
23
25
  __exportStar(require("./modules"), exports);
24
26
  __exportStar(require("./abstract"), exports);
25
27
  __exportStar(require("./decorators"), exports);
@@ -1,5 +1,5 @@
1
- import { Express } from "express";
2
1
  import { BaseServerModule } from "../abstract";
2
+ import { ServerApp, ServerContext } from "../abstract";
3
3
  import "reflect-metadata";
4
4
  export declare class ControllersModule extends BaseServerModule {
5
5
  private controllers;
@@ -7,5 +7,5 @@ export declare class ControllersModule extends BaseServerModule {
7
7
  priority: number;
8
8
  constructor(controllers: Function[]);
9
9
  getModuleName(): string;
10
- init(app: Express): void;
10
+ init(app: ServerApp, context?: ServerContext): void;
11
11
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ControllersModule = void 0;
4
- const express_1 = require("express");
5
4
  const abstract_1 = require("../abstract");
5
+ const adapters_1 = require("../adapters");
6
6
  const MetadataKeys_1 = require("../decorators/MetadataKeys");
7
7
  const Parameters_1 = require("../decorators/Parameters");
8
8
  require("reflect-metadata");
@@ -16,16 +16,16 @@ class ControllersModule extends abstract_1.BaseServerModule {
16
16
  getModuleName() {
17
17
  return this.name;
18
18
  }
19
- init(app) {
19
+ init(app, context) {
20
20
  this.controllers.forEach((ControllerClass) => {
21
21
  const basePath = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.BASE_PATH, ControllerClass);
22
22
  const classMiddlewares = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.MIDDLEWARE, ControllerClass) || [];
23
- const routers = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.ROUTERS, ControllerClass.prototype) ||
24
- [];
23
+ const routers = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.ROUTERS, ControllerClass.prototype) || [];
25
24
  if (!basePath) {
26
25
  return;
27
26
  }
28
- const router = (0, express_1.Router)();
27
+ const adapter = context?.adapter ?? new adapters_1.ExpressAdapter();
28
+ const router = adapter.createRouter();
29
29
  // Create a single controller instance per controller class (singleton pattern)
30
30
  const controllerInstance = new ControllerClass();
31
31
  // Apply class-level middlewares
@@ -36,7 +36,8 @@ class ControllersModule extends abstract_1.BaseServerModule {
36
36
  const routeHandler = ControllerClass.prototype[handlerName];
37
37
  const routeMiddlewares = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.MIDDLEWARE, ControllerClass.prototype, handlerName) || [];
38
38
  const parameterMetadata = Reflect.getMetadata(MetadataKeys_1.MetadataKeys.PARAMETERS, ControllerClass.prototype, handlerName) || [];
39
- router[method](path, ...routeMiddlewares, async (req, res, next) => {
39
+ const routerMethod = router[method]?.bind(router);
40
+ routerMethod(path, ...routeMiddlewares, async (req, res, next) => {
40
41
  if (parameterMetadata.length === 0) {
41
42
  try {
42
43
  const result = routeHandler.call(controllerInstance, req, res, next);
@@ -54,8 +55,13 @@ class ControllersModule extends abstract_1.BaseServerModule {
54
55
  parameterMetadata.forEach((param) => {
55
56
  switch (param.type) {
56
57
  case Parameters_1.ParameterType.BODY:
57
- args[param.index] =
58
- param.data && req.body ? req.body[param.data] : req.body;
58
+ if (param.data) {
59
+ const body = req.body;
60
+ args[param.index] = body ? body[param.data] : undefined;
61
+ }
62
+ else {
63
+ args[param.index] = req.body;
64
+ }
59
65
  break;
60
66
  case Parameters_1.ParameterType.PARAM:
61
67
  args[param.index] = param.data
@@ -1,5 +1,5 @@
1
- import { Express } from "express";
2
1
  import { BaseServerModule } from "../abstract";
2
+ import { ServerApp, ServerContext } from "../abstract";
3
3
  import cors from "cors";
4
4
  export declare class CorsModule extends BaseServerModule {
5
5
  name: string;
@@ -7,5 +7,5 @@ export declare class CorsModule extends BaseServerModule {
7
7
  private corsOptions;
8
8
  constructor(corsOptions?: cors.CorsOptions);
9
9
  getModuleName(): string;
10
- init(app: Express): void;
10
+ init(app: ServerApp, context?: ServerContext): void;
11
11
  }