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
91 lines
No EOL
2.6 KiB
TypeScript
91 lines
No EOL
2.6 KiB
TypeScript
#!/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!') |