TypeFetched/tests/config-test.ts
Casey Collier b85b9a63e2 Initial commit: TypedFetch - Zero-dependency, type-safe HTTP client
Features:
- Zero configuration, just works out of the box
- Runtime type inference and validation
- Built-in caching with W-TinyLFU algorithm
- Automatic retries with exponential backoff
- Circuit breaker for resilience
- Request deduplication
- Offline support with queue
- OpenAPI schema discovery
- Full TypeScript support with type descriptors
- Modular architecture
- Configurable for advanced use cases

Built with bun, ready for npm publishing
2025-07-20 12:35:43 -04:00

91 lines
No EOL
2.6 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bun
/**
* Configuration System Test
* Verifies that TypedFetch works with zero-config and custom configurations
*/
import { tf, createTypedFetch } from '../src/index.js'
console.log('🧪 Testing TypedFetch Configuration System\n')
// Test 1: Zero-config (should work out of the box)
console.log('1⃣ Zero-Config Test')
try {
const response = await tf.get('https://api.github.com/users/github')
console.log('✅ Zero-config works! Got user:', response.data.name)
} catch (error) {
console.error('❌ Zero-config failed:', error)
}
// Test 2: Global configuration
console.log('\n2⃣ Global Configuration Test')
tf.configure({
cache: { maxSize: 1000, ttl: 60000 }, // 1 minute cache
retry: { maxAttempts: 5 },
debug: { verbose: true }
})
console.log('✅ Global configuration applied')
// Test 3: Per-instance configuration
console.log('\n3⃣ Per-Instance Configuration Test')
const customClient = tf.create({
retry: { maxAttempts: 1 }, // No retries
cache: { enabled: false }, // No caching
request: {
timeout: 5000, // 5 second timeout
headers: { 'X-Custom-Header': 'test' }
}
})
console.log('✅ Custom instance created with specific config')
// Test 4: Verify configurations are independent
console.log('\n4⃣ Configuration Independence Test')
const metrics1 = tf.getMetrics()
const metrics2 = customClient.getMetrics()
console.log('✅ Main instance metrics:', metrics1)
console.log('✅ Custom instance metrics:', metrics2)
// Test 5: Test error handling with context
console.log('\n5⃣ Enhanced Error Context Test')
try {
await tf.get('https://httpstat.us/404')
} catch (error: any) {
console.log('✅ Error with context:', error.message)
console.log(' - URL:', error.url)
console.log(' - Method:', error.method)
console.log(' - Status:', error.status)
console.log(' - Suggestions:', error.suggestions)
}
// Test 6: Test configuration validation
console.log('\n6⃣ Configuration Options Test')
const testClient = createTypedFetch({
cache: { maxSize: 10, ttl: 1000 },
retry: {
maxAttempts: 2,
delays: [50, 100],
retryableStatuses: [500, 503]
},
circuit: {
threshold: 3,
timeout: 10000,
enabled: true
},
request: {
timeout: 15000,
baseURL: 'https://api.github.com'
},
metrics: { enabled: true },
debug: { verbose: false, logErrors: true }
})
// Test with base URL
const user = await testClient.get('/users/torvalds')
console.log('✅ Base URL works! Got user:', user.data.name)
// Test metrics
const finalMetrics = testClient.getMetrics()
console.log('✅ Metrics collected:', finalMetrics)
console.log('\n✨ All configuration tests passed!')