This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

injets

1.2.8 • Public • Published

npm version

Logo

Injets is a TypeScript Dependency Injection library inspired by NestJS that uses reflect-metadata and annotations to make your code more organized.

Features

💉 Dependency Injection: Organize your code in modules and inject providers in other modules.

✏️ Annotations: Use annotations in your module classes to make your dependecy flow easier to understand.

🏢 Singletons and Transient providers: Create singletons just by specifying a single flag, and don't bother when you should instantiate your classes anymore.

Table of Contents

Installation

using npm or yarn

npm i injets
yarn add injets

Getting started

To start using injets, you first have to create a root module.

A root module is simply a starting point for your project, so all other modules are going to be attached to it.

Here's how you can register a provider on the root module:

// --- hello-world.provider.ts ---
import { Provider } from 'injets'
 
@Provider()
export class HelloWorldProvider {
  hello () {
    console.log('Hello World!')
  }
}
 
// --- app.module.ts ---
import { Module } from 'injets';
import { HelloProvider } from './hello-world.provider.ts'
 
@Module({
    providers: [HelloProvider]
})
export class AppModule {}

Instantiating the root module

To get your app up and running you first have to instantiate the root module. To do so, there's a static helper called createModule. This is your entry point:

// --- index.js ---
import { createModule } from 'injets'
import { AppModule } from './app.module.ts'
 
async function main() {
    const app = await createModule(AppModule)
    const helloProvider = await app.get(HelloProvider)
 
    // -> Hello World
    helloProvider.hello()
}
 
main()

Accessing Providers

Providers are entities that are responsible for executing the logic of your application. You often have to access logic from other parts of your app.

To access another Provider, you can simply declare its type on the current Provider constructor:

// --- app.module.ts ---
import { Module } from 'injets';
import { LogProvider } from './log.provider.ts'
import { HttpProvider } from './http.provider.ts'
 
@Module({
    providers: [HelloProvider, HttpProvider]
})
export class AppModule {}
 
// --- http.provider.ts ---
import { Provider } from 'injets';
import { LogProvider } from './log.provider.ts'
 
@Provider()
export class HttpProvider {
    constructor(
        public logProvider: LogProvider
    ) {}
 
    async sendRequest(message: string) {
      try {
        await fetch('https://my-api.com')
        this.logProvider.success('Request was sent 🚀')
      } catch (error) {
        this.logProvider.error('Error while sending request ❌', error)
      }
    }
}

Documentation

Modules

Modules are a group of providers, your application must have at least one module (root module).

Structure of a module

@Module({
    // List of modules that will be imported into this module
    imports: [],
    // providers that will be registered in this module
    providers: [],
    // providers of this module that can be used in other modules where this module was imported
    exports: []
})
export class MyModule {
    // This method is called when the module and all its dependencies are initialized
    onModuleInit() {
        // Module Ready!
    }
}

Providers

A provider can be anything you want to use anywhere in your application, but they are generally used to provide business logic methods for your app.

Structure of a provider

// A provider can have SINGLETON or TRANSIENT scope
@Provider(scope)
export class MyProvider {
    construtor (
        // You can import other providers here
        public usersProvider: UsersProvider
    ) {}
 
    method () {
        // You can call methods from imported providers like this
        this.usersProvider.method();
    }
}

Package Sidebar

Install

npm i injets

Weekly Downloads

21

Version

1.2.8

License

MIT

Unpacked Size

79.4 kB

Total Files

65

Last publish

Collaborators

  • salomaosnff
  • trickstival