Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
323 changes: 285 additions & 38 deletions docs/@catbee/utils/config.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
---
sidebar_position: 1
---

# Config

Global configuration management for logging, cache, and runtime settings. Provides type-safe access and mutation of config values, with sensible defaults and environment variable support.
Global configuration management for logging, cache, and server settings. Provides type-safe access and mutation of config values, with sensible defaults and comprehensive environment variable support.

## API Summary

- [**`setConfig(value: Partial<Config>): void`**](#setconfig) – update configuration at runtime.
- [**`getConfig(): Config`**](#getconfig) – get the current configuration object.
- [**`getCatbeeGlobalConfig(): CatbeeConfig`**](#getcatbeeglobalconfig) – get the current global configuration object.
- [**`setCatbeeGlobalConfig(value: Partial<CatbeeConfig>): void`**](#setcatbeeglobalconfig) – update global configuration at runtime.
- [**`getCatbeeServerGlobalConfig(): CatbeeGlobalServerConfig`**](#getcatbeeserverglobalconfig) – get the current server configuration.
- [**`setCatbeeServerGlobalConfig(value: Partial<CatbeeGlobalServerConfig>): void`**](#setcatbeeserverglobalconfig) – update server configuration at runtime.
- [**`getConfig()`**](#getconfig-deprecated) – ⚠️ **deprecated** alias for `getCatbeeGlobalConfig`.
- [**`setConfig(value)`**](#setconfig-deprecated) – ⚠️ **deprecated** alias for `setCatbeeGlobalConfig`.

---

## Function Documentation & Usage Examples

### `setConfig()`
### `getCatbeeGlobalConfig()`

Returns the current global Catbee configuration including logger, cache, and server settings.

**Method Signature:**

```ts
function getCatbeeGlobalConfig(): CatbeeConfig;
```

**Returns:**

- The current Catbee configuration object (deep cloned).

**Examples:**

```ts
import { getCatbeeGlobalConfig } from '@catbee/utils/config';

const config = getCatbeeGlobalConfig();
console.log(config.logger.level); // "info" or "debug"
console.log(config.cache.defaultTtl); // 3600000
console.log(config.server.port); // 3000
```

---

Updates configuration settings at runtime. Only provided keys are merged; others remain unchanged.
### `setCatbeeGlobalConfig()`

Updates global configuration settings at runtime. Merges the provided partial configuration with the existing one using deep object merge.

**Method Signature:**

```ts
function setConfig(value: Partial<Config>): void;
function setCatbeeGlobalConfig(value: Partial<CatbeeConfig>): void;
```

**Parameters:**
Expand All @@ -28,87 +63,299 @@ function setConfig(value: Partial<Config>): void;
**Examples:**

```ts
import { setConfig, getConfig } from '@catbee/utils';
import { setCatbeeGlobalConfig } from '@catbee/utils/config';

setConfig({
logger: { level: 'debug', name: 'custom-logger', pretty: true }
setCatbeeGlobalConfig({
logger: {
level: 'debug',
name: 'my-app',
pretty: true,
colorize: true
},
cache: {
defaultTtl: 7200000 // 2 hours
}
});
```

---

### `getConfig()`
### `getCatbeeServerGlobalConfig()`

Returns the current configuration object.
Returns the current server-specific configuration.

**Method Signature:**

```ts
function getConfig(): Config;
function getCatbeeServerGlobalConfig(): CatbeeGlobalServerConfig;
```

**Returns:**

- The current configuration object.
- The current server configuration object.

**Examples:**

```ts
import { getConfig } from '@catbee/utils';
import { getCatbeeServerGlobalConfig } from '@catbee/utils/config';

const currentConfig = getConfig();
console.log(currentConfig.logger.level); // "info"
console.log(currentConfig.cache.defaultTtl); // 3600000
const serverConfig = getCatbeeServerGlobalConfig();
console.log(serverConfig.port); // 3000
console.log(serverConfig.host); // "0.0.0.0"
console.log(serverConfig.appName); // "catbee_server"
```

---

## Default Values
### `setCatbeeServerGlobalConfig()`

All configuration options have sensible defaults:
Updates server configuration settings at runtime. Merges the provided partial server configuration with the existing one.

**Method Signature:**

```ts
const config = {
logger: {
level: 'info', // Logging level
name: '@catbee/utils', // Logger name
pretty: true, // Pretty print logs (dev only)
singleLine: false // Single line pretty print
},
cache: {
defaultTtl: 3600000 // Default cache TTL (1 hour, in ms)
function setCatbeeServerGlobalConfig(value: Partial<CatbeeGlobalServerConfig>): void;
```

**Parameters:**

- `value`: Partial server configuration object to merge.

**Examples:**

```ts
import { setCatbeeServerGlobalConfig } from '@catbee/utils/config';

setCatbeeServerGlobalConfig({
port: 8080,
cors: true,
helmet: true,
rateLimit: {
enable: true,
max: 200
}
};
});
```

---

### `getConfig()` (Deprecated)

:::warning Deprecated
Use [`getCatbeeGlobalConfig()`](#getcatbeeglobalconfig) instead.
:::

Alias for `getCatbeeGlobalConfig()`. Returns the current configuration object.

**Method Signature:**

```ts
function getConfig(): CatbeeConfig;
```

---

### `setConfig()` (Deprecated)

:::warning Deprecated
Use [`setCatbeeGlobalConfig()`](#setcatbeeglobalconfig) instead.
:::

Alias for `setCatbeeGlobalConfig()`. Updates configuration settings at runtime.

**Method Signature:**

```ts
function setConfig(value: Partial<CatbeeConfig>): void;
```

---

## Default Values

All configuration options have sensible defaults loaded from environment variables:

### Logger Defaults

```ts
logger: {
level: 'info', // 'debug' in dev/test, otherwise 'info'
name: '@catbee/utils', // or npm package name
pretty: false, // pretty-print logging
colorize: true, // colorize pretty output
singleLine: false, // single-line pretty output
dir: '' // log file directory (empty = disabled)
}
```

**Logger Environment Variables**

For configuring logger behavior via environment variables, see the [Logger documentation](logger#environment-variables).

### Cache Defaults

```ts
cache: {
defaultTtl: 3600000 // 1 hour in milliseconds
}
```

**Cache Environment Variables**

| Environment Variable | Type | Default/Value | Description |
| --------------------------- | -------- | ------------- | ---------------------------------------------- |
| `CACHE_DEFAULT_TTL_SECONDS` | `number` | `3600` | Default cache TTL in seconds (converted to ms) |

### Server Defaults

```ts
server: {
port: 3000,
host: '0.0.0.0',
cors: false,
helmet: false,
compression: false,
bodyParser: {
json: { limit: '1mb' },
urlencoded: { extended: true, limit: '1mb' }
},
cookieParser: false,
isMicroservice: false,
appName: 'catbee_server',
globalHeaders: {},
rateLimit: {
enable: false,
windowMs: 900000, // 15 minutes
max: 100,
message: 'Too many requests, please try again later.',
standardHeaders: true,
legacyHeaders: false
},
requestLogging: {
enable: true, // true in dev, false in production
ignorePaths: ['/healthz', '/favicon.ico', '/metrics', '/docs', '/.well-known'],
skipNotFoundRoutes: true
},
trustProxy: false,
openApi: {
enable: false,
mountPath: '/docs',
verbose: false,
withGlobalPrefix: false
},
healthCheck: {
path: '/healthz',
detailed: true,
withGlobalPrefix: false
},
requestTimeout: 0,
responseTime: {
enable: false,
addHeader: true,
logOnComplete: false
},
requestId: {
headerName: 'x-request-id',
exposeHeader: true,
generator: () => uuid()
},
metrics: {
enable: false,
path: '/metrics',
withGlobalPrefix: false
},
serviceVersion: {
enable: false,
headerName: 'x-service-version',
version: '0.0.0'
},
skipHealthzChecksValidation: false
}
```

**Server Environment Variables**

For overriding server config options via environment variables, see the [Express Server documentation](server#server-environment-variables).

## Types

### CatbeeConfig

Main configuration interface combining logger, cache, and server settings.

```ts
interface CatbeeConfig {
logger?: {
level?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
name?: string;
pretty?: boolean;
colorize?: boolean;
singleLine?: boolean;
dir?: string;
};
cache: {
defaultTtl: number; // milliseconds
};
server: CatbeeGlobalServerConfig;
}
```

### Logger Configuration

```ts
interface LoggerConfig {
level: 'trace' | 'debug' | 'info' | 'warn' | 'error';
name: string;
pretty: boolean;
singleLine: boolean;
level?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'; /** Logging level */
name?: string; /** Logger instance name */
pretty?: boolean; /** Enable pretty-print logging */
colorize?: boolean; /** Enable colorized output for pretty-print */
singleLine?: boolean; /** Single line output for pretty-print (default: false) */
dir?: string; /** Directory to write log files (empty = disabled) */
}
```

### Cache Configuration

```ts
interface CacheConfig {
defaultTtl: number; // milliseconds
/** Default TTL (time to live) in milliseconds */
defaultTtl: number;
}
```

### Config Object
### Server Configuration

The server configuration is extensive. Key interfaces include:

```ts
interface Config {
logger: LoggerConfig;
cache: CacheConfig;
interface CatbeeGlobalServerConfig {
port: number;
host?: string;
cors?: boolean | CorsOptions;
helmet?: boolean | HelmetOptions;
compression?: boolean | CompressionOptions;
bodyParser?: {
json?: JsonParserOptions;
urlencoded?: UrlencodedParserOptions;
};
cookieParser?: boolean | CookieParseOptions;
trustProxy?: boolean | number | string | string[];
staticFolders?: StaticFolderConfig[];
isMicroservice?: boolean;
appName?: string;
globalHeaders?: Record<string, string | (() => string)>;
rateLimit?: RateLimitConfig;
requestLogging?: RequestLoggingConfig;
healthCheck?: HealthCheckConfig;
requestTimeout?: number;
responseTime?: ResponseTimeConfig;
requestId?: RequestIdConfig;
globalPrefix?: string;
openApi?: OpenApiConfig;
metrics?: MetricsConfig;
serviceVersion?: ServiceVersionConfig;
https?: HttpsConfig;
skipHealthzChecksValidation?: boolean;
}
```

For detailed server configuration options, see the [Express Server documentation](server)
Loading