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

@jpex-js/node

v2.1.2

Published

This is a very small wrapper around the `jpex` library that allows you to enapsulate and sandbox your dependency injection lifecycles.

Downloads

121

Readme

@jpex-js/node

This is a very small wrapper around the jpex library that allows you to enapsulate and sandbox your dependency injection lifecycles.

Effectively this allows you to provide a new jpex container that your application will use to resolve its dependencies.

Example

Let's learn through a basic example. Suppose we have a dependency, Foo:

import jpex from 'jpex';

jpex.constant<Foo>('foo');
import { encase, resolve } from '@jpex-js/node';

const fn1 = encase((foo: Foo) => () => {
  return foo;
});

const fn2 = () => {
  const foo = resolve<Foo>();
  return foo;
};

In normal circumstances you would expect to get the following output:

fn1(); // 'foo'
fn2(); // 'foo'

Both functions use the global jpex instance to resolve their dependencies. This is good and is how jpex works. However, sometimes, such as when writing tests, you want to mock certain dependencies. You don't want to register mocks on the global jpex container as this may bleed into other tests, but you can't exactly tell these functions to use another container, can you?

Yes you can!

import { provide } from '@jpex-js/node';

// this creates a sort of "sandbox" and everything inside it will be
// given this shiny new instance
provide((jpex) => {
  jpex.constant<Foo>('fake foo');

  fn1(); // 'fake foo'
  fn2(); // 'fake foo'
});

Notably, you can pass in a pre-configured jpex instance of your own if you prefer:

import { provider } from '@jpex-js/node';

provide(myPreStubbedJpex, () => {
  fn1(); // 'fake foo'
  fn2(); // 'fake foo'
});

But the coolest part is that the provider can handle asynchronous tasks too. Under the hood we're using AsyncLocalStorage which is able to track the context through multiple promises as callbacks!

import { provider } from '@jpex-js/node';

const doStuff = () =>
  new Promise((res) => {
    setTimeout(() => {
      res(fn1() + fn2());
    }, 3000);
  });

const result = await provider(async () => {
  await someAsyncFn();

  const result = await doStuff();

  // oh yeah you can also return data out of the provider!
  return result;
});

result; // 'fake foofake foo';

Api

provide

<T>(instance?: Jpex, callback: (jpex: Jpex) => T): T

Creates a new context where in all calls to resolve encase and getJpex are bound to instance. If no instance is provided, it will create a new one.

getJpex

(): Jpex

Returns the current jpex instance. If called outside of any provide context, it will return the global instance.

resolve

<T>(opts?: object): T

Resolve a dependency. Essentially the same as jpex.resolve except it uses the provided context.

resolveAsync

<T>(opts?: object): Promise<T>

resolveWith

<T, ...rest>(with: any[]): T

resolveAsyncWith

<T, ...rest>(with: any[]): Promise<T>

encase

(...deps: any[]) =>
  (...args: any[]) =>
    any;

Essentially the same as jpex.encase. When the resulting function is called, it will use the provided context to resolve the dependencies.