This package has been deprecated

Author message:

@discordbuddy/di has been replaced with rdi, please use rdi instead!

@discordbuddy/di
TypeScript icon, indicating that this package has built-in type declarations

0.0.2-alpha.3 • Public • Published

DiscordBuddy - Depencency Injector

This is the dependency injector of DiscordBuddy. It is insipred by the DI of Angular v4.

This module is still under development and it is not production ready yet!

An example of what you can expect in the future

import { Command, BuddyCommand, BuddyMessage } from '@discordbuddy/core';
import { LOGGER, Logger } from '@discordbuddy/util';
import { Inject } from '@discordbuddy/di';

@Command({
    name: 'ping',
    description: 'Tests ping of bot'
})
export class PingCommand extends BuddyCommand {
    // This is were the dependency injector will help you,
    // it will inject the Logger mentioned in the DITestModule
    // Below is an example on how to add a depenceny to the di
    @Inject() private readonly logger: Logger;

    async run(message: BuddyMessage) {
        this.logger.info("We just got pinged, let's ping back");

        return message.reply('Pong!');
    }
}

// Below is an example on how to register a new dependency,
// in this example LOGGER is an instance of a logger
// and Logger is just an interface on how the logger should look like
@Module({
    name: 'ditest',
    providers: [
        {
            provide: LOGGER, factory: Logger
        }
    ],
    commands: [PingCommand]
})
export class DITestModule {}

It is also possible for a command itself to register providers, you can do this by the same way as you register providers in a module:

...
@Command({
    ...
    providers: [
        {provide: LOGGER, factory: Logger}
    ]
    ...
})
...

With this method is is also possible to inject BuddyClient anywere you want it is only important to note that only decorated classes can make use of the DI

If your class aint a command for example you have to decorate it with @Injectable

import { Injectable, Inject } from '@discordbuddy/di';
import { BuddyClient } from '@discordbuddy/core';

@Injectable()
export class Logger {
    @Inject()
    private client: BuddyClient;

    public info(message: string) {
        console.log(`${this.client.user.tag} - ${message}`);
    }
}

@Inject can also be used in a constructor like so

import { Injectable, Inject } from '@discordbuddy/di';
import { BuddyClient } from '@discordbuddy/core';

@Injectable()
export class Logger {
    
    constructor(@Inject() private client: BuddyClient) {}

    public info(message: string) {
        console.log(`${this.client.user.tag} - ${message}`);
    }
}

Big thanks to the Angular team for the inspiration.

Package Sidebar

Install

npm i @discordbuddy/di

Weekly Downloads

1

Version

0.0.2-alpha.3

License

MIT

Last publish

Collaborators

  • itsdizzy
  • victionn