Skip to content

Commit fb1fc7c

Browse files
author
Pavlo Kulyk
committed
feat: enhance getPluginById method to support custom identifiers and improve error messaging
1 parent 14209e3 commit fb1fc7c

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

adminforth/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,12 @@ class AdminForth implements IAdminForth {
257257
throw new Error(`Attempt to activate Plugin ${pluginInstance.constructor.name} second time for same resource, but plugin does not support it.
258258
To support multiple plugin instance pre one resource, plugin should return unique string values for each installation from instanceUniqueRepresentation`);
259259
}
260-
if (pluginInstance.id) {
261-
if (this.pluginsById[pluginInstance.id]) {
262-
throw new Error(`Plugin with id "${pluginInstance.id}" already exists!`);
260+
const pluginId = pluginInstance.pluginOptions?.id;
261+
if (pluginId) {
262+
if (this.pluginsById[pluginId]) {
263+
throw new Error(`Plugin with id "${pluginId}" already exists!`);
263264
}
264-
this.pluginsById[pluginInstance.id] = pluginInstance;
265+
this.pluginsById[pluginId] = pluginInstance;
265266
}
266267
this.activatedPlugins.push(pluginInstance);
267268
afLogger.trace(`🔌 Plugin ${pluginInstance.constructor.name} activated successfully`);
@@ -289,13 +290,14 @@ class AdminForth implements IAdminForth {
289290
return plugins[0] as T;
290291
}
291292

292-
getPluginById<T = any>(id: string): T {
293+
getPluginById<T = any>(id: string): T {
293294
const plugin = this.pluginsById[id];
294295
if (!plugin) {
295-
throw new Error(`Plugin with id "${id}" not found`);
296+
const similar = suggestIfTypo(Object.keys(this.pluginsById), id);
297+
throw new Error(`Plugin with id "${id}" not found.${similar ? ` Did you mean "${similar}"?` : ''}`);
298+
}
299+
return plugin as T;
296300
}
297-
return plugin as T;
298-
}
299301

300302
validateFieldGroups(fieldGroups: {
301303
groupName: string;

adminforth/types/Back.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,15 @@ export interface IAdminForth {
426426
getPluginByClassName<T>(className: string): T;
427427

428428
/**
429-
* This method can be used when you want to get a plugin instance by its unique pluginInstanceId.
430-
* @param pluginInstanceId - unique id of the plugin instance
429+
*This method can be used when you want to get a plugin instance by its unique identifier.
430+
* @param id - unique id of the plugin instance (custom identifier passed when registering/configuring the plugin)
431431
*
432432
* Example:
433433
* ```ts
434-
* const auditLog = adminforth.getPluginById<AuditLogPlugin>('TestPlugin');
434+
* const auditLog = adminforth.getPluginById<AuditLogPlugin>('AuditLogPlugin');
435435
* ```
436436
*/
437-
getPluginById<T>(pluginInstanceId: string): T;
437+
getPluginById<T>(id: string): T;
438438
}
439439

440440

0 commit comments

Comments
 (0)