Skip to content
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

Merged
merged 9 commits into from
Oct 7, 2020
Merged

feat: Add ability to fetch custom IoC container #803

merged 9 commits into from
Oct 7, 2020

Conversation

alxwrd
Copy link
Contributor

@alxwrd alxwrd commented Oct 1, 2020

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 the iocContainer 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.


  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue?

Co-authored-by: Wolfgang Hobmaier <wolfgang.hobmaier@sensoring.de>
@WoH
Copy link
Collaborator

WoH commented Oct 2, 2020

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.

@alxwrd
Copy link
Contributor Author

alxwrd commented Oct 2, 2020

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}});

@alxwrd
Copy link
Contributor Author

alxwrd commented Oct 2, 2020

@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?

@WoH
Copy link
Collaborator

WoH commented Oct 2, 2020

@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.

@WoH
Copy link
Collaborator

WoH commented Oct 2, 2020

Alright, this looks pretty good now, only some nitpicking left. Great job!

Copy link
Collaborator

@WoH WoH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alxwrd
Copy link
Contributor Author

alxwrd commented Oct 5, 2020

hey @WoH. Thanks for your review and your help ironing this out. Do you need anything else from me, or can this be merged?

@WoH WoH changed the title Add ability to fetch custom IoC container feat: Add ability to fetch custom IoC container Oct 7, 2020
@WoH WoH merged commit dcf5a34 into lukeautry:master Oct 7, 2020
@alxwrd alxwrd deleted the ioc-container-function branch October 7, 2020 07:49
WoH pushed a commit to tsoa-community/docs that referenced this pull request Oct 18, 2020
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow routes to have a unique IoC container per request
2 participants