-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Finalize functionality for initial release of @feathersjs/hooks…
… package (#1)
- Loading branch information
Showing
8 changed files
with
272 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Middleware } from './compose'; | ||
|
||
export const HOOKS: string = Symbol('@feathersjs/hooks') as any; | ||
|
||
/** | ||
* | ||
* @param target The target object or function | ||
* @param middleware | ||
*/ | ||
export function registerMiddleware<T> (target: T, middleware: Middleware[]) { | ||
const current: Middleware[] = (target as any)[HOOKS] || []; | ||
|
||
(target as any)[HOOKS] = current.concat(middleware); | ||
|
||
return target; | ||
} | ||
|
||
export function getMiddleware (target: any): Middleware[] { | ||
return (target && target[HOOKS]) || []; | ||
} | ||
|
||
/** | ||
* The base hook context. | ||
*/ | ||
export class HookContext<T = any> { | ||
result?: T; | ||
arguments: any[]; | ||
[key: string]: any; | ||
|
||
constructor (data: { [key: string]: any } = {}) { | ||
Object.assign(this, data); | ||
} | ||
} | ||
|
||
/** | ||
* A function that updates the hook context with the `this` reference and | ||
* arguments of the function call. | ||
*/ | ||
export type ContextUpdater<T = any> = (self: any, args: any[], context: HookContext<T>) => HookContext<T>; | ||
|
||
/** | ||
* Returns a ContextUpdater function that turns function arguments like | ||
* `function (data, name)` into named properties (`context.data`, `context.name`) | ||
* on the hook context | ||
* | ||
* @param params The list of parameter names | ||
*/ | ||
export function withParams<T = any> (...params: string[]) { | ||
return (self: any, args: any[], context: HookContext<T>) => { | ||
params.forEach((name, index) => { | ||
context[name] = args[index]; | ||
}); | ||
|
||
if (params.length > 0) { | ||
Object.defineProperty(context, 'arguments', { | ||
get (this: HookContext<T>) { | ||
const result = params.map(name => this[name]); | ||
|
||
return Object.freeze(result); | ||
} | ||
}); | ||
} else { | ||
context.arguments = args; | ||
} | ||
|
||
if (self) { | ||
context.self = self; | ||
} | ||
|
||
return context; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
import { functionHooks, ContextCreator } from './function'; | ||
import { objectHooks, MiddlewareMap, ContextCreatorMap } from './object'; | ||
import { functionHooks } from './function'; | ||
import { Middleware } from './compose'; | ||
import { ContextUpdater } from './base'; | ||
import { objectHooks, MiddlewareMap, ContextUpdaterMap } from './object'; | ||
import { hookDecorator } from './decorator'; | ||
|
||
export * from './function'; | ||
export * from './compose'; | ||
export * from './object'; | ||
export * from './base'; | ||
|
||
export function hooks<F, T = any> (method: F, _hooks: Array<Middleware<T>>, defaultContext?: ContextCreator<T>): F; | ||
export function hooks<O> (obj: O, hookMap: MiddlewareMap, contextMap?: ContextCreatorMap): O; | ||
// hooks(fn, hooks, updateContext?) | ||
export function hooks<F, T = any> ( | ||
fn: F, | ||
hooks: Array<Middleware<T>>, | ||
updateContext?: ContextUpdater<T> | ||
): F&((...rest: any[]) => Promise<T>); | ||
// hooks(object, methodHookMap, methodUpdateContextMap?) | ||
export function hooks<T> (obj: T, hookMap: MiddlewareMap, contextMap?: ContextUpdaterMap): T; | ||
// @hooks(hooks) | ||
export function hooks<F, T = any> ( | ||
hooks: Array<Middleware<T>>, | ||
updateContext?: ContextUpdater<T> | ||
): any; | ||
// Fallthrough to actual implementation | ||
export function hooks (...args: any[]) { | ||
const [ target, _hooks, ...rest ] = args; | ||
|
||
if (Array.isArray(_hooks)) { | ||
return functionHooks(target, _hooks, ...rest); | ||
} | ||
|
||
if (Array.isArray(target)) { | ||
return hookDecorator(target, _hooks); | ||
} | ||
|
||
return objectHooks(target, _hooks, ...rest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.