From 7985649bc6097e857c8d4d75e093031694ce7b83 Mon Sep 17 00:00:00 2001 From: Serhii Sol Date: Mon, 30 Jan 2023 15:18:16 +0200 Subject: [PATCH] feat(express): updated di version (#134) --- express/package-lock.json | 76 +++++++++++++++++++++++---------------- express/package.json | 12 +++---- express/src/express.ts | 8 ++--- express/src/middleware.ts | 8 ++--- 4 files changed, 60 insertions(+), 44 deletions(-) diff --git a/express/package-lock.json b/express/package-lock.json index 586dfbb..7283d9f 100644 --- a/express/package-lock.json +++ b/express/package-lock.json @@ -1,15 +1,15 @@ { "name": "@decorators/express", - "version": "2.8.0", + "version": "2.9.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@decorators/express", - "version": "2.8.0", + "version": "2.9.0", "license": "MIT", "devDependencies": { - "@decorators/di": ">=1.0.3", + "@decorators/di": "../di", "@types/express": "4.17.8", "@typescript-eslint/eslint-plugin": "^5.6.0", "@typescript-eslint/parser": "^5.6.0", @@ -17,26 +17,43 @@ "eslint-plugin-import": "^2.25.3", "eslint-plugin-jsdoc": "^37.1.0", "eslint-plugin-prefer-arrow": "^1.2.3", - "express": ">=4.16.3", - "typescript": ">=4.9.4" + "express": "^4.16.3", + "typescript": "^4.9.4" }, "peerDependencies": { - "@decorators/di": ">=1.0.3", - "express": ">=4.16.3" + "@decorators/di": "^2.0.0", + "express": "^4.16.3" } }, - "node_modules/@decorators/di": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@decorators/di/-/di-1.0.3.tgz", - "integrity": "sha512-wafyQo5lqGABT+Lh7Od9/qULg7DG/kZFU3mLUKZFuiV/KATYlnv198yQxaZUZerhUDoTl/cZKu9t4mJa0rZK4Q==", + "../di": { + "name": "@decorators/di", + "version": "2.0.0", "dev": true, + "license": "MIT", "dependencies": { "reflect-metadata": "0.1.13" }, - "engines": { - "node": ">=7.1.0" + "devDependencies": { + "@types/chai": "4.3.4", + "@types/mocha": "10.0.1", + "@typescript-eslint/eslint-plugin": "^5.6.0", + "@typescript-eslint/parser": "^5.6.0", + "chai": "4.3.7", + "eslint": "^8.4.0", + "eslint-plugin-import": "^2.25.3", + "eslint-plugin-jsdoc": "^37.1.0", + "eslint-plugin-prefer-arrow": "^1.2.3", + "jest": "29.3.1", + "mocha": "10.2.0", + "ts-jest": "29.0.5", + "ts-node": "10.9.1", + "typescript": "^4.9.4" } }, + "node_modules/@decorators/di": { + "resolved": "../di", + "link": true + }, "node_modules/@es-joy/jsdoccomment": { "version": "0.20.1", "resolved": "https://registry.npmjs.org/@es-joy/jsdoccomment/-/jsdoccomment-0.20.1.tgz", @@ -2762,12 +2779,6 @@ "node": ">= 0.8" } }, - "node_modules/reflect-metadata": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", - "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==", - "dev": true - }, "node_modules/regexp.prototype.flags": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz", @@ -3408,12 +3419,23 @@ }, "dependencies": { "@decorators/di": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@decorators/di/-/di-1.0.3.tgz", - "integrity": "sha512-wafyQo5lqGABT+Lh7Od9/qULg7DG/kZFU3mLUKZFuiV/KATYlnv198yQxaZUZerhUDoTl/cZKu9t4mJa0rZK4Q==", - "dev": true, + "version": "file:../di", "requires": { - "reflect-metadata": "0.1.13" + "@types/chai": "4.3.4", + "@types/mocha": "10.0.1", + "@typescript-eslint/eslint-plugin": "^5.6.0", + "@typescript-eslint/parser": "^5.6.0", + "chai": "4.3.7", + "eslint": "^8.4.0", + "eslint-plugin-import": "^2.25.3", + "eslint-plugin-jsdoc": "^37.1.0", + "eslint-plugin-prefer-arrow": "^1.2.3", + "jest": "29.3.1", + "mocha": "10.2.0", + "reflect-metadata": "0.1.13", + "ts-jest": "29.0.5", + "ts-node": "10.9.1", + "typescript": "^4.9.4" } }, "@es-joy/jsdoccomment": { @@ -5412,12 +5434,6 @@ "unpipe": "1.0.0" } }, - "reflect-metadata": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", - "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==", - "dev": true - }, "regexp.prototype.flags": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz", diff --git a/express/package.json b/express/package.json index 76788ae..4038fd3 100644 --- a/express/package.json +++ b/express/package.json @@ -1,15 +1,15 @@ { "name": "@decorators/express", - "version": "2.8.1", + "version": "2.9.0", "description": "node decorators - decorators for express library", "main": "lib/index.js", "types": "lib/index.d.ts", "peerDependencies": { - "express": ">=4.16.3", - "@decorators/di": ">=1.0.3" + "express": "^4.16.3", + "@decorators/di": "^2.0.0" }, "devDependencies": { - "@decorators/di": ">=1.0.3", + "@decorators/di": "../di", "@types/express": "4.17.8", "@typescript-eslint/eslint-plugin": "^5.6.0", "@typescript-eslint/parser": "^5.6.0", @@ -17,8 +17,8 @@ "eslint-plugin-jsdoc": "^37.1.0", "eslint-plugin-prefer-arrow": "^1.2.3", "eslint": "^8.4.0", - "express": ">=4.16.3", - "typescript": ">=4.9.4" + "express": "^4.16.3", + "typescript": "^4.9.4" }, "keywords": [ "nodejs", diff --git a/express/src/express.ts b/express/src/express.ts index 6e912f2..977482d 100644 --- a/express/src/express.ts +++ b/express/src/express.ts @@ -27,12 +27,12 @@ export function attachControllerInstances(app: Express | Router, controllers: In /** * Register controller via registering new Router */ -function registerController( +async function registerController( app: Application | Router, Controller: Type | InstanceType, - extractController: (c: Type | InstanceType) => InstanceType, + extractController: (c: Type | InstanceType) => Promise> | InstanceType, ) { - const controller = extractController(Controller); + const controller = await extractController(Controller); const meta = getMeta(controller); const router = Router(meta.routerOptions); @@ -142,7 +142,7 @@ function extractParameters(req: Request, res: Response, next: NextFunction, para /** * Get controller instance from container or instantiate one */ -function getController(Controller: Type): ExpressClass { +function getController(Controller: Type): Promise | ExpressClass { try { return Container.get(Controller); } catch { diff --git a/express/src/middleware.ts b/express/src/middleware.ts index 4a72a5a..4e3b9d9 100644 --- a/express/src/middleware.ts +++ b/express/src/middleware.ts @@ -20,7 +20,7 @@ export type ErrorMiddleware = ErrorMiddlewareFunction | Type { - invokeMiddleware(middleware, [req, res, next]); + invokeMiddleware(middleware, [req, res, next]).catch(next); }; } @@ -34,14 +34,14 @@ export const ERROR_MIDDLEWARE = new InjectionToken('ERROR_MIDDLEWARE'); */ export function errorMiddlewareHandler(): ErrorRequestHandler { return (error: Error, req: Request, res: Response, next: NextFunction) => { - invokeMiddleware(ERROR_MIDDLEWARE, [error, req, res, next]); + invokeMiddleware(ERROR_MIDDLEWARE, [error, req, res, next]).catch(next); }; } /** * Instantiate middleware and invoke it with arguments */ -function invokeMiddleware( +async function invokeMiddleware( middleware: InjectionToken | Middleware | ErrorMiddleware, args: Parameters | Parameters, ) { @@ -57,7 +57,7 @@ function invokeMiddleware( instance = middleware as MiddlewareFunction; } } else { - instance = Container.get(middleware); + instance = await Container.get(middleware); } const handler = (instance as MiddlewareClass | ErrorMiddlewareClass)?.use ?? instance;