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

fix(hooks): Allow hooks on functions that have properties #102

Merged
merged 2 commits into from
Apr 27, 2022
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
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"exclude": []
},
"rules": {
"tags": [],
"tags": ["recommended"],
"include": [],
"exclude": ["no-explicit-any", "require-await"]
"exclude": ["no-explicit-any", "require-await", "no-self-assign"]
}
},
"fmt": {
Expand Down
15 changes: 7 additions & 8 deletions main/hooks/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,25 @@ export type HookMap<O = any> = {
[L in keyof O]?: HookOptions;
};

export function objectHooks(_obj: any, hooks: HookMap | AsyncMiddleware[]) {
const obj = typeof _obj === 'function' ? _obj.prototype : _obj;

export function objectHooks(obj: any, hooks: HookMap | AsyncMiddleware[]) {
if (Array.isArray(hooks)) {
return setMiddleware(obj, hooks);
}

return Object.keys(hooks).reduce((result, method) => {
const fn = obj[method];
for (const method of Object.keys(hooks)) {
const target = typeof obj[method] === 'function' ? obj : obj.prototype;
const fn = target && target[method];

if (typeof fn !== 'function') {
throw new Error(`Can not apply hooks. '${method}' is not a function`);
}

const manager = convertOptions(hooks[method]);

result[method] = functionHooks(fn, manager.props({ method }));
target[method] = functionHooks(fn, manager.props({ method }));
}

return result;
}, obj);
return obj;
}

export const hookDecorator = (managerOrMiddleware?: HookOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion main/hooks/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function copyFnProperties<F>(target: F, original: any) {

Object.defineProperty(target, prop, { value });
}
} catch (e) {
} catch (_e) {
// Avoid IE error
}

Expand Down
18 changes: 18 additions & 0 deletions main/hooks/test/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ it('hooking object on class adds to the prototype', async () => {
assertEquals(await instance.addOne(1), 3);
});

it('hooking object works on function that has property', async () => {
const app = function () {};

app.sayHi = (name: string) => `Hello ${name}`;

hooks(app as any, {
sayHi: middleware([
async (ctx: HookContext, next: NextFunction) => {
await next();

ctx.result += '?';
},
]).params('name'),
});

assertEquals(await app.sayHi('David'), 'Hello David?');
});

it('works with inheritance', async () => {
const DummyClass = createDummyClass();

Expand Down
2 changes: 1 addition & 1 deletion main/hooks/test/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ it('compose: should catch downstream errors', async () => {
arr.push(6);
await next();
arr.push(7);
} catch (err) {
} catch (_err) {
arr.push(2);
}
arr.push(3);
Expand Down
2 changes: 1 addition & 1 deletion main/hooks/test/decorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals, assertThrows, it } from './dependencies.ts';
import { assertEquals, assertThrows, it } from './dependencies.ts';
import { HookContext, hooks, middleware, NextFunction } from '../src/index.ts';

it('hook decorator on method and classes with inheritance', async () => {
Expand Down
2 changes: 1 addition & 1 deletion main/hooks/test/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals, assertStrictEquals, assertThrows, it } from './dependencies.ts';
import { assertEquals, assertStrictEquals, assertThrows, it } from './dependencies.ts';
import { HookContext, hooks, middleware, NextFunction } from '../src/index.ts';

interface HookableObject {
Expand Down