diff --git a/.eslintrc.json b/.eslintrc.json index 6104de87af7..999130f0b60 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -56,6 +56,7 @@ "parserOptions": { "project": [ "./tsconfig.json", + "./api/tsconfig.json", "./config/tsconfig.json", "./tools/ui-components/tsconfig.json", "./utils/tsconfig.json", diff --git a/api/index.ts b/api/index.ts index bcc66a7b9ce..e7e0a199b0a 100644 --- a/api/index.ts +++ b/api/index.ts @@ -18,9 +18,6 @@ const start = async () => { // NOTE: Awaited to ensure `.use` is registered on `fastify` await fastify.register(middie); - // @ts-expect-error Types are not exported from Fastify, - // and TypeScript is not smart enough to realise types - // defined within this module have the same signature void fastify.use('/test', testMiddleware); void fastify.register(dbConnector); diff --git a/api/middleware/index.ts b/api/middleware/index.ts index ecea2a68d12..ee50e841e7d 100644 --- a/api/middleware/index.ts +++ b/api/middleware/index.ts @@ -1,16 +1,20 @@ -import { NextFunction } from '../utils'; +import type { NextFunction, NextHandleFunction } from '@fastify/middie'; export async function auth0Verify() { // Verify user authorization code with Auth0 } +type MiddieRequest = Parameters[0]; +type MiddieResponse = Parameters[1]; + export function testMiddleware( - req: Request, - _res: Response, + req: MiddieRequest, + res: MiddieResponse, next: NextFunction ) { - // console.log('Test middleware running'); console.log(req.headers); + console.log(req.query); + res.setHeader('X-Test-Header', 'test'); next(); } diff --git a/api/package.json b/api/package.json index f8c53e9fa54..f456a9bce81 100644 --- a/api/package.json +++ b/api/package.json @@ -4,7 +4,7 @@ "url": "https://github.com/freeCodeCamp/freeCodeCamp/issues" }, "dependencies": { - "@fastify/middie": "8.0.0", + "@fastify/middie": "8.1", "@fastify/mongodb": "6.1.0", "fastify": "4.9.2", "fastify-plugin": "4.3.0" @@ -32,7 +32,7 @@ "url": "git+https://github.com/freeCodeCamp/freeCodeCamp.git" }, "scripts": { - "build": "tsc index.ts", + "build": "tsc", "develop": "nodemon index.ts", "start": "NODE_ENV=production node index.js", "test": "NODE_OPTIONS='--test-only' ts-node **/*.test.ts" diff --git a/api/tsconfig.json b/api/tsconfig.json new file mode 100644 index 00000000000..10dcdad3a1f --- /dev/null +++ b/api/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es2022", + "module": "CommonJS", + "allowJs": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true, + "resolveJsonModule": true + } +} diff --git a/api/utils/index.ts b/api/utils/index.ts index 7c1f0bdf0ef..17899676d89 100644 --- a/api/utils/index.ts +++ b/api/utils/index.ts @@ -13,7 +13,3 @@ function sha256(buf: Buffer) { return createHash('sha256').update(buf).digest(); } export const challenge = base64URLEncode(sha256(Buffer.from(verifier))); - -// This is used for Fastify middleware, but is not exported from Fastify itself. -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type NextFunction = (err?: any) => void; diff --git a/package-lock.json b/package-lock.json index 6b3e7d6ec3a..ff9441d9032 100644 --- a/package-lock.json +++ b/package-lock.json @@ -114,7 +114,7 @@ "version": "0.0.1", "license": "BSD-3-Clause", "dependencies": { - "@fastify/middie": "8.0.0", + "@fastify/middie": "8.1", "@fastify/mongodb": "6.1.0", "fastify": "4.9.2", "fastify-plugin": "4.3.0" @@ -4575,24 +4575,18 @@ } }, "node_modules/@fastify/middie": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/@fastify/middie/-/middie-8.0.0.tgz", - "integrity": "sha512-SsZUzJwRV2IBhko8TNI5gGzUdUp2Xd0XCrU+pBTfsMN8LYGsksDI/Hb3qcUZ2/Kfg6ecbFEeRO4nZmHeFCDpHQ==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/@fastify/middie/-/middie-8.1.0.tgz", + "integrity": "sha512-VvUCLfKx2j6KSnh8puT8QW7d5YNzi2fD/4HcFvRQ3a7sHlCo+qtfX2fqzFvNqnMVbNft7GX1JL5if/riUiXsyg==", "dependencies": { - "fastify-plugin": "^3.0.0", + "fastify-plugin": "^4.0.0", "path-to-regexp": "^6.1.0", "reusify": "^1.0.4" } }, - "node_modules/@fastify/middie/node_modules/fastify-plugin": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-3.0.1.tgz", - "integrity": "sha512-qKcDXmuZadJqdTm6vlCqioEbyewF60b/0LOFCcYN1B6BIZGlYJumWWOYs70SFYLDAH4YqdE1cxH/RKMG7rFxgA==" - }, "node_modules/@fastify/middie/node_modules/path-to-regexp": { "version": "6.2.1", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.1.tgz", - "integrity": "sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==" + "license": "MIT" }, "node_modules/@fastify/mongodb": { "version": "6.1.0", diff --git a/tsconfig.json b/tsconfig.json index 3977b9c76d2..9e2f6e026d7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "include": [ - "api", "client/i18n/**/*", "client/plugins/**/*", "client/src/**/*",