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(feathers): Always enable hooks on default service methods #2275

Merged
merged 2 commits into from
Mar 31, 2021
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 packages/feathers/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getManager, HookContextData, HookManager, HookMap, HOOKS, hooks, Middleware } from '@feathersjs/hooks';
import { Service, ServiceOptions, HookContext, FeathersService, Application } from '../declarations';
import { defaultServiceArguments } from '../service';
import { defaultServiceArguments, getHookMethods } from '../service';
import {
collectLegacyHooks,
enableLegacyHooks,
Expand Down Expand Up @@ -59,7 +59,7 @@ export function hookMixin<A> (
}

const app = this;
const serviceMethodHooks = options.methods.reduce((res, method) => {
const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => {
const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];

res[method] = new FeathersHookManager<A>(app, method)
Expand Down
8 changes: 8 additions & 0 deletions packages/feathers/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export const defaultEventMap = {
remove: 'removed'
}

export function getHookMethods (service: any, options: ServiceOptions) {
const { methods } = options;

return defaultServiceMethods.filter(m =>
typeof service[m] === 'function' && !methods.includes(m)
).concat(methods);
}

export function getServiceOptions (
service: any, options: ServiceOptions = {}
): ServiceOptions {
Expand Down
11 changes: 8 additions & 3 deletions packages/feathers/test/hooks/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,16 @@ describe('hooks basics', () => {
});
});

it('can register hooks on a custom method', async () => {
it('can register hooks on a custom method, still adds hooks to default methods', async () => {
class Dummy {
async get (id: Id) {
return { id };
}

async create (data: any) {
return data;
}

async custom (data: any) {
return data;
}
Expand All @@ -322,9 +326,10 @@ describe('hooks basics', () => {

app.service('dummy').hooks({
custom: [async (context, next) => {
(context.data as any).fromHook = true;
context.data.fromHook = true;
await next();
}]
}],
create: [async (_context, next) => next()]
});

assert.deepStrictEqual(await app.service('dummy').custom({
Expand Down