Commit c57598f2 authored by Frauke's avatar Frauke
Browse files

Initial commit

parents
---
extends: eslint-config-semistandard
\ No newline at end of file
# Change Log
## [v1.3.1](https://github.com/feathersjs/commons/tree/v1.3.1) (2018-01-12)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.3.0...v1.3.1)
**Merged pull requests:**
- Allow array for sorting [\#66](https://github.com/feathersjs/commons/pull/66) ([daffl](https://github.com/daffl))
- Update semistandard to the latest version 🚀 [\#64](https://github.com/feathersjs/commons/pull/64) ([greenkeeper[bot]](https://github.com/apps/greenkeeper))
## [v1.3.0](https://github.com/feathersjs/commons/tree/v1.3.0) (2017-11-20)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.2.0...v1.3.0)
**Merged pull requests:**
- Add a toJSON method to the hook context [\#63](https://github.com/feathersjs/commons/pull/63) ([daffl](https://github.com/daffl))
- updating contributing guide and issue template [\#61](https://github.com/feathersjs/commons/pull/61) ([ekryski](https://github.com/ekryski))
## [v1.2.0](https://github.com/feathersjs/commons/tree/v1.2.0) (2017-10-25)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.1.0...v1.2.0)
**Merged pull requests:**
- Bring back makeUrl [\#62](https://github.com/feathersjs/commons/pull/62) ([daffl](https://github.com/daffl))
- adding codeclimate config [\#60](https://github.com/feathersjs/commons/pull/60) ([ekryski](https://github.com/ekryski))
## [v1.1.0](https://github.com/feathersjs/commons/tree/v1.1.0) (2017-10-23)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.0.0...v1.1.0)
**Merged pull requests:**
- Remove unused utilities and add some inline documentation [\#59](https://github.com/feathersjs/commons/pull/59) ([daffl](https://github.com/daffl))
- Add feathers-query-filters [\#58](https://github.com/feathersjs/commons/pull/58) ([daffl](https://github.com/daffl))
## [v1.0.0](https://github.com/feathersjs/commons/tree/v1.0.0) (2017-10-19)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.0.0-pre.3...v1.0.0)
**Merged pull requests:**
- Rename repository and add to npm scope [\#57](https://github.com/feathersjs/commons/pull/57) ([daffl](https://github.com/daffl))
- Updates for Feathers v3 \(Buzzard\) [\#56](https://github.com/feathersjs/commons/pull/56) ([daffl](https://github.com/daffl))
## [v1.0.0-pre.3](https://github.com/feathersjs/commons/tree/v1.0.0-pre.3) (2017-10-18)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.0.0-pre.2...v1.0.0-pre.3)
**Merged pull requests:**
- Update the client test suite [\#55](https://github.com/feathersjs/commons/pull/55) ([daffl](https://github.com/daffl))
- Update mocha to the latest version 🚀 [\#54](https://github.com/feathersjs/commons/pull/54) ([greenkeeper[bot]](https://github.com/apps/greenkeeper))
## [v1.0.0-pre.2](https://github.com/feathersjs/commons/tree/v1.0.0-pre.2) (2017-07-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.0.0-pre.1...v1.0.0-pre.2)
**Merged pull requests:**
- Update to new plugin infrastructure [\#53](https://github.com/feathersjs/commons/pull/53) ([daffl](https://github.com/daffl))
## [v1.0.0-pre.1](https://github.com/feathersjs/commons/tree/v1.0.0-pre.1) (2017-06-28)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.7...v1.0.0-pre.1)
**Merged pull requests:**
- Commons for Feathers v3 [\#52](https://github.com/feathersjs/commons/pull/52) ([daffl](https://github.com/daffl))
- Update chai to the latest version 🚀 [\#51](https://github.com/feathersjs/commons/pull/51) ([greenkeeper[bot]](https://github.com/apps/greenkeeper))
- Update semistandard to the latest version 🚀 [\#50](https://github.com/feathersjs/commons/pull/50) ([greenkeeper[bot]](https://github.com/apps/greenkeeper))
- Update dependencies to enable Greenkeeper 🌴 [\#49](https://github.com/feathersjs/commons/pull/49) ([greenkeeper[bot]](https://github.com/apps/greenkeeper))
## [v0.8.7](https://github.com/feathersjs/commons/tree/v0.8.7) (2016-11-30)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.6...v0.8.7)
**Closed issues:**
- Matcher function blows up with null values [\#46](https://github.com/feathersjs/commons/issues/46)
**Merged pull requests:**
- matcher now doesn't blow up with null values. Closes \#46 [\#47](https://github.com/feathersjs/commons/pull/47) ([ekryski](https://github.com/ekryski))
## [v0.8.6](https://github.com/feathersjs/commons/tree/v0.8.6) (2016-11-25)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.5...v0.8.6)
**Merged pull requests:**
- Allow to pass an object to hook object [\#45](https://github.com/feathersjs/commons/pull/45) ([daffl](https://github.com/daffl))
## [v0.8.5](https://github.com/feathersjs/commons/tree/v0.8.5) (2016-11-19)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.4...v0.8.5)
**Merged pull requests:**
- Deep merge and toObject [\#44](https://github.com/feathersjs/commons/pull/44) ([ekryski](https://github.com/ekryski))
- Expose lodash functions [\#43](https://github.com/feathersjs/commons/pull/43) ([ekryski](https://github.com/ekryski))
- Make url [\#42](https://github.com/feathersjs/commons/pull/42) ([ekryski](https://github.com/ekryski))
- Expect syntax [\#41](https://github.com/feathersjs/commons/pull/41) ([ekryski](https://github.com/ekryski))
## [v0.8.4](https://github.com/feathersjs/commons/tree/v0.8.4) (2016-11-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.3...v0.8.4)
## [v0.8.3](https://github.com/feathersjs/commons/tree/v0.8.3) (2016-11-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.2...v0.8.3)
## [v0.8.2](https://github.com/feathersjs/commons/tree/v0.8.2) (2016-11-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.1...v0.8.2)
**Merged pull requests:**
- One more fix for select on arrays [\#40](https://github.com/feathersjs/commons/pull/40) ([daffl](https://github.com/daffl))
## [v0.8.1](https://github.com/feathersjs/commons/tree/v0.8.1) (2016-11-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.8.0...v0.8.1)
**Merged pull requests:**
- Fixing select utility methods to work with query selector [\#39](https://github.com/feathersjs/commons/pull/39) ([daffl](https://github.com/daffl))
## [v0.8.0](https://github.com/feathersjs/commons/tree/v0.8.0) (2016-11-09)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.8...v0.8.0)
**Merged pull requests:**
- Implementing lodash utilities and helpers for selecting [\#38](https://github.com/feathersjs/commons/pull/38) ([daffl](https://github.com/daffl))
- jshint —\> semistandard [\#37](https://github.com/feathersjs/commons/pull/37) ([corymsmith](https://github.com/corymsmith))
## [v0.7.8](https://github.com/feathersjs/commons/tree/v0.7.8) (2016-10-21)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.7...v0.7.8)
**Merged pull requests:**
- Make getting the service in base test dynamic [\#36](https://github.com/feathersjs/commons/pull/36) ([daffl](https://github.com/daffl))
## [v0.7.7](https://github.com/feathersjs/commons/tree/v0.7.7) (2016-10-21)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.6...v0.7.7)
**Merged pull requests:**
- Allow app in hookObject [\#35](https://github.com/feathersjs/commons/pull/35) ([daffl](https://github.com/daffl))
## [v0.7.6](https://github.com/feathersjs/commons/tree/v0.7.6) (2016-10-20)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.5...v0.7.6)
**Merged pull requests:**
- Add test for matching and increase code coverage [\#34](https://github.com/feathersjs/commons/pull/34) ([daffl](https://github.com/daffl))
- omit '$select' in matcher [\#33](https://github.com/feathersjs/commons/pull/33) ([beeplin](https://github.com/beeplin))
- adding code coverage [\#32](https://github.com/feathersjs/commons/pull/32) ([ekryski](https://github.com/ekryski))
## [v0.7.5](https://github.com/feathersjs/commons/tree/v0.7.5) (2016-09-05)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.4...v0.7.5)
**Closed issues:**
- Feathers should accept other type of data beside only the object type. [\#26](https://github.com/feathersjs/commons/issues/26)
- Send better error messages for method normalization [\#12](https://github.com/feathersjs/commons/issues/12)
**Merged pull requests:**
- Allow matching nested $or queries [\#29](https://github.com/feathersjs/commons/pull/29) ([daffl](https://github.com/daffl))
- Add default export to `hooks.js` [\#28](https://github.com/feathersjs/commons/pull/28) ([KenanY](https://github.com/KenanY))
- Update mocha to version 3.0.0 🚀 [\#27](https://github.com/feathersjs/commons/pull/27) ([greenkeeperio-bot](https://github.com/greenkeeperio-bot))
## [v0.7.4](https://github.com/feathersjs/commons/tree/v0.7.4) (2016-05-29)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.3...v0.7.4)
**Merged pull requests:**
- Use forEach instead of ES6 'for of' loop [\#25](https://github.com/feathersjs/commons/pull/25) ([lopezjurip](https://github.com/lopezjurip))
- mocha@2.5.0 breaks build 🚨 [\#24](https://github.com/feathersjs/commons/pull/24) ([greenkeeperio-bot](https://github.com/greenkeeperio-bot))
- Update babel-plugin-add-module-exports to version 0.2.0 🚀 [\#23](https://github.com/feathersjs/commons/pull/23) ([greenkeeperio-bot](https://github.com/greenkeeperio-bot))
## [v0.7.3](https://github.com/feathersjs/commons/tree/v0.7.3) (2016-05-05)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.2...v0.7.3)
**Merged pull requests:**
- Make sure arguments from hook objects are created properly for known … [\#22](https://github.com/feathersjs/commons/pull/22) ([daffl](https://github.com/daffl))
## [v0.7.2](https://github.com/feathersjs/commons/tree/v0.7.2) (2016-04-26)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.1...v0.7.2)
**Merged pull requests:**
- Update test fixture to use promises and add error cases [\#19](https://github.com/feathersjs/commons/pull/19) ([daffl](https://github.com/daffl))
## [v0.7.1](https://github.com/feathersjs/commons/tree/v0.7.1) (2016-04-04)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.6.2...v0.7.1)
**Merged pull requests:**
- Adding functionality and tests for shared query and list handling [\#17](https://github.com/feathersjs/commons/pull/17) ([daffl](https://github.com/daffl))
## [v0.6.2](https://github.com/feathersjs/commons/tree/v0.6.2) (2016-02-09)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.7.0...v0.6.2)
## [v0.7.0](https://github.com/feathersjs/commons/tree/v0.7.0) (2016-02-08)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.6.1...v0.7.0)
## [v0.6.1](https://github.com/feathersjs/commons/tree/v0.6.1) (2016-02-08)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.6.0...v0.6.1)
**Merged pull requests:**
- Add NSP check to test script. [\#16](https://github.com/feathersjs/commons/pull/16) ([marshallswain](https://github.com/marshallswain))
## [v0.6.0](https://github.com/feathersjs/commons/tree/v0.6.0) (2016-01-21)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.5.0...v0.6.0)
**Closed issues:**
- Rename hooks to hookUtils to make room for common hooks. [\#13](https://github.com/feathersjs/commons/issues/13)
**Merged pull requests:**
- Remove shared socket functionality [\#15](https://github.com/feathersjs/commons/pull/15) ([daffl](https://github.com/daffl))
- Support socket routes with apps mounted on a path [\#14](https://github.com/feathersjs/commons/pull/14) ([daffl](https://github.com/daffl))
## [v0.5.0](https://github.com/feathersjs/commons/tree/v0.5.0) (2016-01-10)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.4.0...v0.5.0)
## [v0.4.0](https://github.com/feathersjs/commons/tree/v0.4.0) (2016-01-10)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.3.4...v0.4.0)
## [v0.3.4](https://github.com/feathersjs/commons/tree/v0.3.4) (2016-01-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.3.3...v0.3.4)
**Merged pull requests:**
- Fix SocketIO client iteration for all cases [\#11](https://github.com/feathersjs/commons/pull/11) ([daffl](https://github.com/daffl))
## [v0.3.3](https://github.com/feathersjs/commons/tree/v0.3.3) (2016-01-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.3.2...v0.3.3)
**Closed issues:**
- Socket.io 1.4.0 broke feathers [\#10](https://github.com/feathersjs/commons/issues/10)
## [v0.3.2](https://github.com/feathersjs/commons/tree/v0.3.2) (2016-01-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.3.1...v0.3.2)
## [v0.3.1](https://github.com/feathersjs/commons/tree/v0.3.1) (2016-01-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.3.0...v0.3.1)
## [v0.3.0](https://github.com/feathersjs/commons/tree/v0.3.0) (2015-12-11)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.11...v0.3.0)
**Closed issues:**
- babel inside package.json breaks react-native [\#9](https://github.com/feathersjs/commons/issues/9)
## [v0.2.11](https://github.com/feathersjs/commons/tree/v0.2.11) (2015-11-30)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.10...v0.2.11)
**Merged pull requests:**
- getOrRemove did not check id property type [\#8](https://github.com/feathersjs/commons/pull/8) ([daffl](https://github.com/daffl))
## [v0.2.10](https://github.com/feathersjs/commons/tree/v0.2.10) (2015-11-28)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.9...v0.2.10)
**Closed issues:**
- Remove dependency on lodash [\#6](https://github.com/feathersjs/commons/issues/6)
**Merged pull requests:**
- Migrate to Babel 6 and remove Lodash dependency [\#7](https://github.com/feathersjs/commons/pull/7) ([daffl](https://github.com/daffl))
## [v0.2.9](https://github.com/feathersjs/commons/tree/v0.2.9) (2015-11-17)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.8...v0.2.9)
**Closed issues:**
- Event dispatcher context is not being set to the service [\#5](https://github.com/feathersjs/commons/issues/5)
- .create with no callback throws error [\#4](https://github.com/feathersjs/commons/issues/4)
## [v0.2.8](https://github.com/feathersjs/commons/tree/v0.2.8) (2015-10-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.7...v0.2.8)
**Closed issues:**
- getArguments not exporting correctly [\#1](https://github.com/feathersjs/commons/issues/1)
**Merged pull requests:**
- Add hookObject utilities and remove Lodash dependency from arguments.js [\#3](https://github.com/feathersjs/commons/pull/3) ([daffl](https://github.com/daffl))
## [v0.2.7](https://github.com/feathersjs/commons/tree/v0.2.7) (2015-03-07)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.6...v0.2.7)
## [v0.2.6](https://github.com/feathersjs/commons/tree/v0.2.6) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/v0.2.5...v0.2.6)
## [v0.2.5](https://github.com/feathersjs/commons/tree/v0.2.5) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/0.2.3...v0.2.5)
## [0.2.3](https://github.com/feathersjs/commons/tree/0.2.3) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/0.2.2...0.2.3)
## [0.2.2](https://github.com/feathersjs/commons/tree/0.2.2) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/0.2.1...0.2.2)
## [0.2.1](https://github.com/feathersjs/commons/tree/0.2.1) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/0.2.0...0.2.1)
## [0.2.0](https://github.com/feathersjs/commons/tree/0.2.0) (2015-03-06)
[Full Changelog](https://github.com/feathersjs/commons/compare/0.1.0...0.2.0)
## [0.1.0](https://github.com/feathersjs/commons/tree/0.1.0) (2015-03-06)
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2015 Feathers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Feathers Commons
[![Greenkeeper badge](https://badges.greenkeeper.io/feathersjs/commons.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/feathersjs/commons.png?branch=master)](https://travis-ci.org/feathersjs/commons)
[![Test Coverage](https://codeclimate.com/github/feathersjs/commons/badges/coverage.svg)](https://codeclimate.com/github/feathersjs/commons/coverage)
[![Dependency Status](https://img.shields.io/david/feathersjs/commons.svg?style=flat-square)](https://david-dm.org/feathersjs/commons)
[![Download Status](https://img.shields.io/npm/dm/@feathersjs/commons.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/commons)
> Shared Feathers utility functions
## About
This is a repository for utility functionality that is shared between different Feathers plugin and used by the main repository.
## Authors
[Feathers contributors](https://github.com/feathersjs/commons/graphs/contributors)
## License
Copyright (c) 2017 Feathers contributors
Licensed under the [MIT license](LICENSE).
const paramCounts = {
find: 1,
get: 2,
create: 2,
update: 3,
patch: 3,
remove: 2
};
function isObjectOrArray (value) {
return typeof value === 'object' && value !== null;
}
exports.validateArguments = function validateArguments (method, args) {
// Check if the last argument is a callback which are no longer supported
if (typeof args[args.length - 1] === 'function') {
throw new Error('Callbacks are no longer supported. Use Promises or async/await instead.');
}
const methodParamCount = paramCounts[method];
// Check the number of arguments and throw an error if too many are provided
if (methodParamCount && args.length > methodParamCount) {
throw new Error(`Too many arguments for '${method}' method`);
}
// `params` is always the last argument
const params = args[methodParamCount - 1];
// Check if `params` is an object (can be undefined though)
if (params !== undefined && !isObjectOrArray(params)) {
throw new Error(`Params for '${method}' method must be an object`);
}
// Validate other arguments for each method
switch (method) {
case 'create':
if (!isObjectOrArray(args[0])) {
throw new Error(`A data object must be provided to the 'create' method`);
}
break;
case 'get':
case 'remove':
case 'update':
case 'patch':
if (args[0] === undefined) {
throw new Error(`An id must be provided to the '${method}' method`);
}
if ((method === 'update' || method === 'patch') && !isObjectOrArray(args[1])) {
throw new Error(`A data object must be provided to the '${method}' method`);
}
}
return true;
};
const utils = require('./utils');
const hooks = require('./hooks');
const args = require('./arguments');
const filterQuery = require('./filter-query');
module.exports = Object.assign({}, utils, args, { hooks, filterQuery });
const { _ } = require('./utils');
// Officially supported query parameters ($populate is kind of special)
const PROPERTIES = ['$sort', '$limit', '$skip', '$select', '$populate'];
function parse (number) {
if (typeof number !== 'undefined') {
return Math.abs(parseInt(number, 10));
}
}
// Returns the pagination limit and will take into account the
// default and max pagination settings
function getLimit (limit, paginate) {
if (paginate && paginate.default) {
const lower = typeof limit === 'number' ? limit : paginate.default;
const upper = typeof paginate.max === 'number' ? paginate.max : Number.MAX_VALUE;
return Math.min(lower, upper);
}
return limit;
}
// Makes sure that $sort order is always converted to an actual number
function convertSort (sort) {
if (typeof sort !== 'object' || Array.isArray(sort)) {
return sort;
}
const result = {};
Object.keys(sort).forEach(key => {
result[key] = typeof sort[key] === 'object'
? sort[key] : parseInt(sort[key], 10);
});
return result;
}
// Converts Feathers special query parameters and pagination settings
// and returns them separately a `filters` and the rest of the query
// as `query`
module.exports = function (query, paginate) {
let filters = {
$sort: convertSort(query.$sort),
$limit: getLimit(parse(query.$limit), paginate),
$skip: parse(query.$skip),
$select: query.$select,
$populate: query.$populate
};
return { filters, query: _.omit(query, ...PROPERTIES) };
};
const { each, pick } = require('./utils')._;
function convertGetOrRemove (args) {
const [ id, params = {} ] = args;
return { id, params };
}
function convertUpdateOrPatch (args) {
const [ id, data, params = {} ] = args;
return { id, data, params };
}
// To skip further hooks
const SKIP = exports.SKIP = typeof Symbol !== 'undefined' ? Symbol('__feathersSkipHooks') : '__feathersSkipHooks';
// Converters from service method arguments to hook object properties
exports.converters = {
find (args) {
const [ params = {} ] = args;
return { params };
},
create (args) {
const [ data, params = {} ] = args;
return { data, params };
},
get: convertGetOrRemove,
remove: convertGetOrRemove,
update: convertUpdateOrPatch,
patch: convertUpdateOrPatch
};
// Create a hook object for a method with arguments `args`
// `data` is additional data that will be added
exports.createHookObject = function createHookObject (method, args, data = {}) {
const hook = exports.converters[method](args);
Object.defineProperty(hook, 'toJSON', {
value () {
return pick(this, 'type', 'method', 'path',
'params', 'id', 'data', 'result', 'error');
}
});
return Object.assign(hook, data, {
method,
// A dynamic getter that returns the path of the service
get path () {
const { app, service } = data;
if (!service || !app || !app.services) {
return null;
}
return Object.keys(app.services)
.find(path => app.services[path] === service);
}
});
};
// Fallback used by `makeArguments` which usually won't be used
exports.defaultMakeArguments = function defaultMakeArguments (hook) {
const result = [];
if (typeof hook.id !== 'undefined') {
result.push(hook.id);
}
if (hook.data) {
result.push(hook.data);
}
result.push(hook.params || {});
return result;
};
// Turns a hook object back into a list of arguments
// to call a service method with
exports.makeArguments = function makeArguments (hook) {
switch (hook.method) {
case 'find':
return [ hook.params ];
case 'get':
case 'remove':
return [ hook.id, hook.params ];
case 'update':
case 'patch':
return [ hook.id, hook.data, hook.params ];
case 'create':
return [ hook.data, hook.params ];
}
return exports.defaultMakeArguments(hook);
};
// Converts different hook registration formats into the
// same internal format
exports.convertHookData = function convertHookData (obj) {
var hook = {};
if (Array.isArray(obj)) {
hook = { all: obj };
} else if (typeof obj !==