Socket
Socket
Sign inDemoInstall

@feathersjs/commons

Package Overview
Dependencies
Maintainers
4
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@feathersjs/commons - npm Package Compare versions

Comparing version 4.0.0 to 4.3.0-pre.1

lib/hooks.d.ts

60

CHANGELOG.md

@@ -6,2 +6,62 @@ # Change Log

# [4.3.0-pre.1](https://github.com/feathersjs/feathers/compare/v4.0.0-pre.5...v4.3.0-pre.1) (2019-07-11)
**Note:** Version bump only for package @feathersjs/commons
# [4.0.0-pre.5](https://github.com/feathersjs/feathers/compare/v4.0.0-pre.4...v4.0.0-pre.5) (2019-07-10)
**Note:** Version bump only for package @feathersjs/commons
# [4.0.0-pre.4](https://github.com/feathersjs/feathers/compare/v4.0.0-pre.3...v4.0.0-pre.4) (2019-07-05)
**Note:** Version bump only for package @feathersjs/commons
# [4.0.0-pre.3](https://github.com/feathersjs/feathers/compare/v4.0.0-pre.2...v4.0.0-pre.3) (2019-06-01)
### Bug Fixes
* Update dependencies and fix tests ([#1373](https://github.com/feathersjs/feathers/issues/1373)) ([d743a7f](https://github.com/feathersjs/feathers/commit/d743a7f))
# [4.0.0-pre.0](https://github.com/feathersjs/feathers/compare/v3.2.0-pre.1...v4.0.0-pre.0) (2019-04-21)
### Bug Fixes
* Make Mocha a proper devDependency for every repository ([#1053](https://github.com/feathersjs/feathers/issues/1053)) ([9974803](https://github.com/feathersjs/feathers/commit/9974803))
* Update all dependencies to latest ([#1206](https://github.com/feathersjs/feathers/issues/1206)) ([e51e0f6](https://github.com/feathersjs/feathers/commit/e51e0f6))
* use minimal RegExp matching for better performance ([#977](https://github.com/feathersjs/feathers/issues/977)) ([3ca7e97](https://github.com/feathersjs/feathers/commit/3ca7e97))
### Features
* Add TypeScript definitions ([#1275](https://github.com/feathersjs/feathers/issues/1275)) ([9dd6713](https://github.com/feathersjs/feathers/commit/9dd6713))
* Common database adapter utilities and test suite ([#1130](https://github.com/feathersjs/feathers/issues/1130)) ([17b3dc8](https://github.com/feathersjs/feathers/commit/17b3dc8))
* Remove (hook, next) signature and SKIP support ([#1269](https://github.com/feathersjs/feathers/issues/1269)) ([211c0f8](https://github.com/feathersjs/feathers/commit/211c0f8))
### BREAKING CHANGES
* Move database adapter utilities from @feathersjs/commons into its own module
<a name="4.0.0"></a>

@@ -8,0 +68,0 @@ # [4.0.0](https://github.com/feathersjs/feathers/compare/@feathersjs/commons@3.0.1...@feathersjs/commons@4.0.0) (2018-12-16)

345

lib/hooks.js

@@ -1,206 +0,167 @@

const { _: { each, pick }, createSymbol } = require('./utils');
// To skip further hooks
const SKIP = createSymbol('__feathersSkipHooks');
exports.SKIP = SKIP;
exports.ACTIVATE_HOOKS = createSymbol('__feathersActivateHooks');
exports.createHookObject = function createHookObject (method, data = {}) {
const hook = {};
Object.defineProperty(hook, 'toJSON', {
value () {
return pick(this, 'type', 'method', 'path',
'params', 'id', 'data', 'result', 'error');
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const { each, pick } = utils_1._;
exports.ACTIVATE_HOOKS = utils_1.createSymbol('__feathersActivateHooks');
function createHookObject(method, data = {}) {
const hook = {};
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);
}
});
}
exports.createHookObject = createHookObject;
// Fallback used by `makeArguments` which usually won't be used
function defaultMakeArguments(hook) {
const result = [];
if (typeof hook.id !== 'undefined') {
result.push(hook.id);
}
});
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);
if (hook.data) {
result.push(hook.data);
}
});
};
// 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;
};
result.push(hook.params || {});
return result;
}
exports.defaultMakeArguments = defaultMakeArguments;
// 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);
};
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 defaultMakeArguments(hook);
}
exports.makeArguments = makeArguments;
// Converts different hook registration formats into the
// same internal format
exports.convertHookData = function convertHookData (obj) {
let hook = {};
if (Array.isArray(obj)) {
hook = { all: obj };
} else if (typeof obj !== 'object') {
hook = { all: [ obj ] };
} else {
each(obj, function (value, key) {
hook[key] = !Array.isArray(value) ? [ value ] : value;
});
}
return hook;
};
function convertHookData(obj) {
let hook = {};
if (Array.isArray(obj)) {
hook = { all: obj };
}
else if (typeof obj !== 'object') {
hook = { all: [obj] };
}
else {
each(obj, function (value, key) {
hook[key] = !Array.isArray(value) ? [value] : value;
});
}
return hook;
}
exports.convertHookData = convertHookData;
// Duck-checks a given object to be a hook object
// A valid hook object has `type` and `method`
exports.isHookObject = function isHookObject (hookObject) {
return typeof hookObject === 'object' &&
typeof hookObject.method === 'string' &&
typeof hookObject.type === 'string';
};
function isHookObject(hookObject) {
return typeof hookObject === 'object' &&
typeof hookObject.method === 'string' &&
typeof hookObject.type === 'string';
}
exports.isHookObject = isHookObject;
// 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) {
const appHooks = app.__hooks[type][method] || [];
const serviceHooks = service.__hooks[type][method] || [];
if (appLast) {
// Run hooks in the order of service -> app -> finally
return serviceHooks.concat(appHooks);
}
return appHooks.concat(serviceHooks);
};
exports.processHooks = function processHooks (hooks, initialHookObject) {
let hookObject = initialHookObject;
let updateCurrentHook = current => {
// Either use the returned hook object or the current
// hook object from the chain if the hook returned undefined
if (current) {
if (current === SKIP) {
return SKIP;
}
if (!exports.isHookObject(current)) {
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
}
hookObject = current;
function getHooks(app, service, type, method, appLast = false) {
const appHooks = app.__hooks[type][method] || [];
const serviceHooks = service.__hooks[type][method] || [];
if (appLast) {
// Run hooks in the order of service -> app -> finally
return serviceHooks.concat(appHooks);
}
return hookObject;
};
// Go through all hooks and chain them into our promise
const promise = hooks.reduce((promise, fn) => {
const hook = fn.bind(this);
if (hook.length === 2) { // function(hook, next)
promise = promise.then(hookObject => hookObject === SKIP ? SKIP : new Promise((resolve, reject) => {
hook(hookObject, (error, result) =>
error ? reject(error) : resolve(result)
);
}));
} else { // function(hook)
promise = promise.then(hookObject => hookObject === SKIP ? SKIP : hook(hookObject));
return appHooks.concat(serviceHooks);
}
exports.getHooks = getHooks;
function processHooks(hooks, initialHookObject) {
let hookObject = initialHookObject;
const updateCurrentHook = (current) => {
// Either use the returned hook object or the current
// hook object from the chain if the hook returned undefined
if (current) {
if (!isHookObject(current)) {
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
}
hookObject = current;
}
return hookObject;
};
// Go through all hooks and chain them into our promise
const promise = hooks.reduce((current, fn) => {
// @ts-ignore
const hook = fn.bind(this);
// Use the returned hook object or the old one
return current.then((currentHook) => hook(currentHook)).then(updateCurrentHook);
}, Promise.resolve(hookObject));
return promise.then(() => hookObject).catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
}
exports.processHooks = processHooks;
// Add `.hooks` functionality to an object
function enableHooks(obj, methods, types) {
if (typeof obj.hooks === 'function') {
return obj;
}
// Use the returned hook object or the old one
return promise.then(updateCurrentHook);
}, Promise.resolve(hookObject));
return promise.then(() => hookObject).catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
};
// Add `.hooks` functionality to an object
exports.enableHooks = function enableHooks (obj, methods, types) {
if (typeof obj.hooks === 'function') {
return obj;
}
const __hooks = {};
types.forEach(type => {
// Initialize properties where hook functions are stored
__hooks[type] = {};
});
// Add non-enumerable `__hooks` property to the object
Object.defineProperty(obj, '__hooks', {
value: __hooks
});
return Object.assign(obj, {
hooks (allHooks) {
each(allHooks, (obj, type) => {
if (!this.__hooks[type]) {
throw new Error(`'${type}' is not a valid hook type`);
const hookData = {};
types.forEach(type => {
// Initialize properties where hook functions are stored
hookData[type] = {};
});
// Add non-enumerable `__hooks` property to the object
Object.defineProperty(obj, '__hooks', {
value: hookData
});
return Object.assign(obj, {
hooks(allHooks) {
each(allHooks, (current, type) => {
// @ts-ignore
if (!this.__hooks[type]) {
throw new Error(`'${type}' is not a valid hook type`);
}
const hooks = convertHookData(current);
each(hooks, (_value, method) => {
if (method !== 'all' && methods.indexOf(method) === -1) {
throw new Error(`'${method}' is not a valid hook method`);
}
});
methods.forEach(method => {
// @ts-ignore
const myHooks = this.__hooks[type][method] || (this.__hooks[type][method] = []);
if (hooks.all) {
myHooks.push.apply(myHooks, hooks.all);
}
if (hooks[method]) {
myHooks.push.apply(myHooks, hooks[method]);
}
});
});
return this;
}
const hooks = exports.convertHookData(obj);
each(hooks, (value, method) => {
if (method !== 'all' && methods.indexOf(method) === -1) {
throw new Error(`'${method}' is not a valid hook method`);
}
});
methods.forEach(method => {
const myHooks = this.__hooks[type][method] ||
(this.__hooks[type][method] = []);
if (hooks.all) {
myHooks.push.apply(myHooks, hooks.all);
}
if (hooks[method]) {
myHooks.push.apply(myHooks, hooks[method]);
}
});
});
return this;
}
});
};
});
}
exports.enableHooks = enableHooks;
//# sourceMappingURL=hooks.js.map

@@ -1,4 +0,16 @@

const utils = require('./utils');
const hooks = require('./hooks');
module.exports = Object.assign({}, utils, { hooks });
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const hookUtils = __importStar(require("./hooks"));
__export(require("./utils"));
exports.hooks = hookUtils;
//# sourceMappingURL=index.js.map

@@ -0,112 +1,101 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Removes all leading and trailing slashes from a path
exports.stripSlashes = function stripSlashes (name) {
return name.replace(/^(\/+)|(\/+)$/g, '');
};
function stripSlashes(name) {
return name.replace(/^(\/+)|(\/+)$/g, '');
}
exports.stripSlashes = stripSlashes;
// A set of lodash-y utility functions that use ES6
const _ = exports._ = {
each (obj, callback) {
if (obj && typeof obj.forEach === 'function') {
obj.forEach(callback);
} else if (_.isObject(obj)) {
Object.keys(obj).forEach(key => callback(obj[key], key));
}
},
some (value, callback) {
return Object.keys(value)
.map(key => [ value[key], key ])
.some(([val, key]) => callback(val, key));
},
every (value, callback) {
return Object.keys(value)
.map(key => [ value[key], key ])
.every(([val, key]) => callback(val, key));
},
keys (obj) {
return Object.keys(obj);
},
values (obj) {
return _.keys(obj).map(key => obj[key]);
},
isMatch (obj, item) {
return _.keys(item).every(key => obj[key] === item[key]);
},
isEmpty (obj) {
return _.keys(obj).length === 0;
},
isObject (item) {
return (typeof item === 'object' && !Array.isArray(item) && item !== null);
},
isObjectOrArray (value) {
return typeof value === 'object' && value !== null;
},
extend (...args) {
return Object.assign(...args);
},
omit (obj, ...keys) {
const result = _.extend({}, obj);
keys.forEach(key => delete result[key]);
return result;
},
pick (source, ...keys) {
return keys.reduce((result, key) => {
if (source[key] !== undefined) {
result[key] = source[key];
}
return result;
}, {});
},
// Recursively merge the source object into the target object
merge (target, source) {
if (_.isObject(target) && _.isObject(source)) {
Object.keys(source).forEach(key => {
if (_.isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
_.merge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
exports._ = {
each(obj, callback) {
if (obj && typeof obj.forEach === 'function') {
obj.forEach(callback);
}
});
else if (exports._.isObject(obj)) {
Object.keys(obj).forEach(key => callback(obj[key], key));
}
},
some(value, callback) {
return Object.keys(value)
.map(key => [value[key], key])
.some(([val, key]) => callback(val, key));
},
every(value, callback) {
return Object.keys(value)
.map(key => [value[key], key])
.every(([val, key]) => callback(val, key));
},
keys(obj) {
return Object.keys(obj);
},
values(obj) {
return exports._.keys(obj).map(key => obj[key]);
},
isMatch(obj, item) {
return exports._.keys(item).every(key => obj[key] === item[key]);
},
isEmpty(obj) {
return exports._.keys(obj).length === 0;
},
isObject(item) {
return (typeof item === 'object' && !Array.isArray(item) && item !== null);
},
isObjectOrArray(value) {
return typeof value === 'object' && value !== null;
},
extend(first, ...rest) {
return Object.assign(first, ...rest);
},
omit(obj, ...keys) {
const result = exports._.extend({}, obj);
keys.forEach(key => delete result[key]);
return result;
},
pick(source, ...keys) {
return keys.reduce((result, key) => {
if (source[key] !== undefined) {
result[key] = source[key];
}
return result;
}, {});
},
// Recursively merge the source object into the target object
merge(target, source) {
if (exports._.isObject(target) && exports._.isObject(source)) {
Object.keys(source).forEach(key => {
if (exports._.isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
exports._.merge(target[key], source[key]);
}
else {
Object.assign(target, { [key]: source[key] });
}
});
}
return target;
}
return target;
}
};
// Duck-checks if an object looks like a promise
exports.isPromise = function isPromise (result) {
return _.isObject(result) &&
typeof result.then === 'function';
};
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)}`;
};
exports.createSymbol = name => {
return typeof Symbol !== 'undefined' ? Symbol(name) : name;
};
function isPromise(result) {
return exports._.isObject(result) &&
typeof result.then === 'function';
}
exports.isPromise = isPromise;
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)}`;
}
exports.makeUrl = makeUrl;
function createSymbol(name) {
return typeof Symbol !== 'undefined' ? Symbol(name) : name;
}
exports.createSymbol = createSymbol;
//# sourceMappingURL=utils.js.map
{
"name": "@feathersjs/commons",
"version": "4.0.0",
"version": "4.3.0-pre.1",
"description": "Shared Feathers utility functions",

@@ -26,5 +26,8 @@ "homepage": "https://feathersjs.com",

},
"main": "lib/index.js",
"main": "lib/",
"types": "lib/",
"scripts": {
"test": "mocha --opts ../../mocha.opts"
"prepublish": "npm run compile",
"compile": "shx rm -rf lib/ && tsc",
"test": "mocha --opts ../../mocha.ts.opts --recursive test/**.test.ts test/**/*.test.ts"
},

@@ -38,6 +41,12 @@ "directories": {

"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0"
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.7",
"@types/node": "^12.6.2",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"shx": "^0.3.2",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
},
"gitHead": "3bbba49daf1e62f358b426002ec9684f4bcaac89"
"gitHead": "e0b7e978ae31d55a168d8257195134e461c4c6fb"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc