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

refactor(common): Add generic to InjectionToken type #11555

Merged
merged 33 commits into from
Apr 25, 2023

Conversation

MaurerKrisztian
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

The InjectionToken type permits the use of any string, Type, Abstract, etc. However, it is not possible to indicate an InjectionToken Type that implements a specific interface.

Issue Number: N/A

What is the new behavior?

The new code now incorporates a generic type parameter T into the InjectionToken. This addition is particularly useful for creating dynamic modules that require an InjectionToken provider token.
Without this generic type, the code would be less robust in terms of type safety.
Fortunately, this change maintains compatibility with the previous behavior.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

renovate bot and others added 30 commits April 17, 2023 11:28
Bumps [mongoose](https://github.com/Automattic/mongoose) from 7.0.3 to 7.0.4.
- [Release notes](https://github.com/Automattic/mongoose/releases)
- [Changelog](https://github.com/Automattic/mongoose/blob/master/CHANGELOG.md)
- [Commits](Automattic/mongoose@7.0.3...7.0.4)

---
updated-dependencies:
- dependency-name: mongoose
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [@types/sinon](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/sinon) from 10.0.13 to 10.0.14.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/sinon)

---
updated-dependencies:
- dependency-name: "@types/sinon"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
chore(deps): update dependency tsconfig-paths to v4.2.0
fix(deps): update dependency @nestjs/swagger to v6.3.0
fix(deps): update dependency @nestjs/schedule to v2.2.1
fix(deps): update dependency @grpc/grpc-js to v1.8.14
fix(deps): update dependency @fastify/static to v6.10.1
fix(deps): update dependency @nestjs/graphql to v10.2.1
…ypes/sinon-10.0.14

chore(deps-dev): bump @types/sinon from 10.0.13 to 10.0.14
…ongoose-7.0.4

chore(deps-dev): bump mongoose from 7.0.3 to 7.0.4
fix(deps): update dependency mongoose to v6.10.5
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 18.15.11 to 18.15.12.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
…ypes/node-18.15.12

chore(deps-dev): bump @types/node from 18.15.11 to 18.15.12
Bumps [@apollo/server](https://github.com/apollographql/apollo-server/tree/HEAD/packages/server) from 4.6.0 to 4.7.0.
- [Release notes](https://github.com/apollographql/apollo-server/releases)
- [Changelog](https://github.com/apollographql/apollo-server/blob/main/packages/server/CHANGELOG.md)
- [Commits](https://github.com/apollographql/apollo-server/commits/@apollo/server@4.7.0/packages/server)

---
updated-dependencies:
- dependency-name: "@apollo/server"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [sinon](https://github.com/sinonjs/sinon) from 15.0.3 to 15.0.4.
- [Release notes](https://github.com/sinonjs/sinon/releases)
- [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md)
- [Commits](sinonjs/sinon@v15.0.3...v15.0.4)

---
updated-dependencies:
- dependency-name: sinon
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
…inon-15.0.4

chore(deps-dev): bump sinon from 15.0.3 to 15.0.4
…pollo/server-4.7.0

chore(deps-dev): bump @apollo/server from 4.6.0 to 4.7.0
chore: update repo references in package.json files
Bumps [cache-manager](https://github.com/node-cache-manager/node-cache-manager) from 5.2.0 to 5.2.1.
- [Release notes](https://github.com/node-cache-manager/node-cache-manager/releases)
- [Changelog](https://github.com/node-cache-manager/node-cache-manager/blob/master/CHANGELOG.md)
- [Commits](jaredwray/cacheable@5.2.0...5.2.1)

---
updated-dependencies:
- dependency-name: cache-manager
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [prettier](https://github.com/prettier/prettier) from 2.8.7 to 2.8.8.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](prettier/prettier@2.8.7...2.8.8)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 18.15.12 to 18.16.0.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
…ache-manager-5.2.1

chore(deps-dev): bump cache-manager from 5.2.0 to 5.2.1
kamilmysliwiec and others added 3 commits April 24, 2023 10:57
…ypes/node-18.16.0

chore(deps-dev): bump @types/node from 18.15.12 to 18.16.0
…rettier-2.8.8

chore(deps-dev): bump prettier from 2.8.7 to 2.8.8
@coveralls
Copy link

Pull Request Test Coverage Report for Build 5aed9202-f4a6-47ab-a0e2-d7d85f50e81b

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.675%

Totals Coverage Status
Change from base Build 8e8f7ddd-90d9-49bd-a8f9-0d8b865d99bf: 0.0%
Covered Lines: 6516
Relevant Lines: 7031

💛 - Coveralls

@jmcdo29
Copy link
Member

jmcdo29 commented Apr 24, 2023

Could you show how the generic will help here?

@MaurerKrisztian
Copy link
Contributor Author

MaurerKrisztian commented Apr 24, 2023

Sure! Here's a simplified use case for the generic.
Imagine you have a dynamic module that requires an InjectionToken parameter and the useFactory will resolve it.

export interface IProvider {
    provide(context: string): unknown;
}
@Module({})
export class ExampleModule implements NestModule {
    static forRootAsync(options: { customProvider: InjectionToken<IProvider> }): DynamicModule {
        return {
            module: ExampleModule,
            imports: [],
            providers: [
                {
                    provide: 'Key', // this is used in this module to do some things..
                    useFactory: (provider: IProvider, context: string) => {
                        // the module will provide the context..
                        const result = provider.provide(context);
                        // ...
                    	return result;
                    },
                    inject: [options.customProvider, 'context'],
                },
                {
                    provide: 'context',
                    useValue: 'world!',
                },
            ],
            exports: [],
        };
    }

    configure(consumer: MiddlewareConsumer): void {}
}

the module user perspective:

export class ExampleProvider implements IProvider {
    // the context is provided by the module
    provide(context: string): unknown {
        return `Hello ${context}`;
    }
}

App module imports:

    ExampleModule.forRootAsync({ customProvider: ExampleProvider }),
    or
    ExampleModule.forRootAsync({ customProvider: 'ProviderKey' }),

This customProvider must implement the IProvider interface because the module uses it.

@kamilmysliwiec kamilmysliwiec changed the base branch from master to 10.0.0 April 25, 2023 06:27
@kamilmysliwiec kamilmysliwiec added this to the 10.0.0 milestone Apr 25, 2023
@kamilmysliwiec kamilmysliwiec merged commit f6f577a into nestjs:10.0.0 Apr 25, 2023
@kamilmysliwiec
Copy link
Member

lgtm

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.

6 participants