Modernize: use node-fetch, fix naming, improve error handling #1
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "ada:feat/modernize"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Changes
This PR cleans up several issues in the codebase:
Use
node-fetchinstead of rawhttps.requestThe custom
fetchwrapper usinghttps.requestwas reinventing the wheel —node-fetchis already a dependency inpackage.json. Replaced the custom implementation with the real thing. This also fixes the fact that the custom fetch did not parse JSON, sores.json()would not have worked on the rawhttp.IncomingMessageobject it returned.Remove
Promise.sleepmonkey-patchReplaced
Promise.sleep = (ms) => ...(mutating the Promise prototype) with a localsleep()function. Monkey-patching globals is a code smell and can cause issues if this is ever imported as a module.Fix naming inconsistency in
filterGamesThe parameter was named
targetTeamIdbut it is compared againstteamTricode— it is a 3-letter string code, not a numeric ID. Renamed totargetTeamCodefor clarity.Remove unused
teamsparameter fromgameToEventgameToEventtook ateamsargument that was never used. Removed it and updated the caller.Replace bare
throwstrings withErrorobjectsthrow "Invalid config.json..."creates a string exception, which is not a proper Error and loses the stack trace. Replaced withthrow new Error(...).Replace bare
returnwiththrowin main flowreturn "Error fetching season"silently exits the async IIFE with an unhandled string. Replaced withthrow new Error(...).Add rate limiting on calendar inserts
Added a
sleep(CALENDAR_INSERT_DELAY_MS)after eachcalendar.events.insertcall, matching the pattern already used inclearEvents. This prevents hitting Google API rate limits on the insert path too.Extract constants
NBA_SCHEDULE_URLandCALENDAR_INSERT_DELAY_MSare now named constants at the top of the file instead of inline magic strings/numbers.Remove dead code
downloadAndSaveanddownloadAndSaveSchedulewere only used to fetch the schedule, butgetSeasonalready handled caching. Simplified to a singlefetchSchedule()function.Improve README
clearEventsoptionPull request closed