feat: add OperationHookContext interface

Added OperationHookContext interface.

Signed-off-by: Hage Yaapa <hage.yaapa@in.ibm.com>
This commit is contained in:
Hage Yaapa 2020-02-24 20:32:56 +05:30 committed by Hage Yaapa
parent 36303b3b63
commit dcfda58fda
1 changed files with 17 additions and 3 deletions

View File

@ -4,8 +4,22 @@
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
import {Callback, PromiseOrVoid} from './common'; import {Callback, PromiseOrVoid} from './common';
import {PersistedModel, PersistedModelClass} from './persisted-model';
export type Listener = (ctx: object, next: (err?: any) => void) => void; export interface OperationHookContext<T extends typeof PersistedModel> {
/**
* The constructor of the model that triggered the operation.
*/
Model: T;
/**
* Additional context properties, not typed yet.
* See https://loopback.io/doc/en/lb3/Operation-hooks.html#hooks
*/
[property: string]: any;
}
export type Listener<Ctx> = (ctx: Ctx, next: (err?: any) => void) => void;
export interface ObserverMixin { export interface ObserverMixin {
/** /**
@ -36,7 +50,7 @@ export interface ObserverMixin {
* `this` set to the model constructor, e.g. `User`. * `this` set to the model constructor, e.g. `User`.
* @end * @end
*/ */
observe(operation: string, listener: Listener): void; observe(operation: string, listener: Listener<OperationHookContext<PersistedModelClass>>): void;
/** /**
* Unregister an asynchronous observer for the given operation (event). * Unregister an asynchronous observer for the given operation (event).
@ -54,7 +68,7 @@ export interface ObserverMixin {
* @callback {function} listener The listener function. * @callback {function} listener The listener function.
* @end * @end
*/ */
removeObserver(operation: string, listener: Listener): Listener | undefined; removeObserver(operation: string, listener: Listener<OperationHookContext<PersistedModelClass>>): Listener<OperationHookContext<PersistedModelClass>> | undefined;
/** /**
* Unregister all asynchronous observers for the given operation (event). * Unregister all asynchronous observers for the given operation (event).