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

depined

v0.2.2

Published

[![npm version](https://img.shields.io/npm/v/depined.svg)](https://www.npmjs.com/package/depined) [![Downloads](https://img.shields.io/npm/dm/depined.svg)](https://www.npmjs.com/package/depined) [![Size](https://img.shields.io/bundlephobia/min/depined)](h

Downloads

4

Readme

npm version Downloads Size depined

Depined

Depined is a dependency injection tool for JavaScript and TypeScript. It is a lightweight solution with full typing support.

  • Awesome testability
  • Works great for JavaScript
  • Works even better for TypeScript without (beta) annotation and type casting
  • Only needed dependencies are being build
  • Works with interfaces, classes, functions, promises and constants
  • Get typescript errors if the dependencies change without accidentally calling some io as with mocking

Installation

npm install depined

or

yarn add depined

Usage

The general idea is to write functions or classes without having strongly coupled dependencies (except for the interfaces). For example, make sure all dependencies are made available via parameters or the constructor.

Here are some code examples:


// Start by making a container

const container = depined()

// This container is empty and will always be empty because injecting stuff creates a new container, it never changes the current one.

// The main way to use Depined is to call the inject function. it needs 2 arguments, first is the token (should always be unique, second is the actual generator for the function or class)

const filledContainer = container.inject('logger', () => console.log)

// We can use everying in the container to initialize the next function/class

const containerWithClass = filledContainer.inject('class', ({logger}) => new ClassThatNeedsALogger(logger))

// When everying is injected, call the resolve and use it.

const di = await container.resolve()

// di.class is the initialized ClassThatNeedsALogger

Example container module

const container = await depined()
  .inject('logger', () => console.log)
  .inject('class', ({logger}) => new ClassThatNeedsALogger(logger))
  .resolve()

Constant / Non dependant injections

const container = await depined()
      .set('config', 'secret')
      .inject('dep', ({ config }) => `hello ${config}`)
      .resolve()

container.dep // 'hello secret'

Async factories

const container = await depined()
  .inject('dbConfig', async () => {
    const secret = await getSecret();
    return secret.connectionString;
  })
  .inject('dbConnection', async ({dbConfig}) => new Connection(await dbConfig))
  .resolve()

container.dbConnection // Connection class with the async configuration

Combining 2 containers

const configDi = depined()
  .set('config', 'secret')

const main = await depined()
  .combine(await configDi.resolve())
  .inject('connect', ({ config }) => `connect with config: "${config}"`)
  .resolve()

main.connect // 'connect with config: "secret"'

Check out the unit tests: tests/depined.test.ts

Test driven development

  1. Create a test for your function
  2. Develop the function
  3. Add types for your dependencies as you need them
  4. Implement the dependencies starting at 1.