-
Notifications
You must be signed in to change notification settings - Fork 509
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
feat: Add ability to fetch custom IoC container #803
Conversation
Co-authored-by: Wolfgang Hobmaier <wolfgang.hobmaier@sensoring.de>
Alright, I think I'm fine with the changed API as a new way to create containers per request. A remaining DX issue is the typing thought. We should at least expose type information what the container can be (function request => Container or Container) and export an interface a developer can implement and be sure. |
I can add the following for typing information. export interface TsoaIocContainer { get<T>(controller: any): T;
export type TsoaIocContainerMethod = (request: any) => TsoaIocContainer This would allow the users ioc module to type the method or container if they desired. // src/ioc.ts
export const iocContainer: TsoaIocContainerMethod = (request: Express.Request): TsoaIocContainer => {
return new Container();
}; Tsoa itself could then use these types in the routes file. const container: TsoaIocContainer = typeof iocContainer === 'function' ?
(iocContainer as TsoaIocContainerMethod)(request) : iocContainer;
const controller: any = container.get<{{../name}}>({{../name}}); |
@WoH My last 2 commits should address the typing issue. I have a change ready to add the typings the new test included in this PR, but would like the typings applied to the other tests as well? |
My bad, didn't notice the last commits. |
Alright, this looks pretty good now, only some nitpicking left. Great job! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
hey @WoH. Thanks for your review and your help ironing this out. Do you need anything else from me, or can this be merged? |
* update di documentation to include tsoa#803 lukeautry/tsoa#803 * update docs to include new interfaces * fix typo Co-authored-by: Thibaud Desodt <tibo.desodt@gmail.com> Co-authored-by: Thibaud Desodt <tibo.desodt@gmail.com>
Hey! Happy hacktoberfest 🎃! This closes #779.
This adds a check to see if
iocContainer
is a function and if it is, calls it with the request to resolve the IoC container. This means that theiocContainer
can be created/updated/fetched per request.The reason I've added in the check to see if it's a function is to avoid being a breaking change.
I will need to raise a PR over at tsoa-community/docs to document this change.