@socketdocs/nestjs
v0.3.0
Published
NestJS adapter for SocketDocs, providing decorators for WebSocket documentation.
Maintainers
Readme
@socketdocs/nestjs
The NestJS adapter for SocketDocs. Bring typed, validated, and documented WebSockets to your NestJS applications.
📦 Installation
npm install @socketdocs/nestjs @socketdocs/core @nestjs/websockets @nestjs/platform-socket.io zod🚀 Getting Started
- Define your contract:
import { createContract } from '@socketdocs/core';
import { z } from 'zod';
export const contract = createContract({ name: 'NestJS API', version: '0.2.0' });
contract.namespace('default').event({
name: 'message',
direction: 'client_to_server',
payload: z.object({ text: z.string() })
});- Attach the adapter in your main file:
import { NestFactory } from '@nestjs/core';
import { bindNestjsAdapter } from '@socketdocs/nestjs';
import { contract } from './contract';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Handlers for the events defined in the contract
const handlers = {
'default': {
'message': async ({ payload, client }: any) => {
console.log(`NestJS received: ${payload.text}`);
}
}
};
bindNestjsAdapter(app, contract, handlers);
await app.listen(3000);
}
bootstrap();🛠️ Features
- NestJS Architecture Support: Integrates with NestJS's dependency injection and gateway system.
- Validation: Automatic validation of incoming payloads using Zod before they reach your logic.
- Unified Handlers: Use the
handlersmap to keep your WebSocket logic clean and documented. - Dependency Injection: Seamlessly work alongside your existing NestJS providers and services.
⚖️ License
MIT
