1+ 'use strict'
2+
13import { expectType , expectError , expectAssignable } from 'tsd'
2- import fastify , { FastifyInstance , FastifyReply , RouteShorthandOptions } from 'fastify'
4+ import fastify , { FastifyInstance , RouteShorthandOptions } from 'fastify'
35import fastifySSE , { SSEPluginOptions , SSEMessage , SSESource , SSEReplyInterface } from '..'
46import { Readable } from 'stream'
57
@@ -63,17 +65,26 @@ expectType<string | undefined>(message.id)
6365expectType < string | undefined > ( message . event )
6466expectType < any > ( message . data )
6567expectType < number | undefined > ( message . retry )
68+ expectAssignable < SSEMessage > ( fullMessage )
6669
6770// Test SSE sources
6871const stringSource : SSESource = 'hello'
72+ expectAssignable < SSESource > ( stringSource )
73+
6974const bufferSource : SSESource = Buffer . from ( 'hello' )
75+ expectAssignable < SSESource > ( bufferSource )
76+
7077const messageSource : SSESource = { data : 'test' }
78+ expectAssignable < SSESource > ( messageSource )
79+
7180const streamSource : SSESource = new Readable ( )
81+ expectAssignable < SSESource > ( streamSource )
7282
73- async function * asyncGenerator ( ) : AsyncIterable < SSEMessage > {
83+ async function * asyncGenerator ( ) : AsyncIterable < SSEMessage > {
7484 yield { data : 'test' }
7585}
7686const asyncIterableSource : SSESource = asyncGenerator ( )
87+ expectAssignable < SSESource > ( asyncIterableSource )
7788
7889// Test SSE reply interface
7990app . get ( '/test-reply' , { sse : true } , async ( request , reply ) => {
@@ -130,11 +141,11 @@ const complexMessage: SSEMessage = {
130141expectAssignable < SSESource > ( complexMessage )
131142
132143// Test async iterator types
133- async function * typedAsyncGenerator ( ) : AsyncIterable < string > {
144+ async function * typedAsyncGenerator ( ) : AsyncIterable < string > {
134145 yield 'test'
135146}
136147
137- async function * mixedAsyncGenerator ( ) : AsyncIterable < SSEMessage | string | Buffer > {
148+ async function * mixedAsyncGenerator ( ) : AsyncIterable < SSEMessage | string | Buffer > {
138149 yield { data : 'message' }
139150 yield 'string'
140151 yield Buffer . from ( 'buffer' )
@@ -151,6 +162,7 @@ const pluginOptions: SSEPluginOptions = {
151162 return JSON . stringify ( data )
152163 }
153164}
165+ expectAssignable < SSEPluginOptions > ( pluginOptions )
154166
155167// Test serializer function type
156168const customSerializer = ( data : any ) : string => {
@@ -159,7 +171,6 @@ const customSerializer = (data: any): string => {
159171 }
160172 return String ( data )
161173}
162-
163174expectAssignable < SSEPluginOptions > ( {
164175 serializer : customSerializer
165176} )
@@ -168,17 +179,20 @@ expectAssignable<SSEPluginOptions>({
168179const routeOptions1 : RouteShorthandOptions = {
169180 sse : true
170181}
182+ expectAssignable < RouteShorthandOptions > ( routeOptions1 )
171183
172184const routeOptions2 : RouteShorthandOptions = {
173185 sse : false
174186}
187+ expectAssignable < RouteShorthandOptions > ( routeOptions2 )
175188
176189const routeOptions3 : RouteShorthandOptions = {
177190 sse : {
178191 heartbeat : false ,
179192 serializer : ( data ) => String ( data )
180193 }
181194}
195+ expectAssignable < RouteShorthandOptions > ( routeOptions3 )
182196
183197// Test invalid route options - these tests verify type checking
184198expectError ( app . get ( '/invalid' , {
@@ -189,4 +203,4 @@ expectError(app.get('/invalid2', {
189203 sse : {
190204 unknownOption : true
191205 }
192- } , async ( request , reply ) => { } ) )
206+ } , async ( request , reply ) => { } ) )
0 commit comments