Boilerplate
← Back to blog

How swappable providers keep the codebase honest

The Team1 min read

The core primitive is about forty lines: a createRegistry that maps a provider name to a lazy factory. Nothing about a provider is loaded until it's actually selected.

Why lazy?

An unused provider should cost nothing — no SDK in the bundle, no env vars required, no startup penalty. Registering import()-ed factories means the Stripe SDK never loads when you're on Lemon Squeezy, and the headless-CMS client never loads when you're serving markdown from disk.

registry.register('markdown', async () => {
  const { MarkdownContentSource } = await import('@repo/cms-markdown');
  return new MarkdownContentSource();
});

Contracts make swaps safe

Because every provider implements the same abstract class, the pages that render this blog don't know or care where the content came from. The contract is the only thing they import. Add a provider, register it, flip an env var — done.

That discipline is what keeps a growing codebase honest: features compose against interfaces, never against a specific vendor.