A lightweight, type-safe event routing library for TypeScript applications with built-in validation support.
- π― Type-safe event routing - Full TypeScript support with type inference
- π Validation support - Built-in integration with TypeBox and Zod
- π¨ Flexible API - Intuitive builder pattern for configuration
- π Modular design - Use only what you need with separate validation packages
- π³ Tree-shakeable - Import only the features you use
- π¦ Zero dependencies - Core package has no external dependencies
# Using npm
npm install @eventix/core
# Optional validation packages
npm install @eventix/validation @sinclair/typebox
# or
npm install @eventix/validation zodimport { EventRouter } from '@eventix/core';
const router = new EventRouter()
.on('message', (ctx) => {
console.log(`Received message: ${ctx.payload.text}`);
});
router.handleMessage({
type: 'message',
payload: { text: 'Hello, World!' }
});import { EventRouter } from '@eventix/core';
import { fromTypebox } from '@eventix/validation/typebox';
import { Type } from '@sinclair/typebox';
const messageSchema = Type.Object({
text: Type.String(),
userId: Type.String(),
timestamp: Type.Number()
});
const router = new EventRouter()
.on('message', (ctx) => {
console.log(`[${ctx.payload.userId}]: ${ctx.payload.text}`);
}, {
schema: fromTypebox(messageSchema)
});import { EventRouter } from '@eventix/core';
import { fromZod } from '@eventix/validation/zod';
import { z } from 'zod';
const messageSchema = z.object({
text: z.string(),
userId: z.string(),
timestamp: z.number()
});
const router = new EventRouter()
.on('message', (ctx) => {
console.log(`[${ctx.payload.userId}]: ${ctx.payload.text}`);
}, {
schema: fromZod(messageSchema)
});const chatRouter = new EventRouter({ prefix: 'chat:' })
.on('message', handler) // Will handle 'chat:message'
.on('typing', handler); // Will handle 'chat:typing'router.on('connect', handler, { once: true });const mainRouter = new EventRouter()
.use(chatRouter)
.use(userRouter);import { z } from 'zod';
import { fromZod } from '@eventix/validation/zod';
const messageSchema = z.object({
userId: z.string(),
});
const router = new EventRouter()
.on('user:login', (ctx) => {
// ctx.payload is typed as { userId: string }
}, { schema: fromZod(messageSchema) });| Package | Description |
|---|---|
@eventix/core |
Core event routing functionality |
@eventix/validation |
Validation adapters for TypeBox and Zod |
new EventRouter({
prefix?: string; // Optional prefix for all routes
}).on(event, handler, options?)- Register an event handler.use(router)- Compose with another router.handleMessage(message)- Handle an incoming message.outgoingEvents<T>()- Define outgoing event types
fromTypebox(schema: TSchema): ValidatorfromZod(schema: ZodType): ValidatorCheck out the examples folder for more detailed usage:
Contributions are welcome! Please feel free to submit a Pull Request.
MIT