Skip to content

Commit

Permalink
Open hook workflow to custom methods (#72)
Browse files Browse the repository at this point in the history
* open hooks workflow to custom methods

* simplify converters

* add makeConverter method

* use Object.assign for node 6

* use hook.arguments

* remove arguments converting from hook data

* remove useless export

* remove makeConverter function

* simplify condition

* remove converters from createHookObject

* validateArguments for all methods
  • Loading branch information
bertho-zero authored and daffl committed Aug 21, 2018
1 parent af9b50e commit 5f2cfc0
Show file tree
Hide file tree
Showing 7 changed files with 1,222 additions and 1,346 deletions.
3 changes: 3 additions & 0 deletions packages/commons/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ node_modules

# Users Environment Variables
.lock-wscript

# IDEs
.idea
63 changes: 24 additions & 39 deletions packages/commons/lib/arguments.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
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) {
exports.validateArguments = function validateArguments (argsOrders, 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];
const methodArgs = argsOrders[method] || ['params'];
const methodParamCount = methodArgs.length;

// 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;
return methodArgs.every((argName, index) => {
switch (argName) {
case 'id':
if (args[index] === undefined) {
throw new Error(`An id must be provided to the '${method}' method`);
}
break;
case 'data':
if (!isObjectOrArray(args[index])) {
throw new Error(`A data object must be provided to the '${method}' method`);
}
break;
case 'params':
if (args[index] !== undefined && !isObjectOrArray(args[index])) {
throw new Error(`Params for '${method}' method must be an object`);
}
break;
}

return true;
});
};
27 changes: 2 additions & 25 deletions packages/commons/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,8 @@ const SKIP = exports.SKIP = typeof Symbol !== 'undefined'
? Symbol('__feathersSkipHooks')
: '__feathersSkipHooks';

const convertGetOrRemove = ([ id, params = {} ]) => ({ id, params });
const convertUpdateOrPatch = ([ id, data, params = {} ]) => ({ id, data, params });

// 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);
exports.createHookObject = function createHookObject (method, data = {}) {
const hook = {};

Object.defineProperty(hook, 'toJSON', {
value () {
Expand Down
Loading

0 comments on commit 5f2cfc0

Please sign in to comment.