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

make type definitons "module": "nodenext" compatible #311

Merged
merged 6 commits into from
Jul 23, 2022
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
170 changes: 89 additions & 81 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,100 +8,108 @@ import { Stats } from 'fs';
declare module "fastify" {
interface FastifyReply {
sendFile(filename: string, rootPath?: string): FastifyReply;
sendFile(filename: string, options?: SendOptions): FastifyReply;
sendFile(filename: string, rootPath?: string, options?: SendOptions): FastifyReply;
download(filepath: string, options?: SendOptions): FastifyReply;
sendFile(filename: string, options?: fastifyStatic.SendOptions): FastifyReply;
sendFile(filename: string, rootPath?: string, options?: fastifyStatic.SendOptions): FastifyReply;
download(filepath: string, options?: fastifyStatic.SendOptions): FastifyReply;
download(filepath: string, filename?: string): FastifyReply;
download(filepath: string, filename?: string, options?: SendOptions): FastifyReply;
download(filepath: string, filename?: string, options?: fastifyStatic.SendOptions): FastifyReply;
}
}

interface ExtendedInformation {
fileCount: number;
totalFileCount: number;
folderCount: number;
totalFolderCount: number;
totalSize: number;
lastModified: number;
}

interface ListDir {
href: string;
name: string;
stats: Stats;
extendedInfo?: ExtendedInformation;
}
type FastifyStaticPlugin = FastifyPluginCallback<NonNullable<fastifyStatic.FastifyStaticOptions>>;

interface ListFile {
href: string;
name: string;
stats: Stats;
}
declare namespace fastifyStatic {
export interface ExtendedInformation {
fileCount: number;
totalFileCount: number;
folderCount: number;
totalFolderCount: number;
totalSize: number;
lastModified: number;
}

interface ListRender {
(dirs: ListDir[], files: ListFile[]): string;
}
export interface ListDir {
href: string;
name: string;
stats: Stats;
extendedInfo?: ExtendedInformation;
}

interface ListOptions {
names?: string[];
extendedFolderInfo?: boolean;
jsonFormat?: 'names' | 'extended';
}
export interface ListFile {
href: string;
name: string;
stats: Stats;
}

interface ListOptionsJsonFormat extends ListOptions {
format: 'json';
// Required when the URL parameter `format=html` exists
render?: ListRender;
}
export interface ListRender {
(dirs: ListDir[], files: ListFile[]): string;
}

interface ListOptionsHtmlFormat extends ListOptions {
format: 'html';
render: ListRender;
}
export interface ListOptions {
names?: string[];
extendedFolderInfo?: boolean;
jsonFormat?: 'names' | 'extended';
}

// Passed on to `send`
interface SendOptions {
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[] | string | false;
lastModified?: boolean;
maxAge?: string | number;
}
export interface ListOptionsJsonFormat extends ListOptions {
format: 'json';
// Required when the URL parameter `format=html` exists
render?: ListRender;
}

export interface FastifyStaticOptions extends SendOptions {
root: string | string[];
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root?: string) => boolean;
/**
* @description
* Opt-in to looking for pre-compressed files
*/
preCompressed?: boolean;
export interface ListOptionsHtmlFormat extends ListOptions {
format: 'html';
render: ListRender;
}

// Passed on to `send`
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[] | string | false;
lastModified?: boolean;
maxAge?: string | number;
export interface SendOptions {
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[] | string | false;
lastModified?: boolean;
maxAge?: string | number;
}

export interface FastifyStaticOptions extends SendOptions {
root: string | string[];
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root?: string) => boolean;
/**
* @description
* Opt-in to looking for pre-compressed files
*/
preCompressed?: boolean;

// Passed on to `send`
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[] | string | false;
lastModified?: boolean;
maxAge?: string | number;
}

export const fastifyStatic: FastifyStaticPlugin;

export { fastifyStatic as default };
}

export declare const fastifyStatic: FastifyPluginCallback<FastifyStaticOptions>
declare function fastifyStatic(...params: Parameters<FastifyStaticPlugin>): ReturnType<FastifyStaticPlugin>;

export default fastifyStatic;
export = fastifyStatic;
34 changes: 31 additions & 3 deletions test/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import fastify from 'fastify'
import { expectAssignable, expectError } from 'tsd'
import fastifyStatic, { FastifyStaticOptions } from '../..'
import fastify, { FastifyInstance, FastifyPluginCallback } from 'fastify'
import { Server } from 'http';
import { expectAssignable, expectError, expectType } from 'tsd'
import * as fastifyStaticStar from '../..';
import fastifyStatic, {
FastifyStaticOptions,
fastifyStatic as fastifyStaticNamed,
} from '../..'

import fastifyStaticCjsImport = require('../..');
const fastifyStaticCjs = require('../..');

const app: FastifyInstance = fastify();

app.register(fastifyStatic);
app.register(fastifyStaticNamed);
app.register(fastifyStaticCjs);
app.register(fastifyStaticCjsImport.default);
app.register(fastifyStaticCjsImport.fastifyStatic);
app.register(fastifyStaticStar.default);
app.register(fastifyStaticStar.fastifyStatic);

expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(fastifyStatic);
expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(fastifyStaticNamed);
expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.default);
expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.fastifyStatic);
expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(fastifyStaticStar.default);
expectType<FastifyPluginCallback<FastifyStaticOptions, Server>>(
fastifyStaticStar.fastifyStatic
);
expectType<any>(fastifyStaticCjs);

const appWithImplicitHttp = fastify()
const options: FastifyStaticOptions = {
Expand Down