mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 02:51:14 +00:00
⚡ Make it possible to supply hook data with reload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
Db,
|
||||
IExternalHooksClass,
|
||||
IExternalHooksFileData,
|
||||
IExternalHooksFunctions,
|
||||
} from './';
|
||||
|
||||
@@ -26,9 +27,14 @@ class ExternalHooksClass implements IExternalHooksClass {
|
||||
}
|
||||
|
||||
|
||||
async reload() {
|
||||
async reload(externalHooks?: IExternalHooksFileData) {
|
||||
this.externalHooks = {};
|
||||
|
||||
if (externalHooks === undefined) {
|
||||
await this.loadHooksFiles(true);
|
||||
} else {
|
||||
this.loadHooks(externalHooks);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,10 +51,19 @@ class ExternalHooksClass implements IExternalHooksClass {
|
||||
delete require.cache[require.resolve(hookFilePath)];
|
||||
}
|
||||
|
||||
const hookFile = require(hookFilePath);
|
||||
const hookFile = require(hookFilePath) as IExternalHooksFileData;
|
||||
this.loadHooks(hookFile);
|
||||
} catch (error) {
|
||||
throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const resource of Object.keys(hookFile)) {
|
||||
for (const operation of Object.keys(hookFile[resource])) {
|
||||
|
||||
loadHooks(hookFileData: IExternalHooksFileData) {
|
||||
for (const resource of Object.keys(hookFileData)) {
|
||||
for (const operation of Object.keys(hookFileData[resource])) {
|
||||
// Save all the hook functions directly under their string
|
||||
// format in an array
|
||||
const hookString = `${resource}.${operation}`;
|
||||
@@ -56,12 +71,7 @@ class ExternalHooksClass implements IExternalHooksClass {
|
||||
this.externalHooks[hookString] = [];
|
||||
}
|
||||
|
||||
this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFile[resource][operation]);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`);
|
||||
}
|
||||
this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFileData[resource][operation]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +219,12 @@ export interface IExternalHooks {
|
||||
};
|
||||
}
|
||||
|
||||
export interface IExternalHooksFileData {
|
||||
[key: string]: {
|
||||
[key: string]: Array<(...args: any[]) => Promise<void>>; //tslint:disable-line:no-any
|
||||
};
|
||||
}
|
||||
|
||||
export interface IExternalHooksFunctions {
|
||||
dbCollections: IDatabaseCollections;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user