# Config API

Access MAJK configuration using path-based dot notation.

## Quick Start

```typescript
handler: async (input, ctx) => {
  // Get config value
  const region = ctx.majk.config.get('aws.region', 'us-east-1');

  // Check if config exists
  if (ctx.majk.config.has('bedrock.models.claude')) {
    const model = ctx.majk.config.get('bedrock.models.claude');
  }

  // Discover available keys
  const awsKeys = ctx.majk.config.keys('aws');

  return { region, awsKeys };
}
```

## API Reference

### get<T>(path, defaultValue?)

Get a configuration value by path.

```typescript
// Simple get
const region = ctx.majk.config.get('aws.region');

// With default value
const timeout = ctx.majk.config.get('plugins.timeout', 30000);

// Type-safe get
interface BedrockConfig {
  models: string[];
  region: string;
}
const bedrock = ctx.majk.config.get<BedrockConfig>('bedrock');

// Get entire section
const awsConfig = ctx.majk.config.get('aws');
```

### has(path)

Check if a configuration path exists.

```typescript
if (ctx.majk.config.has('aws.pluginMarket.apiEndpoint')) {
  // Plugin market is configured
}

// Feature detection
if (ctx.majk.config.has('experimental.newFeature')) {
  // Enable experimental feature
}
```

### keys(path?)

Get all keys under a path.

```typescript
// Top-level namespaces
const namespaces = ctx.majk.config.keys();
// ['aws', 'auth', 'bedrock', 'litellm', 'plugins']

// Keys under aws
const awsKeys = ctx.majk.config.keys('aws');
// ['pluginMarket', 'telemetry', 'region', ...]

// Nested keys
const marketKeys = ctx.majk.config.keys('aws.pluginMarket');
// ['apiEndpoint', 'region', 'enabled']
```

### getAll()

Get the entire configuration object.

```typescript
// For debugging only - prefer specific get() calls
const allConfig = ctx.majk.config.getAll();
console.log('Full config:', JSON.stringify(allConfig, null, 2));
```

## Configuration Namespaces

### aws.*

AWS resources and services.

```typescript
ctx.majk.config.get('aws.region');
ctx.majk.config.get('aws.pluginMarket.apiEndpoint');
ctx.majk.config.get('aws.telemetry.enabled');
ctx.majk.config.get('aws.tokenUsage.tableName');
```

### auth.*

Authentication configuration.

```typescript
ctx.majk.config.get('auth.cognito.userPoolId');
ctx.majk.config.get('auth.cognito.clientId');
ctx.majk.config.get('auth.cognito.region');
```

### bedrock.*

Bedrock AI model settings.

```typescript
ctx.majk.config.get('bedrock.region');
ctx.majk.config.get('bedrock.models');
ctx.majk.config.get('bedrock.defaultModel');
```

### litellm.*

LiteLLM proxy configuration.

```typescript
ctx.majk.config.get('litellm.apiBase');
ctx.majk.config.get('litellm.apiKey');
ctx.majk.config.get('litellm.models');
```

### plugins.*

Plugin system configuration.

```typescript
ctx.majk.config.get('plugins.directory');
ctx.majk.config.get('plugins.autoStart');
ctx.majk.config.get('plugins.hotReload');
```

## Common Patterns

### Safe Config Access

```typescript
function getAwsConfig() {
  const region = ctx.majk.config.get('aws.region');
  if (!region) {
    throw new Error('AWS region not configured');
  }

  return {
    region,
    endpoint: ctx.majk.config.get('aws.pluginMarket.apiEndpoint'),
    enabled: ctx.majk.config.get('aws.pluginMarket.enabled', false)
  };
}
```

### Feature Flags

```typescript
function isFeatureEnabled(feature: string): boolean {
  return ctx.majk.config.get(`features.${feature}`, false);
}

if (isFeatureEnabled('newUI')) {
  // Use new UI
}
```

### Dynamic Configuration

```typescript
// Build config object from keys
const awsKeys = ctx.majk.config.keys('aws');
const awsConfig = {};

for (const key of awsKeys) {
  awsConfig[key] = ctx.majk.config.get(`aws.${key}`);
}
```

### Environment-Specific Config

```typescript
const env = ctx.majk.config.get('environment', 'development');
const apiUrl = ctx.majk.config.get(`api.${env}.url`);
const timeout = ctx.majk.config.get(`api.${env}.timeout`, 5000);
```

## Best Practices

1. **Use defaults** - Always provide sensible defaults
2. **Check existence** - Use `has()` before optional configs
3. **Type assertions** - Use generics for type safety
4. **Avoid getAll()** - Use specific paths for better performance
5. **Consistent paths** - Follow dot notation conventions

## Next Steps

Run `npx @majkapp/plugin-kit --secrets` - Secrets management
Run `npx @majkapp/plugin-kit --auth` - Authentication API
