mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
⚡ Make it possible to supply hook data with reload
This commit is contained in:
parent
8ada4534ec
commit
ed1f29feba
|
@ -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 = {};
|
||||
await this.loadHooksFiles(true);
|
||||
|
||||
if (externalHooks === undefined) {
|
||||
await this.loadHooksFiles(true);
|
||||
} else {
|
||||
this.loadHooks(externalHooks);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,20 +51,8 @@ class ExternalHooksClass implements IExternalHooksClass {
|
|||
delete require.cache[require.resolve(hookFilePath)];
|
||||
}
|
||||
|
||||
const hookFile = require(hookFilePath);
|
||||
|
||||
for (const resource of Object.keys(hookFile)) {
|
||||
for (const operation of Object.keys(hookFile[resource])) {
|
||||
// Save all the hook functions directly under their string
|
||||
// format in an array
|
||||
const hookString = `${resource}.${operation}`;
|
||||
if (this.externalHooks[hookString] === undefined) {
|
||||
this.externalHooks[hookString] = [];
|
||||
}
|
||||
|
||||
this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFile[resource][operation]);
|
||||
}
|
||||
}
|
||||
const hookFile = require(hookFilePath) as IExternalHooksFileData;
|
||||
this.loadHooks(hookFile);
|
||||
} catch (error) {
|
||||
throw new Error(`Problem loading external hook file "${hookFilePath}": ${error.message}`);
|
||||
}
|
||||
|
@ -67,6 +61,22 @@ class ExternalHooksClass implements IExternalHooksClass {
|
|||
}
|
||||
|
||||
|
||||
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}`;
|
||||
if (this.externalHooks[hookString] === undefined) {
|
||||
this.externalHooks[hookString] = [];
|
||||
}
|
||||
|
||||
this.externalHooks[hookString].push.apply(this.externalHooks[hookString], hookFileData[resource][operation]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async run(hookName: string, hookParameters?: any[]): Promise<void> { // tslint:disable-line:no-any
|
||||
const externalHookFunctions: IExternalHooksFunctions = {
|
||||
dbCollections: Db.collections,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue