# @edirect/audit-exporter-http-axios

HTTP exporter for the eDirect Audit system using Axios. Implements the `Exporter` interface from `@edirect/audit-domain` and sends audit events to the audit service via HTTP POST.

## Features

- Implements the `Exporter` interface (`export` and `bulkExport`)
- Per-request token override support (for multi-tenant auth)
- Bearer token sanitization (accepts both `Bearer <token>` and raw token formats)
- Bulk export support for batching multiple audit events in a single request
- Generated Axios API client for type-safe communication

## Installation

```sh
pnpm add @edirect/audit-exporter-http-axios
# or
npm install @edirect/audit-exporter-http-axios
```

## Usage

### Standalone

```ts
import { HttpAxiosExporter } from '@edirect/audit-exporter-http-axios';
import { AuditRequest } from '@edirect/audit-domain';

const exporter = new HttpAxiosExporter({
  basePath: 'https://audit.internal.example.com',
  accessToken: process.env.AUDIT_ACCESS_TOKEN, // optional: common token for all requests
});

// Export a single event
const event: AuditRequest = {
  tenant: 'th-broker',
  service: 'policy-issuing-service',
  domain: 'policy',
  originType: 'HTTP',
  actorType: 'AGENT',
  actorGroup: 'th-broker-agents',
  actionCategory: 'DATA_MODIFICATION_EVENT',
  actionKey: 'CREATE',
  targetKey: 'Policy',
  actionDetail: 'Agent created a motor policy',
  resultKey: 'SUCCESS',
  eventTimestamp: new Date(),
};

await exporter.export(event);

// Or bulk export
await exporter.bulkExport([event1, event2, event3]);
```

### With `@edirect/audit-nestjs` (most common usage)

```ts
import { AuditModule } from '@edirect/audit-nestjs';
import { HttpAxiosExporter } from '@edirect/audit-exporter-http-axios';

AuditModule.register({
  // The exporter factory is called once per request
  // The request object allows you to pass the current user's token
  exporter: request =>
    new HttpAxiosExporter({
      basePath: 'https://audit.internal.example.com',
      accessToken: request.headers.authorization,
    }),
  service: 'my-service',
  domain: 'policy',
  tenant: 'th-broker',
});
```

## API

### `HttpAxiosExporter`

#### `constructor(config: HttpAxiosExporterConfig)`

| Property      | Type     | Required | Description                                                       |
| ------------- | -------- | -------- | ----------------------------------------------------------------- |
| `basePath`    | `string` | Yes      | Base URL of the audit service (e.g., `https://audit.example.com`) |
| `accessToken` | `string` | No       | Default Bearer token for all requests                             |

#### `export(data: AuditRequest, accessToken?: string): Promise<void>`

Sends a single audit event. The optional `accessToken` overrides the constructor-level token for this call.

#### `bulkExport(data: AuditRequest[], accessToken?: string): Promise<void>`

Sends multiple audit events in a single request. The optional `accessToken` overrides the constructor-level token.

> **Note:** `export(data)` is implemented as `bulkExport([data])` internally.

## Token Handling

The exporter accepts tokens in both formats:

- `'Bearer eyJ...'` → stripped to `'eyJ...'` automatically
- `'eyJ...'` → used as-is
