Skip to content

Commit a1ffd4d

Browse files
authored
types: improve onResponse hook typing (#423)
1 parent 0afe34c commit a1ffd4d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

types/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Agent,
1616
AgentOptions,
1717
IncomingHttpHeaders,
18+
IncomingMessage,
1819
RequestOptions,
1920
} from 'node:http'
2021
import {
@@ -55,6 +56,11 @@ declare namespace fastifyReplyFrom {
5556
retriesCount: number;
5657
getDefaultDelay: () => number | null;
5758
}
59+
60+
export type RawServerResponse<T extends RawServerBase> = RawReplyDefaultExpression<T> & {
61+
stream: IncomingMessage
62+
}
63+
5864
export interface FastifyReplyFromHooks {
5965
queryString?: { [key: string]: unknown } | QueryStringFunction;
6066
contentType?: string;
@@ -63,7 +69,7 @@ declare namespace fastifyReplyFrom {
6369
onResponse?: (
6470
request: FastifyRequest<RequestGenericInterface, RawServerBase>,
6571
reply: FastifyReply<RouteGenericInterface, RawServerBase>,
66-
res: RawReplyDefaultExpression<RawServerBase>
72+
res: RawServerResponse<RawServerBase>
6773
) => void;
6874
onError?: (
6975
reply: FastifyReply<RouteGenericInterface, RawServerBase>,

types/index.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import fastify, { FastifyReply, FastifyRequest, RawReplyDefaultExpression, RawServerBase, RequestGenericInterface, RouteGenericInterface } from 'fastify'
1+
import fastify, { FastifyReply, FastifyRequest, RawServerBase, RequestGenericInterface, RouteGenericInterface } from 'fastify'
22
import * as http from 'node:http'
33
import { IncomingHttpHeaders } from 'node:http2'
44
import * as https from 'node:https'
55
import { AddressInfo } from 'node:net'
66
import { expectType } from 'tsd'
77
import { Agent, Client, Dispatcher, Pool } from 'undici'
8-
import replyFrom, { FastifyReplyFromOptions } from '..'
8+
import replyFrom, { FastifyReplyFromOptions, RawServerResponse } from '..'
99
// @ts-ignore
1010
import tap from 'tap'
1111

@@ -83,7 +83,7 @@ async function main () {
8383
onResponse (request, reply, res) {
8484
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request)
8585
expectType<FastifyReply<RouteGenericInterface, RawServerBase>>(reply)
86-
expectType<RawReplyDefaultExpression<RawServerBase>>(res)
86+
expectType<RawServerResponse<RawServerBase>>(res)
8787
expectType<number>(res.statusCode)
8888
}
8989
})

0 commit comments

Comments
 (0)