Skip to content

refactor: Extract duplicated code patterns into shared utilities#3

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1781203804-refactor-shared-utilities
Open

refactor: Extract duplicated code patterns into shared utilities#3
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1781203804-refactor-shared-utilities

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

Eliminates ~380 lines of duplicated code across the backend controllers and frontend by extracting recurring patterns into reusable shared utilities.

Backend — new shared utilities:

Utility Purpose
asyncHandler(fn) Wraps async controllers to auto-catch errors → 500 { message: "Error in server" }. Removes ~25 identical try/catch blocks.
prismaSelects.ts Typed Prisma select constants (repoSummarySelect, issueDetailSelect, userSummarySelect, commentDetailSelect) — previously copy-pasted 4-5x each.
findRepoWithAccess(owner, repo, userId?) Repo lookup with public/owner visibility check — was duplicated in issue, comment, and fork controllers.
verifyRepoOwner(username, req, res) Owner lookup + 404/403 guard — was duplicated 3x in repo.controller.ts.
invalidateTokens(access, refresh, res) Token decode → blacklist → clear cookies — was duplicated in logout, refresh, and delete-user flows.
buildUpdateData(fields, res) Filters truthy fields into update object, returns 400 if empty — was duplicated in update-repo, update-issue (2x), update-user.
validate middleware Reusable Zod validation for params/query/body (available for future route-level usage).

Frontend:

  • Consolidated UserProfile / Repository interfaces into types/user.ts (was defined separately in useUser.ts and ProfilePage.tsx).
  • Removed duplicate getMe API functionauth.api.ts now delegates to getCurrentUserApi from user.api.ts.
  • Fixed implicit any type on repo map parameter in ProfilePage.tsx.

Example — before vs after (controller pattern):

// Before: repeated in every handler
export const getMyRepos = async (req, res) => {
  try {
    // ...logic...
    // ...same select object copy-pasted...
  } catch (error) {
    console.log("Error in getMyRepos: ", error);
    res.status(500).json({ message: "Error in server" });
  }
};

// After:
export const getMyRepos = asyncHandler(async (req, res) => {
  // ...logic using repoListItemSelect...
  // error handling is automatic
});

Link to Devin session: https://app.devin.ai/sessions/1f25cda24527464e95ff67b60b2e1f9b
Requested by: @amnkarn

Backend:
- asyncHandler: wraps controllers to eliminate ~25 duplicated try/catch blocks
- prismaSelects: shared Prisma select constants (repoSummary, issueDetail, userSummary, commentDetail)
- repoHelpers: findRepoWithAccess + verifyRepoOwner for repo lookup patterns
- invalidateTokens: consolidates token decode/blacklist/clear-cookie logic
- buildUpdateData: extracts partial-update building pattern
- validate middleware: reusable Zod validation middleware for routes

Frontend:
- Consolidate duplicate UserProfile interface into types/user.ts
- Consolidate duplicate getMe API call (auth.api.ts delegates to user.api.ts)
- Fix implicit 'any' type on repo param in ProfilePage

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant