Skip to content

[Dove] Add Hooks To Custom Service Methods #2386

Answered by daffl
forgot asked this question in Q&A
Discussion options

You must be logged in to vote

Custom methods only work with the new hooks which can be registered like this:

app.service('myservice').hooks({
  myCustomMethod: [
    async (context, next) => {
      console.log(`Doing things before ${context.method}`);
      await next();
      console.log(`Doing things after ${context.method}`);
    }
  }
});

Alternatively you can bring them closer to the service class using this format:

import { hooks } from '@feathersjs/hooks';

export class MyService {
  async find (params) {
    return [];
  }

  async myCustomMethod (data, params) {
    return data;
  }
}

// Hooks for all service methods
hooks(MyService.prototype, [
  authenticate('jwt')
]);

hooks(MyService, {
  find: [
    //…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@forgot
Comment options

@daffl
Comment options

@forgot
Comment options

@daffl
Comment options

Answer selected by forgot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants