-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inversify-express-utils: Inject server level middleware #453
Comments
+1 |
1 similar comment
+1 |
Since Looking at the code, there is no point in time where you can inject a global middleware that can receive per request injections. inversify-express-utils/src/server.ts Line 224 in 01725a5
Probably wouldn't be a bad idea to implement something like Web API's various (Not a maintainer here, just a drive by suggestion) |
It is possible to derive the Reflect.getMetadata('inversify-express-utils:httpcontext', req); I use a base middleware with a static method to help register server level middleware. (It manages async handlers as well.) The helper method does need direct access to your DI container to create a new instance of the middleware for each request. import { NextFunction, Request, Response } from 'express-serve-static-core';
import { Container, injectable } from 'inversify';
import { BaseMiddleware as InversifyBaseMiddleware } from 'inversify-express-utils';
export type MiddlewareFunction = (req: Request, res: Response, next: NextFunction) => void;
export interface IMiddleware {
handler: MiddlewareFunction;
}
@injectable()
export abstract class BaseMiddleware extends InversifyBaseMiddleware implements IMiddleware {
public static getHttpContext(req: Request) {
// FIXME: playing in inversify-express-utils' sandbox
return Reflect.getMetadata(
'inversify-express-utils:httpcontext',
req,
);
}
public static serverHandler(di: Container, middlewareToken: symbol): MiddlewareFunction {
return (req: Request, res: Response, next: NextFunction) => {
const middleware = di.get<BaseMiddleware>(middlewareToken);
// cast to any to allow writing of httpContext
(middleware as any).httpContext = BaseMiddleware.getHttpContext(req);
middleware.handler(req, res, next);
};
}
public handler(req: Request, res: Response, next: NextFunction) {
try {
const result = this.handle(req, res, next);
if (result != null && typeof (result as Promise<void>).then === 'function') {
Promise.resolve(result).catch(err => next(err));
}
} catch (err) {
next(err);
}
}
protected abstract handle(req: Request, res: Response, next: NextFunction): Promise<void> | void;
} Assuming I have const fooHandler = BaseMiddleware.serverHandler(container, FooMiddlewareToken);
app.use(fooHandler); |
@notaphplover @dcavanagh do you think we can maybe do something to actually be able to I don't think we can actually apply any decorators before the |
Hi @PodaruDragos I need to do some research, I'm almost new to the |
I don't see a clear way to inject server level express middleware, if that middleware has HttpContext as dependency.
Expected Behavior
Middleware is injected at setConfig level.
Current Behavior
The following error is thrown:
No matching bindings found for serviceIdentifier: Symbol(HttpContext)
The text was updated successfully, but these errors were encountered: