fix: plugins & helpers

This commit is contained in:
abdou6666 2025-01-02 09:54:50 +01:00
parent 9770a344aa
commit 1c613790ea
2 changed files with 17 additions and 7 deletions

View File

@ -6,7 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { Injectable } from '@nestjs/common';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
@ -34,7 +34,12 @@ export class HelperService {
* @param name - The helper to be registered.
*/
public register<H extends BaseHelper>(helper: H) {
const helpers = this.registry.get(helper.getType());
const helpers = this.registry.get(helper.getType()) as Map<string, H>;
if (helpers.has(helper.getName())) {
throw new InternalServerErrorException(
`Helper with Name ${helper.getName()} and Type ${helper.getType()} already exist`,
);
}
helpers.set(helper.getName(), helper);
this.logger.log(`Helper "${helper.getName()}" has been registered!`);
}
@ -48,7 +53,7 @@ export class HelperService {
* @returns - The helper
*/
public get<T extends HelperType>(type: T, name: HelperName) {
const helpers = this.registry.get(type);
const helpers = this.registry.get(type) as Map<string, BaseHelper>;
if (!helpers.has(name)) {
throw new Error('Uknown type of helpers');

View File

@ -6,7 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { Injectable } from '@nestjs/common';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { BasePlugin } from './base-plugin.service';
import { PluginInstance } from './map-types';
@ -44,7 +44,12 @@ export class PluginService<T extends BasePlugin = BasePlugin> {
* @param plugin The plugin instance to register.
*/
public setPlugin(type: PluginType, name: PluginName, plugin: T) {
const registry = this.registry.get(type);
const registry = this.registry.get(type) as Map<PluginName, T>;
if (registry.has(name)) {
throw new InternalServerErrorException(
`setPlugin: Unable to setPlugin with name ${name} of type ${type} (duplicate names)`,
);
}
registry.set(name, plugin);
}
@ -54,7 +59,7 @@ export class PluginService<T extends BasePlugin = BasePlugin> {
* @returns An array containing all the registered plugins.
*/
public getAllByType<PT extends PluginType>(type: PT): PluginInstance<PT>[] {
const registry = this.registry.get(type);
const registry = this.registry.get(type) as Map<PluginName, T>;
return Array.from(registry.values()) as PluginInstance<PT>[];
}
@ -76,7 +81,7 @@ export class PluginService<T extends BasePlugin = BasePlugin> {
* @returns The plugin associated with the given key, or `undefined` if not found.
*/
public getPlugin<PT extends PluginType>(type: PT, name: PluginName) {
const registry = this.registry.get(type);
const registry = this.registry.get(type) as Map<PluginName, T>;
const plugin = registry.get(name);
return plugin ? (plugin as PluginInstance<PT>) : undefined;
}