# @yuno-payments/sdk-event-log

SDK library for registering application logs, events, and metrics from Yuno's frontend SDKs.

## Installation

```bash
npm install @yuno-payments/sdk-event-log
```

**Peer dependency:** `axios ^1.13.6`

## Usage

```typescript
import { EventLog } from '@yuno-payments/sdk-event-log'

const eventLog = new EventLog({
  organizationName: 'my-org',
  publicApiKey: 'your-public-api-key',
  enableEvents: true,
  enableLogs: true,
  enableCron: true,
  batchTime: 5000,
})
```

### Events

```typescript
eventLog.event({
  source: 'checkout',
  event: 'payment_started',
  description: 'User started payment flow',
  original_created_at: new Date().toISOString(),
  sdk_version: '1.0.0',
  application_name: 'checkout-sdk',
  application_session: 'session-123',
  environment: 'production',
})
```

### Logs

```typescript
eventLog.logger.info({
  original_created_at: new Date().toISOString(),
  sdk_version: '1.0.0',
  application_name: 'checkout-sdk',
  application_session: 'session-123',
  environment: 'production',
  step_name: 'payment',
  step_function: 'submitPayment',
  step_location: 'checkout.ts',
})

eventLog.logger.error({ /* same shape */ })
eventLog.logger.debug({ /* same shape */ })
```

### Metrics

```typescript
eventLog.setMetric({
  environment: 'production',
  dynamic_sdk: false,
  original_created_at: new Date().toISOString(),
  original_created_at_ms: Date.now(),
  flow_trace_id: 'trace-abc',
  sdk_version: '1.0.0',
  type: 'performance',
  metadata: {
    public_api_key: 'your-key',
  },
  payload: { loadTime: 1200 },
})
```

### Manual batch send

```typescript
await eventLog.sendBatch()        // Send queued logs/events
await eventLog.sendMetricBatch()  // Send queued metrics
```

### Runtime configuration

```typescript
eventLog.setConfig({
  enableEvents: true,
  enableLogs: false,
  enableCron: true,
  batchTime: 10000,
  maxBatchQueued: 50,
})
```

## Constructor options

| Option | Type | Default | Description |
|---|---|---|---|
| `organizationName` | `string` | **required** | Organization identifier |
| `publicApiKey` | `string` | **required** | Public API key for authentication |
| `enableEvents` | `boolean` | `true` | Enable event tracking |
| `enableLogs` | `boolean` | `true` | Enable log tracking |
| `enableCron` | `boolean` | `undefined` | Enable automatic batch sending |
| `batchTime` | `number` | `undefined` | Interval (ms) for cron batch sends |
| `maxBatchQueued` | `number` | `30` | Max items before auto-flush |
| `platform` | `string` | `'Web'` | Platform identifier |
| `language` | `string` | `undefined` | Accept-Language header value |
| `country` | `string` | `undefined` | Country code |
| `xVersion` | `string` | `undefined` | SDK version header |
| `workflow` | `string` | `'SDK_CHECKOUT'` | Workflow identifier |
| `cookieName` | `string` | `'yuno'` | Cookie name for device ID |
| `deviceId` | `string` | auto-generated | Override device ID |
| `clientAppDomain` | `string` | `undefined` | Override client app domain |
| `enableMetrics` | `boolean` | `false` | Enable metrics collection |
| `debug` | `boolean` | `false` | Enable console debug output |

## Storage

Events are queued locally before being sent in batches:

- **Browser**: Uses `localStorage` (key: `YUNO_EVENT_LOGS_{orgName}`)
- **Non-browser**: Uses in-memory storage

Both storages cap at 200 items to prevent unbounded growth.

## Development

```bash
npm test          # Run tests
npm run test:dev  # Run tests in watch mode
npm run build     # Test + clean + build (CJS + ESM)
```

### Local testing with npm link

```bash
# In this repo
npm run build && npm link

# In the consuming project
npm link @yuno-payments/sdk-event-log
```
