refactor(core): Refactor custom config types, add additional tests (no-changelog) (#14083)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-03-20 19:23:20 +01:00
committed by GitHub
parent 5aacc2e0c2
commit c34ffd0e7c
6 changed files with 50 additions and 19 deletions

View File

@@ -1,14 +1,6 @@
import { ColonSeparatedStringArray } from '../custom-types';
import { Config, Env } from '../decorators';
class ColonSeparatedStringArray<T extends string = string> extends Array<T> {
constructor(str: string) {
super();
const parsed = str.split(':') as this;
const filtered = parsed.filter((i) => typeof i === 'string' && i.length);
return filtered.length ? filtered : [];
}
}
@Config
export class ExternalHooksConfig {
/** Files containing external hooks. Multiple files can be separated by colon (":") */

View File

@@ -1,5 +1,5 @@
import { CommaSeperatedStringArray } from '../custom-types';
import { Config, Env, Nested } from '../decorators';
import { StringArray } from '../utils';
/** Scopes (areas of functionality) to filter logs by. */
export const LOG_SCOPES = [
@@ -14,6 +14,7 @@ export const LOG_SCOPES = [
'scaling',
'waiting-executions',
'task-runner',
'insights',
] as const;
export type LogScope = (typeof LOG_SCOPES)[number];
@@ -57,7 +58,7 @@ export class LoggingConfig {
* @example `N8N_LOG_OUTPUT=console,file` will output to both console and file.
*/
@Env('N8N_LOG_OUTPUT')
outputs: StringArray<'console' | 'file'> = ['console'];
outputs: CommaSeperatedStringArray<'console' | 'file'> = ['console'];
@Nested
file: FileLoggingConfig;
@@ -84,5 +85,5 @@ export class LoggingConfig {
* `N8N_LOG_SCOPES=license,waiting-executions`
*/
@Env('N8N_LOG_SCOPES')
scopes: StringArray<LogScope> = [];
scopes: CommaSeperatedStringArray<LogScope> = [];
}

View File

@@ -0,0 +1,19 @@
abstract class StringArray<T extends string> extends Array<T> {
constructor(str: string, delimiter: string) {
super();
const parsed = str.split(delimiter) as this;
return parsed.filter((i) => typeof i === 'string' && i.length);
}
}
export class CommaSeperatedStringArray<T extends string> extends StringArray<T> {
constructor(str: string) {
super(str, ',');
}
}
export class ColonSeparatedStringArray<T extends string = string> extends StringArray<T> {
constructor(str: string) {
super(str, ':');
}
}

View File

@@ -36,6 +36,7 @@ export { S3Config } from './configs/external-storage.config';
export { LOG_SCOPES } from './configs/logging.config';
export type { LogScope } from './configs/logging.config';
export { WorkflowsConfig } from './configs/workflows.config';
export * from './custom-types';
@Config
export class GlobalConfig {

View File

@@ -1,7 +0,0 @@
export class StringArray<T extends string> extends Array<T> {
constructor(str: string) {
super();
const parsed = str.split(',') as StringArray<T>;
return parsed.every((i) => typeof i === 'string') ? parsed : [];
}
}

View File

@@ -0,0 +1,25 @@
import { CommaSeperatedStringArray, ColonSeparatedStringArray } from '../src/custom-types';
describe('CommaSeperatedStringArray', () => {
it('should parse comma-separated string into array', () => {
const result = new CommaSeperatedStringArray('a,b,c');
expect(result).toEqual(['a', 'b', 'c']);
});
it('should handle empty strings', () => {
const result = new CommaSeperatedStringArray('a,b,,,');
expect(result).toEqual(['a', 'b']);
});
});
describe('ColonSeparatedStringArray', () => {
it('should parse colon-separated string into array', () => {
const result = new ColonSeparatedStringArray('a:b:c');
expect(result).toEqual(['a', 'b', 'c']);
});
it('should handle empty strings', () => {
const result = new ColonSeparatedStringArray('a::b:::');
expect(result).toEqual(['a', 'b']);
});
});