npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dik

v0.2.0

Published

A small dependency-injection container with Promises support

Downloads

2

Readme

dik

A small dependency-injection container with Promise support

Installation

$ npm install --save dik

Dik requires an ES5-compatible environment and a native (globally available) Promise. It is recommended to use babel.

Example

A simple example of server-side React rendering with express and react-router.

Although Dik is isomorphic, the example below is only half (arguably not that interesting half) of what it can do. A more involved example with client- and server-side Flux is coming soon.

start.js

import { createServer } from 'http'
import di from './index'

di.get('app').then((app) => createServer(app).listen(3000))

index.js

import Dik from 'dik'
import createApp from './app'
import createRoutes from './routes'

const di = new Dik()
  .register('app', createApp, ['$get'])
  .register('routes', createRoutes)

export default di

app.js

import express from 'express'
import React from 'react'
import Router from 'react-router'

function createApp ($get) {
  return $get('routes').then(createHandler)
}

function createHandler (routes) {
  const app = express()
  app.get('*', function (req, res) {
    Router.run(routes, req.url, function (Handler) {
      res.send(React.renderToStaticMarkup(<Handler/>))
    })
  })
  return app
}

export default createApp

routes.js

import React from 'react'
import Router from 'react-router'

const { Route, DefaultRoute, RouteHandler } = Router

const App = React.createClass({
  render () {
    return <div className="App"><RouteHandler /></div>
  }
})

const Main = React.createClass({
  render () {
    return <div className="Main"><h1>Main</h1></div>
  }
})

const About = React.createClass({
  render () {
    return <div className="About"><h1>About</h1></div>
  }
})

export default function createRoutes () {
  return (
    <Route handler={App} path="/">
      <DefaultRoute handler={Main} />
      <Route name="about" handler={About} path="about" />
    </Route>
  )
}

Contributing

Issues and pull requests are welcomed. For code style, please use editorconfig and jshint.

Running tests

$ git clone https://github.com/zaim/dik.git
$ npm install
$ npm test

Prior Art

Dik was inspired by Cation and create-container.

Cation is a fully customizable DIC with Promises/async support. If you need custom resource providers (e.g. Factories, Services, Static), use Cation.

create-container on the other hand is a minimal application container, but without Promises/async. If you don't want or need asynchronous support, use create-container.

License

Dik is released under the MIT license.

API

Dik

The Dik container class.

Example

const dik = new Dik()

Dik#register(id, fn, options)

Register a resource provider

The options argument can be an options object, which may have these properties:

  • deps - array of resource provider ID strings that this resource is dependent upon.

  • factory - boolean, if true the resource will not be cached and a new instance is returned on every get call.

As a shortcut, the array of ID strings can also be passed directly as the options argument.

A special resource id $get can be specified to get access to the Dik#get method in the resource provider function in order to look-up other resources (see example below)

Parameters

| parameter | type | description | | --------- | -------- | ----------------------------------------------------- | | id | string | The unique ID to register the resource as | | fn | function | The resource provider function | | options | object | optional: Options object or array of dependency IDs |

Example

// Simple resource provider:
dik.register('foo', function () {
  return 'FOO'
})

// Specify dependencies in options object:
dik.register('bar', function (baz) {
  return 'BAR -> ' + baz
}, { deps: ['baz'] })

// Specify dependencies in directly:
dik.register('bar', function (baz) {
  return 'BAR -> ' + baz
}, ['baz'])

// Lookup other resources:
dik.register('baz', function ($get) {
  return $get('foo').then((foo) => {
    return 'BAZ -> ' + foo
  })
}, [$get'])

Returns Dik, self

Dik#get(id)

Look up a registered resource and its dependencies

Parameters

| parameter | type | description | | --------- | ------ | ------------------------------------- | | id | string | The registered resource provider's ID |

Example

dik.get('bar').then((res) => {
  expect(res).toEqual('BAR -> BAZ -> FOO')
})

Returns Promise, A Promise for the created resource object