1 import { RegisteredExternalAuthConfig } from '@shared/models'
2 import { HookType } from '../../models/plugins/hook-type.enum'
3 import { isCatchable, isPromise } from '../common/promises'
5 function getHookType (hookName: string) {
6 if (hookName.startsWith('filter:')) return HookType.FILTER
7 if (hookName.startsWith('action:')) return HookType.ACTION
12 async function internalRunHook <T> (options: {
17 onError: (err: Error) => void
19 const { handler, hookType, result, params, onError } = options
22 if (hookType === HookType.FILTER) {
23 const p = handler(result, params)
25 const newResult = isPromise(p)
32 // Action/static hooks do not have result value
33 const p = handler(params)
35 if (hookType === HookType.STATIC) {
36 if (isPromise(p)) await p
41 if (hookType === HookType.ACTION) {
42 if (isCatchable(p)) p.catch((err: any) => onError(err))
53 function getExternalAuthHref (apiUrl: string, auth: RegisteredExternalAuthConfig) {
54 return apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`