From 273ce7815cd29a23f91db1869e2e222c260ab3c6 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Wed, 26 Feb 2020 15:58:50 +0530 Subject: [PATCH] feat: apply feedback Applied feedback. Signed-off-by: Hage Yaapa --- types/observer-mixin.d.ts | 18 ++++++++--- types/persisted-model.d.ts | 63 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/types/observer-mixin.d.ts b/types/observer-mixin.d.ts index 6a020e6cd..2c82721c1 100644 --- a/types/observer-mixin.d.ts +++ b/types/observer-mixin.d.ts @@ -4,12 +4,22 @@ // License text available at https://opensource.org/licenses/MIT import {Callback, PromiseOrVoid} from './common'; +import {PersistedModel, PersistedModelClass} from './persisted-model'; -export interface OperationHookContext { +export interface OperationHookContext { + /** + * 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: OperationHookContext, next: (err?: any) => void) => void; +export type Listener = (ctx: Ctx, next: (err?: any) => void) => void; export interface ObserverMixin { /** @@ -40,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>): void; /** * Unregister an asynchronous observer for the given operation (event). @@ -58,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>): Listener> | undefined; /** * Unregister all asynchronous observers for the given operation (event). diff --git a/types/persisted-model.d.ts b/types/persisted-model.d.ts index dd4af1ad8..ed0461722 100644 --- a/types/persisted-model.d.ts +++ b/types/persisted-model.d.ts @@ -5,7 +5,7 @@ import {Callback, Options, PromiseOrVoid} from './common'; import {ModelBase, ModelData} from './model'; -import {Listener} from './observer-mixin'; +import {Listener, OperationHookContext} from './observer-mixin'; import {Filter, Where} from './query'; /** @@ -521,9 +521,66 @@ export declare class PersistedModel extends ModelBase { */ static getIdName(): string; - definePersistedModel(entityClass: T): PersistedModel; + /** + * Register an asynchronous observer for the given operation (event). + * + * Example: + * + * Registers a `before save` observer for a given model. + * + * ```js + * MyModel.observe('before save', function filterProperties(ctx, next) { + if (ctx.options && ctx.options.skipPropertyFilter) return next(); + if (ctx.instance) { + FILTERED_PROPERTIES.forEach(function(p) { + ctx.instance.unsetAttribute(p); + }); + } else { + FILTERED_PROPERTIES.forEach(function(p) { + delete ctx.data[p]; + }); + } + next(); + }); + * ``` + * + * @param {String} operation The operation name. + * @callback {function} listener The listener function. It will be invoked with + * `this` set to the model constructor, e.g. `User`. + */ + static observe(operation: string, handler: Listener>): void; + + /** + * Unregister an asynchronous observer for the given operation (event). + * + * Example: + * + * ```js + * MyModel.removeObserver('before save', function removedObserver(ctx, next) { + // some logic user want to apply to the removed observer... + next(); + }); + * ``` + * + * @param {String} operation The operation name. + * @callback {function} listener The listener function. + */ + static removeObserver(operation: string, handler: Listener>): void; - static observe(operation: string, handler: Listener): void; + /** + * Unregister all asynchronous observers for the given operation (event). + * + * Example: + * + * Remove all observers connected to the `before save` operation. + * + * ```js + * MyModel.clearObservers('before save'); + * ``` + * + * @param {String} operation The operation name. + */ + static clearObservers(operation: string): void; } export type PersistedModelClass = typeof PersistedModel;