@feathersjs/feathers
Advanced tools
Comparing version 5.0.0-pre.27 to 5.0.0-pre.28
@@ -6,2 +6,14 @@ # Change Log | ||
# [5.0.0-pre.28](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.27...v5.0.0-pre.28) (2022-08-03) | ||
### Bug Fixes | ||
* **cli:** Improve generated application and client ([#2701](https://github.com/feathersjs/feathers/issues/2701)) ([bd55ffb](https://github.com/feathersjs/feathers/commit/bd55ffb812e89bf215f4515e7f137656ea888c3f)) | ||
* **core:** Get hooks to work reliably with custom methods ([#2714](https://github.com/feathersjs/feathers/issues/2714)) ([8d7e04a](https://github.com/feathersjs/feathers/commit/8d7e04acd0f0e2af9f4c13efee652d296dd3bc51)) | ||
# [5.0.0-pre.27](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.26...v5.0.0-pre.27) (2022-07-13) | ||
@@ -8,0 +20,0 @@ |
import { HookContextData, HookManager, Middleware } from '@feathersjs/hooks'; | ||
import { Service, ServiceOptions, HookContext, FeathersService, HookMap, AroundHookFunction, HookFunction } from './declarations'; | ||
export declare function collectHooks(target: any, method: string): any; | ||
declare type HookStore = { | ||
around: { | ||
[method: string]: AroundHookFunction[]; | ||
}; | ||
before: { | ||
[method: string]: HookFunction[]; | ||
}; | ||
after: { | ||
[method: string]: HookFunction[]; | ||
}; | ||
error: { | ||
[method: string]: HookFunction[]; | ||
}; | ||
collected: { | ||
[method: string]: AroundHookFunction[]; | ||
}; | ||
}; | ||
declare type HookEnabled = { | ||
__hooks: HookStore; | ||
}; | ||
export declare function convertHookData(input: any): { | ||
[method: string]: HookFunction<import("./declarations").Application<any, any>, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>[] | AroundHookFunction<import("./declarations").Application<any, any>, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>[]; | ||
[method: string]: AroundHookFunction<import("./declarations").Application<any, any>, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>[] | HookFunction<import("./declarations").Application<any, any>, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>[]; | ||
}; | ||
export declare function enableHooks(object: any, methods?: string[]): (this: any, input: HookMap<any, any>) => any; | ||
export declare function collectHooks(target: HookEnabled, method: string): AroundHookFunction<import("./declarations").Application<any, any>, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>[]; | ||
export declare function enableHooks(object: any): (this: HookEnabled, input: HookMap<any, any>) => HookEnabled; | ||
export declare function createContext(service: Service, method: string, data?: HookContextData): HookContext<import("./declarations").Application<any, any>, any>; | ||
@@ -18,1 +38,2 @@ export declare class FeathersHookManager<A> extends HookManager { | ||
export declare function hookMixin<A>(this: A, service: FeathersService<A>, path: string, options: ServiceOptions): FeathersService<A, Service<any, Partial<any>, import("./declarations").Params<import("./declarations").Query>>>; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hookMixin = exports.FeathersHookManager = exports.createContext = exports.enableHooks = exports.convertHookData = exports.collectHooks = void 0; | ||
exports.hookMixin = exports.FeathersHookManager = exports.createContext = exports.enableHooks = exports.collectHooks = exports.convertHookData = void 0; | ||
const hooks_1 = require("@feathersjs/hooks"); | ||
const service_1 = require("./service"); | ||
function collectHooks(target, method) { | ||
return target.__hooks.hooks[method] || []; | ||
} | ||
exports.collectHooks = collectHooks; | ||
const types = ['before', 'after', 'error', 'around']; | ||
const isType = (value) => types.includes(value); | ||
// Converts different hook registration formats into the | ||
@@ -29,41 +27,14 @@ // same internal format | ||
exports.convertHookData = convertHookData; | ||
const types = ['before', 'after', 'error', 'around']; | ||
const isType = (value) => types.includes(value); | ||
const createMap = (input, methods) => { | ||
const map = {}; | ||
Object.keys(input).forEach((type) => { | ||
if (!isType(type)) { | ||
throw new Error(`'${type}' is not a valid hook type`); | ||
} | ||
const data = convertHookData(input[type]); | ||
Object.keys(data).forEach((method) => { | ||
if (method !== 'all' && !methods.includes(method) && !service_1.defaultServiceMethods.includes(method)) { | ||
throw new Error(`'${method}' is not a valid hook method`); | ||
} | ||
}); | ||
map[type] = data; | ||
}); | ||
return map; | ||
}; | ||
const updateStore = (store, map) => Object.keys(store.hooks).forEach((method) => { | ||
Object.keys(map).forEach((key) => { | ||
var _a; | ||
const type = key; | ||
const allHooks = map[type].all || []; | ||
const methodHooks = map[type][method] || []; | ||
if (allHooks.length || methodHooks.length) { | ||
const list = [...allHooks, ...methodHooks]; | ||
const hooks = ((_a = store[type])[method] || (_a[method] = [])); | ||
hooks.push(...list); | ||
} | ||
}); | ||
const collected = (0, hooks_1.collect)({ | ||
before: store.before[method] || [], | ||
after: store.after[method] || [], | ||
error: store.error[method] || [] | ||
}); | ||
store.hooks[method] = [...(store.around[method] || []), collected]; | ||
}); | ||
function collectHooks(target, method) { | ||
const { collected, around } = target.__hooks; | ||
return [ | ||
...(around.all || []), | ||
...(around[method] || []), | ||
...(collected.all || []), | ||
...(collected[method] || []) | ||
]; | ||
} | ||
exports.collectHooks = collectHooks; | ||
// Add `.hooks` functionality to an object | ||
function enableHooks(object, methods = service_1.defaultServiceMethods) { | ||
function enableHooks(object) { | ||
const store = { | ||
@@ -74,7 +45,4 @@ around: {}, | ||
error: {}, | ||
hooks: {} | ||
collected: {} | ||
}; | ||
for (const method of methods) { | ||
store.hooks[method] = []; | ||
} | ||
Object.defineProperty(object, '__hooks', { | ||
@@ -87,4 +55,24 @@ configurable: true, | ||
const store = this.__hooks; | ||
const map = createMap(input, methods); | ||
updateStore(store, map); | ||
const map = Object.keys(input).reduce((map, type) => { | ||
if (!isType(type)) { | ||
throw new Error(`'${type}' is not a valid hook type`); | ||
} | ||
map[type] = convertHookData(input[type]); | ||
return map; | ||
}, {}); | ||
const types = Object.keys(map); | ||
types.forEach((type) => Object.keys(map[type]).forEach((method) => { | ||
var _a; | ||
const mapHooks = map[type][method]; | ||
const storeHooks = ((_a = store[type])[method] || (_a[method] = [])); | ||
storeHooks.push(...mapHooks); | ||
if (store.before[method] || store.after[method] || store.error[method]) { | ||
const collected = (0, hooks_1.collect)({ | ||
before: store.before[method] || [], | ||
after: store.after[method] || [], | ||
error: store.error[method] || [] | ||
}); | ||
store.collected[method] = [collected]; | ||
} | ||
})); | ||
return this; | ||
@@ -151,3 +139,3 @@ }; | ||
}, {}); | ||
const registerHooks = enableHooks(service, hookMethods); | ||
const registerHooks = enableHooks(service); | ||
(0, hooks_1.hooks)(service, serviceMethodHooks); | ||
@@ -154,0 +142,0 @@ service.hooks = function (hookOptions) { |
@@ -1,2 +0,2 @@ | ||
declare const _default: "5.0.0-pre.27"; | ||
declare const _default: "5.0.0-pre.28"; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = '5.0.0-pre.27'; | ||
exports.default = '5.0.0-pre.28'; | ||
//# sourceMappingURL=version.js.map |
{ | ||
"name": "@feathersjs/feathers", | ||
"description": "A framework for real-time applications and REST API with JavaScript and TypeScript", | ||
"version": "5.0.0-pre.27", | ||
"version": "5.0.0-pre.28", | ||
"homepage": "http://feathersjs.com", | ||
@@ -50,3 +50,4 @@ "repository": { | ||
"publish": "npm run reset-version", | ||
"compile": "shx rm -rf lib/ && tsc", | ||
"pack": "npm pack --pack-destination ../cli/test", | ||
"compile": "shx rm -rf lib/ && tsc && npm run pack", | ||
"test": "mocha --config ../../.mocharc.json --recursive test/" | ||
@@ -61,3 +62,3 @@ }, | ||
"dependencies": { | ||
"@feathersjs/commons": "^5.0.0-pre.27", | ||
"@feathersjs/commons": "^5.0.0-pre.28", | ||
"@feathersjs/hooks": "^0.7.5", | ||
@@ -68,9 +69,9 @@ "events": "^3.3.0" | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^18.0.1", | ||
"@types/node": "^18.6.3", | ||
"mocha": "^10.0.0", | ||
"shx": "^0.3.4", | ||
"ts-node": "^10.8.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.7.4" | ||
}, | ||
"gitHead": "6032742bce059781e7013fd662870df45bd72bb5" | ||
"gitHead": "bf8e54fddc14d688ba8f505e72c9630a71656ff1" | ||
} |
131
src/hooks.ts
@@ -19,8 +19,22 @@ import { | ||
} from './declarations' | ||
import { defaultServiceArguments, defaultServiceMethods, getHookMethods } from './service' | ||
import { defaultServiceArguments, getHookMethods } from './service' | ||
export function collectHooks(target: any, method: string) { | ||
return target.__hooks.hooks[method] || [] | ||
type HookType = 'before' | 'after' | 'error' | 'around' | ||
type ConvertedMap = { [type in HookType]: ReturnType<typeof convertHookData> } | ||
type HookStore = { | ||
around: { [method: string]: AroundHookFunction[] } | ||
before: { [method: string]: HookFunction[] } | ||
after: { [method: string]: HookFunction[] } | ||
error: { [method: string]: HookFunction[] } | ||
collected: { [method: string]: AroundHookFunction[] } | ||
} | ||
type HookEnabled = { __hooks: HookStore } | ||
const types: HookType[] = ['before', 'after', 'error', 'around'] | ||
const isType = (value: any): value is HookType => types.includes(value) | ||
// Converts different hook registration formats into the | ||
@@ -45,66 +59,15 @@ // same internal format | ||
type HookTypes = 'before' | 'after' | 'error' | 'around' | ||
export function collectHooks(target: HookEnabled, method: string) { | ||
const { collected, around } = target.__hooks | ||
type ConvertedMap = { [type in HookTypes]: ReturnType<typeof convertHookData> } | ||
type HookStore = { | ||
around: { [method: string]: AroundHookFunction[] } | ||
before: { [method: string]: HookFunction[] } | ||
after: { [method: string]: HookFunction[] } | ||
error: { [method: string]: HookFunction[] } | ||
hooks: { [method: string]: AroundHookFunction[] } | ||
return [ | ||
...(around.all || []), | ||
...(around[method] || []), | ||
...(collected.all || []), | ||
...(collected[method] || []) | ||
] as AroundHookFunction[] | ||
} | ||
const types: HookTypes[] = ['before', 'after', 'error', 'around'] | ||
const isType = (value: any): value is HookTypes => types.includes(value) | ||
const createMap = (input: HookMap<any, any>, methods: string[]) => { | ||
const map = {} as ConvertedMap | ||
Object.keys(input).forEach((type) => { | ||
if (!isType(type)) { | ||
throw new Error(`'${type}' is not a valid hook type`) | ||
} | ||
const data = convertHookData(input[type]) | ||
Object.keys(data).forEach((method) => { | ||
if (method !== 'all' && !methods.includes(method) && !defaultServiceMethods.includes(method)) { | ||
throw new Error(`'${method}' is not a valid hook method`) | ||
} | ||
}) | ||
map[type] = data | ||
}) | ||
return map | ||
} | ||
const updateStore = (store: HookStore, map: ConvertedMap) => | ||
Object.keys(store.hooks).forEach((method) => { | ||
Object.keys(map).forEach((key) => { | ||
const type = key as HookTypes | ||
const allHooks = map[type].all || [] | ||
const methodHooks = map[type][method] || [] | ||
if (allHooks.length || methodHooks.length) { | ||
const list = [...allHooks, ...methodHooks] as any | ||
const hooks = (store[type][method] ||= []) | ||
hooks.push(...list) | ||
} | ||
}) | ||
const collected = collect({ | ||
before: store.before[method] || [], | ||
after: store.after[method] || [], | ||
error: store.error[method] || [] | ||
}) | ||
store.hooks[method] = [...(store.around[method] || []), collected] | ||
}) | ||
// Add `.hooks` functionality to an object | ||
export function enableHooks(object: any, methods: string[] = defaultServiceMethods) { | ||
export function enableHooks(object: any) { | ||
const store: HookStore = { | ||
@@ -115,9 +78,5 @@ around: {}, | ||
error: {}, | ||
hooks: {} | ||
collected: {} | ||
} | ||
for (const method of methods) { | ||
store.hooks[method] = [] | ||
} | ||
Object.defineProperty(object, '__hooks', { | ||
@@ -129,8 +88,34 @@ configurable: true, | ||
return function registerHooks(this: any, input: HookMap<any, any>) { | ||
return function registerHooks(this: HookEnabled, input: HookMap<any, any>) { | ||
const store = this.__hooks | ||
const map = createMap(input, methods) | ||
const map = Object.keys(input).reduce((map, type) => { | ||
if (!isType(type)) { | ||
throw new Error(`'${type}' is not a valid hook type`) | ||
} | ||
updateStore(store, map) | ||
map[type] = convertHookData(input[type]) | ||
return map | ||
}, {} as ConvertedMap) | ||
const types = Object.keys(map) as HookType[] | ||
types.forEach((type) => | ||
Object.keys(map[type]).forEach((method) => { | ||
const mapHooks = map[type][method] | ||
const storeHooks: any[] = (store[type][method] ||= []) | ||
storeHooks.push(...mapHooks) | ||
if (store.before[method] || store.after[method] || store.error[method]) { | ||
const collected = collect({ | ||
before: store.before[method] || [], | ||
after: store.after[method] || [], | ||
error: store.error[method] || [] | ||
}) | ||
store.collected[method] = [collected] | ||
} | ||
}) | ||
) | ||
return this | ||
@@ -157,3 +142,3 @@ } | ||
collectMiddleware(self: any, args: any[]): Middleware[] { | ||
const appHooks = collectHooks(this.app, this.method) | ||
const appHooks = collectHooks(this.app as any as HookEnabled, this.method) | ||
const middleware = super.collectMiddleware(self, args) | ||
@@ -208,3 +193,3 @@ const methodHooks = collectHooks(self, this.method) | ||
const registerHooks = enableHooks(service, hookMethods) | ||
const registerHooks = enableHooks(service) | ||
@@ -211,0 +196,0 @@ hooks(service, serviceMethodHooks) |
@@ -1,1 +0,1 @@ | ||
export default '5.0.0-pre.27' | ||
export default '5.0.0-pre.28' |
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
138683
1590