diff options
Diffstat (limited to 'server/lib/plugins/hooks.ts')
-rw-r--r-- | server/lib/plugins/hooks.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/server/lib/plugins/hooks.ts b/server/lib/plugins/hooks.ts new file mode 100644 index 000000000..7bb907e6a --- /dev/null +++ b/server/lib/plugins/hooks.ts | |||
@@ -0,0 +1,26 @@ | |||
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 | |||
6 | // Helpers to run hooks | ||
7 | const Hooks = { | ||
8 | wrapObject: <T, U extends ServerFilterHookName>(obj: T, hookName: U) => { | ||
9 | return PluginManager.Instance.runHook(hookName, obj) as Promise<T> | ||
10 | }, | ||
11 | |||
12 | wrapPromise: async <T, U extends ServerFilterHookName>(fun: Promise<T> | Bluebird<T>, hookName: U) => { | ||
13 | const result = await fun | ||
14 | |||
15 | return PluginManager.Instance.runHook(hookName, result) | ||
16 | }, | ||
17 | |||
18 | runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => { | ||
19 | PluginManager.Instance.runHook(hookName, params) | ||
20 | .catch(err => logger.error('Fatal hook error.', { err })) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | export { | ||
25 | Hooks | ||
26 | } | ||