Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: plugins strict null check #493

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
`setPlugin: Unable to setPlugin with name ${name} of type ${type} (duplicate names)`,
marrouchi marked this conversation as resolved.
Show resolved Hide resolved
);
}
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
Loading