Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add OperationHookContext interface #1820

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions types/observer-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@
// License text available at https://opensource.org/licenses/MIT

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;
bajtos marked this conversation as resolved.
Show resolved Hide resolved
}

export type Listener<Ctx> = (ctx: Ctx, next: (err?: any) => void) => void;

export interface ObserverMixin {
/**
Expand Down Expand Up @@ -36,7 +50,7 @@ export interface ObserverMixin {
* `this` set to the model constructor, e.g. `User`.
* @end
*/
observe(operation: string, listener: Listener): void;
observe(operation: string, listener: Listener<OperationHookContext<PersistedModelClass>>): void;
bajtos marked this conversation as resolved.
Show resolved Hide resolved

/**
* Unregister an asynchronous observer for the given operation (event).
Expand All @@ -54,7 +68,7 @@ export interface ObserverMixin {
* @callback {function} listener The listener function.
* @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).
Expand Down