All files / src/plugins pluginSystem.ts

75.67% Statements 28/37
50% Branches 1/2
25% Functions 1/4
75.67% Lines 28/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x       1x 1x 2x 2x 2x 1x       1x 1x    
 
 
import { Name as ManagedLoginName, GDmanagedLogin, defaultConfig as managedLoginDefaultConfig } from './managedLogin/main'
import { Name as SecureAuthName, GDdoubleAuthentication, defaultConfig as secureAuthDefaultConfig } from './secureAuth/main'
 
const allPlugins = {
  GDdoubleAuthentication,
  GDmanagedLogin,
} as const satisfies Record<PluginNames, any>
 
const defaultConfigs = {
  GDdoubleAuthentication: secureAuthDefaultConfig,
  GDmanagedLogin: managedLoginDefaultConfig,
} as const satisfies Record<PluginNames, any>
 
export type InstanciatedPlugin = InstanceType<(typeof allPlugins)[PluginNames]>
 
const registeredPlugins = [] as InstanciatedPlugin[]
 
export function registerPlugin(plugin: InstanciatedPlugin) {
  registeredPlugins.push(plugin)
}
 
export function getPlugin<T extends PluginNames>(name: T) {
  return registeredPlugins.find(p => p.name === name) as InstanceType<(typeof allPlugins)[T]> | null
}
 
/** Will return plugon config if registered or default config otherwise */
export function getPluginConfig<T extends PluginNames>(name: T) {
  return (registeredPlugins.find(p => p.name === name)?.config || defaultConfigs[name]) as InstanceType<(typeof allPlugins)[T]>['config']
}
 
export function getServicesToRegister() {
  return registeredPlugins.map(p => p.serviceToRegister).flat()
}
 
export type PluginNames = ManagedLoginName | SecureAuthName