Fragments

Fragments are great way to add partial presets for configuration. Some usecases include:

  • Local development preset.
  • Quick start configuration.
  • Composable snippets of config.

To start using it, add a few fragments and choose a fragment key. See below for more details.

.addConfigFragment(name, fragment)

This option adds a fragment, using the first parameter as its identifier.

config.ts
import { createConfigLoader } from 'neat-config';
import { z } from 'zod';

process.env = {
  USE_PRESETS: "my-name"
}

const nameFragment = {
  name: "john doe"
}

export const config = createConfigLoader()
  .addFromEnvironment()
  .addConfigFragment("my-name", nameFragment)
  .setFragmentKey("USE_PRESETS")
  .load();
result
{
  name: "john doe"
}

.addConfigFragments(fragments)

This option adds multiple fragments. For every entry, the key is used as the fragment name and the value being the fragment itself.

config.ts
import { createConfigLoader } from 'neat-config';
import { z } from 'zod';

process.env = {
  USE_PRESETS: "myName"
}

const fragments = {
  myName: {
    name: "John doe"
  },
  devServer: {
    port: 8080
  }
}

export const config = createConfigLoader()
  .addFromEnvironment()
  .addConfigFragments(fragments)
  .setFragmentKey("USE_PRESETS")
  .load();
result
{
  name: "john doe"
}

.setFragmentKey(key)

This option sets the key that defines which fragments get loaded into the configuration. The key can come from any loaders.

The value associated with key is a comma seperated list of fragments to load.

config.ts
import { createConfigLoader } from 'neat-config';
import { z } from 'zod';

process.env = {
  USE_PRESETS: "myName,devServer"
}

const fragments = {
  myName: {
    name: "John doe"
  },
  devServer: {
    port: 8080
  }
}

export const config = createConfigLoader()
  .addFromEnvironment()
  .addConfigFragments(fragments)
  .setFragmentKey("USE_PRESETS")
  .load();
result
{
  name: "john doe",
  port: 8080,
}