npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fluent-kafka-events

v0.1.1

Published

A TypeScript library for standardising building and publishing Kafka messages with a fluent API.

Readme

Fluent Kafka Events

npm version

A TypeScript library for standardising building and publishing Kafka messages with a fluent API.

Features

  • 🔀 Fluent Builder API: Chain methods to construct Kafka messages with a clean, readable syntax
  • 🔍 TypeScript Support: Full TypeScript definitions and generic payload typing
  • Validation: Optional JSON Schema validation of message payloads
  • 📦 Serialization: Support for custom serializers
  • 🔄 Kafka Integration: Ready-to-use Kafka producer service

Installation

Install from npm:

pnpm add fluent-kafka-events

Quick Start

import { MessageBuilder, KafkaService } from 'fluent-kafka-events';

// Define your message payload type
interface UserCreatedPayload {
  userId: string;
  email: string;
  createdAt: string;
}

// Create a message builder
const builder = new MessageBuilder<UserCreatedPayload>({
  pubId: 'user-service',
  // Optional topic resolver - converts actions to Kafka topics
  topicResolver: (action) => `${action.split('.')[0]}-events`
});

// Build a message
const message = builder
  .withContext({
    action: 'user.created',
    corrId: '123456789'
  })
  .withPayload({
    userId: 'usr_123',
    email: '[email protected]',
    createdAt: new Date().toISOString()
  })
  .build();

// Publish to Kafka
async function publishMessage() {
  const kafka = new KafkaService({
    brokerUrls: ['kafka:9092']
  });
  
  await kafka.connect();
  await kafka.publishBuiltMessage(message);
  await kafka.disconnect();
}

Message Builder API

Creating a Builder

// Simple builder
const builder = new MessageBuilder<YourPayloadType>({
  pubId: 'your-service-id'
});

// With topic resolver
const builder = new MessageBuilder<YourPayloadType>({
  pubId: 'your-service-id',
  topicResolver: (action) => `${action.split('.')[0]}-events`
});

// With schema validation
import { JSONSchemaType } from 'ajv';

const schema: JSONSchemaType<YourPayloadType> = {
  type: 'object',
  properties: {
    // Your schema here
  },
  required: ['id', 'name'],
  additionalProperties: false
};

const builder = new MessageBuilder<YourPayloadType>({
  pubId: 'your-service-id',
  validationSchema: schema
});

Builder Methods

withContext(context)

Sets the context information for the message.

builder.withContext({
  action: 'user.created',
  corrId: '123456789',
  // Any additional context fields
});

withMeta(meta)

Sets metadata for the message.

builder.withMeta({
  topic: 'user-events',
  source: 'user-api',
  version: '1.0'
});

withPayload(data)

Sets the payload data for the message.

builder.withPayload({
  userId: 'usr_123',
  email: '[email protected]'
});

set(path, value)

Sets a specific property in the payload using a path.

builder.set('userId', 'usr_123');
builder.set('user.address.city', 'New York');
builder.set('items[0].quantity', 5);

setIfDefined(path, value)

Sets a property only if the value is not undefined or null.

builder.setIfDefined('optionalField', maybeUndefined);

withSerializer(serializer)

Sets a custom serializer for the message.

builder.withSerializer({
  serialize: (data) => JSON.stringify(data),
  deserialize: (data) => JSON.parse(data)
});

validate()

Validates the current message against the schema if provided.

const errors = builder.validate();
if (errors) {
  console.error('Validation failed:', errors);
}

build()

Builds the complete Kafka message.

const message = builder.build();

reset()

Resets the builder to its initial state.

builder.reset();

Kafka Service API

Creating a Service

const kafkaService = new KafkaService({
  brokerUrls: ['kafka:9092'],
  clientId: 'your-client-id',
  connectionTimeout: 30000,
  retry: {
    initialRetryMS: 300,
    maxRetryMS: 30000,
    retries: 5
  }
});

Methods

connect()

Connects to Kafka and creates a producer.

await kafkaService.connect();

disconnect()

Disconnects from Kafka.

await kafkaService.disconnect();

publish(topic, message, settings)

Publishes a message to a specific Kafka topic.

await kafkaService.publish('user-events', message, {
  partition: 0,
  key: 'user-123'
});

publishBuiltMessage(message, settings)

Publishes a message built from the MessageBuilder using the topic from the message's meta.

await kafkaService.publishBuiltMessage(message);

Schema Validation

The library provides JSON Schema validation using Ajv:

import { createValidator } from 'fluent-kafka-events';
import { JSONSchemaType } from 'ajv';

// Define your schema
const userSchema: JSONSchemaType<UserPayload> = {
  type: 'object',
  properties: {
    userId: { type: 'string' },
    email: { type: 'string', format: 'email' },
    age: { type: 'number', minimum: 18 }
  },
  required: ['userId', 'email'],
  additionalProperties: false
};

// Create a validator
const validator = createValidator(userSchema);

// Validate data
const errors = validator(data);
if (errors) {
  console.error('Validation failed:', errors);
}

License

MIT