@feathersjs/hooks
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -6,2 +6,15 @@ # Change Log | ||
## [0.6.1](https://github.com/feathersjs/hooks/compare/v0.6.0...v0.6.1) (2020-12-11) | ||
### Bug Fixes | ||
* **hooks:** fix some errors for feathers integration ([#67](https://github.com/feathersjs/hooks/issues/67)) ([fcfc0ca](https://github.com/feathersjs/hooks/commit/fcfc0ca6423a8062959d41f34e673f81d3c006dd)) | ||
* **hooks:** Remove redundant method call ([#65](https://github.com/feathersjs/hooks/issues/65)) ([4ff10a9](https://github.com/feathersjs/hooks/commit/4ff10a9935682276b8ca3ffb699275b627230dfa)) | ||
* **hooks:** Stricter condition ([#64](https://github.com/feathersjs/hooks/issues/64)) ([6de77a1](https://github.com/feathersjs/hooks/commit/6de77a1afcbee4867b7e464be0b556a8dc9656e3)) | ||
# [0.6.0](https://github.com/feathersjs/hooks/compare/v0.5.0...v0.6.0) (2020-11-12) | ||
@@ -8,0 +21,0 @@ |
import { Middleware } from './compose.ts'; | ||
import { copyToSelf } from './utils'; | ||
import { copyToSelf, copyProperties } from './utils'; | ||
@@ -49,11 +49,7 @@ export const HOOKS: string = Symbol('@feathersjs/hooks') as any; | ||
if (previous) { | ||
if (this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return previous; | ||
if (previous && this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return this._middleware; | ||
return previous || this._middleware; | ||
} | ||
@@ -65,11 +61,7 @@ | ||
if (otherMiddleware) { | ||
if (middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return otherMiddleware; | ||
if (otherMiddleware && middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return this.getMiddleware(); | ||
return otherMiddleware || middleware; | ||
} | ||
@@ -82,3 +74,3 @@ | ||
Object.assign(this._props, props); | ||
copyProperties(this._props, props); | ||
@@ -91,7 +83,7 @@ return this; | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && this._props) { | ||
return copyProperties({}, previous, this._props); | ||
} | ||
return this._props; | ||
return previous || this._props; | ||
} | ||
@@ -108,2 +100,6 @@ | ||
if (previous && this._params) { | ||
return previous.concat(this._params); | ||
} | ||
return previous || this._params; | ||
@@ -122,7 +118,7 @@ } | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && defaults) { | ||
return Object.assign({}, previous, defaults); | ||
} | ||
return defaults; | ||
return previous || defaults; | ||
} | ||
@@ -143,3 +139,3 @@ | ||
params.forEach((name, index) => { | ||
if (props?.[name]) { | ||
if (props?.[name] !== undefined) { | ||
throw new Error(`Hooks can not have a property and param named '${name}'. Use .defaults instead.`); | ||
@@ -161,3 +157,3 @@ } | ||
if (props) { | ||
Object.assign(ContextClass.prototype, props); | ||
copyProperties(ContextClass.prototype, props); | ||
} | ||
@@ -164,0 +160,0 @@ |
@@ -5,2 +5,3 @@ import { compose, Middleware } from './compose.ts'; | ||
} from './base.ts'; | ||
import { copyProperties } from './utils'; | ||
@@ -11,17 +12,2 @@ export function getOriginal (fn: any): any { | ||
function copyProperties <F> (target: F, original: any) { | ||
const originalProps = (Object.keys(original) as any) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
return target; | ||
} | ||
export function functionHooks <F> (fn: F, managerOrMiddleware: HookOptions) { | ||
@@ -28,0 +14,0 @@ if (typeof fn !== 'function') { |
@@ -34,3 +34,3 @@ import { Middleware } from './compose.ts'; | ||
if (options.params) { | ||
manager.params(options.params); | ||
manager.params(...options.params); | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -15,5 +15,10 @@ const proto = Object.prototype as any; | ||
if (getter && hasProtoDefinitions) { | ||
if (hasProtoDefinitions && getter) { | ||
target.__defineGetter__(key, getter); | ||
target.__defineSetter__(key, target.constructor.prototype.__lookupSetter__(key)); | ||
const setter = target.constructor.prototype.__lookupSetter__(key); | ||
if (setter) { | ||
target.__defineSetter__(key, setter); | ||
} | ||
} else if (getter) { | ||
@@ -27,1 +32,18 @@ Object.defineProperty(target, key, getter); | ||
} | ||
export function copyProperties <F> (target: F, ...originals: any[]) { | ||
for (const original of originals) { | ||
const originalProps = (Object.keys(original) as any) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
} | ||
return target; | ||
} |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.hooks=e():t.hooks=e()}(this,(function(){return(()=>{"use strict";var t={150:(t,e,o)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.setMiddleware=e.getMiddleware=e.setManager=e.getManager=e.convertOptions=e.HookManager=e.HookContext=e.HOOKS=void 0;const r=o(930);e.HOOKS=Symbol("@feathersjs/hooks");class n{constructor(t={}){Object.assign(this,t)}}e.HookContext=n;class s{constructor(){this._parent=null,this._params=null,this._middleware=null,this._props=null}parent(t){return this._parent=t,this}middleware(t){return this._middleware=(null==t?void 0:t.length)?t:null,this}getMiddleware(){var t;const e=null===(t=this._parent)||void 0===t?void 0:t.getMiddleware();return e?this._middleware?e.concat(this._middleware):e:this._middleware}collectMiddleware(t,e){const o=c(t),r=this.getMiddleware();return o?r?o.concat(r):o:this.getMiddleware()}props(t){return this._props||(this._props={}),Object.assign(this._props,t),this}getProps(){var t;const e=null===(t=this._parent)||void 0===t?void 0:t.getProps();return e?Object.assign({},e,this._props):this._props}params(...t){return this._params=t,this}getParams(){var t;return(null===(t=this._parent)||void 0===t?void 0:t.getParams())||this._params}defaults(t){return this._defaults=t,this}getDefaults(t,e,o){var r;const n="function"==typeof this._defaults?this._defaults(t,e,o):null,s=null===(r=this._parent)||void 0===r?void 0:r.getDefaults(t,e,o);return s?Object.assign({},s,this._props):n}getContextClass(t=n){const e=class extends t{constructor(t){super(t),r.copyToSelf(this)}},o=this.getParams(),s=this.getProps();return o&&o.forEach(((t,o)=>{if(null==s?void 0:s[t])throw new Error(`Hooks can not have a property and param named '${t}'. Use .defaults instead.`);Object.defineProperty(e.prototype,t,{enumerable:!0,get(){return null==this?void 0:this.arguments[o]},set(t){this.arguments[o]=t}})})),s&&Object.assign(e.prototype,s),e}initializeContext(t,e,o){const r=this._parent?this._parent.initializeContext(t,e,o):o,n=this.getDefaults(t,e,r);if(t&&(r.self=t),r.arguments=e,n)for(const t of Object.keys(n))void 0===r[t]&&(r[t]=n[t]);return r}}function i(t){return t&&t[e.HOOKS]||null}function a(t,o){const r=i(t);return t[e.HOOKS]=o.parent(r),t}function c(t){const e=i(t);return e?e.getMiddleware():null}e.HookManager=s,e.convertOptions=function(t=null){return t?Array.isArray(t)?(new s).middleware(t):t:new s},e.getManager=i,e.setManager=a,e.getMiddleware=c,e.setMiddleware=function(t,e){return a(t,(new s).middleware(e))}},85:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.compose=void 0,e.compose=function(t){if(!Array.isArray(t))throw new TypeError("Middleware stack must be an array!");for(const e of t)if("function"!=typeof e)throw new TypeError("Middleware must be composed of functions!");return function(e,o){let r=-1;return function n(s){if(s<=r)return Promise.reject(new Error("next() called multiple times"));r=s;let i=t[s];if(s===t.length&&(i=o),!i)return Promise.resolve();try{return Promise.resolve(i.call(this,e,n.bind(this,s+1)))}catch(t){return Promise.reject(t)}}.call(this,0)}}},460:(t,e,o)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.hookDecorator=e.objectHooks=e.functionHooks=e.getOriginal=void 0;const r=o(85),n=o(150);function s(t){return"function"==typeof t.original?s(t.original):t}function i(t,e){if("function"!=typeof t)throw new Error("Can not apply hooks to non-function");const o=n.convertOptions(e),i=function(...t){const{Context:e,original:n}=i,s=t[t.length-1]instanceof e,a=s?t.pop():new e,c=o.initializeContext(this,t,a),u=[(t,e)=>e().then((()=>s?t:t.result))],l=o.collectMiddleware(this,t);return l&&Array.prototype.push.apply(u,l),u.push(((t,e)=>void 0===t.result?Promise.resolve(n.apply(this,t.arguments)).then((o=>(t.result=o,e()))):e())),r.compose(u).call(this,c)};return function(t,e){const o=Object.keys(e).concat(Object.getOwnPropertySymbols(e));for(const r of o){const o=Object.getOwnPropertyDescriptor(e,r);t.hasOwnProperty(r)||Object.defineProperty(t,r,o)}}(i,t),n.setManager(i,o),Object.assign(i,{original:s(t),Context:o.getContextClass(),createContext:(t={})=>new i.Context(t)})}e.getOriginal=s,e.functionHooks=i,e.objectHooks=function(t,e){const o="function"==typeof t?t.prototype:t;return Array.isArray(e)?n.setMiddleware(o,e):Object.keys(e).reduce(((t,r)=>{const s=o[r];if("function"!=typeof s)throw new Error(`Can not apply hooks. '${r}' is not a function`);const a=n.convertOptions(e[r]);return t[r]=i(s,a.props({method:r})),t}),o)},e.hookDecorator=t=>(e,o,r)=>{const s=n.convertOptions(t);if(!r)return n.setManager(e.prototype,s),e;const a=r.value;if("function"!=typeof a)throw new Error(`Can not apply hooks. '${o}' is not a function`);return r.value=i(a,s.props({method:o})),r}},920:function(t,e,o){var r=this&&this.__createBinding||(Object.create?function(t,e,o,r){void 0===r&&(r=o),Object.defineProperty(t,r,{enumerable:!0,get:function(){return e[o]}})}:function(t,e,o,r){void 0===r&&(r=o),t[r]=e[o]}),n=this&&this.__exportStar||function(t,e){for(var o in t)"default"===o||Object.prototype.hasOwnProperty.call(e,o)||r(e,t,o)};Object.defineProperty(e,"__esModule",{value:!0}),e.hooks=e.middleware=void 0;const s=o(150),i=o(460);n(o(460),e),n(o(85),e),n(o(150),e),e.middleware=function(t,e){const o=(new s.HookManager).middleware(t);return e&&(e.params&&o.params(e.params),e.defaults&&o.defaults(e.defaults),e.props&&o.props(e.props)),o},e.hooks=function(...t){const[e,o]=t;return"function"==typeof e&&(o instanceof s.HookManager||Array.isArray(o))?i.functionHooks(e,o):2===t.length?i.objectHooks(e,o):i.hookDecorator(e)}},930:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.copyToSelf=void 0;const o=Object.prototype,r="function"==typeof o.__lookupGetter__&&"function"==typeof o.__defineGetter__&&"function"==typeof o.__defineSetter__;e.copyToSelf=function(t){for(const e in t)if(!t.hasOwnProperty(e)){const o=r?t.constructor.prototype.__lookupGetter__(e):Object.getOwnPropertyDescriptor(t,e);o&&r?(t.__defineGetter__(e,o),t.__defineSetter__(e,t.constructor.prototype.__lookupSetter__(e))):o?Object.defineProperty(t,e,o):t[e]=t[e]}}}},e={};return function o(r){if(e[r])return e[r].exports;var n=e[r]={exports:{}};return t[r].call(n.exports,n,n.exports,o),n.exports}(920)})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.hooks=e():t.hooks=e()}(this,(function(){return(()=>{"use strict";var t={150:(t,e,o)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.setMiddleware=e.getMiddleware=e.setManager=e.getManager=e.convertOptions=e.HookManager=e.HookContext=e.HOOKS=void 0;const r=o(930);e.HOOKS=Symbol("@feathersjs/hooks");class n{constructor(t={}){Object.assign(this,t)}}e.HookContext=n;class s{constructor(){this._parent=null,this._params=null,this._middleware=null,this._props=null}parent(t){return this._parent=t,this}middleware(t){return this._middleware=(null==t?void 0:t.length)?t:null,this}getMiddleware(){var t;const e=null===(t=this._parent)||void 0===t?void 0:t.getMiddleware();return e&&this._middleware?e.concat(this._middleware):e||this._middleware}collectMiddleware(t,e){const o=c(t),r=this.getMiddleware();return o&&r?o.concat(r):o||r}props(t){return this._props||(this._props={}),r.copyProperties(this._props,t),this}getProps(){var t;const e=null===(t=this._parent)||void 0===t?void 0:t.getProps();return e&&this._props?r.copyProperties({},e,this._props):e||this._props}params(...t){return this._params=t,this}getParams(){var t;const e=null===(t=this._parent)||void 0===t?void 0:t.getParams();return e&&this._params?e.concat(this._params):e||this._params}defaults(t){return this._defaults=t,this}getDefaults(t,e,o){var r;const n="function"==typeof this._defaults?this._defaults(t,e,o):null,s=null===(r=this._parent)||void 0===r?void 0:r.getDefaults(t,e,o);return s&&n?Object.assign({},s,n):s||n}getContextClass(t=n){const e=class extends t{constructor(t){super(t),r.copyToSelf(this)}},o=this.getParams(),s=this.getProps();return o&&o.forEach(((t,o)=>{if(void 0!==(null==s?void 0:s[t]))throw new Error(`Hooks can not have a property and param named '${t}'. Use .defaults instead.`);Object.defineProperty(e.prototype,t,{enumerable:!0,get(){return null==this?void 0:this.arguments[o]},set(t){this.arguments[o]=t}})})),s&&r.copyProperties(e.prototype,s),e}initializeContext(t,e,o){const r=this._parent?this._parent.initializeContext(t,e,o):o,n=this.getDefaults(t,e,r);if(t&&(r.self=t),r.arguments=e,n)for(const t of Object.keys(n))void 0===r[t]&&(r[t]=n[t]);return r}}function i(t){return t&&t[e.HOOKS]||null}function a(t,o){const r=i(t);return t[e.HOOKS]=o.parent(r),t}function c(t){const e=i(t);return e?e.getMiddleware():null}e.HookManager=s,e.convertOptions=function(t=null){return t?Array.isArray(t)?(new s).middleware(t):t:new s},e.getManager=i,e.setManager=a,e.getMiddleware=c,e.setMiddleware=function(t,e){return a(t,(new s).middleware(e))}},85:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.compose=void 0,e.compose=function(t){if(!Array.isArray(t))throw new TypeError("Middleware stack must be an array!");for(const e of t)if("function"!=typeof e)throw new TypeError("Middleware must be composed of functions!");return function(e,o){let r=-1;return function n(s){if(s<=r)return Promise.reject(new Error("next() called multiple times"));r=s;let i=t[s];if(s===t.length&&(i=o),!i)return Promise.resolve();try{return Promise.resolve(i.call(this,e,n.bind(this,s+1)))}catch(t){return Promise.reject(t)}}.call(this,0)}}},460:(t,e,o)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.hookDecorator=e.objectHooks=e.functionHooks=e.getOriginal=void 0;const r=o(85),n=o(150),s=o(930);function i(t){return"function"==typeof t.original?i(t.original):t}function a(t,e){if("function"!=typeof t)throw new Error("Can not apply hooks to non-function");const o=n.convertOptions(e),a=function(...t){const{Context:e,original:n}=a,s=t[t.length-1]instanceof e,i=s?t.pop():new e,c=o.initializeContext(this,t,i),p=[(t,e)=>e().then((()=>s?t:t.result))],u=o.collectMiddleware(this,t);return u&&Array.prototype.push.apply(p,u),p.push(((t,e)=>void 0===t.result?Promise.resolve(n.apply(this,t.arguments)).then((o=>(t.result=o,e()))):e())),r.compose(p).call(this,c)};return s.copyProperties(a,t),n.setManager(a,o),Object.assign(a,{original:i(t),Context:o.getContextClass(),createContext:(t={})=>new a.Context(t)})}e.getOriginal=i,e.functionHooks=a,e.objectHooks=function(t,e){const o="function"==typeof t?t.prototype:t;return Array.isArray(e)?n.setMiddleware(o,e):Object.keys(e).reduce(((t,r)=>{const s=o[r];if("function"!=typeof s)throw new Error(`Can not apply hooks. '${r}' is not a function`);const i=n.convertOptions(e[r]);return t[r]=a(s,i.props({method:r})),t}),o)},e.hookDecorator=t=>(e,o,r)=>{const s=n.convertOptions(t);if(!r)return n.setManager(e.prototype,s),e;const i=r.value;if("function"!=typeof i)throw new Error(`Can not apply hooks. '${o}' is not a function`);return r.value=a(i,s.props({method:o})),r}},920:function(t,e,o){var r=this&&this.__createBinding||(Object.create?function(t,e,o,r){void 0===r&&(r=o),Object.defineProperty(t,r,{enumerable:!0,get:function(){return e[o]}})}:function(t,e,o,r){void 0===r&&(r=o),t[r]=e[o]}),n=this&&this.__exportStar||function(t,e){for(var o in t)"default"===o||Object.prototype.hasOwnProperty.call(e,o)||r(e,t,o)};Object.defineProperty(e,"__esModule",{value:!0}),e.hooks=e.middleware=void 0;const s=o(150),i=o(460);n(o(460),e),n(o(85),e),n(o(150),e),e.middleware=function(t,e){const o=(new s.HookManager).middleware(t);return e&&(e.params&&o.params(...e.params),e.defaults&&o.defaults(e.defaults),e.props&&o.props(e.props)),o},e.hooks=function(...t){const[e,o]=t;return"function"==typeof e&&(o instanceof s.HookManager||Array.isArray(o))?i.functionHooks(e,o):2===t.length?i.objectHooks(e,o):i.hookDecorator(e)}},930:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.copyProperties=e.copyToSelf=void 0;const o=Object.prototype,r="function"==typeof o.__lookupGetter__&&"function"==typeof o.__defineGetter__&&"function"==typeof o.__defineSetter__;e.copyToSelf=function(t){for(const e in t)if(!t.hasOwnProperty(e)){const o=r?t.constructor.prototype.__lookupGetter__(e):Object.getOwnPropertyDescriptor(t,e);if(r&&o){t.__defineGetter__(e,o);const r=t.constructor.prototype.__lookupSetter__(e);r&&t.__defineSetter__(e,r)}else o?Object.defineProperty(t,e,o):t[e]=t[e]}},e.copyProperties=function(t,...e){for(const o of e){const e=Object.keys(o).concat(Object.getOwnPropertySymbols(o));for(const r of e){const e=Object.getOwnPropertyDescriptor(o,r);t.hasOwnProperty(r)||Object.defineProperty(t,r,e)}}return t}}},e={};return function o(r){if(e[r])return e[r].exports;var n=e[r]={exports:{}};return t[r].call(n.exports,n,n.exports,o),n.exports}(920)})()})); |
@@ -33,9 +33,6 @@ "use strict"; | ||
const previous = (_a = this._parent) === null || _a === void 0 ? void 0 : _a.getMiddleware(); | ||
if (previous) { | ||
if (this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return previous; | ||
if (previous && this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return this._middleware; | ||
return previous || this._middleware; | ||
} | ||
@@ -45,9 +42,6 @@ collectMiddleware(self, _args) { | ||
const middleware = this.getMiddleware(); | ||
if (otherMiddleware) { | ||
if (middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return otherMiddleware; | ||
if (otherMiddleware && middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return this.getMiddleware(); | ||
return otherMiddleware || middleware; | ||
} | ||
@@ -58,3 +52,3 @@ props(props) { | ||
} | ||
Object.assign(this._props, props); | ||
utils_1.copyProperties(this._props, props); | ||
return this; | ||
@@ -65,6 +59,6 @@ } | ||
const previous = (_a = this._parent) === null || _a === void 0 ? void 0 : _a.getProps(); | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && this._props) { | ||
return utils_1.copyProperties({}, previous, this._props); | ||
} | ||
return this._props; | ||
return previous || this._props; | ||
} | ||
@@ -78,2 +72,5 @@ params(...params) { | ||
const previous = (_a = this._parent) === null || _a === void 0 ? void 0 : _a.getParams(); | ||
if (previous && this._params) { | ||
return previous.concat(this._params); | ||
} | ||
return previous || this._params; | ||
@@ -89,6 +86,6 @@ } | ||
const previous = (_a = this._parent) === null || _a === void 0 ? void 0 : _a.getDefaults(self, args, context); | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && defaults) { | ||
return Object.assign({}, previous, defaults); | ||
} | ||
return defaults; | ||
return previous || defaults; | ||
} | ||
@@ -106,3 +103,3 @@ getContextClass(Base = HookContext) { | ||
params.forEach((name, index) => { | ||
if (props === null || props === void 0 ? void 0 : props[name]) { | ||
if ((props === null || props === void 0 ? void 0 : props[name]) !== undefined) { | ||
throw new Error(`Hooks can not have a property and param named '${name}'. Use .defaults instead.`); | ||
@@ -122,3 +119,3 @@ } | ||
if (props) { | ||
Object.assign(ContextClass.prototype, props); | ||
utils_1.copyProperties(ContextClass.prototype, props); | ||
} | ||
@@ -125,0 +122,0 @@ return ContextClass; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const base_1 = require("./base"); | ||
const utils_1 = require("./utils"); | ||
function getOriginal(fn) { | ||
@@ -11,13 +12,2 @@ return typeof fn.original === 'function' ? getOriginal(fn.original) : fn; | ||
exports.getOriginal = getOriginal; | ||
function copyProperties(target, original) { | ||
const originalProps = Object.keys(original) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
return target; | ||
} | ||
function functionHooks(fn, managerOrMiddleware) { | ||
@@ -58,3 +48,3 @@ if (typeof fn !== 'function') { | ||
}; | ||
copyProperties(wrapper, fn); | ||
utils_1.copyProperties(wrapper, fn); | ||
base_1.setManager(wrapper, manager); | ||
@@ -61,0 +51,0 @@ return Object.assign(wrapper, { |
@@ -27,3 +27,3 @@ "use strict"; | ||
if (options.params) { | ||
manager.params(options.params); | ||
manager.params(...options.params); | ||
} | ||
@@ -30,0 +30,0 @@ if (options.defaults) { |
export declare function copyToSelf(target: any): void; | ||
export declare function copyProperties<F>(target: F, ...originals: any[]): F; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.copyToSelf = void 0; | ||
exports.copyProperties = exports.copyToSelf = void 0; | ||
const proto = Object.prototype; | ||
@@ -16,5 +16,8 @@ // These are non-standard but offer a more reliable prototype based | ||
: Object.getOwnPropertyDescriptor(target, key); | ||
if (getter && hasProtoDefinitions) { | ||
if (hasProtoDefinitions && getter) { | ||
target.__defineGetter__(key, getter); | ||
target.__defineSetter__(key, target.constructor.prototype.__lookupSetter__(key)); | ||
const setter = target.constructor.prototype.__lookupSetter__(key); | ||
if (setter) { | ||
target.__defineSetter__(key, setter); | ||
} | ||
} | ||
@@ -31,2 +34,16 @@ else if (getter) { | ||
exports.copyToSelf = copyToSelf; | ||
function copyProperties(target, ...originals) { | ||
for (const original of originals) { | ||
const originalProps = Object.keys(original) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
} | ||
return target; | ||
} | ||
exports.copyProperties = copyProperties; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@feathersjs/hooks", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Async middleware for JavaScript and TypeScript", | ||
@@ -67,3 +67,3 @@ "homepage": "https://feathersjs.com", | ||
}, | ||
"gitHead": "84a9b4bb12114a391d464fbcde7ee95fecf57e53" | ||
"gitHead": "6c99cbda13ada09ff78ba4816ebe404c0b541692" | ||
} |
import { Middleware } from './compose'; | ||
import { copyToSelf } from './utils'; | ||
import { copyToSelf, copyProperties } from './utils'; | ||
@@ -49,11 +49,7 @@ export const HOOKS: string = Symbol('@feathersjs/hooks') as any; | ||
if (previous) { | ||
if (this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return previous; | ||
if (previous && this._middleware) { | ||
return previous.concat(this._middleware); | ||
} | ||
return this._middleware; | ||
return previous || this._middleware; | ||
} | ||
@@ -65,11 +61,7 @@ | ||
if (otherMiddleware) { | ||
if (middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return otherMiddleware; | ||
if (otherMiddleware && middleware) { | ||
return otherMiddleware.concat(middleware); | ||
} | ||
return this.getMiddleware(); | ||
return otherMiddleware || middleware; | ||
} | ||
@@ -82,3 +74,3 @@ | ||
Object.assign(this._props, props); | ||
copyProperties(this._props, props); | ||
@@ -91,7 +83,7 @@ return this; | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && this._props) { | ||
return copyProperties({}, previous, this._props); | ||
} | ||
return this._props; | ||
return previous || this._props; | ||
} | ||
@@ -108,2 +100,6 @@ | ||
if (previous && this._params) { | ||
return previous.concat(this._params); | ||
} | ||
return previous || this._params; | ||
@@ -122,7 +118,7 @@ } | ||
if (previous) { | ||
return Object.assign({}, previous, this._props); | ||
if (previous && defaults) { | ||
return Object.assign({}, previous, defaults); | ||
} | ||
return defaults; | ||
return previous || defaults; | ||
} | ||
@@ -143,3 +139,3 @@ | ||
params.forEach((name, index) => { | ||
if (props?.[name]) { | ||
if (props?.[name] !== undefined) { | ||
throw new Error(`Hooks can not have a property and param named '${name}'. Use .defaults instead.`); | ||
@@ -161,3 +157,3 @@ } | ||
if (props) { | ||
Object.assign(ContextClass.prototype, props); | ||
copyProperties(ContextClass.prototype, props); | ||
} | ||
@@ -164,0 +160,0 @@ |
@@ -5,2 +5,3 @@ import { compose, Middleware } from './compose'; | ||
} from './base'; | ||
import { copyProperties } from './utils'; | ||
@@ -11,17 +12,2 @@ export function getOriginal (fn: any): any { | ||
function copyProperties <F> (target: F, original: any) { | ||
const originalProps = (Object.keys(original) as any) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
return target; | ||
} | ||
export function functionHooks <F> (fn: F, managerOrMiddleware: HookOptions) { | ||
@@ -28,0 +14,0 @@ if (typeof fn !== 'function') { |
@@ -34,3 +34,3 @@ import { Middleware } from './compose'; | ||
if (options.params) { | ||
manager.params(options.params); | ||
manager.params(...options.params); | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -15,5 +15,10 @@ const proto = Object.prototype as any; | ||
if (getter && hasProtoDefinitions) { | ||
if (hasProtoDefinitions && getter) { | ||
target.__defineGetter__(key, getter); | ||
target.__defineSetter__(key, target.constructor.prototype.__lookupSetter__(key)); | ||
const setter = target.constructor.prototype.__lookupSetter__(key); | ||
if (setter) { | ||
target.__defineSetter__(key, setter); | ||
} | ||
} else if (getter) { | ||
@@ -27,1 +32,18 @@ Object.defineProperty(target, key, getter); | ||
} | ||
export function copyProperties <F> (target: F, ...originals: any[]) { | ||
for (const original of originals) { | ||
const originalProps = (Object.keys(original) as any) | ||
.concat(Object.getOwnPropertySymbols(original)); | ||
for (const prop of originalProps) { | ||
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop); | ||
if (!target.hasOwnProperty(prop)) { | ||
Object.defineProperty(target, prop, propDescriptor); | ||
} | ||
} | ||
} | ||
return target; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
73674
1323
0