Skip to content

Commit

Permalink
Merge pull request #493 from Hexastack/fix/plugins-strict-null-check
Browse files Browse the repository at this point in the history
Fix: plugins strict null check
  • Loading branch information
marrouchi authored Jan 3, 2025
2 parents ad45a70 + 7e116fa commit 0b3036c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 8 additions & 3 deletions api/src/helper/helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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!`);
}
Expand All @@ -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');
Expand Down
13 changes: 9 additions & 4 deletions api/src/plugins/plugins.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
`Unable to setPlugin() with name ${name} of type ${type} (possible duplicate)`,
);
}
registry.set(name, plugin);
}

Expand All @@ -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>[];
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 0b3036c

Please sign in to comment.