Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve types for setHeaders option #428

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Leo <https://github.com/leomelzer>
/// <reference types="node" />

import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify'
import { FastifyPluginAsync, FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
import { Stats } from 'fs'

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

declare namespace fastifyStatic {
export interface SetHeadersResponse {
getHeader: FastifyReply['getHeader'];
setHeader: FastifyReply['header'];
readonly filename: string;
statusCode: number;
}
mcollina marked this conversation as resolved.
Show resolved Hide resolved

export interface ExtendedInformation {
fileCount: number;
totalFileCount: number;
Expand Down Expand Up @@ -83,7 +90,7 @@ declare namespace fastifyStatic {
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
setHeaders?: (res: SetHeadersResponse, path: string, stat: Stats) => void;
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
Expand Down
14 changes: 11 additions & 3 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify'
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify'
import { Server } from 'http'
import { Stats } from 'fs'
import { expectAssignable, expectError, expectType } from 'tsd'
import * as fastifyStaticStar from '..'
import fastifyStatic, {
Expand Down Expand Up @@ -49,8 +50,15 @@ const options: FastifyStaticOptions = {
serve: true,
wildcard: true,
list: false,
setHeaders: (res: any, pathName: any) => {
res.setHeader('test', pathName)
setHeaders: (res, path, stat) => {
expectType<string>(res.filename)
expectType<number>(res.statusCode)
expectType<ReturnType<FastifyReply['getHeader']>>(res.getHeader('X-Test'))
res.setHeader('X-Test', 'string')

expectType<string>(path)

expectType<Stats>(stat)
},
preCompressed: false,
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {
Expand Down