Skip to content

Latest commit

 

History

History
80 lines (51 loc) · 1.76 KB

README.md

File metadata and controls

80 lines (51 loc) · 1.76 KB

injector

Parameter names based dependency injector for nodejs.

Limitations

  • does not work with default parameters
  • minification of code is not supported
  • not typesafe
  • classes not supported
  • slower than a normal function call

Changelog

API

createInject()

returns a new instance of an injector function to work with.

injector(function, scope)

injects arguments into function and invokes it

function - required, function to inject parameters and call scope - optional, default = {}, this argument for the function

$provide(key, value)

predefined injectable function

key - string, required value - unknown

to get it, inject it into the function

inject(($provide: Provider) => {
    $provide('service', service);
});

Example

import createInject from '@slimlib/injector';

const inject = createInject();

inject(($provide: Provider) => {
    $provide('config', {
        url: 'http://example.com/json',
        format: 'json'
    });
});

inject(async (config: Json) => {
    const data = await fetch(config.url);
    const result = config.json ? await data.json() : data;
    // and so on
});

FAQ

  1. Is it a good solution to mock something in unit tests?
  1. Is it a good solution to use in frontend code?
  • no, it will not work after minification
  1. Is it good for nodejs applications?
  • only in some edge cases, please use singletons/factories/something else if possible

License

MIT