Config
The config system provides typed configuration files for @t-req/core projects.
defineConfig
Section titled “defineConfig”Define a typed configuration file.
import { defineConfig } from '@t-req/core/config';
export default defineConfig({ variables: { baseUrl: 'https://api.example.com', apiVersion: 'v1', }, resolvers: { $env: (key) => process.env[key] || '', }, defaults: { timeoutMs: 30000, headers: { 'User-Agent': 't-req/1.0', }, },});Config Options
Section titled “Config Options”| Option | Type | Description |
|---|---|---|
variables | Record<string, unknown> | Default variables |
resolvers | Record<string, Resolver> | Custom resolver functions |
defaults | ConfigDefaults | Default request settings |
ConfigDefaults
Section titled “ConfigDefaults”interface ConfigDefaults { timeoutMs?: number; headers?: Record<string, string>; followRedirects?: boolean; validateSSL?: boolean;}loadConfig
Section titled “loadConfig”Load configuration from the filesystem.
import { loadConfig } from '@t-req/core/config';
const { config, configPath } = await loadConfig({ startDir: process.cwd(),});
console.log('Loaded config from:', configPath);console.log('Variables:', config?.variables);Options
Section titled “Options”| Option | Type | Description |
|---|---|---|
startDir | string | Directory to start searching from |
configPath | string | Explicit path to config file (optional) |
Returns
Section titled “Returns”interface LoadConfigResult { config: TReqConfig | null; configPath: string | null;}Config File Discovery
Section titled “Config File Discovery”loadConfig searches for these files (in order):
treq.config.tstreq.config.jstreq.config.mjs
It searches the start directory and all parent directories.
mergeConfig
Section titled “mergeConfig”Merge multiple configuration sources.
import { mergeConfig } from '@t-req/core/config';
const merged = mergeConfig({ file: fileConfig, // From loadConfig cli: cliConfig, // From command line env: envConfig, // From environment});Merge Priority
Section titled “Merge Priority”Later sources override earlier ones:
file- Lowest prioritycli- Higher priorityenv- Highest priority
Example
Section titled “Example”const { config } = await loadConfig({ startDir: process.cwd() });
const merged = mergeConfig({ file: config, cli: { variables: { env: 'staging', // Override from CLI flag }, },});Using Config with Client
Section titled “Using Config with Client”import { loadConfig, mergeConfig } from '@t-req/core/config';import { createClient } from '@t-req/core';import { createNodeIO } from '@t-req/core/runtime';
// Load configconst { config } = await loadConfig({ startDir: process.cwd() });const merged = mergeConfig({ file: config });
// Create client with configconst client = createClient({ io: createNodeIO(), variables: merged.variables, resolvers: merged.resolvers, defaults: merged.defaults,});Environment-Specific Configs
Section titled “Environment-Specific Configs”Use environment variables or separate files:
import { defineConfig } from '@t-req/core/config';
const env = process.env.TREQ_ENV || 'development';
const configs = { development: { baseUrl: 'http://localhost:3000', }, staging: { baseUrl: 'https://staging-api.example.com', }, production: { baseUrl: 'https://api.example.com', },};
export default defineConfig({ variables: configs[env], resolvers: { $env: (key) => process.env[key] || '', },});Config Inheritance
Section titled “Config Inheritance”For monorepos, create a base config and extend it:
export const baseConfig = { resolvers: { $env: (key) => process.env[key] || '', $uuid: () => crypto.randomUUID(), }, defaults: { timeoutMs: 30000, },};
// apps/api/treq.config.tsimport { defineConfig } from '@t-req/core/config';import { baseConfig } from '@shared/treq.base';
export default defineConfig({ ...baseConfig, variables: { baseUrl: 'https://api.example.com', },});TypeScript Types
Section titled “TypeScript Types”import type { TReqConfig, ConfigDefaults, LoadConfigOptions, LoadConfigResult, MergeConfigOptions,} from '@t-req/core/config';