Refactor the repeated request-id cast into a single typed accessor
Description
The expression (req as Request & { id?: string }).id (and its { id: string } variant) appears more than 20 times throughout src/index.ts — in nearly every handler and middleware that builds an error body or logs. This verbose, repeated cast is noise that obscures the handlers and risks subtle drift (id? vs id). This issue replaces it with one typed helper, narrowly scoped so it does not require the full AppError/module split tracked elsewhere.
Requirements and context
- Repository scope: Agentpay-Org/Agentpay-backend only.
- Add a single
getRequestId(req): string helper (and/or an Express Request type augmentation in a .d.ts) and replace every (req as Request & { id... }).id site with it.
- Produce byte-for-byte identical responses and logs — the value returned must equal what the cast returned (the id set by the request-id middleware).
- Do not change route paths, status codes, or response shapes; this is a pure readability refactor distinct from the broader
AppError centralization.
- Ensure the existing test suite stays green unchanged.
Suggested execution
- Fork the repo and create a branch
git checkout -b refactor/api-75-request-id-helper
- Implement changes
- Write code in: a
getRequestId helper (and optional src/types/express.d.ts) and the call sites in src/index.ts.
- Write comprehensive tests in: new
src/request-id-helper.test.ts — helper returns the middleware-set id, error bodies still carry the same requestId.
- Add documentation: none beyond TSDoc.
- Add TSDoc on the helper.
- Validate security assumptions: the id is still the sanitized/minted value, never user-controlled beyond the existing middleware.
- Test and commit
Test and commit
- Run
npm test and npm run lint.
- Cover edge cases: id echoed in 400/404/429/503 bodies unchanged, log line unchanged.
- Include the full
npm test output in the PR description.
Example commit message
refactor: replace repeated request-id casts with a typed helper
Guidelines
- Minimum 95 percent test coverage for impacted modules.
- Clear, reviewer-focused documentation.
- Timeframe: 96 hours.
Community & contribution rewards
- 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
- ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.
Refactor the repeated request-id cast into a single typed accessor
Description
The expression
(req as Request & { id?: string }).id(and its{ id: string }variant) appears more than 20 times throughoutsrc/index.ts— in nearly every handler and middleware that builds an error body or logs. This verbose, repeated cast is noise that obscures the handlers and risks subtle drift (id?vsid). This issue replaces it with one typed helper, narrowly scoped so it does not require the fullAppError/module split tracked elsewhere.Requirements and context
getRequestId(req): stringhelper (and/or an ExpressRequesttype augmentation in a.d.ts) and replace every(req as Request & { id... }).idsite with it.AppErrorcentralization.Suggested execution
git checkout -b refactor/api-75-request-id-helpergetRequestIdhelper (and optionalsrc/types/express.d.ts) and the call sites insrc/index.ts.src/request-id-helper.test.ts— helper returns the middleware-set id, error bodies still carry the samerequestId.Test and commit
npm testandnpm run lint.npm testoutput in the PR description.Example commit message
refactor: replace repeated request-id casts with a typed helperGuidelines
Community & contribution rewards