Skip to content

refactor(connect): improve type safety in connect.ts and remove remaining any usage#596

Open
hariom888 wants to merge 1 commit into
Dev-Card:mainfrom
hariom888:fix/548-connect-ts-type-safety
Open

refactor(connect): improve type safety in connect.ts and remove remaining any usage#596
hariom888 wants to merge 1 commit into
Dev-Card:mainfrom
hariom888:fix/548-connect-ts-type-safety

Conversation

@hariom888

Copy link
Copy Markdown
Contributor

Summary

Closes #548.

Removes the 13 remaining any usages in connect.ts — 4 duplicated preauth boilerplate blocks (GET /status, GET /github, GET /github/autodiscover, DELETE /:platform), the 4 (request.user as any).id casts those blocks fed into, and the (await tokenRes.json()) as any in github/callback.

Root cause

Each authenticated route in this file carried its own copy of:

const server = request.server as any;
if (typeof server?.authenticate === 'function') { ... }
if (typeof (app as any).authenticate === 'function') { ... }

bypassing the FastifyInstance.authenticate decorator type already declared in types/fastify.d.ts, and the FastifyJWT.user augmentation that already types request.user as AuthenticatedUser. auth.ts's /me and DELETE /logout routes already use the clean preHandler: [app.authenticate] form — this PR brings connect.ts in line with that existing pattern rather than introducing a new one.

Separately, github/callback's token-exchange response was untyped (as any), even though auth.ts already defines GitHubTokenResponse/GitHubTokenErrorResponse and isGitHubTokenError for the exact same GitHub token endpoint in the login flow. This PR reuses those exports from utils/error.util.ts instead of duplicating or leaving it untyped.

Fix

Collapse each preauth block:

// Before
preHandler: [async (request, reply) => {
  const server = request.server as any;
  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); return }
  try { await request.jwtVerify() } catch { reply.status(401).send({ error: 'Unauthorized' }) }
}],
// After
preHandler: [app.authenticate],

Type the GitHub token exchange:

// Before
const tokenData = (await tokenRes.json()) as any;
if (tokenData.error) { ... }
// After
const tokenData = (await tokenRes.json()) as GitHubTokenResponse | GitHubTokenErrorResponse;
if (isGitHubTokenError(tokenData)) { ... }

Files changed

File Change
apps/backend/src/routes/connect.ts Remove all any usage; reuse shared auth/JWT types and GitHub token types from error.util.ts

No changes needed in types/fastify.d.ts or utils/error.util.ts — both already export everything this PR reuses.

Tests

No new tests — existing __tests__/connect.test.ts already covers both the GitHub token-exchange success and error paths, and both still pass against isGitHubTokenError. This is a pure typing refactor with no behavior change.

Checklist

  • No any usages remain in connect.ts
  • Reuses existing FastifyJWT/Fastify types (types/fastify.d.ts) and GitHub token types (utils/error.util.ts) — no duplicate definitions introduced
  • tsc --noEmit passes
  • eslint clean on the changed file
  • Existing connect.test.ts suite passes unchanged

@vercel

vercel Bot commented Jun 17, 2026

Copy link
Copy Markdown

@hariom888 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.

@github-actions github-actions Bot added backend gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. labels Jun 17, 2026
@github-actions

Copy link
Copy Markdown

Hi @hariom888,

Thanks for opening this pull request.

This PR has been automatically classified based on the files modified.

Applied Labels

  • gssoc:approved
  • backend

Primary Review Area

  • backend

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!

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown

CI — All Checks Passed

Backend — PASS

Check Result
Lint PASS
Test PASS
Typecheck PASS

Mobile — SKIP

Check Result
Lint -
Test -

Web — SKIP

Check Result
Build -

Last updated: Wed, 17 Jun 2026 19:59:05 GMT

…response

Collapses the 4 boilerplate preauth blocks (GET /status, GET /github, GET /github/autodiscover, DELETE /:platform) down to preHandler: [app.authenticate], matching the pattern already used in auth.ts's /me and DELETE /logout routes. The FastifyJWT augmentation in types/fastify.d.ts already types request.user as AuthenticatedUser, so the (request.user as any).id casts are no longer needed.

Also types the GitHub token-exchange response in github/callback using GitHubTokenResponse/GitHubTokenErrorResponse and isGitHubTokenError from utils/error.util.ts, reusing the same types auth.ts already defines for the equivalent login-flow exchange instead of �s any.
@hariom888 hariom888 force-pushed the fix/548-connect-ts-type-safety branch from 5f02f64 to 7bec51c Compare June 17, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Type Safety in connect.ts and Remove Remaining any Usage

1 participant