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';
class ColonSeperatedStringArray<T extends string = string> extends Array<T> {
class ColonSeparatedStringArray<T extends string = string> extends Array<T> {
constructor(str: string) {
super();
const parsed = str.split(':') as this;
@ -13,5 +13,5 @@ class ColonSeperatedStringArray<T extends string = string> extends Array<T> {
export class ExternalHooksConfig {
/** Files containing external hooks. Multiple files can be separated by colon (":") */
@Env('EXTERNAL_HOOK_FILES')
files: ColonSeperatedStringArray = [];
files: ColonSeparatedStringArray = [];
}

View file

@ -24,12 +24,12 @@ type Repositories = {
Workflow: WorkflowRepository;
};
type Hooks = {
type ExternalHooksMap = {
'n8n.ready': [server: AbstractServer, config: Config];
'n8n.stop': [];
'worker.ready': [];
'n8n.stop': never;
'worker.ready': never;
'activeWorkflows.initialized': [];
'activeWorkflows.initialized': never;
'credentials.create': [encryptedData: ICredentialsDb];
'credentials.update': [newCredentialData: ICredentialsDb];
@ -78,7 +78,7 @@ type Hooks = {
executionId: string,
];
};
type HookNames = keyof Hooks;
type HookNames = keyof ExternalHooksMap;
// TODO: Derive this type from Hooks
interface IExternalHooksFileData {
@ -90,7 +90,7 @@ interface IExternalHooksFileData {
@Service()
export class ExternalHooks {
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;
@ -146,7 +146,7 @@ export class ExternalHooks {
async run<HookName extends HookNames>(
hookName: HookName,
hookParameters?: Hooks[HookName],
hookParameters?: ExternalHooksMap[HookName],
): Promise<void> {
const { registered, dbCollections } = this;
const hookFunctions = registered[hookName];