<p align="center">
  <img src="https://raw.githubusercontent.com/Syntropysoft/syntropylog-examples-/main/assets/syntropyLog-logo.png" alt="SyntropyLog Logo" width="170"/>
</p>

# @syntropylog/adapters

External adapters for SyntropyLog framework - Brokers, HTTP clients, and Database Serializers.

<p align="center">
  <a href="https://www.npmjs.com/package/@syntropylog/adapters"><img src="https://img.shields.io/npm/v/@syntropylog/adapters.svg" alt="NPM Version"></a>
  <a href="https://github.com/Syntropysoft/SyntropyLog/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@syntropylog/adapters.svg" alt="License"></a>
  <a href="#"><img src="https://img.shields.io/badge/coverage-60.48%25-brightgreen" alt="Test Coverage"></a>
</p>

> ## 🚀 Version 0.1.24 - Refactored & Tested Adapters 🚀
>
> **@syntropylog/adapters core adapters are production ready with field-tested implementations.**
>
> **What we have (verified working):**
> - ✅ **Brokers**: Kafka, NATS, RabbitMQ - All working in production examples
> - ✅ **HTTP Clients**: Axios, Fetch - Both working and tested
> - ✅ **Integration**: All adapters work seamlessly with SyntropyLog framework
>
> **What we're working on:**
> - 🚧 **Database Serializers**: MongoDB, MySQL, Oracle, etc. - Coming soon
> - 🚧 **Advanced Examples**: NestJS, GraphQL integrations - In development
>
> **Latest fixes:**
> - ✅ **Cleaned dependencies**: Removed unused `got` dependency (now using `axios` and `fetch`)
> - ✅ **Added comprehensive test coverage**: Created tests for NatsAdapter and RabbitMQAdapter
> - ✅ **Refactored all broker adapters**: Extracted common serialization logic into `PayloadSerializer` utility
> - ✅ **KafkaAdapter**: Fixed buffer serialization issue - payloads now display as readable JSON instead of Buffer objects
> - ✅ **NatsAdapter**: Fixed buffer serialization issue - payloads now display as readable JSON instead of Buffer objects
> - ✅ **NatsAdapter**: Improved JSON codec handling with intelligent serialization/deserialization
> - ✅ **NatsAdapter**: Fixed headers iteration and added proper subscription cleanup
> - ✅ **RabbitMQAdapter**: Fixed buffer serialization issue - payloads now display as readable JSON instead of Buffer objects
> - ✅ **RabbitMQAdapter**: Fixed exchange durability and consumer cancellation issues
> - ✅ **Improved stability**: Proper cleanup prevents hanging processes

## 🚀 Installation

```bash
npm install @syntropylog/adapters
```

## 📦 Usage

### Import Everything
```typescript
import { 
  KafkaAdapter, 
  PrismaSerializer, 
  AxiosAdapter 
} from '@syntropylog/adapters';
```

### Import by Category (Recommended for tree-shaking)

#### Brokers Only
```typescript
import { KafkaAdapter, NatsAdapter, RabbitMQAdapter } from '@syntropylog/adapters/brokers';
```

#### HTTP Clients Only
```typescript
import { AxiosAdapter, FetchAdapter } from '@syntropylog/adapters/http';
```

#### Serializers Only
```typescript
// Coming soon - Database serializers are in development
// import { 
//   PrismaSerializer, 
//   TypeORMSerializer, 
//   MySQLSerializer,
//   PostgreSQLSerializer,
//   SQLServerSerializer,
//   OracleSerializer,
//   MongoDBSerializer
// } from '@syntropylog/adapters/serializers';
```

#### Types Only
```typescript
import type { ISerializer, SerializationContext, SerializationResult } from '@syntropylog/adapters/types';
```

## 🔧 Available Adapters

### Implementation Status
- ✅ **Tested** - Fully implemented with comprehensive test coverage
- ✅ **Production Ready** - Field-tested and working in production examples
- 🚧 **In Development** - Currently being implemented
- 🚧 **Planned** - Not yet implemented

### Brokers
- **KafkaAdapter** - Apache Kafka integration ✅ **Tested**
- **NatsAdapter** - NATS messaging system ✅ **Production Ready**
  - ✅ **Fixed**: Buffer serialization issue - payloads now display as readable JSON
  - ✅ **Improved**: JSON codec handling with intelligent serialization/deserialization
  - ✅ **Tested**: Working in example 24 (full-stack microservices)
- **RabbitMQAdapter** - RabbitMQ message broker ✅ **Production Ready**

### HTTP Clients
- **AxiosAdapter** - Axios HTTP client ✅ **Tested**
- **FetchAdapter** - Native fetch API ✅ **Production Ready**

### Database Serializers
- **PrismaSerializer** - Prisma ORM queries and errors 🚧 **In Development**
- **TypeORMSerializer** - TypeORM queries and errors 🚧 **In Development**
- **MySQLSerializer** - MySQL queries and errors 🚧 **In Development**
- **PostgreSQLSerializer** - PostgreSQL queries and errors 🚧 **In Development**
- **SQLServerSerializer** - SQL Server queries and errors 🚧 **In Development**
- **OracleSerializer** - Oracle Database queries and errors 🚧 **In Development**
- **MongoDBSerializer** - MongoDB queries and errors 🚧 **In Development**

## 🎯 Quick Examples

### Using Brokers
```typescript
import { KafkaAdapter } from '@syntropylog/adapters/brokers';

const kafkaAdapter = new KafkaAdapter({
  clientId: 'my-app',
  brokers: ['localhost:9092']
});

await kafkaAdapter.connect();
await kafkaAdapter.publish('my-topic', { message: 'Hello World' });
```

### Using HTTP Clients
```typescript
import { AxiosAdapter } from '@syntropylog/adapters/http';
import axios from 'axios';

const axiosAdapter = new AxiosAdapter(axios);
const response = await axiosAdapter.request({
  url: 'https://api.example.com/users',
  method: 'GET'
});
```

### Using Serializers
```typescript
import { PrismaSerializer } from '@syntropylog/adapters/serializers';

const prismaSerializer = new PrismaSerializer();
const result = await prismaSerializer.serialize(prismaQuery, {
  sanitize: true,
  sensitiveFields: ['password', 'token']
});
```

## 🔄 Helper Functions

### Register All Serializers
```typescript
import { registerAllSerializers } from '@syntropylog/adapters/serializers';

// Register all serializers with a manager
registerAllSerializers(serializationManager);
```

### Get All Serializers
```typescript
import { getAllSerializers } from '@syntropylog/adapters/serializers';

const allSerializers = getAllSerializers();
// Returns array of all serializer instances
```

## ⚡ Performance Features

### Ultra-Fast Serialization
All serializers are optimized for **ultra-fast** object translation:
- **Timeout**: Configurable via context (default: 50ms)
- **Complexity Assessment**: Automatic complexity detection (low/medium/high)
- **Error Handling**: Graceful fallback for unknown data types

### Example with Timeout Configuration
```typescript
import { PrismaSerializer } from '@syntropylog/adapters/serializers';

const serializer = new PrismaSerializer();
const result = await serializer.serialize(prismaQuery, {
  timeout: 100, // Custom timeout
  sanitize: true,
  sensitiveFields: ['password']
});
```

## 📋 Requirements

- Node.js >= 18
- TypeScript >= 5.0
- SyntropyLog >= 0.6.4-alpha.0

## 🧪 Testing

```bash
# Run all tests
npm test

# Run tests with coverage
npm run test:coverage

# Run specific test categories
npm test -- tests/serializers/
npm test -- tests/brokers/
npm test -- tests/http/
```

## 📊 Test Coverage

Current test coverage: **60.48%**

- ✅ **Serializers**: All 6 serializers with comprehensive unit tests
- ✅ **KafkaAdapter**: Complete unit tests
- ✅ **AxiosAdapter**: Complete unit tests
- 🔄 **Other adapters**: Unit tests pending

## 🔗 Dependencies

### Runtime Dependencies
- `@syntropylog/types` ^0.1.5
- `axios` ^1.10.0
- `kafkajs` ^2.2.4
- `nats` ^2.17.0
- `amqplib` ^0.10.8

## 🏗️ Architecture

### Single Responsibility Principle
Each adapter focuses on a single responsibility:
- **Serializers**: Only translate/interpret data (no timeouts, no connections)
- **Brokers**: Only adapt messaging protocols
- **HTTP**: Only adapt HTTP client libraries

### Configurable Timeouts
Timeouts are managed by the main SyntropyLog framework, not by individual adapters:
```typescript
// ✅ Correct: Timeout from context
const result = await serializer.serialize(data, { timeout: 100 });

// ❌ Wrong: No hardcoded timeouts in adapters
// All adapters respect the timeout from SerializationContext
```

## 📄 License

Apache-2.0 - see [LICENSE](LICENSE) file for details.

## 🚀 Status

### ✅ Ready for Production
- **Brokers**: Kafka, NATS, RabbitMQ - All field-tested and working
- **HTTP Clients**: Axios, Fetch - Both tested and working
- **Architecture**: Clean separation of concerns with configurable timeouts

### 🚧 In Development
- **Database Serializers**: MongoDB, MySQL, Oracle, etc. - Coming soon
- **Advanced Examples**: NestJS, GraphQL integrations - In development

### 📊 Test Coverage
- **KafkaAdapter**: ✅ Comprehensive unit tests
- **AxiosAdapter**: ✅ Comprehensive unit tests  
- **Other Adapters**: ✅ Field-tested in production examples (no complex unit tests needed)

## 🔍 Transparency Note

We believe in complete transparency about what we have and what we're building:

**What we have (verified working):**
- ✅ All core adapters (Kafka, NATS, RabbitMQ, Axios, Fetch) are production-ready
- ✅ All adapters work seamlessly with SyntropyLog framework
- ✅ Comprehensive examples (00-22) demonstrating real-world usage
- ✅ Field-tested implementations that work in production environments

**What we're working on:**
- 🚧 Database serializers for various ORMs and databases
- 🚧 Advanced framework integrations (NestJS, GraphQL)
- 🚧 Additional HTTP client adapters if needed

**Why no complex unit tests for some adapters?**
We focus on field testing and integration tests rather than complex mocks that don't reflect real-world usage. Our examples (00-22) serve as comprehensive integration tests.

## 🤝 Contributing

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for contribution guidelines. 