diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..60822b4 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,51 @@ +{ + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "standard" + ], + "parser": "@typescript-eslint/parser", + "plugins": ["@typescript-eslint"], + "env": { "node": true }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module", + "project": "./tsconfig.eslint.json", + "createDefaultProgram": true + }, + "rules": { + "no-console": "off", + "@typescript-eslint/indent": ["error", 2], + "semi": ["error", "never"], + "import/export": "off" // this errors on multiple exports (overload interfaces) + }, + "overrides": [ + { + "files": ["*.d.ts","*.test-d.ts"], + "rules": { + "no-use-before-define": "off", + "no-redeclare": "off", + "@typescript-eslint/no-explicit-any": "off" + } + }, + { + "files": ["*.test-d.ts"], + "rules": { + "@typescript-eslint/no-var-requires": "off", + "no-unused-vars": "off", + "n/handle-callback-err": "off", + "@typescript-eslint/no-empty-function": "off", + "@typescript-eslint/explicit-function-return-type": "off", + "@typescript-eslint/no-unused-vars": "off", + "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/no-misused-promises": ["error", { + "checksVoidReturn": false + }] + }, + "globals": { + "NodeJS": "readonly" + } + } + ] + } \ No newline at end of file diff --git a/package.json b/package.json index 9598144..c521d5a 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,10 @@ "types": "types/index.d.ts", "scripts": { "coverage": "npm run test:unit -- --coverage-report=html", - "lint": "standard | snazzy", - "lint:fix": "standard --fix", + "lint": "npm run lint:javascript && npm run lint:typescript", + "lint:javascript": "standard | snazzy", + "lint:fix": "standard --fix && npm run lint:typescript -- --fix", + "lint:typescript": "eslint -c .eslintrc.json types/**/*.d.ts types/**/*.test-d.ts", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", "test:unit": "tap", @@ -28,20 +30,20 @@ }, "homepage": "https://github.com/fastify/fastify-static", "dependencies": { - "content-disposition": "^0.5.3", "@fastify/accept-negotiator": "^1.0.0", + "@fastify/send": "^2.0.0", + "content-disposition": "^0.5.3", "fastify-plugin": "^4.0.0", "glob": "^8.0.1", "p-limit": "^3.1.0", - "readable-stream": "^4.0.0", - "@fastify/send": "^2.0.0" + "readable-stream": "^4.0.0" }, "devDependencies": { "@fastify/compress": "^6.0.0", "@fastify/pre-commit": "^2.0.2", "@types/node": "^20.1.0", - "@typescript-eslint/eslint-plugin": "^2.29.0", - "@typescript-eslint/parser": "^2.29.0", + "@typescript-eslint/eslint-plugin": "^6.3.0", + "@typescript-eslint/parser": "^6.3.0", "concat-stream": "^2.0.0", "coveralls": "^3.0.4", "eslint-plugin-typescript": "^0.14.0", @@ -53,7 +55,8 @@ "snazzy": "^9.0.0", "standard": "^17.0.0", "tap": "^16.0.0", - "tsd": "^0.28.0" + "tsd": "^0.28.0", + "typescript": "^5.1.6" }, "tsd": { "directory": "test/types" diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 0000000..3b9ddea --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", + "lib": [ "es2015" ], + "module": "commonjs", + "noEmit": true, + "strict": true + }, + "include": [ + "types/*.test-d.ts", + "types/*.d.ts" + ] + } \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index 9e2224d..c352556 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2,10 +2,10 @@ // Leo /// -import {FastifyPluginAsync, FastifyRequest, RouteOptions} from 'fastify'; -import { Stats } from 'fs'; +import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify' +import { Stats } from 'fs' -declare module "fastify" { +declare module 'fastify' { interface FastifyReply { sendFile(filename: string, rootPath?: string): FastifyReply; sendFile(filename: string, options?: fastifyStatic.SendOptions): FastifyReply; @@ -107,9 +107,9 @@ declare namespace fastifyStatic { constraints?: RouteOptions['constraints']; } - export const fastifyStatic: FastifyStaticPlugin; + export const fastifyStatic: FastifyStaticPlugin - export { fastifyStatic as default }; + export { fastifyStatic as default } } declare function fastifyStatic(...params: Parameters): ReturnType; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 719263b..883a878 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,34 +1,34 @@ import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify' -import { Server } from 'http'; +import { Server } from 'http' import { expectAssignable, expectError, expectType } from 'tsd' -import * as fastifyStaticStar from '..'; +import * as fastifyStaticStar from '..' import fastifyStatic, { FastifyStaticOptions, - fastifyStatic as fastifyStaticNamed, + 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>(fastifyStatic); -expectType>(fastifyStaticNamed); -expectType>(fastifyStaticCjsImport.default); -expectType>(fastifyStaticCjsImport.fastifyStatic); -expectType>(fastifyStaticStar.default); +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>(fastifyStatic) +expectType>(fastifyStaticNamed) +expectType>(fastifyStaticCjsImport.default) +expectType>(fastifyStaticCjsImport.fastifyStatic) +expectType>(fastifyStaticStar.default) expectType>( -fastifyStaticStar.fastifyStatic -); -expectType(fastifyStaticCjs); + fastifyStaticStar.fastifyStatic +) +expectType(fastifyStaticCjs) const appWithImplicitHttp = fastify() const options: FastifyStaticOptions = { @@ -54,7 +54,7 @@ const options: FastifyStaticOptions = { }, preCompressed: false, allowedPath: (pathName: string, root: string, request: FastifyRequest) => { - return true; + return true }, constraints: { host: /.*\.example\.com/, @@ -70,7 +70,7 @@ expectError({ expectAssignable({ root: '', list: { - format: 'json', + format: 'json' } }) @@ -93,7 +93,7 @@ expectAssignable({ expectError({ root: '', list: { - format: 'html', + format: 'html' } }) @@ -123,7 +123,7 @@ appWithHttp2 }) appWithHttp2.get('/download/2', (request, reply) => { - reply.download('some-file-name', 'some-filename' ,{ cacheControl: false, acceptRanges: true }) + reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) }) }) @@ -168,7 +168,7 @@ noIndexApp noIndexApp.get('/', (request, reply) => { reply.send('

fastify-static

') }) -}) + }) const defaultIndexApp = fastify() options.index = 'index.html'