Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:typescript": "tstyche",
"test:unit": "borp -C --check-coverage --lines 100",
"example": "node example/server.js",
"example:benchmark": "node example/server-benchmark.js"
Expand Down Expand Up @@ -78,12 +78,9 @@
"neostandard": "^0.12.0",
"pino": "^10.0.0",
"proxyquire": "^2.1.3",
"tsd": "^0.33.0"
},
"tsd": {
"directory": "test/types"
"tstyche": "^7.0.0"
},
"publishConfig": {
"access": "public"
}
}
}
31 changes: 18 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,19 @@ declare namespace fastifyStatic {

type Root = string | string[] | URL | URL[]

type RootOptions = {
serve: true;
root: Root;
} | {
serve?: false;
root?: Root;
}
type RootOptions =
| {
serve: true;
root: Root;
}
| {
serve?: false;
root?: Root;
}

export type FastifyStaticOptions =
SendOptions
& RootOptions
& {
export type FastifyStaticOptions = SendOptions &
RootOptions &
Omit<import('fastify').RegisterOptions, 'logLevel'> & {
// Added by this plugin
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
Expand All @@ -104,7 +105,11 @@ declare namespace fastifyStatic {
wildcard?: boolean;
globIgnore?: string[];
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root: string, request: FastifyRequest) => boolean;
allowedPath?: (
pathName: string,
root: string,
request: FastifyRequest
) => boolean;
/**
* @description
* Opt-in to looking for pre-compressed files
Expand All @@ -123,7 +128,7 @@ declare namespace fastifyStatic {
lastModified?: boolean;
maxAge?: string | number;
constraints?: RouteOptions['constraints'];
logLevel?: RouteOptions['logLevel'];
logLevel?: NonNullable<RouteOptions['logLevel']>;
Comment thread
Tony133 marked this conversation as resolved.
}

export const fastifyStatic: FastifyStaticPlugin
Expand Down
131 changes: 59 additions & 72 deletions types/index.test-d.ts → types/index.tst.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify'
import { Server } from 'node:http'
import { Stats } from 'node:fs'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, { type FastifyInstance, type FastifyPluginAsync, type FastifyRequest, type FastifyReply } from 'fastify'
import { type Server } from 'node:http'
import { type Stats } from 'node:fs'
import { expect } from 'tstyche'
import * as fastifyStaticStar from '..'
import fastifyStatic, {
FastifyStaticOptions,
type FastifyStaticOptions,
fastifyStatic as fastifyStaticNamed
} from '..'

Expand All @@ -21,15 +21,13 @@ app.register(fastifyStaticCjsImport.fastifyStatic, { root: __dirname })
app.register(fastifyStaticStar.default, { root: __dirname })
app.register(fastifyStaticStar.fastifyStatic, { root: __dirname })

expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStatic)
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticNamed)
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.default)
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.fastifyStatic)
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticStar.default)
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(
fastifyStaticStar.fastifyStatic
)
expectType<any>(fastifyStaticCjs)
expect(fastifyStatic).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticNamed).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticCjsImport.default).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticCjsImport.fastifyStatic).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticStar.default).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticStar.fastifyStatic).type.toBe<FastifyPluginAsync<FastifyStaticOptions, Server>>()
expect(fastifyStaticCjs).type.toBe<any>()

const appWithImplicitHttp = fastify()
const options: FastifyStaticOptions = {
Expand All @@ -53,14 +51,14 @@ const options: FastifyStaticOptions = {
globIgnore: ['**/*.private'],
list: false,
setHeaders: (res, path, stat) => {
expectType<string>(res.filename)
expectType<number>(res.statusCode)
expectType<ReturnType<FastifyReply['getHeader']>>(res.getHeader('X-Test'))
expect(res.filename).type.toBe<string>()
expect(res.statusCode).type.toBe<number>()
expect(res.getHeader('X-Test')).type.toBe<ReturnType<FastifyReply['getHeader']>>()
res.setHeader('X-Test', 'string')

expectType<string>(path)
expect(path).type.toBe<string>()

expectType<Stats>(stat)
expect(stat).type.toBe<Stats>()
},
preCompressed: false,
allowedPath: (_pathName: string, _root: string, _request: FastifyRequest) => {
Expand All @@ -73,64 +71,53 @@ const options: FastifyStaticOptions = {
logLevel: 'warn'
}

expectError<FastifyStaticOptions>({
root: '',
wildcard: '**/**'
})

expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'json'
}
})

expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'json',
render: () => ''
}
})

expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'html',
render: () => ''
}
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
root: '',
list: {
format: 'json' as const
}
})

expectError<FastifyStaticOptions>({
root: '',
list: {
format: 'html'
}
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
root: '',
list: {
format: 'json' as const,
render: () => ''
}
})

expectAssignable<FastifyStaticOptions>({
root: ['']
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
root: '',
list: {
format: 'html' as const,
render: () => ''
}
})

expectAssignable<FastifyStaticOptions>({
root: new URL('')
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
root: ['']
})

expectAssignable<FastifyStaticOptions>({
root: [new URL('')]
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
root: new URL('file://')
})

expectError<FastifyStaticOptions>({
serve: true
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({ root: [new URL('file://')] })

expectAssignable<FastifyStaticOptions>({
serve: true,
root: ''
})
expect<FastifyStaticOptions>()
.type.toBeAssignableFrom({
serve: true as const,
root: ''
})

Comment thread
Tony133 marked this conversation as resolved.
expectAssignable<FastifyStaticOptions>({
serve: false
expect<FastifyStaticOptions>().type.not.toBeAssignableFrom({
serve: true as const,
})

appWithImplicitHttp
Expand Down Expand Up @@ -218,7 +205,7 @@ noIndexApp
})
})

options.root = new URL('')
options.root = new URL('file://')

const URLRootApp = fastify()
URLRootApp.register(fastifyStatic, options)
Expand All @@ -229,7 +216,7 @@ URLRootApp.register(fastifyStatic, options)
})

const defaultIndexApp = fastify()
options.index = 'index.html'
options.index = 'index.html' as const
Comment thread
Tony133 marked this conversation as resolved.

defaultIndexApp
.register(fastifyStatic, options)
Expand Down
Loading