UNPKG

@nestjsvn/swagger-sse

Version:

OpenAPI documentation and interactive Swagger UI for NestJS Server-Sent Events endpoints

87 lines (86 loc) 2.6 kB
import { Type } from '@nestjs/common'; import { ApiSseOptions } from '../decorators/api-sse.options'; /** * OpenAPI schema generation for SSE endpoints * * This module handles the creation of OpenAPI schemas for Server-Sent Events endpoints, * including proper oneOf/discriminator patterns for different event types. */ /** * Schema path reference interface */ export interface SchemaReference { $ref: string; } /** * OpenAPI schema object */ export interface OpenAPISchema { type?: string; oneOf?: OpenAPISchema[]; discriminator?: { propertyName: string; mapping: Record<string, string>; }; properties?: Record<string, OpenAPISchema>; const?: string; description?: string; [key: string]: any; } /** * Generate OpenAPI schema for SSE endpoint * * Creates a schema using oneOf/discriminator pattern to represent * different event types that can be streamed from the endpoint. * * @param options ApiSse decorator options * @returns OpenAPI schema object * * @example * ```typescript * const schema = generateSseSchema({ * events: { * 'user-created': UserCreatedDto, * 'user-updated': UserUpdatedDto * } * }); * // Returns oneOf schema with discriminator * ``` */ export declare function generateSseSchema(options: ApiSseOptions): OpenAPISchema; /** * Generate discriminator mapping for event types * * @param events Event type mapping * @returns Discriminator mapping object */ export declare function generateDiscriminatorMapping(events: Record<string, any>): Record<string, string>; /** * Get schema path for a given type * * @param type Type constructor or string * @returns Schema reference path */ export declare function getSchemaPath(type: string | Type<unknown> | (new (...args: any[]) => any) | [new (...args: any[]) => any]): string; /** * Generate a unique schema name for an event type * * @param eventName Name of the event * @param dtoClass DTO class constructor * @returns Unique schema name */ export declare function getEventSchemaName(eventName: string, _dtoClass: any): string; /** * Validate that all event DTOs are properly decorated with @nestjs/swagger decorators * * @param events Event name to DTO class mapping * @throws Error if any DTO is not properly decorated */ export declare function validateEventDtos(events: Record<string, any>): void; /** * Register SSE event schemas in OpenAPI document * * @param document OpenAPI document * @param options ApiSse options */ export declare function registerSseSchemas(document: any, options: ApiSseOptions): void;