register() #

Available since 2.8.0

Registers a new hook on an event.

This method can only be used before the application is started.

register<TEventDefinition extends EventDefinition = EventDefinition>(
  event: TEventDefinition["name"],
  handler: HookEventHandler<TEventDefinition>
): void

ArgumentTypeDescription
event
string
Event name
handler
HookEventHandler
Function to execute when the event is triggered

Usage #

app.hook.register('request:onError', (request: KuzzleRequest) => {
  app.log.error(request.error);
});

Strong typing #

It's possible to specify the arguments with whom the handler will be called.

type EventGenericDocumentAfterWrite = {
  name: 'generic:document:afterWrite';

  args: [Document[], KuzzleRequest];
}

app.hook.register<EventGenericDocumentAfterWrite>(
  'generic:document:afterWrite',
  async (documents: Document[], request: KuzzleRequest) => {
    app.log.error(documents);
  });