Skip to content

covenix / Sse

Function: Sse()

Sse(schema?, options?): MethodDecorator

Defined in: src/decorators.ts:271

Declares the route as a Server-Sent Events (text/event-stream) stream. The handler returns an AsyncIterable of events (typically an async generator); covenix sets the SSE headers, frames each yielded value as an event, and on client disconnect calls the iterator's return() so the generator's finally runs (cleanup/abort). Yield a plain value to emit a data: frame, or a import('./sse.js').SseEvent to set event/id/retry.

Parameters

schema?

ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

Zod schema each event's data is validated against (and emitted in the OpenAPI text/event-stream response); omit for raw/string events.

options?

SseOptions = {}

Stream options (status, keepAlive).

Returns

MethodDecorator

Example

ts
@Get('chat/{id}/stream')
@Sse(TokenSchema, { keepAlive: 15000 })
async *stream(@Param('id') id: string): AsyncGenerator<Token> {
  try { for await (const t of llm.stream(id)) yield t; }
  finally { /* runs on disconnect */ }
}