# @ahmetshbz/zap

🚀 **Enterprise-grade HTTP Client** - Production-ready, type-safe, universal HTTP client for modern JavaScript/TypeScript applications.

## Features

### 🔒 Security

- **Request Signing** - AWS Signature v4 style request signing
- **OAuth 2.0 / OIDC** - Token management with auto-refresh and PKCE support
- **TLS/SSL** - Certificate pinning and validation
- **SSRF Protection** - Prevent Server-Side Request Forgery attacks
- **Secrets Management** - Secure credential storage with encryption

### ⚡ Performance

- **Multi-layer Caching** - L1 (memory) + L2 (Redis) with LRU/LFU/ARC algorithms
- **Request Deduplication** - Coalesce identical concurrent requests
- **Compression** - Automatic gzip, brotli, deflate support
- **Streaming** - Upload/download streaming with progress tracking
- **Resume Support** - Resume interrupted downloads

### 🛡️ Resilience

- **Advanced Retry** - Exponential backoff with decorrelated jitter
- **Circuit Breaker** - Prevent cascading failures
- **Rate Limiting** - Token bucket, sliding window algorithms
- **Timeout Management** - Per-request and global timeouts
- **Bulkhead Pattern** - Isolate failures

## Installation

```bash
bun add @ahmetshbz/zap
```

## Quick Start

```typescript
import { HttpClient } from '@ahmetshbz/zap';

const client = new HttpClient({
  baseURL: 'https://api.example.com',
  timeout: 5000,
});

// Simple requests
const data = await client.get('/users/1');
const created = await client.post('/users', { name: 'John' });
```

## Configuration Examples

### Security

```typescript
const client = new HttpClient({
  baseURL: 'https://api.example.com',
  securityConfig: {
    signing: {
      enabled: true,
      algorithm: 'AWS4-HMAC-SHA256',
      accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
    },
    oauth: {
      enabled: true,
      clientId: process.env.OAUTH_CLIENT_ID!,
      clientSecret: process.env.OAUTH_CLIENT_SECRET!,
      tokenUrl: 'https://auth.example.com/token',
    },
  },
});
```

### Performance

```typescript
const client = new HttpClient({
  cacheConfig: {
    enabled: true,
    algorithm: 'lru',
    maxSize: 1000,
    defaultTtl: 60000,
  },
  compressionConfig: {
    enabled: true,
    preferredAlgorithms: ['br', 'gzip'],
  },
  streamConfig: {
    enableUploadProgress: true,
    enableDownloadProgress: true,
  },
});
```

### Resilience

```typescript
const client = new HttpClient({
  resilienceConfig: {
    retry: {
      enabled: true,
      maxAttempts: 3,
      jitterType: 'decorrelated',
    },
    circuitBreaker: {
      enabled: true,
      failureThreshold: 5,
    },
    rateLimit: {
      enabled: true,
      maxRequests: 100,
      windowMs: 60000,
    },
  },
});
```

## Advanced Features

### Progress Tracking

```typescript
await client.post('/upload', fileData, {
  onUploadProgress: (progress) => {
    console.log(`${progress.percentage}% - ${progress.rate} B/s`);
  },
});
```

### Interceptors

```typescript
const client = new HttpClient({
  onRequest: async (ctx) => {
    ctx.options.headers = {
      ...ctx.options.headers,
      Authorization: `Bearer ${await getToken()}`,
    };
  },
  onResponse: async (ctx) => {
    return { ...ctx.response._data, timestamp: Date.now() };
  },
});
```

### Cache Management

```typescript
import { MultiLayerCache } from '@ahmetshbz/zap';

const cache = new MultiLayerCache(l1Config, l2Config);
await cache.set('key', data, { ttl: 60000, tags: ['users'] });
await cache.invalidateByTags(['users']);
```

## TypeScript

Fully typed with TypeScript 5.0+:

```typescript
interface User {
  id: number;
  name: string;
}

const user = await client.get<User>('/users/1');
// user is typed as User
```

## Runtime Support

✅ Node.js 18+ | ✅ Bun | ✅ Deno | ✅ Browser | ✅ Edge Runtime

## License

MIT
