Skip to content

inject config

awekrx edited this page May 29, 2026 · 1 revision

inject-config

Import

import { injectConfig } from '@dev-suite/decorators/inject-config'

Category

  • class

Use Case

Inject shared configuration into class instances without repeating constructor plumbing.

Replaces

  • Passing the same config object through many constructors
  • Manual this.config = ... setup per class

Example 1

Without decorator

class StripeAdapter {
  private readonly config: { apiKey: string; region: string };

  constructor(config: { apiKey: string; region: string }) {
    this.config = config;
  }

  createCharge() {
    return this.client.charge({ key: this.config.apiKey, region: this.config.region });
  }
}

With decorator

import { injectConfig } from '@dev-suite/decorators/inject-config';

@injectConfig({
  config: {
    apiKey: process.env.STRIPE_API_KEY,
    region: 'us-east-1',
  },
})
class StripeAdapter {
  createCharge() {
    return this.client.charge({ key: this.config.apiKey, region: this.config.region });
  }
}

Why better

  • Removes repeated config constructor signatures
  • Co-locates adapter config with class definition

Example 2

Without decorator

class MailgunAdapter {
  constructor(private readonly cfg: { domain: string; token: string }) {}

  send(payload: MailPayload) {
    return this.client.send({ ...payload, domain: this.cfg.domain, token: this.cfg.token });
  }
}

With decorator

import { injectConfig } from '@dev-suite/decorators/inject-config';

@injectConfig({ config: { domain: 'mg.example.com', token: process.env.MAILGUN_TOKEN } })
class MailgunAdapter {
  send(payload: MailPayload) {
    return this.client.send({ ...payload, domain: this.config.domain, token: this.config.token });
  }
}

Why better

  • Standardized configuration shape across adapters
  • Easier instantiation in tests and factories

Clone this wiki locally