apeyed
v1.0.1
Published
A small module designed to help manage your application dependencies and business logic using higher order functions.
Downloads
3
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.