diff --git a/README.md b/README.md index c5be2ce0..89768593 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![CI](https://github.com/fastify/fastify-autoload/workflows/CI/badge.svg) [![NPM version](https://img.shields.io/npm/v/@fastify/autoload.svg?style=flat)](https://www.npmjs.com/package/@fastify/autoload) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) +[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) Convenience plugin for Fastify that loads all plugins found in a directory and automatically configures routes matching the folder structure. diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 00000000..ab47489b --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,11 @@ +'use strict' + +module.exports = require('neostandard')({ + ignores: [ + ...require('neostandard').resolveIgnoresFromGitignore(), + 'test/commonjs/syntax-error/lib/a.js', + 'test/issues/369/invalid-autohooks', + 'test/issues/369/non-SyntaxError' + ], + ts: true +}) diff --git a/package.json b/package.json index b0e29946..79802a84 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "type": "commonjs", "types": "types/index.d.ts", "scripts": { - "lint": "standard | snazzy", - "lint:fix": "standard --fix | snazzy", + "lint": "eslint", + "lint:fix": "eslint --fix", "test": "npm run typescript && npm run typescript:jest && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild && npm run unit", "typescript": "tsd", "typescript:jest": "jest", @@ -56,8 +56,7 @@ "fastify": "^5.0.0", "fastify-plugin": "^5.0.0", "jest": "^29.7.0", - "snazzy": "^9.0.0", - "standard": "^17.1.0", + "neostandard": "^0.11.9", "tap": "^19.0.2", "ts-jest": "^29.1.4", "ts-node": "^10.9.2", @@ -70,11 +69,6 @@ "vite": "^5.2.12", "vitest": "^2.0.3" }, - "standard": { - "ignore": [ - "test/*/*-error/lib/a.js" - ] - }, "jest": { "preset": "ts-jest", "testEnvironment": "node", diff --git a/test/commonjs/babel-node/routes/foo/index.ts b/test/commonjs/babel-node/routes/foo/index.ts index 28d1c6f5..ba1d63ed 100644 --- a/test/commonjs/babel-node/routes/foo/index.ts +++ b/test/commonjs/babel-node/routes/foo/index.ts @@ -1,5 +1,5 @@ module.exports = async (fastify) => { - fastify.get("/", function () { - return { foo: "bar" }; + fastify.get('/', function () { + return { foo: 'bar' } }) -}; +} diff --git a/test/commonjs/babel-node/routes/root.ts b/test/commonjs/babel-node/routes/root.ts index 9ec10530..a6fe3366 100644 --- a/test/commonjs/babel-node/routes/root.ts +++ b/test/commonjs/babel-node/routes/root.ts @@ -1,5 +1,5 @@ module.exports = async (fastify) => { - fastify.get("/", function () { - return { hello: "world" }; + fastify.get('/', function () { + return { hello: 'world' } }) -}; +} diff --git a/test/commonjs/ts-node/routes/bar/index.ts b/test/commonjs/ts-node/routes/bar/index.ts index 32556790..abef30bb 100644 --- a/test/commonjs/ts-node/routes/bar/index.ts +++ b/test/commonjs/ts-node/routes/bar/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { - fastify.get("/", function () { - return { bar: "bar" }; + fastify.get('/', function () { + return { bar: 'bar' } }) -}; +} diff --git a/test/commonjs/ts-node/routes/foo/baz/index.ts b/test/commonjs/ts-node/routes/foo/baz/index.ts index 7e1d53c9..081d0369 100644 --- a/test/commonjs/ts-node/routes/foo/baz/index.ts +++ b/test/commonjs/ts-node/routes/foo/baz/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { - fastify.get("/customPath", function () { - return { baz: "baz" }; + fastify.get('/customPath', function () { + return { baz: 'baz' } }) -}; +} diff --git a/test/commonjs/ts-node/routes/foo/index.ts b/test/commonjs/ts-node/routes/foo/index.ts index f1021774..befcfc87 100644 --- a/test/commonjs/ts-node/routes/foo/index.ts +++ b/test/commonjs/ts-node/routes/foo/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { - fastify.get("/", function () { - return { foo: "foo" }; + fastify.get('/', function () { + return { foo: 'foo' } }) -}; +} diff --git a/test/commonjs/ts-node/routes/root.ts b/test/commonjs/ts-node/routes/root.ts index 3766802b..a2da6d45 100644 --- a/test/commonjs/ts-node/routes/root.ts +++ b/test/commonjs/ts-node/routes/root.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { - fastify.get("/", function () { - return { hello: "world" }; + fastify.get('/', function () { + return { hello: 'world' } }) -}; +} diff --git a/test/issues/369/routes/.autohooks.js b/test/issues/369/routes/.autohooks.js index 951e9583..e540230b 100644 --- a/test/issues/369/routes/.autohooks.js +++ b/test/issues/369/routes/.autohooks.js @@ -2,7 +2,7 @@ module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('root') - }) + req.hooked = req.hooked || [] + req.hooked.push('root') + }) } diff --git a/test/issues/369/routes/child/.autohooks.js b/test/issues/369/routes/child/.autohooks.js index 06cffbb6..85774b4b 100644 --- a/test/issues/369/routes/child/.autohooks.js +++ b/test/issues/369/routes/child/.autohooks.js @@ -1,8 +1,8 @@ 'use strict' module.exports = async function (app, opts) { - app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('child') - }) + app.addHook('onRequest', async (req, reply) => { + req.hooked = req.hooked || [] + req.hooked.push('child') + }) } diff --git a/test/issues/369/routes/promisified/.autohooks.js b/test/issues/369/routes/promisified/.autohooks.js index a0babdaa..4c7c280d 100644 --- a/test/issues/369/routes/promisified/.autohooks.js +++ b/test/issues/369/routes/promisified/.autohooks.js @@ -1,10 +1,10 @@ 'use strict' -module.exports = new Promise((res) => { - res(async function (app, opts) { - app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('promisified') - }) +module.exports = new Promise((resolve) => { + resolve(async function (app, opts) { + app.addHook('onRequest', async (req, reply) => { + req.hooked = req.hooked || [] + req.hooked.push('promisified') }) + }) }) diff --git a/test/issues/374/routes/entity/.autohooks.js b/test/issues/374/routes/entity/.autohooks.js index 951e9583..e540230b 100644 --- a/test/issues/374/routes/entity/.autohooks.js +++ b/test/issues/374/routes/entity/.autohooks.js @@ -2,7 +2,7 @@ module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('root') - }) + req.hooked = req.hooked || [] + req.hooked.push('root') + }) } diff --git a/test/issues/376/routes/entity/.autohooks.js b/test/issues/376/routes/entity/.autohooks.js index 951e9583..e540230b 100644 --- a/test/issues/376/routes/entity/.autohooks.js +++ b/test/issues/376/routes/entity/.autohooks.js @@ -2,7 +2,7 @@ module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('root') - }) + req.hooked = req.hooked || [] + req.hooked.push('root') + }) } diff --git a/test/typescript-jest/babel-node/index.test.ts b/test/typescript-jest/babel-node/index.test.ts index 9ad24cf9..bf5570b7 100644 --- a/test/typescript-jest/babel-node/index.test.ts +++ b/test/typescript-jest/babel-node/index.test.ts @@ -2,17 +2,17 @@ import fastify from 'fastify' import { join } from 'path' import AutoLoad from '../../../' -describe("load typescript using babel-node", () => { +describe('load typescript using babel-node', () => { const app = fastify() - beforeAll( done => { + beforeAll(done => { app.register(AutoLoad, { dir: join(__dirname, '../../commonjs/babel-node/routes') }) app.ready(done) }) - it("tests the root route", async function () { + it('tests the root route', async function () { const response = await app.inject({ method: 'GET', url: '/' @@ -20,7 +20,7 @@ describe("load typescript using babel-node", () => { expect(response.statusCode).toEqual(200) expect(JSON.parse(response.payload)).toEqual({ hello: 'world' }) }) - it("tests /foo route", async function () { + it('tests /foo route', async function () { const response = await app.inject({ method: 'GET', url: '/foo' @@ -28,4 +28,4 @@ describe("load typescript using babel-node", () => { expect(response.statusCode).toBe(200) expect(JSON.parse(response.payload)).toEqual({ foo: 'bar' }) }) -}) \ No newline at end of file +}) diff --git a/test/typescript-jest/basic/app.ts b/test/typescript-jest/basic/app.ts index f9b0e914..24283632 100644 --- a/test/typescript-jest/basic/app.ts +++ b/test/typescript-jest/basic/app.ts @@ -10,4 +10,4 @@ const app: FastifyPluginCallback = function (fastify, opts, next): void { next() } -export default app; +export default app diff --git a/test/typescript-jest/basic/basic.test.ts b/test/typescript-jest/basic/basic.test.ts index 2a8eabf5..87e6f636 100644 --- a/test/typescript-jest/basic/basic.test.ts +++ b/test/typescript-jest/basic/basic.test.ts @@ -5,12 +5,12 @@ const app = fastify() app.register(basicApp) -describe("load typescript plugin in jest environment", () => { - beforeAll( done => { +describe('load typescript plugin in jest environment', () => { + beforeAll(done => { app.ready(done) }) - it("should response code 200 OK", async () => { + it('should response code 200 OK', async () => { const { payload, statusCode } = await app.inject({ url: '/typescript' }) diff --git a/test/typescript-jest/integration/instance.ts b/test/typescript-jest/integration/instance.ts index 00dfdebf..079340b4 100644 --- a/test/typescript-jest/integration/instance.ts +++ b/test/typescript-jest/integration/instance.ts @@ -1,13 +1,13 @@ -import fastify from "fastify"; -import basicApp from "../../typescript/basic/app"; +import fastify from 'fastify' +import basicApp from '../../typescript/basic/app' -const app = fastify(); -app.register(basicApp); +const app = fastify() +app.register(basicApp) app.listen({ port: Math.floor(Math.random() * 3000 + 3000) }, function (err) { - if (err) process.stderr.write("failed"); - process.stdout.write("success"); - app.close(); -}); + if (err) process.stderr.write('failed') + process.stdout.write('success') + app.close() +}) diff --git a/test/typescript-jest/integration/integration.test.ts b/test/typescript-jest/integration/integration.test.ts index 20b257d6..5d298e80 100644 --- a/test/typescript-jest/integration/integration.test.ts +++ b/test/typescript-jest/integration/integration.test.ts @@ -1,26 +1,26 @@ -import { exec } from "child_process"; +import { exec } from 'child_process' -describe("integration test", function () { - test.concurrent.each(["ts-node", "ts-node-dev"])( - "integration with %s", +describe('integration test', function () { + test.concurrent.each(['ts-node', 'ts-node-dev'])( + 'integration with %s', async function (instance) { await new Promise(function (resolve, reject) { - const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`); - let stderr = ""; - child.stderr?.on("data", function (b) { - stderr = stderr + b.toString(); - }); - let stdout = ""; - child.stdout?.on("data", function (b) { - stdout = stdout + b.toString(); - }); - child.once("close", function () { - expect(stderr.includes("failed")).toStrictEqual(false); - expect(stdout.includes("success")).toStrictEqual(true); - resolve(""); - }); - }); + const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`) + let stderr = '' + child.stderr?.on('data', function (b) { + stderr = stderr + b.toString() + }) + let stdout = '' + child.stdout?.on('data', function (b) { + stdout = stdout + b.toString() + }) + child.once('close', function () { + expect(stderr.includes('failed')).toStrictEqual(false) + expect(stdout.includes('success')).toStrictEqual(true) + resolve('') + }) + }) }, 30000 - ); -}); + ) +}) diff --git a/test/typescript/basic/app.ts b/test/typescript/basic/app.ts index 539d6888..ac2f87bc 100644 --- a/test/typescript/basic/app.ts +++ b/test/typescript/basic/app.ts @@ -10,4 +10,4 @@ const app: FastifyPluginCallback = function (fastify, opts, next): void { next() } -export default app; +export default app diff --git a/test/vitest/basic.test.ts b/test/vitest/basic.test.ts index cc4714e6..496b38d6 100644 --- a/test/vitest/basic.test.ts +++ b/test/vitest/basic.test.ts @@ -5,13 +5,13 @@ import Fastify from 'fastify' import AutoLoad from '../../index' import { join } from 'path' -describe.concurrent("Vitest test suite", function () { +describe.concurrent('Vitest test suite', function () { const app = Fastify() - app.register(AutoLoad, { - dir: join(__dirname, '../commonjs/ts-node/routes') - }) + app.register(AutoLoad, { + dir: join(__dirname, '../commonjs/ts-node/routes') + }) - test("Test the root route", async function () { + test('Test the root route', async function () { const response = await app.inject({ method: 'GET', url: '/' @@ -20,7 +20,7 @@ describe.concurrent("Vitest test suite", function () { expect(JSON.parse(response.payload)).toEqual({ hello: 'world' }) }) - test("Test /foo route", async function () { + test('Test /foo route', async function () { const response = await app.inject({ method: 'GET', url: '/foo' diff --git a/test/vitest/filter.test.ts b/test/vitest/filter.test.ts index 41c6fbc4..46a546f1 100644 --- a/test/vitest/filter.test.ts +++ b/test/vitest/filter.test.ts @@ -5,14 +5,14 @@ import Fastify from 'fastify' import AutoLoad from '../../index' import { join } from 'path' -describe.concurrent("Vitest ignore filters test suite", function () { +describe.concurrent('Vitest ignore filters test suite', function () { const app = Fastify() - app.register(AutoLoad, { - dir: join(__dirname, '../commonjs/ts-node/routes'), - ignoreFilter: "foo" - }) + app.register(AutoLoad, { + dir: join(__dirname, '../commonjs/ts-node/routes'), + ignoreFilter: 'foo' + }) - test("Test the root route", async function () { + test('Test the root route', async function () { const response = await app.inject({ method: 'GET', url: '/' @@ -20,7 +20,7 @@ describe.concurrent("Vitest ignore filters test suite", function () { expect(response.statusCode).toEqual(200) }) - test("Test /foo route", async function () { + test('Test /foo route', async function () { const response = await app.inject({ method: 'GET', url: '/foo' @@ -28,7 +28,7 @@ describe.concurrent("Vitest ignore filters test suite", function () { expect(response.statusCode).toBe(404) }) - test("Test /bar route", async function () { + test('Test /bar route', async function () { const response = await app.inject({ method: 'GET', url: '/bar' @@ -36,7 +36,7 @@ describe.concurrent("Vitest ignore filters test suite", function () { expect(response.statusCode).toBe(200) }) - test("Test /baz route", async function () { + test('Test /baz route', async function () { const response = await app.inject({ method: 'GET', url: '/foo/baz' @@ -45,14 +45,14 @@ describe.concurrent("Vitest ignore filters test suite", function () { }) }) -describe.concurrent("Vitest match filters test suite", function () { +describe.concurrent('Vitest match filters test suite', function () { const app = Fastify() app.register(AutoLoad, { dir: join(__dirname, '../commonjs/ts-node/routes'), - matchFilter: (path) => path.startsWith("/foo") + matchFilter: (path) => path.startsWith('/foo') }) - test("Test the root route", async function () { + test('Test the root route', async function () { const response = await app.inject({ method: 'GET', url: '/' @@ -60,7 +60,7 @@ describe.concurrent("Vitest match filters test suite", function () { expect(response.statusCode).toEqual(404) }) - test("Test /foo route", async function () { + test('Test /foo route', async function () { const response = await app.inject({ method: 'GET', url: '/foo' @@ -68,7 +68,7 @@ describe.concurrent("Vitest match filters test suite", function () { expect(response.statusCode).toBe(200) }) - test("Test /bar route", async function () { + test('Test /bar route', async function () { const response = await app.inject({ method: 'GET', url: '/bar' @@ -76,7 +76,7 @@ describe.concurrent("Vitest match filters test suite", function () { expect(response.statusCode).toBe(404) }) - test("Test /baz route", async function () { + test('Test /baz route', async function () { const response = await app.inject({ method: 'GET', url: '/foo/baz/customPath' @@ -85,15 +85,15 @@ describe.concurrent("Vitest match filters test suite", function () { }) }) -describe.concurrent("Vitest match filters without prefix test suite", function () { +describe.concurrent('Vitest match filters without prefix test suite', function () { const app = Fastify() app.register(AutoLoad, { dir: join(__dirname, '../commonjs/ts-node/routes'), dirNameRoutePrefix: false, - matchFilter: (path) => path.startsWith("/foo/baz") + matchFilter: (path) => path.startsWith('/foo/baz') }) - test("Test /baz route", async function () { + test('Test /baz route', async function () { const response = await app.inject({ method: 'GET', url: '/customPath' diff --git a/types/index.d.ts b/types/index.d.ts index c769d012..3107dfae 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -26,11 +26,11 @@ declare namespace fastifyAutoload { } export const fastifyAutoload: FastifyAutoloadPlugin - export { fastifyAutoload as default }; + export { fastifyAutoload as default } } -declare function fastifyAutoload( +declare function fastifyAutoload ( ...params: Parameters -): ReturnType; +): ReturnType -export = fastifyAutoload; +export = fastifyAutoload diff --git a/types/index.test-d.ts b/types/index.test-d.ts index eb0e5cab..b376ade2 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -6,24 +6,24 @@ import fastifyAutoloadDefault, { AutoloadPluginOptions, fastifyAutoload as fasti import fastifyAutoloadCjsImport = require('..') const fastifyAutoloadCjs = require('..') -const app: FastifyInstance = fastify(); -app.register(fastifyAutoloadNamed); -app.register(fastifyAutoloadDefault); -app.register(fastifyAutoloadCjs); -app.register(fastifyAutoloadCjsImport.default); -app.register(fastifyAutoloadCjsImport.fastifyAutoload); -app.register(fastifyAutoloadStar.default); -app.register(fastifyAutoloadStar.fastifyAutoload); +const app: FastifyInstance = fastify() +app.register(fastifyAutoloadNamed) +app.register(fastifyAutoloadDefault) +app.register(fastifyAutoloadCjs) +app.register(fastifyAutoloadCjsImport.default) +app.register(fastifyAutoloadCjsImport.fastifyAutoload) +app.register(fastifyAutoloadStar.default) +app.register(fastifyAutoloadStar.fastifyAutoload) -expectType>(fastifyAutoloadNamed); -expectType>(fastifyAutoloadDefault); -expectType>(fastifyAutoloadCjsImport.default); -expectType>(fastifyAutoloadCjsImport.fastifyAutoload); -expectType>(fastifyAutoloadStar.default); -expectType>(fastifyAutoloadStar.fastifyAutoload); -expectType(fastifyAutoloadCjs); +expectType>(fastifyAutoloadNamed) +expectType>(fastifyAutoloadDefault) +expectType>(fastifyAutoloadCjsImport.default) +expectType>(fastifyAutoloadCjsImport.fastifyAutoload) +expectType>(fastifyAutoloadStar.default) +expectType>(fastifyAutoloadStar.fastifyAutoload) +expectType(fastifyAutoloadCjs) -let opt1: AutoloadPluginOptions = { +const opt1: AutoloadPluginOptions = { dir: 'test' } const opt2: AutoloadPluginOptions = {