Logos DX
    Preparing search index...

    Function makeNestedConfig

    • Loads the flatmap variables and returns a config object. Coerces values to the correct types based on guessing from the value itself. For example, if the value is "true", it will be converted to a boolean true, if it's "123", it will be converted to a number 123, etc.

      Type Parameters

      • C extends object = Record<string, any>
      • F = Record<string, string>

      Parameters

      • _flatConfig: F
      • opts: MakeNestedConfigOpts = {}

        Optional options object

        • Optionalfilter?: (key: string, value: string) => boolean
        • OptionalforceAllCapToLower?: boolean
        • OptionalmemoizeOpts?: false | MemoizeOptions<any>
        • OptionalparseUnits?: boolean
        • Optionalseparator?: string
        • OptionalskipConversion?: (key: string, value: unknown) => boolean
        • OptionalstripPrefix?: string | number

      Returns {
          allConfigs: () => C;
          getConfig: <P extends string, D>(
              path: P,
              defaultValue?: D,
          ) => PathValue<C, P>;
      }

      The full configuration object.

      // Assuming process.env contains:
      // {
      // APP_DB_HOST: 'localhost',
      // APP_DB_PORT: '5432',
      // APP_DEBUG: 'true',
      // APP_FEATURE_X_ENABLED: 'false'
      // APP_WORKER_EMAILS_maxRunsPerMin: '100'
      // APP_WORKER_EMAILS_networkTimeoutMs: '100'
      // }

      const config = makeNestedConfig(process.env, {
      filter: (key) => key.startsWith('APP_'),
      stripPrefix: 'APP_', // Strip the APP_ prefix from all keys
      forceAllCapToLower: true,
      separator: '_',
      memoizeOpts: { ttl: 60000 } // Cache for 60 seconds
      });

      console.log(config());
      // {
      // db: {
      // host: 'localhost',
      // port: 5432
      // },
      // debug: true,
      // feature: {
      // x: {
      // enabled: false
      // }
      // },
      // worker: {
      // emails: {
      // maxRunsPerMin: 100,
      // networkTimeoutMs: 100
      // }
      // }
      // }
      // Using a custom separator
      // Assuming process.env contains:
      // {
      // APP_DB__HOST: 'localhost',
      // APP_DB__PORT: '5432',
      // APP_DEBUG: 'true',
      // APP_FEATURE__X_ENABLED: 'false',
      // APP_WORKER__EMAILS__maxRunsPerMin: '100'
      // APP_WORKER__EMAILS__networkTimeoutMs: '100'
      // }
      const config = makeNestedConfig(process.env, {
      filter: (key) => key.startsWith('APP_'),
      stripPrefix: 'APP_', // Strip the APP_ prefix
      forceAllCapToLower: true,
      separator: '__'
      });

      console.log(config());
      // {
      // db: {
      // host: 'localhost',
      // port: 5432
      // },
      // debug: true,
      // feature: {
      // x_enabled: false
      // },
      // worker: {
      // emails: {
      // maxRunsPerMin: 100,
      // networkTimeoutMs: 100
      // }
      // }
      // }
      // Not forcing all-caps to lowercase
      // Assuming process.env contains:
      // {
      // APP_DB_HOST: 'localhost',
      // APP_DB_PORT: '5432',
      // APP_DEBUG: 'true',
      // APP_FEATURE_X_ENABLED: 'false'
      // APP_WORKER_EMAILS_maxRunsPerMin: '100'
      // APP_WORKER_EMAILS_networkTimeoutMs: '100'
      // }

      const config = makeNestedConfig(process.env, {
      filter: (key) => key.startsWith('APP_'),
      stripPrefix: 'APP_', // Strip the APP_ prefix
      forceAllCapToLower: false,
      separator: '_'
      });

      console.log(config());
      // {
      // DB: {
      // HOST: 'localhost',
      // PORT: 5432
      // },
      // DEBUG: true,
      // FEATURE: {
      // X_ENABLED: false
      // },
      // WORKER: {
      // EMAILS: {
      // maxRunsPerMin: 100,
      // networkTimeoutMs: 100
      // }
      // }
      // }
      // Using parseUnits to convert time and byte values
      // Assuming process.env contains:
      // {
      // APP_SESSION_TIMEOUT: '15m',
      // APP_CACHE_TTL: '1hour',
      // APP_MAX_UPLOAD_SIZE: '10mb',
      // APP_DISK_QUOTA: '100gb'
      // }

      const config = makeNestedConfig(process.env, {
      filter: (key) => key.startsWith('APP_'),
      stripPrefix: 'APP_',
      parseUnits: true // Enable unit parsing
      });

      console.log(config());
      // {
      // session: {
      // timeout: 900000 // 15 minutes in milliseconds
      // },
      // cache: {
      // ttl: 3600000 // 1 hour in milliseconds
      // },
      // max: {
      // upload: {
      // size: 10485760 // 10 megabytes in bytes
      // }
      // },
      // disk: {
      // quota: 107374182400 // 100 gigabytes in bytes
      // }
      // }
      // Using skipConversion to keep certain values as strings
      // Assuming process.env contains:
      // {
      // APP_API_KEY: '12345',
      // APP_SECRET_TOKEN: '67890',
      // APP_PORT: '3000',
      // APP_DEBUG: 'true'
      // }

      const config = makeNestedConfig(process.env, {
      filter: (key) => key.startsWith('APP_'),
      stripPrefix: 'APP_',
      skipConversion: (key) => key.toLowerCase().includes('key') || key.toLowerCase().includes('token')
      });

      console.log(config());
      // {
      // api: {
      // key: '12345' // Kept as string
      // },
      // secret: {
      // token: '67890' // Kept as string
      // },
      // port: 3000, // Converted to number
      // debug: true // Converted to boolean
      // }