All About Hooks
Method hooks
Each service method may have its own hooks.
After all, you'll likely need to do something different for create
than for a find
.
The Feathers code would be:
const messagesHooks = {
before: {
find: hook11(),
get: hook21(),
create: [ hook31(), hook32() ],
update: hook41(),
patch: hook51(),
remove: hook61(),
},
after: {
find: [ hook15(), hook16() ],
create: hook35(),
update: hook45(),
patch: hook55(),
remove: hook65(),
}
};
const messages = app.service('messages');
messages.hooks(messagesHooks);
ProTip: The Feathers service call handler expects the functions it calls to have the signature
context => {}
. So if you have such a hook you would code{ before: { all: myHook } }
. You however commonly want to pass params to the hook, such as which field in the record to delete. So you need to use a signature likeparams => context => { /* use params */ }
and codeall: myHook(params)
. The common hooks and the Feathers community tend to use the latter signature for consistency. So it would be best to set up your hooks to usemyHook()
.
Service hooks
Some hooks, such as authentication, may need to be run for every method. You can specify them once rather than repeating them for every method.
Your Feathers code would additionally include:
const messagesHooks = {
before: {
all: hook01(),
},
after: {
all: hook05(),
}
};
App hooks
You may want to run some hooks for every service. The Feathers profiler, for example, adds before and after hooks to time each service call.
The Feathers code for these application level hooks would be:
app.hooks({
before: {
create: hook30a()
},
after: {
create: hook39a()
},
});
Errors and error hooks
Errors may be thrown inside hooks - by JavaScript, by the Feathers database adapter, or by your own code.
Your hook can, for example, return a formatted message as follows:
// On server
const errors = require('feathers-errors');
throw new errors.BadRequest('Invalid request', {errors: {email: 'Invalid Email'} }); // inside hook
// On client
messages.create(...)
.then(data => ...)
.catch(err => {
console.log(err.messages); // Invalid request
console.log(err.errors.email); // Invalid Email
});
You can optionally deal with thrown errors in the service:
Your Feathers code would additionally include:
app.hooks({
error: {
all: hook00e(),
create: hook30e()
}
});