Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

pureinject

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pureinject

A light, pure javascript, dependency free injection framework for javascript projects.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

pureinject

A light, pure javascript, dependency free injection "framework" for javascript projects.

travis-master

logo

Setup

Just add to your dependencies using NPM or Yarn:

npm install pureinject

or

yarn add pureinject

How to use

const { createInjector } = require('pureinject');

class MyHttpService {
  // ...
}

class MyService {
  constructor(injector) {
    this._httpService = injector.resolve('MyHttpService');
  }
}

const injector = createInjector();

injector.register('MyHttpService', injector => {
  return new MyHttpService();
});

injector.register('MyService', injector => {
  return MyService(injector);
});

With Typescript

import { createInjector, PureInjector } from 'pureinject'

class MyHttpService {
  // ...
}

class MyService {

  private _httpService: MyHttpService

  constructor(injector: PureInjector) {
    this._httpService = injector.resolve<MyHttpService>('MyHttpService');
  }
}

const injector: PureInjector = createInjector()

injector.register('MyHttpService', (injector: PureInjector) => {
  return new MyHttpService();
})

injector.register('MyService', (injector: PureInjector) => {
  return MyService(injector);
});

How it works

It's simple: each time you call injector.resolve, it will run the the registered function and will retrieve the new instance of the service.

You can return what you want inside a registered module.

If you want to resolve a string value, you can!

const { createInjector } = require('pureinject');

class HttpService {
  constructor(injector) {
    this._apiUrl = injector.resolve('API_URL');
  }
}

const injector = createInjector();

injector.register('API_URL', injector => {
  return 'https://api.site.com';
});

injector.register('HttpService', injector => {
  return new HttpService(injector);
});

About

  • CHANGELOG
  • Express app sample

Thanks to

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

Keywords

inject

FAQs

Package last updated on 28 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts