hooks.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models/plugins/server-hook.model'
  2. import { PluginManager } from './plugin-manager'
  3. import { logger } from '../../helpers/logger'
  4. import * as Bluebird from 'bluebird'
  5. type PromiseFunction <U, T> = (params: U) => Promise<T> | Bluebird<T>
  6. type RawFunction <U, T> = (params: U) => T
  7. // Helpers to run hooks
  8. const Hooks = {
  9. wrapObject: <T, U extends ServerFilterHookName>(result: T, hookName: U) => {
  10. return PluginManager.Instance.runHook(hookName, result)
  11. },
  12. wrapPromiseFun: async <U, T, V extends ServerFilterHookName>(fun: PromiseFunction<U, T>, params: U, hookName: V) => {
  13. const result = await fun(params)
  14. return PluginManager.Instance.runHook(hookName, result, params)
  15. },
  16. wrapFun: async <U, T, V extends ServerFilterHookName>(fun: RawFunction<U, T>, params: U, hookName: V) => {
  17. const result = fun(params)
  18. return PluginManager.Instance.runHook(hookName, result, params)
  19. },
  20. runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
  21. PluginManager.Instance.runHook(hookName, undefined, params)
  22. .catch(err => logger.error('Fatal hook error.', { err }))
  23. }
  24. }
  25. export {
  26. Hooks
  27. }