knifecycle
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -0,1 +1,11 @@ | ||
<a name="2.2.0"></a> | ||
# [2.2.0](https://github.com/nfroidure/knifecycle/compare/v2.1.1...v2.2.0) (2017-07-30) | ||
### Features | ||
* **Decorators:** Add the ability to create a initializer from simple function ([f9e505e](https://github.com/nfroidure/knifecycle/commit/f9e505e)), closes [#37](https://github.com/nfroidure/knifecycle/issues/37) | ||
<a name="2.1.1"></a> | ||
@@ -2,0 +12,0 @@ ## [2.1.1](https://github.com/nfroidure/knifecycle/compare/v2.1.0...v2.1.1) (2017-06-15) |
@@ -17,2 +17,3 @@ 'use strict'; | ||
exports.initializer = initializer; | ||
exports.handler = handler; | ||
exports.parseDependencyDeclaration = parseDependencyDeclaration; | ||
@@ -250,2 +251,42 @@ | ||
/** | ||
* Shortcut to create an initializer with a simple handler | ||
* @param {Function} handlerFunction | ||
* The handler function | ||
* @param {Array} [dependencies=[]] | ||
* The dependencies to inject in it | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { initializer, getInstance } from 'knifecycle'; | ||
* | ||
* getInstance() | ||
* .register(handler(getUser, ['db', '?log'])); | ||
* | ||
* const QUERY = `SELECT * FROM users WHERE id=$1` | ||
* async function getUser({ db }, userId) { | ||
* const [row] = await db.query(QUERY, userId); | ||
* | ||
* return row; | ||
* } | ||
*/ | ||
function handler(handlerFunction) { | ||
var dependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
if (!handlerFunction.name) { | ||
throw new _yerror2.default('E_NO_HANDLER_NAME'); | ||
} | ||
return initializer({ | ||
name: handlerFunction.name, | ||
type: 'service', | ||
inject: dependencies | ||
}, function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return Promise.resolve(handlerFunction.bind.apply(handlerFunction, [null].concat(args))); | ||
}); | ||
} | ||
/* Architecture Note #1.3.1: Dependencies declaration syntax | ||
@@ -252,0 +293,0 @@ |
@@ -87,2 +87,39 @@ 'use strict'; | ||
describe('handler', function () { | ||
it('should work', function () { | ||
var injectedServices = ['kikooo', 'lol']; | ||
var services = { | ||
kikooo: 'kikooo', | ||
lol: 'lol' | ||
}; | ||
var theInitializer = (0, _util.handler)(sampleHandler, injectedServices); | ||
_assert2.default.deepEqual(theInitializer.$name, sampleHandler.name); | ||
_assert2.default.deepEqual(theInitializer.$inject, ['kikooo', 'lol']); | ||
return theInitializer(services).then(function (theHandler) { | ||
return theHandler('test'); | ||
}).then(function (result) { | ||
return _assert2.default.deepEqual(result, { | ||
deps: services, | ||
args: ['test'] | ||
}); | ||
}); | ||
function sampleHandler(deps) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
return Promise.resolve({ deps: deps, args: args }); | ||
} | ||
}); | ||
it('should fail for anonymous functions', function () { | ||
_assert2.default.throws(function () { | ||
(0, _util.handler)(function () {}); | ||
}, /E_NO_HANDLER_NAME/); | ||
}); | ||
}); | ||
describe('parseDependencyDeclaration', function () { | ||
@@ -89,0 +126,0 @@ it('should work', function () { |
{ | ||
"name": "knifecycle", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Manage your NodeJS processes's lifecycle.", | ||
@@ -35,3 +35,3 @@ "main": "dist/index.js", | ||
"lint": "eslint src/*.js", | ||
"metapak": "metapak || echo 'Please `npm install --save-dev metapak`' && exit 0", | ||
"metapak": "metapak || exit 0", | ||
"postinstall": "npm run metapak --silent", | ||
@@ -58,2 +58,3 @@ "prepublish": "npm run compile", | ||
"babel-plugin-transform-es2015-modules-systemjs": "^6.9.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
@@ -70,4 +71,4 @@ "babel-register": "^6.9.0", | ||
"jsdoc-to-markdown": "^3.0.0", | ||
"metapak": "0.0.20", | ||
"metapak-nfroidure": "0.5.2", | ||
"metapak": "0.0.21", | ||
"metapak-nfroidure": "0.7.0", | ||
"mocha": "3.2.0", | ||
@@ -74,0 +75,0 @@ "mocha-lcov-reporter": "1.3.0", |
@@ -363,2 +363,5 @@ <!-- | ||
</dd> | ||
<dt><a href="#handler">handler(handlerFunction, [dependencies])</a> ⇒ <code>function</code></dt> | ||
<dd><p>Shortcut to create an initializer with a simple handler</p> | ||
</dd> | ||
<dt><a href="#parseDependencyDeclaration">parseDependencyDeclaration(dependencyDeclaration)</a> ⇒ <code>Object</code></dt> | ||
@@ -770,2 +773,29 @@ <dd><p>Explode a dependency declaration an returns its parts.</p> | ||
``` | ||
<a name="handler"></a> | ||
## handler(handlerFunction, [dependencies]) ⇒ <code>function</code> | ||
Shortcut to create an initializer with a simple handler | ||
**Kind**: global function | ||
**Returns**: <code>function</code> - Returns a new initializer | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| handlerFunction | <code>function</code> | | The handler function | | ||
| [dependencies] | <code>Array</code> | <code>[]</code> | The dependencies to inject in it | | ||
**Example** | ||
```js | ||
import { initializer, getInstance } from 'knifecycle'; | ||
getInstance() | ||
.register(handler(getUser, ['db', '?log'])); | ||
const QUERY = `SELECT * FROM users WHERE id=$1` | ||
async function getUser({ db }, userId) { | ||
const [row] = await db.query(QUERY, userId); | ||
return row; | ||
} | ||
``` | ||
<a name="parseDependencyDeclaration"></a> | ||
@@ -772,0 +802,0 @@ |
@@ -277,2 +277,35 @@ import YError from 'yerror'; | ||
/** | ||
* Shortcut to create an initializer with a simple handler | ||
* @param {Function} handlerFunction | ||
* The handler function | ||
* @param {Array} [dependencies=[]] | ||
* The dependencies to inject in it | ||
* @return {Function} | ||
* Returns a new initializer | ||
* @example | ||
* import { initializer, getInstance } from 'knifecycle'; | ||
* | ||
* getInstance() | ||
* .register(handler(getUser, ['db', '?log'])); | ||
* | ||
* const QUERY = `SELECT * FROM users WHERE id=$1` | ||
* async function getUser({ db }, userId) { | ||
* const [row] = await db.query(QUERY, userId); | ||
* | ||
* return row; | ||
* } | ||
*/ | ||
export function handler(handlerFunction, dependencies = []) { | ||
if(!handlerFunction.name) { | ||
throw new YError('E_NO_HANDLER_NAME'); | ||
} | ||
return initializer({ | ||
name: handlerFunction.name, | ||
type: 'service', | ||
inject: dependencies, | ||
}, (...args) => Promise.resolve( | ||
handlerFunction.bind(null, ...args) | ||
)); | ||
} | ||
@@ -279,0 +312,0 @@ /* Architecture Note #1.3.1: Dependencies declaration syntax |
@@ -7,2 +7,3 @@ import assert from 'assert'; | ||
parseDependencyDeclaration, | ||
handler, | ||
} from './util'; | ||
@@ -78,2 +79,45 @@ | ||
describe('handler', () => { | ||
it('should work', () => { | ||
const injectedServices = ['kikooo', 'lol']; | ||
const services = { | ||
kikooo: 'kikooo', | ||
lol: 'lol', | ||
}; | ||
const theInitializer = handler( | ||
sampleHandler, | ||
injectedServices | ||
); | ||
assert.deepEqual( | ||
theInitializer.$name, | ||
sampleHandler.name | ||
); | ||
assert.deepEqual( | ||
theInitializer.$inject, | ||
['kikooo', 'lol'] | ||
); | ||
return theInitializer(services) | ||
.then(theHandler => theHandler('test')) | ||
.then( | ||
result => | ||
assert.deepEqual(result, { | ||
deps: services, | ||
args: ['test'], | ||
}) | ||
); | ||
function sampleHandler(deps, ...args) { | ||
return Promise.resolve({ deps, args }); | ||
} | ||
}); | ||
it('should fail for anonymous functions', () => { | ||
assert.throws(() => { | ||
handler(() => {}); | ||
}, /E_NO_HANDLER_NAME/); | ||
}); | ||
}); | ||
describe('parseDependencyDeclaration', () => { | ||
@@ -80,0 +124,0 @@ it('should work', () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
200554
4158
824
23