]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/core-utils/plugins/hooks.ts
Add is locale support
[github/Chocobozzz/PeerTube.git] / shared / core-utils / plugins / hooks.ts
CommitLineData
2570fd9c 1import { RegisteredExternalAuthConfig } from '@shared/models'
b4055e1c 2import { HookType } from '../../models/plugins/hook-type.enum'
15a7eafb 3import { isCatchable, isPromise } from '../common/promises'
b4055e1c
C
4
5function getHookType (hookName: string) {
6 if (hookName.startsWith('filter:')) return HookType.FILTER
7 if (hookName.startsWith('action:')) return HookType.ACTION
8
9 return HookType.STATIC
10}
11
22df69fd
C
12async function internalRunHook <T> (options: {
13 handler: Function
14 hookType: HookType
15 result: T
16 params: any
17 onError: (err: Error) => void
18}) {
19 const { handler, hookType, result, params, onError } = options
20
b4055e1c 21 try {
89cd1275
C
22 if (hookType === HookType.FILTER) {
23 const p = handler(result, params)
24
22df69fd
C
25 const newResult = isPromise(p)
26 ? await p
27 : p
89cd1275 28
22df69fd 29 return newResult
89cd1275 30 }
b4055e1c 31
89cd1275
C
32 // Action/static hooks do not have result value
33 const p = handler(params)
34
35 if (hookType === HookType.STATIC) {
36 if (isPromise(p)) await p
37
38 return undefined
39 }
b4055e1c 40
89cd1275 41 if (hookType === HookType.ACTION) {
93cae479 42 if (isCatchable(p)) p.catch((err: any) => onError(err))
b4055e1c 43
89cd1275 44 return undefined
b4055e1c
C
45 }
46 } catch (err) {
47 onError(err)
48 }
49
50 return result
51}
52
2570fd9c
C
53function getExternalAuthHref (apiUrl: string, auth: RegisteredExternalAuthConfig) {
54 return apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
55}
56
b4055e1c
C
57export {
58 getHookType,
2570fd9c
C
59 internalRunHook,
60 getExternalAuthHref
b4055e1c 61}