Search code examples
feathersjsfeathers-hookfeathers-service

Is there FeathersJs syntax for creating an endpoint with hooks in a single command?


I've found how to add hooks to an existing service:

app.use("/hello", { get: async () => "Hello World"});
app.service('/hello').hooks({
  before: { create: someHookFn }
});

But I'm pretty sure this syntax can be improved, and I'm just failing to find an example. Trying to search the source code was no help either, it's pretty hairy in terms of its type definitions.

Is there FeathersJs syntax for creating an endpoint with hooks in a single command?

Something like this:

// non-functional code
app.use("/hello", {
  service: { get: async () => "Hello World"},
  hooks: {
    before: { create: someHookFn }
  }
});

Solution

  • You can make a function which does this like so:

    function createService(service, hooks) {
      return feathers().use('', service).service('').hooks(hooks);
    }
    

    Then use it like so:

    app.use("/hello", createService({
      { get: async () => "Hello World"},
      { before: { create: someHookFn } }
    }));
    

    The reason for doing this for me was that I wanted to have a service which was not connected to an endpoint for use in graphql. Also, I don't like connecting things by string id.