# KafkaRequestDeserializer

**Kind:** Class

**Source:** [`packages/microservices/deserializers/kafka-request.deserializer.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/deserializers/kafka-request.deserializer.ts#L8)

**Part of:** [Microservices](subsystem-packages-microservices)

`KafkaRequestDeserializer` converts raw Kafka request records into the internal `IncomingRequest` or `IncomingEvent` schema used by the microservices layer. It extracts the Kafka message key as the request pattern and forwards the message value as the payload, allowing downstream handlers to process Kafka messages consistently with other transports.

**Extends:** `IncomingRequestDeserializer`

## Methods

| Method | Signature | Returns |
|---|---|---|
| `mapToSchema` | `mapToSchema(data: KafkaRequest, options: Record<string, any>)` | `IncomingRequest | IncomingEvent` |

## Where it refuses work

- `KafkaRequestDeserializer` stops the work with an early return when `!options`.

## Diagram

```mermaid
graph LR
  A[Kafka broker message] --> B[KafkaRequestDeserializer]
  B --> C{Message has a value?}
  C -->|Yes| D[IncomingRequest / IncomingEvent]
  D --> E[pattern: Kafka key]
  D --> F[data: Kafka value]
  C -->|No| G[Return original data]
```

## Usage

```ts
import { KafkaRequestDeserializer } from '@nestjs/microservices/deserializers/kafka-request.deserializer';

const deserializer = new KafkaRequestDeserializer();

const kafkaMessage = {
  key: Buffer.from('orders.create'),
  value: {
    orderId: 'order-123',
    customerId: 'customer-456',
  },
};

const incomingMessage = deserializer.mapToSchema(kafkaMessage);

console.log(incomingMessage);
// {
//   pattern: 'orders.create',
//   data: {
//     orderId: 'order-123',
//     customerId: 'customer-456'
//   }
// }
```

## AI Coding Instructions

- Preserve the Kafka convention: use the message key as the handler pattern and the message value as the payload.
- Handle malformed or value-less Kafka records defensively; the deserializer should not assume every input has a `value` property.
- Keep output compatible with the shared `IncomingRequest` and `IncomingEvent` contracts used by microservice transport handlers.
- When extending Kafka message handling, avoid mutating the original Kafka record or its payload.
- Configure this deserializer at the Kafka transport boundary so application handlers receive normalized messages.
