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

conserve props from original method #19

Merged
merged 3 commits into from
Feb 14, 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
6 changes: 0 additions & 6 deletions packages/hooks/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ 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]) || [];
}
Expand Down
11 changes: 11 additions & 0 deletions packages/hooks/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,16 @@ export const functionHooks = <F, T = any>(original: F, opts: HookSettings<T>) =>
registerContextUpdater(wrapper, updateContext);
registerMiddleware(wrapper, middleware);

const originalProps = (Object.keys(original) as any)
.concat(Object.getOwnPropertySymbols(original));

for (const prop of originalProps) {
const propDescriptor = Object.getOwnPropertyDescriptor(original, prop);

if (!wrapper.hasOwnProperty(prop)) {
Object.defineProperty(wrapper, prop, propDescriptor);
}
}

return Object.assign(wrapper, { original, collect });
};
18 changes: 18 additions & 0 deletions packages/hooks/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,22 @@ describe('functionHooks', () => {
assert.equal(result, 'Hi Bertho!');
assert.equal(called, 1);
});

it('conserves method properties', async () => {
const TEST = Symbol('test');
const hello = (name: any) => `Hi ${name}`;
(hello as any)[TEST] = true;

const sayHi = hooks(hello, [
async (context, next) => {
await next();
context.result += '!';
}
]);

const result = await sayHi('Bertho');

assert.equal(result, 'Hi Bertho!');
assert.equal((sayHi as any)[TEST], (hello as any)[TEST]);
});
});