The motivation for this issue was this comment.
I would like to implement the following:
HttpContextThe idea of HttpContext is based on ASP.NET and is a feature that allows the request and response to objects to be injected.
What should happen then is the following:
this._container.createChild() to prevent rare conditions.TYPE.HttpContext and it includes current request and responseinterface HttpContext {
request: Request;
response: Response;
}
container.get is invoked.TYPE.HttpContext is resolved usinginRequestScope if the controller has a dependency on it.Controller are resolved.TYPE.HttpContext) are injected into de Controller.HttpContextThis will add an extra bit to the previous workflow. What should happen then is the following:
AuthProvider before a request hits the server.this._container.createChild() to prevent rare conditions.AuthProvider if it is available (undefined by default).TYPE.HttpContext and it includes current request, response, and userinterface HttpContext {
request: Request;
response: Response;
user: UserIdentity;
}
container.get is invoked.TYPE.HttpContext is resolved usinginRequestScope if the controller has a dependency on it.Controller are resolved.TYPE.HttpContext) are injected into de Controller.BaseHttpController to reduce boilerplateWe could also create a base controller instance that gets HttpContext injected by default so users will be able to do:
import * as express from "express";
import { interfaces, controller, httpGet, httpPost, httpDelete, request, queryParam, response, requestParam, BaseHttpController } from "inversify-express-utils";
import { injectable, inject } from "inversify";
@controller("/foo")
@injectable()
export class FooController extends BaseHttpController {
@inject("FooService") private fooService: FooService;
@httpGet("/")
private index(): string {
if (this.httpContext.user === undefined) {
throw new Error();
}
return this.fooService.get((this.httpContext.user.id);
}
}
authorize middleware using HttpContextWe can document examples to implement a middleware that uses the HttpContext to validate if an user has access to certan feature:
@controller(
"/foo",
authorize({ feature: FEATURE.SOME_FEATURE_FLAG }),
)
@injectable()
export class FooController extends BaseHttpController {
// ...
}
Or validate if an user has access to certan role:
@controller(
"/foo",
authorize({ role: ROLE.SOME_ROLE }),
)
@injectable()
export class FooController extends BaseHttpController {
// ...
}
The community will then develop libraries for particular databases etc.
I have implemented A, B and C https://github.com/inversify/inversify-express-utils/pull/72 and I have moved D to a new issue https://github.com/inversify/InversifyJS/issues/673
Most helpful comment
I have implemented A, B and C https://github.com/inversify/inversify-express-utils/pull/72 and I have moved D to a new issue https://github.com/inversify/InversifyJS/issues/673