2021-08-27 14:32:44 +02:00
|
|
|
import Bluebird from 'bluebird'
|
2021-05-11 12:04:47 +02:00
|
|
|
import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models'
|
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { PluginManager } from './plugin-manager'
|
2019-07-18 14:28:37 +02:00
|
|
|
|
2019-07-19 17:30:41 +02:00
|
|
|
type PromiseFunction <U, T> = (params: U) => Promise<T> | Bluebird<T>
|
|
|
|
type RawFunction <U, T> = (params: U) => T
|
|
|
|
|
2019-07-18 14:28:37 +02:00
|
|
|
// Helpers to run hooks
|
|
|
|
const Hooks = {
|
2022-08-02 15:29:00 +02:00
|
|
|
wrapObject: <T, U extends ServerFilterHookName>(result: T, hookName: U, context?: any) => {
|
|
|
|
return PluginManager.Instance.runHook(hookName, result, context)
|
2019-07-19 17:30:41 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
wrapPromiseFun: async <U, T, V extends ServerFilterHookName>(fun: PromiseFunction<U, T>, params: U, hookName: V) => {
|
|
|
|
const result = await fun(params)
|
|
|
|
|
|
|
|
return PluginManager.Instance.runHook(hookName, result, params)
|
2019-07-18 14:28:37 +02:00
|
|
|
},
|
|
|
|
|
2019-07-19 17:30:41 +02:00
|
|
|
wrapFun: async <U, T, V extends ServerFilterHookName>(fun: RawFunction<U, T>, params: U, hookName: V) => {
|
|
|
|
const result = fun(params)
|
2019-07-18 14:28:37 +02:00
|
|
|
|
2019-07-19 17:30:41 +02:00
|
|
|
return PluginManager.Instance.runHook(hookName, result, params)
|
2019-07-18 14:28:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
|
2020-04-09 11:35:29 +02:00
|
|
|
PluginManager.Instance.runHook(hookName, undefined, params)
|
2019-07-18 14:28:37 +02:00
|
|
|
.catch(err => logger.error('Fatal hook error.', { err }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
Hooks
|
|
|
|
}
|