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 injectable function #2857

Merged
merged 1 commit into from
Oct 11, 2024
Merged

Feat injectable function #2857

merged 1 commit into from
Oct 11, 2024

Conversation

Romakita
Copy link
Collaborator

@Romakita Romakita commented Oct 11, 2024

Information

Type Breaking change
Feature Yes

Usage example

This feature allow provider registration using injectable, controller functions. Decorators can also be used.

Register class

Functional API:

import {injectable, inject, logger} from "@tsed/di";

class MyInjectable {
   logger = logger()
   anotherService = inject(AnotherService)
}

injectable(MyInjectable)

Decorator API:

import {Injectable, Inject} from "@tsed/di";
import {Logger} from "@tsed/logger";

@Injectable()
class MyInjectable {
   @Inject(Logger)
   logger: Logger;
   
   @Inject(AnotherService)
   anotherService: AnotherService
}

injectable(MyInjectable)

Register factory

Functional API:

import {injectable, inject, logger, ProviderType} from "@tsed/di";

export const MY_TOKEN = injectable(Symbol.for("MY_TOKEN"))
   .deps([AnotherService])
   .factory((anotherService: AnotherService) => {
       return {
         id: anotherService.id() + "HELLO"
       }
   })
   .token();
  
// alternative
export const MY_TOKEN = injectable(Symbol.for("MY_TOKEN"))
   .factory(() => {
       const anotherService = inject(AnotherService);

       return {
         id: anotherService.id() + "HELLO"
       }
   })
   .token();

// MODULE

class MyModule {
   myFactory = inject(MY_TOKEN)
   
   $onInit() {
      console.log(this.myFactory)
   } 
}

injectable(MyModule).type(ProviderType.MODULE)

Equivalent in v7 (still available in v8):

export const MY_TOKEN = Symbol.for("MY_TOKEN");

registerProvider({
   provide: MY_TOKEN
   deps: [AnotherService],
   useFactory: (anotherService: AnotherService) => {
       return {
         id: anotherService.id() + "HELLO"
       }
   }
})

Register async factory

Functional API:

import {injectable, inject, logger} from "@tsed/di";

export const MY_TOKEN = injectable(Symbol.for("MY_TOKEN"))
   .deps([AnotherService])
   .asyncFactory(async (anotherService: AnotherService) => {
       return {
         id: await anotherService.id() + "HELLO"
       }
   })
   .token();
  
// alternative
export const MY_TOKEN = injectable(Symbol.for("MY_TOKEN"))
   .asyncFactory(async () => {
       const anotherService = inject(AnotherService);
       return {
         id: await anotherService.id() + "HELLO"
       }
   })
   .token();

Equivalent in v7 (still available in v8):

export const MY_TOKEN = Symbol.for("MY_TOKEN");

registerProvider({
   provide: MY_TOKEN
   deps: [AnotherService],
   useAsyncFactory: async (anotherService: AnotherService) => {
       return {
         id: await anotherService.id() + "HELLO"
       }
   }
});

register Value

Functional API:

import {injectable, inject, logger} from "@tsed/di";

export const MY_TOKEN = injectable(Symbol.for("MY_TOKEN")).value("VALUE").token();

Equivalent in v7 (still available in v8):

export const MY_TOKEN = Symbol.for("MY_TOKEN");

registerProvider({
   provide: MY_TOKEN
   useValue: "VALUE"
});

removed from v8, available in v7

registerValue(MY_TOKEN, "VALUE");

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

@Romakita Romakita changed the base branch from production to beta October 11, 2024 09:52
@Romakita Romakita force-pushed the feat-injectable-function branch 2 times, most recently from 1880c62 to 4128a6b Compare October 11, 2024 10:08
BREAKING CHANGE: registerValue and registerController are removed in favor of injectable/controller functions
@Romakita Romakita merged commit 7565189 into beta Oct 11, 2024
14 checks passed
@Romakita Romakita deleted the feat-injectable-function branch October 11, 2024 16:05
@Romakita
Copy link
Collaborator Author

🎉 This PR is included in version 8.0.0-beta.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant