@msamblanet/node-config-types
v0.4.0
Published
TS types and utility methods for @msamblanet packages
Downloads
5
Readme
Node Config Types
This repository is part of a collection of my personal node.js libraries and templates. I am making them available to the public - feel free to offer suggestions, report issues, or make PRs via GitHub.
This project provides a minimal implementation of types and base classes for configurable components to allow for consistent usages across projects.
The sample below is provided in example-module.ts
for use as a template for new components (if desired).
Variations of BaseConfigurable
are provided to account for logging classes and event emitters. The full list of base clases are:
BaseConfigurable<C>
BaseEmittingConfigurable<C>
- SubclassesEventEmitter
BaseLoggingConfigurable<C,L>
- Exposeslog
in a protected memberBaseLoggingEmittingConfigurable<C,L>
- Exposeslog
in a protected member, subclassesEventEmitter
- In all of the above:
C
is the type of the config and defaults toIConfig
L
is the type of the logger and it defaults toILogAdapter
from@msamblanet/slf4j
(but can be anything)
import { IConfig, Overrides, mergeOptions, BaseLoggingConfigurable } from '@msamblanet/node-config-types';
export type IExampleLogger = unknown;
// ********************************************************
export interface FooConfig {
a: number;
b: number;
}
export class SimpleFoo extends BaseLoggingConfigurable<FooConfig, IExampleLogger> {
public static readonly DEFAULT_CONFIG = { a: 1, b: 2 };
public constructor(log: IExampleLogger, ...config: Overrides<FooConfig>) {
super(log, SimpleFoo.DEFAULT_CONFIG, ...config);
}
public someMethod(): number {
return this.config.a + this.config.b;
}
}
// ********************************************************
export interface AbstractFooConfig {
a: number;
b: number;
}
export abstract class AbstractFoo<X extends AbstractFooConfig = AbstractFooConfig> extends BaseLoggingConfigurable<X, IExampleLogger> {
public static readonly DEFAULT_CONFIG = { a: 1, b: 2 };
public constructor(log: IExampleLogger, defaults: X, ...overrides: Overrides<X>) {
super(log, defaults, ...overrides);
}
public abstract someMethod(): number;
}
// ********************************************************
export interface ConcreteFooConfig extends AbstractFooConfig {
c: number;
}
export class ConcreteFoo extends AbstractFoo<ConcreteFooConfig> {
public static readonly DEFAULT_CONFIG: ConcreteFooConfig = mergeOptions<ConcreteFooConfig>(AbstractFoo.DEFAULT_CONFIG, {
b: 42,
c: 3
});
public constructor(log: IExampleLogger, ...overrides: Overrides<ConcreteFooConfig>) {
super(log, ConcreteFoo.DEFAULT_CONFIG, ...overrides);
}
public someMethod(): number {
return this.config.a + this.config.b + this.config.c;
}
}