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

node-shared

v3.0.1

Published

Shared logic for Node.JS

Downloads

7

Readme

node-shared

npm version npm bundle size typescript supported

Service provider for Node.JS without pain with Dependency Injection Container, dependency registration, and configuration.

  • You can use it at any place of your application without rewrite your applications architecture or other preparations or initializations.
  • Each dependency can be class or function.
  • You can override your dependencies for unit testing without hack standard Node.JS require mechanism.
  • You can use TypeScript or JavaScript.

Example

import { shared } from "node-shared";

// It is can be service or shared state or cross requests data
class Users {
  async getById(id) {
    ...
  }
}

const sharedUsers = () => shared(Users);

// And with express for example
app.get("/user/:id", async (req, res) => {
  const users = sharedUsers();
  res.json(await users.getById(req.params.id));
});

Unit testing

You can use mock to provide mocks into your dependencies.

import { mock } from "node-shared";
import { Users, Api } from "./shareds";

test("Users service should call api service inside", async () => {
  const apiMock = mock(Api, {
    getUserById: jest.fn();
  });

  const users = new Users();

  await users.getById("John");
  expect(apiMock.getUserById).toHaveBeenCalledWith("John");
});

If you use Jest for unit testing you need to add some code to your jest.config.json file.

// jest.config.json
{
  ...
  setupFilesAfterEnv: [
    "node-shared/jest"
  ],
  ...
}

This code means that after each test cached shareds instances will be clear. For another testing frameworks, you need call free after each test case manually for cleanup cached instances of dependencies.

afterEach(require("node-shared").free);

API Reference

shared

Returns instance of your dependency. Each dependency can be class or function.

  • For class. The class will be instantiated once and cached
  • For function. The function will be called and result cached
const db = shared(Db);

mock

Define resolved value for any dependency.

const mockedDb = mock(Db, {
  connect: jest.fn()
});

shared(Db).connect();
expect(mockedDb.connect).toHaveBeenCalled();

free

Clean all cached shared instances. It's needed for testing usually. Has no parameters.

free()

Install

npm i node-shared

Enjoy and Happy Coding!