Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat(express): updated di version (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Jan 30, 2023
1 parent 781a488 commit 7985649
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
76 changes: 46 additions & 30 deletions express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions express/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"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",
"eslint-plugin-import": "^2.25.3",
"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",
Expand Down
8 changes: 4 additions & 4 deletions express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>,
extractController: (c: Type | InstanceType<Type>) => InstanceType<Type>,
extractController: (c: Type | InstanceType<Type>) => Promise<InstanceType<Type>> | InstanceType<Type>,
) {
const controller = extractController(Controller);
const controller = await extractController(Controller);
const meta = getMeta(controller);
const router = Router(meta.routerOptions);

Expand Down Expand Up @@ -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> | ExpressClass {
try {
return Container.get(Controller);
} catch {
Expand Down
8 changes: 4 additions & 4 deletions express/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ErrorMiddleware = ErrorMiddlewareFunction | Type<ErrorMiddlewareClas
*/
export function middlewareHandler(middleware: Middleware): RequestHandler {
return (req: Request, res: Response, next: NextFunction) => {
invokeMiddleware(middleware, [req, res, next]);
invokeMiddleware(middleware, [req, res, next]).catch(next);
};
}

Expand All @@ -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<MiddlewareFunction> | Parameters<ErrorMiddlewareFunction>,
) {
Expand All @@ -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;
Expand Down

0 comments on commit 7985649

Please sign in to comment.