Skip to content

Commit d83f78a

Browse files
authored
Merge pull request #470 from devforth/feature/AdminForth/1209/implement-getpluginbyid-map-pl
feat: implement getPluginById method and update plugin registration w…
2 parents 122adb7 + fb1fc7c commit d83f78a

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

adminforth/index.ts

Lines changed: 17 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,13 @@ 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+
const pluginId = pluginInstance.pluginOptions?.id;
261+
if (pluginId) {
262+
if (this.pluginsById[pluginId]) {
263+
throw new Error(`Plugin with id "${pluginId}" already exists!`);
264+
}
265+
this.pluginsById[pluginId] = pluginInstance;
266+
}
259267
this.activatedPlugins.push(pluginInstance);
260268
afLogger.trace(`🔌 Plugin ${pluginInstance.constructor.name} activated successfully`);
261269
}
@@ -282,6 +290,15 @@ class AdminForth implements IAdminForth {
282290
return plugins[0] as T;
283291
}
284292

293+
getPluginById<T = any>(id: string): T {
294+
const plugin = this.pluginsById[id];
295+
if (!plugin) {
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;
300+
}
301+
285302
validateFieldGroups(fieldGroups: {
286303
groupName: string;
287304
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 identifier.
430+
* @param id - unique id of the plugin instance (custom identifier passed when registering/configuring the plugin)
431+
*
432+
* Example:
433+
* ```ts
434+
* const auditLog = adminforth.getPluginById<AuditLogPlugin>('AuditLogPlugin');
435+
* ```
436+
*/
437+
getPluginById<T>(id: 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)