Skip to content

Commit 14209e3

Browse files
author
Pavlo Kulyk
committed
feat: implement getPluginById method and update plugin registration with unique IDs
1 parent b578daa commit 14209e3

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

adminforth/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class AdminForth implements IAdminForth {
116116
connectorClasses: any;
117117
runningHotReload: boolean;
118118
activatedPlugins: Array<AdminForthPlugin>;
119+
pluginsById: Record<string, AdminForthPlugin> = {};
119120
configValidator: IConfigValidator;
120121
restApi: AdminForthRestAPI;
121122

@@ -256,6 +257,12 @@ class AdminForth implements IAdminForth {
256257
throw new Error(`Attempt to activate Plugin ${pluginInstance.constructor.name} second time for same resource, but plugin does not support it.
257258
To support multiple plugin instance pre one resource, plugin should return unique string values for each installation from instanceUniqueRepresentation`);
258259
}
260+
if (pluginInstance.id) {
261+
if (this.pluginsById[pluginInstance.id]) {
262+
throw new Error(`Plugin with id "${pluginInstance.id}" already exists!`);
263+
}
264+
this.pluginsById[pluginInstance.id] = pluginInstance;
265+
}
259266
this.activatedPlugins.push(pluginInstance);
260267
afLogger.trace(`🔌 Plugin ${pluginInstance.constructor.name} activated successfully`);
261268
}
@@ -282,6 +289,14 @@ class AdminForth implements IAdminForth {
282289
return plugins[0] as T;
283290
}
284291

292+
getPluginById<T = any>(id: string): T {
293+
const plugin = this.pluginsById[id];
294+
if (!plugin) {
295+
throw new Error(`Plugin with id "${id}" not found`);
296+
}
297+
return plugin as T;
298+
}
299+
285300
validateFieldGroups(fieldGroups: {
286301
groupName: string;
287302
columns: string[];

adminforth/types/Back.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ export interface IAdminForth {
424424
*
425425
*/
426426
getPluginByClassName<T>(className: string): T;
427+
428+
/**
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
431+
*
432+
* Example:
433+
* ```ts
434+
* const auditLog = adminforth.getPluginById<AuditLogPlugin>('TestPlugin');
435+
* ```
436+
*/
437+
getPluginById<T>(pluginInstanceId: string): T;
427438
}
428439

429440

dev-demo/resources/auditLogs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default {
3939
},
4040
plugins: [
4141
new AuditLogPlugin({
42+
id: 'AuditLogPlugin',
4243
// if you want to exclude some resources from logging
4344
//excludeResourceIds: ['adminuser'],
4445
resourceColumns: {

dev-demo/resources/carsResourseTemplate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ export default function carsResourseTemplate(resourceId: string, dataSource: str
373373
return { ok: false, error: result?.error ?? 'Provided 2fa verification data is invalid' };
374374
}
375375
await adminforth
376-
.getPluginByClassName<AuditLogPlugin>('AuditLogPlugin')
376+
// .getPluginByClassName<AuditLogPlugin>('AuditLogPlugin')
377+
.getPluginById<AuditLogPlugin>('AuditLogPlugin')
377378
.logCustomAction({
378379
resourceId: resourceId,
379380
recordId: null,

0 commit comments

Comments
 (0)