Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add setMiddleware #18

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions packages/hooks/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,52 @@ import { Middleware } from './compose';
export const HOOKS: string = Symbol('@feathersjs/hooks') as any;
export const CONTEXT: string = Symbol('@feathersjs/hooks/context') as any;

function walkOriginal (fn: any, method: any, res: any[] = []): any {
return typeof fn.original === 'function'
? walkOriginal(fn.original, method, [...res, ...method(fn)])
: [...res, ...method(fn)];
}

export function getMiddleware<T> (target: any): Array<Middleware<T>> {
return (target && target[HOOKS]) || [];
}

export type MiddlewareSetter = (currentMiddleware: Middleware[]) => Middleware[];

/**
* @param target The target object or function
* @param middleware
* @param middleware or function
*/
export function registerMiddleware<T> (target: T, middleware: Middleware[]) {
const current: Middleware[] = (target as any)[HOOKS] || [];

(target as any)[HOOKS] = current.concat(middleware);
export function setMiddleware<T> (target: T, middleware: Middleware[] | MiddlewareSetter) {
(target as any)[HOOKS] = typeof middleware === 'function' ? middleware(getMiddleware(target)) : middleware;

return target;
}

export function getMiddleware<T> (target: any): Array<Middleware<T>> {
return (target && target[HOOKS]) || [];
/**
* @param target The target object
* @param middleware or a function that takes current middleware as first argument
*/
export function registerMiddleware<T> (target: T, middleware: Middleware[]) {
return setMiddleware(target, (current: Middleware[]) => current.concat(middleware));
}

export function getContextUpdater<T> (target: any): Array<ContextUpdater<T>> {
return (target && target[CONTEXT]) || [];
}

/**
* @param target The target object or function
* @param updaters
*/
export function registerContextUpdater<T> (target: T, updaters: ContextUpdater[]) {
const current: ContextUpdater[] = (target as any)[CONTEXT] || [];
const current = getContextUpdater(target);

(target as any)[CONTEXT] = current.concat(updaters);

return target;
}

export function getContextUpdater<T> (target: any): Array<ContextUpdater<T>> {
return (target && target[CONTEXT]) || [];
}

/**
* The base hook context.
*/
Expand Down
22 changes: 20 additions & 2 deletions packages/hooks/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { strict as assert } from 'assert';
import {
hooks, HookContext, functionHooks,
NextFunction, getMiddleware, registerMiddleware,
withParams, withProps
NextFunction, getMiddleware, setMiddleware, registerMiddleware,
withParams, withProps, Middleware
} from '../src/';

describe('functionHooks', () => {
Expand All @@ -26,6 +26,24 @@ describe('functionHooks', () => {
assert.deepEqual(getMiddleware(fn), []);
});

it('can manually set an array of middleware', () => {
const fn = hooks(hello, []) as any;
const mw = [(_ctx: any, next: any) => next()];

setMiddleware(fn, mw);

assert.deepEqual(getMiddleware(fn), mw);
});

it('can manually set middleware with a function', () => {
const noop = (_ctx: any, next: any) => next();
const fn = hooks(hello, [noop]) as any;

setMiddleware(fn, (current: Middleware[]) => [...current, noop]);

assert.deepEqual(getMiddleware(fn), [noop, noop]);
});

it('can override arguments, has context', async () => {
const addYou = async (ctx: HookContext, next: NextFunction) => {
assert.ok(ctx instanceof HookContext);
Expand Down