diff --git a/@types/fastify/fastify.d.ts b/@types/fastify/fastify.d.ts index f85cbfd5..b9d797bd 100644 --- a/@types/fastify/fastify.d.ts +++ b/@types/fastify/fastify.d.ts @@ -1,7 +1,7 @@ -import { Auth } from "../../src/schemas/auth.ts"; +import { Auth } from '../../src/schemas/auth.ts' -declare module "fastify" { - export interface FastifyRequest { - user: Auth - } +declare module 'fastify' { + export interface FastifyRequest { + user: Auth + } } diff --git a/@types/node/environment.d.ts b/@types/node/environment.d.ts index 7105b751..0069d754 100644 --- a/@types/node/environment.d.ts +++ b/@types/node/environment.d.ts @@ -13,4 +13,4 @@ declare global { } } -export {}; \ No newline at end of file +export {} diff --git a/package.json b/package.json index 393b4189..b26223e7 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "@fastify/swagger-ui": "^5.0.1", "@fastify/type-provider-typebox": "^5.0.0", "@fastify/under-pressure": "^9.0.1", - "@fastify/vite": "^7.0.1", "@sinclair/typebox": "^0.33.12", "@vitejs/plugin-react": "^4.3.1", "concurrently": "^9.0.1", @@ -49,14 +48,12 @@ "react-dom": "^18.3.1" }, "devDependencies": { - "@types/node": "^22.0.0", - "@types/react": "^18.3.4", - "@types/react-dom": "^18.3.0", - "eslint": "^9.4.0", + "@types/node": "^22.5.5", + "eslint": "^9.11.0", "fastify-tsconfig": "^2.0.0", - "mysql2": "^3.10.1", + "mysql2": "^3.11.3", "neostandard": "^0.11.5", "tap": "^21.0.1", - "typescript": "^5.4.5" + "typescript": "^5.6.2" } } diff --git a/src/app.ts b/src/app.ts index 29662200..07eed0f0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -60,10 +60,14 @@ export default async function serviceApp ( 'Unhandled error occurred' ) - const statusCode = err.statusCode ?? 500 - reply.code(statusCode) + reply.code(err.statusCode ?? 500) - return { message: 'Internal Server Error' } + let message = 'Internal Server Error' + if (err.statusCode === 401) { + message = err.message + } + + return { message } }) // An attacker could search for valid URLs if your 404 error handling is not rate limited. diff --git a/src/routes/home.ts b/src/routes/home.ts new file mode 100644 index 00000000..a3c29513 --- /dev/null +++ b/src/routes/home.ts @@ -0,0 +1,24 @@ +import { + FastifyPluginAsyncTypebox, + Type +} from '@fastify/type-provider-typebox' + +const plugin: FastifyPluginAsyncTypebox = async (fastify) => { + fastify.get( + '/', + { + schema: { + response: { + 200: Type.Object({ + message: Type.String() + }) + } + } + }, + async function () { + return { message: 'Welcome to the official fastify demo!' } + } + ) +} + +export default plugin diff --git a/test/routes/home.test.ts b/test/routes/home.test.ts new file mode 100644 index 00000000..41fc3a1d --- /dev/null +++ b/test/routes/home.test.ts @@ -0,0 +1,14 @@ +import { test } from 'node:test' +import assert from 'node:assert' +import { build } from '../helper.js' + +test('GET /', async (t) => { + const app = await build(t) + const res = await app.inject({ + url: '/' + }) + + assert.deepStrictEqual(JSON.parse(res.payload), { + message: 'Welcome to the official fastify demo!' + }) +})