1.1.7 • Published 6 months ago

magnodi v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Features

  • Lightweight
  • Constructor based injection
  • Property based injection
  • Easy usage with TS decorators

Installation

Using npm:

npm install magnodi

Using yarn:

yarn add magnodi

Using pnpm:

pnpm add magnodi

You need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:

"emitDecoratorMetadata": true,
"experimentalDecorators": true

Constructor Based Injection

import { Container, Injectable } from 'magnodi'

@Injectable()
class ExampleInjectedService {
  sayHi() {
    console.log('Hi from injected service!')
  }
}

@Injectable()
class ExampleService {
  constructor(private injectedService: ExampleInjectedService) {}

  greet() {
    this.injectedService.sayHi()
  }
}

const exampleService = Container.resolve<ExampleService>(ExampleService)

exampleService.greet()
// Output: 'Hi from injected service!'

Property Based Injection

import { Container, Injectable, Inject } from 'magnodi'

@Injectable()
class UserService {
  private users = [{ id: 1, name: 'John Doe' }]

  getUsers() {
    return this.users
  }
}

@Injectable()
class App {
  @Inject()
  private userService!: UserService

  printUsers() {
    const users = this.userService.getUsers()
    console.log(users)
  }
}

const app = Container.resolve<App>(App)

app.printUsers()
// Output: [{ id: 1, name: 'John Doe' }]

Contributing

  1. Fork this repository.
  2. Create a new branch with feature name.
  3. Create your feature.
  4. Commit and set commit message with feature name.
  5. Push your code to your fork repository.
  6. Create pull request.

License

MIT

1.1.7

6 months ago

1.1.6

7 months ago

1.1.5

7 months ago

1.1.4

9 months ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago