@msamblanet/node-config-types
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

Node Config Types

npm version License

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> - Subclasses EventEmitter
  • BaseLoggingConfigurable<C,L> - Exposes log in a protected member
  • BaseLoggingEmittingConfigurable<C,L> - Exposes log in a protected member, subclasses EventEmitter
  • In all of the above:
    • C is the type of the config and defaults to IConfig
    • L is the type of the logger and it defaults to ILogAdapter 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;
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @msamblanet/node-config-types

Weekly Downloads

2

Version

0.4.0

License

Apache-2.0

Unpacked Size

12.4 kB

Total Files

6

Last publish

Collaborators

  • msamblanet