Socket
Socket
Sign inDemoInstall

@microservice/koa-inject

Package Overview
Dependencies
9
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @microservice/koa-inject

simple dependency injection for koa


Version published
Weekly downloads
64
increased by8.47%
Maintainers
1
Install size
63.4 kB
Created
Weekly downloads
 

Readme

Source

microservice / koa-inject

minimalist dependency injection for koa applications

usage

app = koa(), app.use(inject(function* | object))

example

var inject = require('@microservice/koa-inject');

app = koa();

// pass an object in to define provider functions
app.use(inject({
  // the dependency 'foo' will be the return value of the function
  foo: function() {
    console.log(this); // the koa context
    return 'foo!';
  },
  // providers are co.wrap'd so they can return promises, etc.
  bar: function() {
    return Promise.resolve('bar!');
  }
}));

// dependency consumer functions are normal middleware
// ... but with extra injected arguments!
app.use(inject(function* (next, foo) {
  console.log(foo); // 'foo!';
  yield* next;
}));

advanced

// they can also be middleware
app.use(inject({
  // but they need to set this[key] correctly before yielding
  baz: function* (next) {
    this.biff = 'bad'; // NO!
    this.baz = 'good'; // ok
    yield* next;
  },
  // and dependency consumers, too!
  blah: function* (next, baz) {
    this.blah = 'blah...' + baz;
    yield* next;
  }
}));

providers in a definition object are (automagically) run in order, depending on their dependencies.

FAQs

Last updated on 24 Aug 2016

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc