1
0
Fork 0
peertube/server/lib/plugins/hooks.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-07-18 08:28:37 -04:00
import * as Bluebird from 'bluebird'
2021-05-11 06:04:47 -04:00
import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models'
import { logger } from '../../helpers/logger'
import { PluginManager } from './plugin-manager'
2019-07-18 08:28:37 -04:00
2019-07-19 11:30:41 -04:00
type PromiseFunction <U, T> = (params: U) => Promise<T> | Bluebird<T>
type RawFunction <U, T> = (params: U) => T
2019-07-18 08:28:37 -04:00
// Helpers to run hooks
const Hooks = {
2019-07-19 11:30:41 -04:00
wrapObject: <T, U extends ServerFilterHookName>(result: T, hookName: U) => {
2019-07-22 05:18:22 -04:00
return PluginManager.Instance.runHook(hookName, result)
2019-07-19 11:30:41 -04: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 08:28:37 -04:00
},
2019-07-19 11:30:41 -04:00
wrapFun: async <U, T, V extends ServerFilterHookName>(fun: RawFunction<U, T>, params: U, hookName: V) => {
const result = fun(params)
2019-07-18 08:28:37 -04:00
2019-07-19 11:30:41 -04:00
return PluginManager.Instance.runHook(hookName, result, params)
2019-07-18 08:28:37 -04:00
},
runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
PluginManager.Instance.runHook(hookName, undefined, params)
2019-07-18 08:28:37 -04:00
.catch(err => logger.error('Fatal hook error.', { err }))
}
}
export {
Hooks
}