README
common-services
A module to gather very common services and their mocks.
This module contains various common injectable services I use into a lot of applications.
API
Functions
- initCodeGenerator(services) ⇒
Promise.<function()>
|Promise.<function()>
Instantiate the codeGenerator service
- initCounter(services) ⇒
Promise.<function()>
Instantiate the counter service
- initDelay(services) ⇒
Promise.<Object>
Instantiate the delay service
- initDelayMock() ⇒
Promise.<Object>
Instantiate the delay service mock
- initLock(services) ⇒
Promise.<Object>
Instantiate the lock service
- initLog(services) ⇒
Promise.<function()>
Instantiate the logging service
- initLogMock() ⇒
Promise.<function()>
Instantiate the logging mock
- initProcess(services) ⇒
Promise.<Object>
Instantiate the process service
- initProcessMock() ⇒
Promise.<Object>
Instantiate the process service
- initRandom(services) ⇒
Promise.<function()>
Instantiate the random service
- initRandomMock() ⇒
Promise.<function()>
Instantiate the random service mock
- initTime(services) ⇒
Promise.<function()>
Instantiate the time service
- initTimeMock() ⇒
Promise.<function()>
Instantiate the time service mock
Promise.<function()>
| Promise.<function()>
initCodeGenerator(services) ⇒ Instantiate the codeGenerator service
Kind: global function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.CHARS_SET] | Object |
EXPLICIT_CHARS |
An optional char set to pick cars into |
[services.random] | Object |
Math.random |
An optional random function to replace the Math.random one used by default |
[services.log] | Object |
noop |
An optional logging function |
Example
import initCodeGenerator from 'common-services/dist/codeGenerator';
const codeGenerator = await initCodeGenerator({
log: console.log.bind(console),
});
Promise.<String>
initCodeGenerator~codeGenerator([length]) ⇒ Returns a random code
Kind: inner method of initCodeGenerator
Returns: Promise.<String>
- A promise of the generated code
Param | Type | Default | Description |
---|---|---|---|
[length] | Number |
6 |
An optional custon code length (defaults to 6) |
Example
console.log([
codeGenerator(),
codeGenerator(),
codeGenerator(),
]);
// Prints: ABCDEF,GHJKMN,PRSTUV
Promise.<function()>
initCounter(services) ⇒ Instantiate the counter service
Kind: global function
Returns: Promise.<function()>
- A promise of the counter function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.COUNTER] | Object |
DEFAULT_COUNTER |
An optional configuration object |
[services.log] | Object |
noop |
An optional logging function |
Example
import initCounter from 'common-services/dist/counter';
const counter = await initCounter({
COUNTER: { firstCount: 1 },
log: console.log.bind(console),
});
Promise.<number>
initCounter~counter() ⇒ Returns the current count and increment the counter
Kind: inner method of initCounter
Returns: Promise.<number>
- A promise of the current count
Example
console.log([
counter(),
counter(),
counter(),
]);
// Prints: 1,2,3
Promise.<Object>
initDelay(services) ⇒ Instantiate the delay service
Kind: global function
Returns: Promise.<Object>
- A promise of the delay service
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.log] | function |
noop |
A logging function |
Example
import initDelay from 'common-services/dist/delay';
const delay = await initDelay({
log: console.log.bind(console)
});
- initDelay(services) ⇒
Promise.<Object>
- ~create(delay) ⇒
Promise
- ~clear(promise) ⇒
Promise
- ~create(delay) ⇒
Promise
initDelay~create(delay) ⇒ Create a new delay
Kind: inner method of initDelay
Returns: Promise
- A promise to be resolved after that delay
or rejected if it is cancelled.
Param | Type | Description |
---|---|---|
delay | Number |
The delay in ms |
Example
await delay.create(1000);
console.log('1000 ms elapsed!');
Promise
initDelay~clear(promise) ⇒ Cancel an earlier created delay
Kind: inner method of initDelay
Returns: Promise
- A promise resolved when cancellation is done.
Param | Type | Description |
---|---|---|
promise | Promise |
The promise of the delay to cancel |
Example
try {
const delayPromise = delay.create(1000);
await Promise.all(delayPromise, delay.clear(delayPromise));
console.log('1000 ms elapsed!');
} catch (err) {
if(err.code != 'E_DELAY_CLEARED') {
trow err;
}
console.log('Cancelled!'));
}
// Prints: Cancelled!
Promise.<Object>
initDelayMock() ⇒ Instantiate the delay service mock
Kind: global function
Returns: Promise.<Object>
- A promise of the mocked delay service
Example
import initDelayMock from 'common-services/dist/delay.mock';
import assert from 'assert';
const delay = await initDelayMock();
const delayPromise = delay.create(1000);
delay.resolve(delayPromise);
delayPromise.then(() => {
// Any code here will execute immediatly
// instead of after a 1000ms delay
});
Promise.<Object>
initLock(services) ⇒ Instantiate the lock service
Kind: global function
Returns: Promise.<Object>
- A promise of the lock service
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.LOCKS_MAP] | Map |
A map to store le current locks (optional) | |
[services.LOCK_TIMEOUT] | Number |
Infitiny |
The timeout in milliseconds for the lock to be released. |
[services.log] | function |
A logging function | |
[services.delay] | Object |
A delay service like the common-services one |
Example
import initLog from 'common-services/dist/log';
import initDelayService from 'common-services/dist/delay';
import initLock from 'common-services/dist/lock';
import ms from 'ms';
const log = await initLogService({
logger: require('winston'),
debug: require('debug')('myapp'),
});
const delay = await initDelayService({ log });
const lock = await initLock({ LOCK_TIMEOUT: ms('5s'), delay, log });
run();
async function run() {
// The following async jobs are done sequentially
// if they have the same `resourceKey` value
await Promise.all(asynTasks.map(async (asyncTask) => {
await lock.take(asyncTask.resourceKey);
await myAsyncStuff1(asyncTask);
await myAsyncStuff2(asyncTask);
await myAsyncStuff3(asyncTask);
lock.release(asyncTask.resourceKey);
});
}
- initLock(services) ⇒
Promise.<Object>
- ~take(key) ⇒
Promise
- ~release(key) ⇒
void
- ~take(key) ⇒
Promise
initLock~take(key) ⇒ Take the lock on the given resource key
Kind: inner method of initLock
Returns: Promise
- A promise to be resolved when the lock
is gained or rejected if the lock release
timeout is reached.
Param | Type | Description |
---|---|---|
key | String |
A unique key for the locked resource |
void
initLock~release(key) ⇒ Release the lock on the given resource key
Kind: inner method of initLock
Param | Type | Description |
---|---|---|
key | String |
A unique key for the resource to release |
Promise.<function()>
initLog(services) ⇒ Instantiate the logging service
Kind: global function
Returns: Promise.<function()>
- A promise of the logging function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
services.logger | Object |
The logger object that output the logs | |
[services.debug] | function |
noop |
A debugging function |
Example
import initLog from 'common-services/dist/log';
const log = await initLog({
logger: require('winston'),
debug: require('debug')('myapp'),
});
void
initLog~log(type, ...args) ⇒ Logging function
Kind: inner method of initLog
Param | Type | Description |
---|---|---|
type | String |
Log type |
...args | * |
Log contents |
Example
log('debug', 'Luke, I am your father!')
Promise.<function()>
initLogMock() ⇒ Instantiate the logging mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
logging function
Example
import initLogMock from 'common-services/dist/log.mock';
import assert from 'assert';
const log = await initLogMock();
log('info', 'Hello!');
log('error', 'Aouch!');
assert.deepEqual(log.args, [[
'info', 'Hello!'
], [
'error', 'Aouch!'
]]);
log.reset();
assert.deepEqual(log.args, []);
Promise.<Object>
initProcess(services) ⇒ Instantiate the process service
Kind: global function
Returns: Promise.<Object>
- A promise of the process object
Param | Type | Description |
---|---|---|
services | Object |
The services to inject |
Promise.<Object>
initProcessMock() ⇒ Instantiate the process service
Kind: global function
Returns: Promise.<Object>
- A promise of the process object
Promise.<function()>
initRandom(services) ⇒ Instantiate the random service
Kind: global function
Returns: Promise.<function()>
- A promise of the random function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.log] | Object |
noop |
A logging function |
Example
import initRandom from 'common-services/dist/random';
const random = await initRandom({
log: console.log.bind(console),
});
number
initRandom~random() ⇒ Returns a new random number
Kind: inner method of initRandom
Returns: number
- The random number
Example
random()
// Prints: 0.3141592653589793
Promise.<function()>
initRandomMock() ⇒ Instantiate the random service mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
random function
Example
import initRandomMock from 'common-services/dist/random.mock';
import assert from 'assert';
const random = await initRandomMock();
random.returns(0.5); // A good limit value to test ;)
assert.equal(random(), 0.5);
assert.deepEqual(random.args, [[]], 'Called once');
random.reset();
assert.deepEqual(random.args, []);
Promise.<function()>
initTime(services) ⇒ Instantiate the time service
Kind: global function
Returns: Promise.<function()>
- A promise of the time function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.log] | Object |
noop |
A logging function |
Example
import initTime from 'common-services/dist/time';
const time = await initTime({
log: console.log.bind(console),
});
number
initTime~time() ⇒ Returns the current timestamp
Kind: inner method of initTime
Returns: number
- The current timestamp
Example
time()
// Prints: 1326585600000
Promise.<function()>
initTimeMock() ⇒ Instantiate the time service mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
time function
Example
import initTimeMock from 'common-services/dist/time.mock';
import assert from 'assert';
const time = await initTimeMock();
// Let's returns Thomas birth date (OMG ya father
// talking me about its childrens :D).
time.returns(new Date('2014-01-26T00:00:00.000Z'));
assert.equal(time(), 1390694400000);
assert.deepEqual(time.args, [[]], 'Called once');
time.reset();
assert.deepEqual(time.args, []);