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

apeyed

v1.0.1

Published

A small module designed to help manage your application dependencies and business logic using higher order functions.

Downloads

1

Readme

apeyed

A small module designed to help manage your application dependencies and business logic using higher order functions.

How does it work

An object of depedencies can be passed to the first argument:

const myDep = new MyDep();
const api = apeyed({ myDep });

An object of functions (namespaces) can be passed to the second argument:

const namespaces = {
  one: () => () => console.log(1),
  two: () => () => console.log(2)
};

const api = apeyed({}, namespaces);

Any key containing a function, that returns a function when called (higher order function) will get mounted as a namespaced method on the api object:

api.one(); // logs 1
api.two(); // logs 2

The outer function of any namespace method is passed the dependencies object which also includes a reference to the api object, you can then use destructuring to access the required dependencies or other namespace methods from the inner function:

const dependencies = {
  log: (msg) => console.log(msg)
};

const namespaces = {
  one: ({ log }) => (msg) => log(msg),
  two: ({ api }) => (msg) => api.one(msg)
};

const api = apeyed(dependencies, namespaces);

api.one(1); // logs 1
api.two(2); // logs 2

Namespaces can also be nested, so the following is possible:

const namespaces = {
  one: () => () => console.log(1),
  two: () => () => console.log(2)
  nested: {
    one: () => () => console.log('nested', 1),
    deeper: {
      two: () => () => console.log('deeper', 2)
    }
  }
};

const api = apeyed({}, namespaces);

api.one(); // logs 1
api.two(); // logs 2
api.nested.one(); // logs nested 1
api.nested.deeper.two(); // logs deeper 2

This allows you to break up your business logic into composable units that make testing simple:

// create-user.js
const createUser = ({ api, db }) => async (email, password) => {
  const existingUser = await api.checkExistingUser(email);
  if (existingUser) return false;

  return db.users.insert({
    email,
    password
  });
}

// create-user.test.js
const test = require('tape');
const sinon = require('sinon');
const createUser = require('./create-user.js');

test('returns false for existing user', async (t) => {
  t.plan(3);

  const api = { checkExistingUser: sinon.fake.resolves(true) };
  const db = { users: { insert: sinon.fake() } };

  let result = await createUser({ api, db })('[email protected]', 'secret');

  t.ok(api.checkExistingUser.calledOnce);
  t.ok(db.users.insert.notCalled);
  t.notOk(result);
});

test('returns true for non-existing user', async (t) => {
  t.plan(3);

  const api = { checkExistingUser: sinon.fake.resolves(false) };
  const db = { users: { insert: sinon.fake.resolves(true) } };

  let result = await createUser({ api, db })('[email protected]', 'secret');

  t.ok(api.checkExistingUser.calledOnce);
  t.ok(db.users.insert.calledOnce);
  t.ok(result);
});

You can then use apeyed instances in web servers, scripts, or any application for that matter. Portions of the namespaces can be mounted in an apeyed instance and used on AWS Lambda for example.

You could even use it in a client side application.

Experimental services

The apeyed constructor takes an additional third parameter which is an object of services. These services are a single function which are passed the api and dependencies as the first argument and called once after all namespaces are mounted:

const dependencies = { a: () => console.log('a') };
const namespaces = { b: () => () => console.log('b') };

const services = {
  test: ({ api, a }) => {
    a();
    api.b();
  }
};

var apeyed = (dependencies, namespaces, services);
// logs a
// logs b

This is a feature I'll likely remove in the future however it's proven useful in development, small application scenarios and timers - so I've left it for now. The original idea was to use this feature to instantiate your web server, rpc server etc.

More documentation coming soon, otherwise see examples and tests folders.