Fix: Eliminate 'fetch failed' error in FL wedge purchases#27
Merged
Conversation
…l HTTP request Eliminates 'fetch failed' production error by replacing internal fetch() call to /api/use-wedge with direct function call to shared wedge service. Changes: - Created src/lib/wedge.js with purchaseFlWedge() service function - Updated app/api/add/route.js to call purchaseFlWedge() directly - Refactored app/api/use-wedge/route.js to use shared service - Added comprehensive service-level tests (__tests__/wedge.test.mjs) - Updated add.test.mjs to mock wedge service instead of fetch - Enhanced logging with [Wedge Service] prefix for easier debugging Benefits: - Fixes production bug where req.nextUrl.origin was undefined/invalid - Eliminates network overhead from internal API calls - Works reliably in all deployment environments - Improved error handling and logging - Better code organization and testability - Net reduction of 58 lines of code All 208 tests passing.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the production bug where FL wedge purchases fail with "fetch failed" error after attempting to download books.
Problem
The
/api/addroute was making an internal HTTP request to/api/use-wedgeusingreq.nextUrl.origin. In production, this caused "fetch failed" errors because:req.nextUrl.originwas undefined or malformed in certain deployment environmentsSolution
Refactored FL wedge logic to use direct function calls instead of internal HTTP requests:
Created shared wedge service (
src/lib/wedge.js)purchaseFlWedge(torrentId)function[Wedge Service]prefixUpdated add route (
app/api/add/route.js)fetch(${req.nextUrl.origin}/api/use-wedge)with directpurchaseFlWedge()callreq.nextUrl.origintokenExpiredflagRefactored API route (
app/api/use-wedge/route.js)purchaseFlWedge()functionAdded comprehensive tests (
__tests__/wedge.test.mjs)Benefits
✅ Fixes the production bug - No more "fetch failed" errors
⚡ Performance improvement - Eliminates network roundtrip overhead
🛡️ More reliable - Works in all deployment environments
📈 Better code quality - Net reduction of 58 lines
🔧 Easier to maintain - Shared service can be reused anywhere
🧪 Better test coverage - 208 tests passing
Testing
Changes Summary
Deployment Notes
Verification Steps
After deployment:
[Wedge Service]messages (no "fetch failed" errors)