fix: improve type safety in team.ts, remove any usages (closes #554)#581
fix: improve type safety in team.ts, remove any usages (closes #554)#581yachikadev wants to merge 2 commits into
Conversation
|
@yachikadev is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @yachikadev, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — Checks FailedBackend — FAIL
Mobile — SKIP
Web — SKIP
Last updated: |
4ddeb93 to
5b06033
Compare
Harxhit
left a comment
There was a problem hiding this comment.
Checked this out and tested locally. The type-safety work on the route file itself is genuinely good — every any is gone and the typing is correct — but the PR breaks the entire team.test.ts suite because a setup change described in the PR body was never actually committed.
Verified ✅
team.ts: allanyusages removed (request.user as any,request.server as any,(app as any),FastifyRequest<{...}>casts).grepforanyreturns nothing, and there are no newas/unsafe casts.- Generic route typing (
app.post<{ Body: ... }>, etc.) is applied correctly, and inferredrequest/replytypes line up. request.user.idis now properly typed via the@fastify/jwt+fastifyaugmentation insrc/types/fastify.d.ts, so dropping the casts is type-safe.- Behavior is preserved against Prisma return shapes (the
GET /:slugmember mapping is unchanged). tsc --noEmitpasses with 0 errors. ESLint on the changed file is clean.
Suggestions
- [blocker] The test suite is fully broken — all 45 tests fail.
npx vitest run src/__tests__/team.test.tserrors inbeforeEachwithTypeError: Cannot read properties of undefined (reading 'bind'). The routes now callapp.authenticate.bind(app), butbuildApp()inteam.test.tsnever decoratesapp.authenticate— so it'sundefinedand.bindthrows at registration. The PR body says "Addedauthenticatedecorator to team.test.ts setup", but that change is not in this diff (onlyteam.tsis modified). Please commit the test-setup change. A minimal version:(Make sure the catch returns 401 so theapp.decorate('authenticate', async function (request, reply) { try { const payload = await request.jwtVerify(); if (payload) request.user = payload; } catch { return reply.status(401).send({ error: 'Unauthorized' }); } });
rejects unauthenticated requestcases keep passing.) With this added locally, the suite goes green. - [minor]
.bind(app)is unnecessary. Theauthenticatedecorator (app.ts:109) closes overappand doesn't usethis, sopreHandler: [app.authenticate]works identically — and that's the pattern already used inauth.ts:614. Dropping.bind(app)keeps this file consistent. - [nit] Consistency with sibling routes. The other route files still carry the old
(app as any).authenticatefallback chains. Out of scope here, but worth a follow-up issue so this cleanup can go repo-wide.
Once the team.test.ts setup change is included so the suite passes, this is a clean type-safety improvement and good to merge.
Reviewed locally — ran tsc --noEmit (0 errors), vitest run src/__tests__/team.test.ts (45/45 failing on missing app.authenticate decorator in test setup), and eslint src/routes/team.ts (clean).
- Add authenticate decorator inside buildApp() before app.register(teamRoutes), since Fastify executes plugin registration during app.ready() — decorator must exist before routes are registered, not after buildApp() returns - Remove redundant .bind(app) calls in team.ts; authenticate decorator doesn't use 'this', consistent with auth.ts pattern - Cast payload via 'as typeof request.user' to satisfy TS in test mock
Closes #554
anyusages in team.ts (preHandler casts, request.user casts)app.authenticatedecoratorapp.post<{...}>etc.) for proper preHandler/handler type matchingauthenticatedecorator to team.test.ts setup (required for app.authenticate to resolve in test env)tsc --noEmitpasses with 0 errors, noanyremains