Skip to content

Commit bcf5664

Browse files
authoredJan 26, 2024
chore: improve types for setHeaders option (#428)
1 parent a34d6a2 commit bcf5664

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed
 

‎types/index.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Leo <https://github.com/leomelzer>
33
/// <reference types="node" />
44

5-
import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify'
5+
import { FastifyPluginAsync, FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
66
import { Stats } from 'fs'
77

88
declare module 'fastify' {
@@ -19,6 +19,13 @@ declare module 'fastify' {
1919
type FastifyStaticPlugin = FastifyPluginAsync<NonNullable<fastifyStatic.FastifyStaticOptions>>;
2020

2121
declare namespace fastifyStatic {
22+
export interface SetHeadersResponse {
23+
getHeader: FastifyReply['getHeader'];
24+
setHeader: FastifyReply['header'];
25+
readonly filename: string;
26+
statusCode: number;
27+
}
28+
2229
export interface ExtendedInformation {
2330
fileCount: number;
2431
totalFileCount: number;
@@ -83,7 +90,7 @@ declare namespace fastifyStatic {
8390
serve?: boolean;
8491
decorateReply?: boolean;
8592
schemaHide?: boolean;
86-
setHeaders?: (...args: any[]) => void;
93+
setHeaders?: (res: SetHeadersResponse, path: string, stat: Stats) => void;
8794
redirect?: boolean;
8895
wildcard?: boolean;
8996
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;

‎types/index.test-d.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify'
1+
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify'
22
import { Server } from 'http'
3+
import { Stats } from 'fs'
34
import { expectAssignable, expectError, expectType } from 'tsd'
45
import * as fastifyStaticStar from '..'
56
import fastifyStatic, {
@@ -49,8 +50,15 @@ const options: FastifyStaticOptions = {
4950
serve: true,
5051
wildcard: true,
5152
list: false,
52-
setHeaders: (res: any, pathName: any) => {
53-
res.setHeader('test', pathName)
53+
setHeaders: (res, path, stat) => {
54+
expectType<string>(res.filename)
55+
expectType<number>(res.statusCode)
56+
expectType<ReturnType<FastifyReply['getHeader']>>(res.getHeader('X-Test'))
57+
res.setHeader('X-Test', 'string')
58+
59+
expectType<string>(path)
60+
61+
expectType<Stats>(stat)
5462
},
5563
preCompressed: false,
5664
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {

0 commit comments

Comments
 (0)
Please sign in to comment.