diff --git a/api/src/helper/helper.service.ts b/api/src/helper/helper.service.ts index fb3e6bfb..1e103715 100644 --- a/api/src/helper/helper.service.ts +++ b/api/src/helper/helper.service.ts @@ -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(helper: H) { - const helpers = this.registry.get(helper.getType()); + const helpers = this.registry.get(helper.getType()) as Map; + 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(type: T, name: HelperName) { - const helpers = this.registry.get(type); + const helpers = this.registry.get(type) as Map; if (!helpers.has(name)) { throw new Error('Uknown type of helpers'); diff --git a/api/src/plugins/plugins.service.ts b/api/src/plugins/plugins.service.ts index 1cb8170a..8a2667c9 100644 --- a/api/src/plugins/plugins.service.ts +++ b/api/src/plugins/plugins.service.ts @@ -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 { * @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; + if (registry.has(name)) { + throw new InternalServerErrorException( + `Unable to setPlugin() with name ${name} of type ${type} (possible duplicate)`, + ); + } registry.set(name, plugin); } @@ -54,7 +59,7 @@ export class PluginService { * @returns An array containing all the registered plugins. */ public getAllByType(type: PT): PluginInstance[] { - const registry = this.registry.get(type); + const registry = this.registry.get(type) as Map; return Array.from(registry.values()) as PluginInstance[]; } @@ -76,7 +81,7 @@ export class PluginService { * @returns The plugin associated with the given key, or `undefined` if not found. */ public getPlugin(type: PT, name: PluginName) { - const registry = this.registry.get(type); + const registry = this.registry.get(type) as Map; const plugin = registry.get(name); return plugin ? (plugin as PluginInstance) : undefined; }