]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/core-utils/plugins/hooks.ts
Redirect to default login url on 401
[github/Chocobozzz/PeerTube.git] / shared / core-utils / plugins / hooks.ts
index 5405e0529ad0eeab00bc08ed6cee44d84a8b5ef6..96bcc945e895e195eab3317d0cea83290064692e 100644 (file)
@@ -1,5 +1,6 @@
+import { RegisteredExternalAuthConfig } from '@shared/models'
 import { HookType } from '../../models/plugins/hook-type.enum'
-import { isCatchable, isPromise } from '../miscs/miscs'
+import { isCatchable, isPromise } from '../common/promises'
 
 function getHookType (hookName: string) {
   if (hookName.startsWith('filter:')) return HookType.FILTER
@@ -8,15 +9,24 @@ function getHookType (hookName: string) {
   return HookType.STATIC
 }
 
-async function internalRunHook <T> (handler: Function, hookType: HookType, result: T, params: any, onError: (err: Error) => void) {
+async function internalRunHook <T> (options: {
+  handler: Function
+  hookType: HookType
+  result: T
+  params: any
+  onError: (err: Error) => void
+}) {
+  const { handler, hookType, result, params, onError } = options
+
   try {
     if (hookType === HookType.FILTER) {
       const p = handler(result, params)
 
-      if (isPromise(p)) result = await p
-      else result = p
+      const newResult = isPromise(p)
+        ? await p
+        : p
 
-      return result
+      return newResult
     }
 
     // Action/static hooks do not have result value
@@ -40,7 +50,12 @@ async function internalRunHook <T> (handler: Function, hookType: HookType, resul
   return result
 }
 
+function getExternalAuthHref (apiUrl: string, auth: RegisteredExternalAuthConfig) {
+  return apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
+}
+
 export {
   getHookType,
-  internalRunHook
+  internalRunHook,
+  getExternalAuthHref
 }