0.14.2 • Published 1 year ago

@clipboard-health/config v0.14.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@clipboard-health/config

Type-safe static configuration management: a pure function to resolve, validate against a Zod schema, and freeze configuration values.

Configuration values resolve in order from highest precedence to lowest:

  1. Environment variables
    • Resolved converting configuration path from camelCase to UPPER_SNAKE. For example, the { myApi: { port: 3000 } } configuration resolves to MY_API_PORT.
  2. Environment-specific overrides, {@link ConfigValue.overrides}
  3. Default values, {@link ConfigValue.defaultValue}

Supported configuration value types:

  • bigint
  • boolean
  • date
  • number
  • string
  • arrays and nested objects using the above types

To override arrays with environment variables, use stringified JSON arrays, e.g. ["a","b"].

IMPORTANT: To avoid runtime errors:

  1. Environment variables are strings, so use z.coerce Zod types for those you plan to override. Note that z.coerce.boolean() coerces any truthy value to true. To restrict to "true" | "false", use the booleanString schema from @clipboard-health/contract-core.
  2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to modify it. The actual return type is ReadonlyDeep<SchemaT>, but the library returns a Readonly<SchemaT> because the former prevents clients from passing configuration values to functions that don't explicitly accept readonly types.

Table of contents

Install

npm install @clipboard-health/config

Usage

Type-safe configuration

import { ok } from "node:assert/strict";

import { createConfig } from "@clipboard-health/config";
import { z } from "zod";

const allowed = ["local", "development", "production"] as const;
type Allowed = (typeof allowed)[number];

function createEnvironmentConfig(current: Allowed) {
  return createConfig({
    config: {
      baseUrl: {
        defaultValue: "http://localhost:3000",
        description: "Base URL for API requests",
        overrides: {
          development: "https://dev.example.com",
          production: "https://api.example.com",
        },
      },
      database: {
        port: {
          defaultValue: 5432,
          description: "Database port",
        },
      },
    },
    environment: { allowed, current },
    schema: z.object({
      baseUrl: z.string().url(),
      database: z.object({
        // Use `z.coerce` to override with environment variables.
        port: z.coerce.number().min(1024).max(65_535),
      }),
    }),
  });
}

{
  // Uses default values.
  const config = createEnvironmentConfig("local");
  ok(config.baseUrl === "http://localhost:3000");
  ok(config.database.port === 5432);
}

{
  // Uses baseUrl environment override.
  const config = createEnvironmentConfig("development");
  ok(config.baseUrl === "https://dev.example.com");
  ok(config.database.port === 5432);
}

// Uses environment variable overrides.
const original = { ...process.env };
try {
  process.env["BASE_URL"] = "https://staging.example.com";
  process.env["DATABASE_PORT"] = "54320";

  const config = createEnvironmentConfig("local");
  ok(config.baseUrl === "https://staging.example.com");
  ok(config.database.port === 54_320);
} finally {
  process.env = { ...original };
}

Local development commands

See package.json scripts for a list of commands.

0.11.8

1 year ago

0.11.9

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.13.0

1 year ago

0.11.2

1 year ago

0.11.3

1 year ago

0.11.4

1 year ago

0.10.10

1 year ago

0.11.5

1 year ago

0.10.11

1 year ago

0.11.6

1 year ago

0.11.7

1 year ago

0.9.0

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.3

2 years ago

0.7.0

2 years ago

0.10.9

1 year ago

0.10.1

2 years ago

0.12.0

1 year ago

0.10.2

2 years ago

0.12.1

1 year ago

0.10.3

2 years ago

0.14.0

1 year ago

0.12.2

1 year ago

0.10.4

1 year ago

0.14.1

1 year ago

0.12.3

1 year ago

0.10.5

1 year ago

0.14.2

1 year ago

0.12.4

1 year ago

0.10.6

1 year ago

0.10.7

1 year ago

0.10.8

1 year ago

0.11.10

1 year ago

0.10.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.11.11

1 year ago

0.11.12

1 year ago

0.6.11

2 years ago

0.6.10

2 years ago

0.6.7

2 years ago

0.6.9

2 years ago

0.6.8

2 years ago

0.6.6

2 years ago

0.6.5

2 years ago

0.6.4

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago