register() #
Available since 2.8.0
Registers a new pipe on an event.
This method can only be used before the application is started, unless the dynamic option is set.
register<TEventDefinition extends EventDefinition = EventDefinition>(
event: TEventDefinition["name"],
handler: PipeEventHandler<TEventDefinition>,
options?: { dynamic?: boolean }
): string | void| Argument | Type | Description |
|---|---|---|
event | string | Event name |
handler | PipeEventHandler | Function to execute when the event is triggered |
options | object | Optional options |
options #
Available since 2.10.2
| Property | Type | Default | Description |
|---|---|---|---|
dynamic | boolean | false | Allows to register the pipe once the application is started |
Returns #
When the pipe is registered once the application is started (with dynamic: true), returns the pipe unique identifier, to be used with unregister.
Otherwise, returns nothing.
Errors #
- plugin.runtime.already_started: the application is started and the
dynamicoption is not set - plugin.assert.invalid_pipe: the handler is not a function
Usage #
app.pipe.register('server:afterNow', async (request: KuzzleRequest) => {
request.result.now = (new Date()).toUTCString();
return request;
});Register a pipe once the application is started #
const pipeId = app.pipe.register(
'server:afterNow',
async (request: KuzzleRequest) => request,
{ dynamic: true });
// Later on
app.pipe.unregister(pipeId);Strong typing #
It's possible to specify the arguments with whom the handler will be called.
This will also ensure that the first argument is returned at the end of the pipe handler.
type EventGenericDocumentAfterWrite = {
name: 'generic:document:afterWrite';
args: [Document[], KuzzleRequest];
}
app.pipe.register<EventGenericDocumentAfterWrite>(
'generic:document:afterWrite',
async (documents: Document[], request: KuzzleRequest) => {
app.log.error(documents);
return documents;
});