@feathersjs/commons
Advanced tools
Comparing version 1.0.0 to 1.1.0
# Change Log | ||
## [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) | ||
@@ -4,0 +12,0 @@ [Full Changelog](https://github.com/feathersjs/commons/compare/v1.0.0-pre.2...v1.0.0-pre.3) |
const utils = require('./utils'); | ||
const hooks = require('./hooks'); | ||
const args = require('./arguments'); | ||
const filterQuery = require('./filter-query'); | ||
module.exports = Object.assign({}, utils, args, { hooks }); | ||
module.exports = Object.assign({}, utils, args, { hooks, filterQuery }); |
const { each } = require('./utils')._; | ||
function getParams (value) { | ||
return value || {}; | ||
} | ||
function convertGetOrRemove (args) { | ||
const [ id, params = {} ] = args; | ||
function convertGetOrRemove (args) { | ||
return { | ||
id: args[0], | ||
params: getParams(args[1]) | ||
}; | ||
return { id, params }; | ||
} | ||
function convertUpdateOrPatch (args) { | ||
return { | ||
id: args[0], | ||
data: args[1], | ||
params: getParams(args[2]) | ||
}; | ||
const [ id, data, params = {} ] = args; | ||
return { id, data, params }; | ||
} | ||
// Converters from service method arguments to hook object properties | ||
exports.converters = { | ||
find (args) { | ||
return { | ||
params: getParams(args[0]) | ||
}; | ||
const [ params = {} ] = args; | ||
return { params }; | ||
}, | ||
create (args) { | ||
return { | ||
data: args[0], | ||
params: getParams(args[1]) | ||
}; | ||
const [ data, params = {} ] = args; | ||
return { data, params }; | ||
}, | ||
@@ -40,2 +33,4 @@ get: convertGetOrRemove, | ||
// 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 = {}) { | ||
@@ -46,2 +41,3 @@ const hook = exports.converters[method](args); | ||
method, | ||
// A dynamic getter that returns the path of the service | ||
get path () { | ||
@@ -60,2 +56,3 @@ const { app, service } = data; | ||
// Fallback used by `makeArguments` which usually won't be used | ||
exports.defaultMakeArguments = function defaultMakeArguments (hook) { | ||
@@ -77,22 +74,23 @@ const result = []; | ||
// Turns a hook object back into a list of arguments | ||
// to call a service method with | ||
exports.makeArguments = function makeArguments (hook) { | ||
if (hook.method === 'find') { | ||
return [ hook.params ]; | ||
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 ]; | ||
} | ||
if (hook.method === 'get' || hook.method === 'remove') { | ||
return [ hook.id, hook.params ]; | ||
} | ||
if (hook.method === 'update' || hook.method === 'patch') { | ||
return [ hook.id, hook.data, hook.params ]; | ||
} | ||
if (hook.method === '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) { | ||
@@ -114,2 +112,4 @@ var hook = {}; | ||
// Duck-checks a given object to be a hook object | ||
// A valid hook object has `type` and `method` | ||
exports.isHookObject = function isHookObject (hookObject) { | ||
@@ -121,2 +121,5 @@ return typeof hookObject === 'object' && | ||
// Returns all service and application hooks combined | ||
// for a given method and type `appLast` sets if the hooks | ||
// from `app` should be added last (or first by default) | ||
exports.getHooks = function getHooks (app, service, type, method, appLast = false) { | ||
@@ -137,2 +140,4 @@ const appHooks = app.__hooks[type][method] || []; | ||
let updateCurrentHook = current => { | ||
// Either use the returned hook object or the current | ||
// hook object from the chain if the hook returned undefined | ||
if (current) { | ||
@@ -148,2 +153,3 @@ if (!exports.isHookObject(current)) { | ||
}; | ||
// First step of the hook chain with the initial hook object | ||
let promise = Promise.resolve(hookObject); | ||
@@ -159,3 +165,4 @@ | ||
hook(hookObject, (error, result) => | ||
error ? reject(error) : resolve(result)); | ||
error ? reject(error) : resolve(result) | ||
); | ||
}); | ||
@@ -178,2 +185,3 @@ }); | ||
// Add `.hooks` functionality to an object | ||
exports.enableHooks = function enableHooks (obj, methods, types) { | ||
@@ -180,0 +188,0 @@ if (typeof obj.hooks === 'function') { |
@@ -0,1 +1,2 @@ | ||
// Removes all leading and trailing slashes from a path | ||
exports.stripSlashes = function stripSlashes (name) { | ||
@@ -5,2 +6,3 @@ return name.replace(/^(\/*)|(\/*)$/g, ''); | ||
// A set of lodash-y utility functions that use ES6 | ||
const _ = exports._ = { | ||
@@ -65,2 +67,3 @@ each (obj, callback) { | ||
// Recursively merge the source object into the target object | ||
merge (target, source) { | ||
@@ -70,3 +73,6 @@ if (_.isObject(target) && _.isObject(source)) { | ||
if (_.isObject(source[key])) { | ||
if (!target[key]) Object.assign(target, { [key]: {} }); | ||
if (!target[key]) { | ||
Object.assign(target, { [key]: {} }); | ||
} | ||
_.merge(target[key], source[key]); | ||
@@ -82,32 +88,5 @@ } else { | ||
exports.specialFilters = { | ||
$in (key, ins) { | ||
return current => ins.indexOf(current[key]) !== -1; | ||
}, | ||
$nin (key, nins) { | ||
return current => nins.indexOf(current[key]) === -1; | ||
}, | ||
$lt (key, value) { | ||
return current => current[key] < value; | ||
}, | ||
$lte (key, value) { | ||
return current => current[key] <= value; | ||
}, | ||
$gt (key, value) { | ||
return current => current[key] > value; | ||
}, | ||
$gte (key, value) { | ||
return current => current[key] >= value; | ||
}, | ||
$ne (key, value) { | ||
return current => current[key] !== value; | ||
} | ||
}; | ||
// Return a function that filters a result object or array | ||
// and picks only the fields passed as `params.query.$select` | ||
// and additional `otherFields` | ||
exports.select = function select (params, ...otherFields) { | ||
@@ -137,29 +116,4 @@ const fields = params && params.query && params.query.$select; | ||
exports.matcher = function matcher (originalQuery) { | ||
const query = _.omit(originalQuery, '$limit', '$skip', '$sort', '$select'); | ||
return function (item) { | ||
if (query.$or && _.some(query.$or, or => matcher(or)(item))) { | ||
return true; | ||
} | ||
return _.every(query, (value, key) => { | ||
if (value !== null && typeof value === 'object') { | ||
return _.every(value, (target, filterType) => { | ||
if (exports.specialFilters[filterType]) { | ||
const filter = exports.specialFilters[filterType](key, target); | ||
return filter(item); | ||
} | ||
return false; | ||
}); | ||
} else if (typeof item[key] !== 'undefined') { | ||
return item[key] === query[key]; | ||
} | ||
return false; | ||
}); | ||
}; | ||
}; | ||
// An in-memory sorting function according to the | ||
// $sort special query parameter | ||
exports.sorter = function sorter ($sort) { | ||
@@ -183,15 +137,3 @@ return function (first, second) { | ||
exports.makeUrl = function makeUrl (path, app = {}) { | ||
const get = typeof app.get === 'function' ? app.get.bind(app) : () => {}; | ||
const env = get('env') || process.env.NODE_ENV; | ||
const host = get('host') || process.env.HOST_NAME || 'localhost'; | ||
const protocol = (env === 'development' || env === 'test' || (env === undefined)) ? 'http' : 'https'; | ||
const PORT = get('port') || process.env.PORT || 3030; | ||
const port = (env === 'development' || env === 'test' || (env === undefined)) ? `:${PORT}` : ''; | ||
path = path || ''; | ||
return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`; | ||
}; | ||
// Duck-checks if an object looks like a promise | ||
exports.isPromise = function isPromise (result) { | ||
@@ -198,0 +140,0 @@ return _.isObject(result) && |
{ | ||
"name": "@feathersjs/commons", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Shared Feathers utility functions", | ||
@@ -32,3 +32,3 @@ "homepage": "https://github.com/feathersjs/commons", | ||
"release:major": "npm version major && npm publish --access public", | ||
"release:prerelease": "npm version prerelease && npm publish --tag pre --access public", | ||
"release:pre": "npm version prerelease && npm publish --tag pre --access public", | ||
"changelog": "github_changelog_generator && git add CHANGELOG.md && git commit -am \"Updating changelog\"", | ||
@@ -35,0 +35,0 @@ "lint": "semistandard --fix", |
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
35202
12
555