@feathersjs/commons
Advanced tools
Comparing version 1.4.4 to 2.0.0
# Change Log | ||
## [v1.4.4](https://github.com/feathersjs/commons/tree/v1.4.4) (2018-08-01) | ||
[Full Changelog](https://github.com/feathersjs/commons/compare/v1.4.3...v1.4.4) | ||
**Merged pull requests:** | ||
- Update utils.js [\#79](https://github.com/feathersjs/commons/pull/79) ([bertho-zero](https://github.com/bertho-zero)) | ||
## [v1.4.3](https://github.com/feathersjs/commons/tree/v1.4.3) (2018-07-25) | ||
@@ -4,0 +11,0 @@ [Full Changelog](https://github.com/feathersjs/commons/compare/v1.4.2...v1.4.3) |
@@ -1,10 +0,1 @@ | ||
const paramCounts = { | ||
find: 1, | ||
get: 2, | ||
create: 2, | ||
update: 3, | ||
patch: 3, | ||
remove: 2 | ||
}; | ||
function isObjectOrArray (value) { | ||
@@ -14,3 +5,3 @@ 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 | ||
@@ -21,3 +12,4 @@ if (typeof args[args.length - 1] === 'function') { | ||
const methodParamCount = paramCounts[method]; | ||
const methodArgs = argsOrders[method] || ['params']; | ||
const methodParamCount = methodArgs.length; | ||
@@ -29,31 +21,24 @@ // Check the number of arguments and throw an error if too many are provided | ||
// `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`); | ||
} | ||
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; | ||
} | ||
if ((method === 'update' || method === 'patch') && !isObjectOrArray(args[1])) { | ||
throw new Error(`A data object must be provided to the '${method}' method`); | ||
} | ||
} | ||
return true; | ||
return true; | ||
}); | ||
}; |
const { _ } = require('./utils'); | ||
// Officially supported query parameters ($populate is kind of special) | ||
const PROPERTIES = ['$sort', '$limit', '$skip', '$select', '$populate']; | ||
function parse (number) { | ||
@@ -41,15 +38,55 @@ if (typeof number !== 'undefined') { | ||
function cleanQuery (query, operators) { | ||
if (Array.isArray(query)) { | ||
return query.map((query) => cleanQuery(query, operators)); | ||
} | ||
if (query && query.constructor === Object) { | ||
const result = {}; | ||
_.each(query, (query, key) => { | ||
if (key[0] === '$' && operators.indexOf(key) === -1) return; | ||
result[key] = cleanQuery(query, operators); | ||
}); | ||
return result; | ||
} | ||
return query; | ||
} | ||
function assignFilters (object, query, filters, options) { | ||
if (Array.isArray(filters)) { | ||
_.each(filters, (key) => { | ||
object[key] = query[key]; | ||
}); | ||
} else { | ||
_.each(filters, (converter, key) => { | ||
object[key] = converter(query[key], options); | ||
}); | ||
} | ||
} | ||
const FILTERS = { | ||
$sort: (value) => convertSort(value), | ||
$limit: (value, options) => getLimit(parse(value), options.paginate), | ||
$skip: (value) => parse(value), | ||
$select: (value) => value | ||
}; | ||
const OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or']; | ||
// 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 | ||
}; | ||
module.exports = function filterQuery (query, options = {}) { | ||
let { filters: additionalFilters = {}, operators: additionalOperators = [] } = options; | ||
let result = {}; | ||
return { filters, query: _.omit(query, ...PROPERTIES) }; | ||
result.filters = {}; | ||
assignFilters(result.filters, query, FILTERS, options); | ||
assignFilters(result.filters, query, additionalFilters, options); | ||
let operators = OPERATORS.concat(additionalOperators); | ||
result.query = cleanQuery(query, operators); | ||
return result; | ||
}; |
@@ -8,28 +8,5 @@ const { each, pick } = require('./utils')._; | ||
const convertGetOrRemove = ([ id, params = {} ]) => ({ id, params }); | ||
const convertUpdateOrPatch = ([ id, data, params = {} ]) => ({ id, data, params }); | ||
exports.createHookObject = function createHookObject (method, data = {}) { | ||
const hook = {}; | ||
// 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', { | ||
@@ -36,0 +13,0 @@ value () { |
{ | ||
"name": "@feathersjs/commons", | ||
"version": "1.4.4", | ||
"version": "2.0.0", | ||
"description": "Shared Feathers utility functions", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/feathersjs/commons", |
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
42914