aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/hooks.ts
blob: 7bb907e6a79d1cd96dd2ea6e6a0513f193a9e883 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models/plugins/server-hook.model'
import { PluginManager } from './plugin-manager'
import { logger } from '../../helpers/logger'
import * as Bluebird from 'bluebird'

// Helpers to run hooks
const Hooks = {
  wrapObject: <T, U extends ServerFilterHookName>(obj: T, hookName: U) => {
    return PluginManager.Instance.runHook(hookName, obj) as Promise<T>
  },

  wrapPromise: async <T, U extends ServerFilterHookName>(fun: Promise<T> | Bluebird<T>, hookName: U) => {
    const result = await fun

    return PluginManager.Instance.runHook(hookName, result)
  },

  runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
    PluginManager.Instance.runHook(hookName, params)
      .catch(err => logger.error('Fatal hook error.', { err }))
  }
}

export {
  Hooks
}