refactor(core): Small improvements to external hooks (#12915)

This commit is contained in:
Iván Ovejero 2025-01-29 11:14:06 +01:00 committed by GitHub
parent be967ebec0
commit 647dc198c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View file

@ -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 = [];
} }

View file

@ -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];