mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Small improvements to external hooks (#12915)
This commit is contained in:
parent
be967ebec0
commit
647dc198c2
|
@ -1,6 +1,6 @@
|
||||||
import { Config, Env } from '../decorators';
|
import { Config, Env } from '../decorators';
|
||||||
|
|
||||||
class ColonSeperatedStringArray<T extends string = string> extends Array<T> {
|
class ColonSeparatedStringArray<T extends string = string> extends Array<T> {
|
||||||
constructor(str: string) {
|
constructor(str: string) {
|
||||||
super();
|
super();
|
||||||
const parsed = str.split(':') as this;
|
const parsed = str.split(':') as this;
|
||||||
|
@ -13,5 +13,5 @@ class ColonSeperatedStringArray<T extends string = string> extends Array<T> {
|
||||||
export class ExternalHooksConfig {
|
export class ExternalHooksConfig {
|
||||||
/** Files containing external hooks. Multiple files can be separated by colon (":") */
|
/** Files containing external hooks. Multiple files can be separated by colon (":") */
|
||||||
@Env('EXTERNAL_HOOK_FILES')
|
@Env('EXTERNAL_HOOK_FILES')
|
||||||
files: ColonSeperatedStringArray = [];
|
files: ColonSeparatedStringArray = [];
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,12 +24,12 @@ type Repositories = {
|
||||||
Workflow: WorkflowRepository;
|
Workflow: WorkflowRepository;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Hooks = {
|
type ExternalHooksMap = {
|
||||||
'n8n.ready': [server: AbstractServer, config: Config];
|
'n8n.ready': [server: AbstractServer, config: Config];
|
||||||
'n8n.stop': [];
|
'n8n.stop': never;
|
||||||
'worker.ready': [];
|
'worker.ready': never;
|
||||||
|
|
||||||
'activeWorkflows.initialized': [];
|
'activeWorkflows.initialized': never;
|
||||||
|
|
||||||
'credentials.create': [encryptedData: ICredentialsDb];
|
'credentials.create': [encryptedData: ICredentialsDb];
|
||||||
'credentials.update': [newCredentialData: ICredentialsDb];
|
'credentials.update': [newCredentialData: ICredentialsDb];
|
||||||
|
@ -78,7 +78,7 @@ type Hooks = {
|
||||||
executionId: string,
|
executionId: string,
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
type HookNames = keyof Hooks;
|
type HookNames = keyof ExternalHooksMap;
|
||||||
|
|
||||||
// TODO: Derive this type from Hooks
|
// TODO: Derive this type from Hooks
|
||||||
interface IExternalHooksFileData {
|
interface IExternalHooksFileData {
|
||||||
|
@ -90,7 +90,7 @@ interface IExternalHooksFileData {
|
||||||
@Service()
|
@Service()
|
||||||
export class ExternalHooks {
|
export class ExternalHooks {
|
||||||
private readonly registered: {
|
private readonly registered: {
|
||||||
[hookName in HookNames]?: Array<(...args: Hooks[hookName]) => Promise<void>>;
|
[hookName in HookNames]?: Array<(...args: ExternalHooksMap[hookName]) => Promise<void>>;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
private readonly dbCollections: Repositories;
|
private readonly dbCollections: Repositories;
|
||||||
|
@ -146,7 +146,7 @@ export class ExternalHooks {
|
||||||
|
|
||||||
async run<HookName extends HookNames>(
|
async run<HookName extends HookNames>(
|
||||||
hookName: HookName,
|
hookName: HookName,
|
||||||
hookParameters?: Hooks[HookName],
|
hookParameters?: ExternalHooksMap[HookName],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { registered, dbCollections } = this;
|
const { registered, dbCollections } = this;
|
||||||
const hookFunctions = registered[hookName];
|
const hookFunctions = registered[hookName];
|
||||||
|
|
Loading…
Reference in a new issue