Hooks - Command hooks
Eicrud provides controller hooks to perform actions triggered by commands.
Note
- In controllers hook you have access to the first HttpRequest and last HttpResponse, this is useful in microservices configuration where the pod executing the command is not always talking to the client directly.
- These hooks are specific to each command. For global hooks see the controller hooks.
beforeControllerHook
my_command.hooks.ts
async beforeControllerHook(
dto: MyCommandDto,
ctx: CrudContext,
): Promise<any> {
// before my_command (entry controller)
return dto;
}
afterControllerHook
my_command.hooks.ts
async afterControllerHook(
dto: MyCommandDto,
result: any,
ctx: CrudContext,
): Promise<any> {
// after my_command (entry controller)
return result;
}
errorControllerHook
my_command.hooks.ts
async errorControllerHook(
dto: MyCommandDto,
error: any,
ctx: CrudContext,
): Promise<any> {
// on my_command error (entry controller)
return Promise.resolve();
}
Note
Returning a value in an error hook prevents the error from throwing. The value is sent back to the client.