fix(backend): remove any usages from profiles.ts to improve type safety#589
Conversation
|
@Satvik-art-creator 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. |
|
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! |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR tightens type safety in the profiles routes by removing any casts, using typed request.user, and improving Prisma error handling for unique-constraint conflicts.
Changes:
- Replace
(request.user as any).idwithrequest.user.idacross profile endpoints. - Update error handling to catch
unknownand narrow Prisma errors viaPrismaClientKnownRequestError. - Remove
anyusage aroundauthenticateaccess onrequest.server/app.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const server = request.server; | ||
| if (typeof server?.authenticate === 'function') { | ||
| await server.authenticate(request, reply); | ||
| return; | ||
| } | ||
| if (typeof (app as any).authenticate === 'function') { | ||
| await (app as any).authenticate(request, reply); | ||
| if (typeof app.authenticate === 'function') { | ||
| await app.authenticate(request, reply); | ||
| return; | ||
| } |
|
|
||
| app.get('/me', async (request: FastifyRequest, reply: FastifyReply) => { | ||
| const userId = (request.user as any).id; | ||
| const userId = request.user.id; |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
1d4858f to
1077cb5
Compare
Harxhit
left a comment
There was a problem hiding this comment.
Clean, well-scoped refactor — the any removals are all correct against the existing fastify.d.ts augmentation (request.user is typed as AuthenticatedUser, and FastifyInstance.authenticate is declared), so request.user.id and app.authenticate type-check without casts. Replacing err: any + err?.code === 'P2002' with err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002' is the right pattern, and the unused getProfileUrl / getErrorMessage imports were correctly dropped. A couple of small inline notes below.
| import { updateProfileSchema, createLinkSchema, reorderLinksSchema } from '../utils/validators.js'; | ||
|
|
||
| import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars |
There was a problem hiding this comment.
ProfileUpdateResponse is now suppressed with an eslint-disable for being unused rather than removed. The PR description says no behavior changes and this type isn't referenced anywhere — prefer deleting the dead type outright over silencing the linter. If it's intentionally kept as living API documentation, a one-line comment saying so would be clearer than the bare disable.
| } | ||
| if (typeof (app as any).authenticate === 'function') { | ||
| await (app as any).authenticate(request, reply); | ||
| if (typeof app.authenticate === 'function') { |
There was a problem hiding this comment.
Now that the casts are gone, the typing makes the redundancy here visible: request.server is the app instance, so the server?.authenticate branch (line 25) and this app.authenticate branch are the same decorator — and FastifyInstance.authenticate is now declared non-optional in fastify.d.ts, so the typeof === 'function' guards and the jwtVerify fallback are effectively dead code. Out of scope for a pure type-safety PR, but worth a follow-up to collapse this to a single await app.authenticate(request, reply).
| } catch (err: any) { | ||
| if (err?.code === 'P2002') return reply.status(409).send({ error: 'Username already taken' }) | ||
| } catch (err: unknown) { | ||
| if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') { |
There was a problem hiding this comment.
Good change. Since Prisma is now imported solely for this guard, just confirm @prisma/client is generated in CI before typecheck/lint runs (the import resolves to generated types) — otherwise lint/typecheck could fail on a clean checkout.
|
Congratulations @Satvik-art-creator on getting PR #589 merged! Thank you for your contribution to the project. To receive the appropriate GSSoC labels and recognition, please mention @Harxhit in the #get-labels channel on our Discord server and share your merged PR link. |
Summary
This PR improves type safety in the profile routes by removing the remaining usages of
anyinapps/backend/src/routes/profiles.ts. By leveraging existing Fastify request typings and Prisma error classes, this makes the route handlers and error handling logic safer and more maintainable.This issue is solved under GSSoC '26.
Closes #552
Type of Change
What Changed
apps/backend/src/routes/profiles.ts: Replacedrequest.server as anyand(app as any).authenticatewith properly typed Fastify instance property access (request.serverandapp.authenticate), leveraging the existingFastifyInstanceaugmentation infastify.d.ts.apps/backend/src/routes/profiles.ts: Replaced all instances of(request.user as any).idwithrequest.user.id, utilizing the native JWT authentication typings.apps/backend/src/routes/profiles.ts: Replaced unsafecatch (err: any)withcatch (err: unknown)and utilizedPrisma.PrismaClientKnownRequestErrorto safely trapP2002(unique constraint) errors.How to Test
git checkout fix/profile-ts-typescd apps/backend && npm installanyerrors:npm run typecheckChecklist
pnpm -r run lintpasses).pnpm -r run typecheck).pnpm -r run test).console.logor debug statements left in the code.Additional Context
No application behavior or logic has been altered. This PR simply removes TS-compiler overrides to improve the codebase quality and safety.