Modernize: use node-fetch, fix naming, improve error handling #1

Closed
ada wants to merge 1 commit from ada:feat/modernize into main
Contributor

Changes

This PR cleans up several issues in the codebase:

Use node-fetch instead of raw https.request

The custom fetch wrapper using https.request was reinventing the wheel — node-fetch is already a dependency in package.json. Replaced the custom implementation with the real thing. This also fixes the fact that the custom fetch did not parse JSON, so res.json() would not have worked on the raw http.IncomingMessage object it returned.

Remove Promise.sleep monkey-patch

Replaced Promise.sleep = (ms) => ... (mutating the Promise prototype) with a local sleep() function. Monkey-patching globals is a code smell and can cause issues if this is ever imported as a module.

Fix naming inconsistency in filterGames

The parameter was named targetTeamId but it is compared against teamTricode — it is a 3-letter string code, not a numeric ID. Renamed to targetTeamCode for clarity.

Remove unused teams parameter from gameToEvent

gameToEvent took a teams argument that was never used. Removed it and updated the caller.

Replace bare throw strings with Error objects

throw "Invalid config.json..." creates a string exception, which is not a proper Error and loses the stack trace. Replaced with throw new Error(...).

Replace bare return with throw in main flow

return "Error fetching season" silently exits the async IIFE with an unhandled string. Replaced with throw new Error(...).

Add rate limiting on calendar inserts

Added a sleep(CALENDAR_INSERT_DELAY_MS) after each calendar.events.insert call, matching the pattern already used in clearEvents. This prevents hitting Google API rate limits on the insert path too.

Extract constants

NBA_SCHEDULE_URL and CALENDAR_INSERT_DELAY_MS are now named constants at the top of the file instead of inline magic strings/numbers.

Remove dead code

downloadAndSave and downloadAndSaveSchedule were only used to fetch the schedule, but getSeason already handled caching. Simplified to a single fetchSchedule() function.

Improve README

  • Added docs for schedule caching behavior
  • Added docs for the clearEvents option
  • Fixed markdown formatting (proper headers, code blocks, bold for warnings)
## Changes This PR cleans up several issues in the codebase: ### Use `node-fetch` instead of raw `https.request` The custom `fetch` wrapper using `https.request` was reinventing the wheel — `node-fetch` is already a dependency in `package.json`. Replaced the custom implementation with the real thing. This also fixes the fact that the custom fetch did not parse JSON, so `res.json()` would not have worked on the raw `http.IncomingMessage` object it returned. ### Remove `Promise.sleep` monkey-patch Replaced `Promise.sleep = (ms) => ...` (mutating the Promise prototype) with a local `sleep()` function. Monkey-patching globals is a code smell and can cause issues if this is ever imported as a module. ### Fix naming inconsistency in `filterGames` The parameter was named `targetTeamId` but it is compared against `teamTricode` — it is a 3-letter string code, not a numeric ID. Renamed to `targetTeamCode` for clarity. ### Remove unused `teams` parameter from `gameToEvent` `gameToEvent` took a `teams` argument that was never used. Removed it and updated the caller. ### Replace bare `throw` strings with `Error` objects `throw "Invalid config.json..."` creates a string exception, which is not a proper Error and loses the stack trace. Replaced with `throw new Error(...)`. ### Replace bare `return` with `throw` in main flow `return "Error fetching season"` silently exits the async IIFE with an unhandled string. Replaced with `throw new Error(...)`. ### Add rate limiting on calendar inserts Added a `sleep(CALENDAR_INSERT_DELAY_MS)` after each `calendar.events.insert` call, matching the pattern already used in `clearEvents`. This prevents hitting Google API rate limits on the insert path too. ### Extract constants `NBA_SCHEDULE_URL` and `CALENDAR_INSERT_DELAY_MS` are now named constants at the top of the file instead of inline magic strings/numbers. ### Remove dead code `downloadAndSave` and `downloadAndSaveSchedule` were only used to fetch the schedule, but `getSeason` already handled caching. Simplified to a single `fetchSchedule()` function. ### Improve README - Added docs for schedule caching behavior - Added docs for the `clearEvents` option - Fixed markdown formatting (proper headers, code blocks, bold for warnings)
- Replace custom https.request fetch with node-fetch (already a dependency)
- Remove Promise.sleep monkey-patch, use local sleep() function
- Extract NBA_SCHEDULE_URL and CALENDAR_INSERT_DELAY_MS as constants
- Fix filterGames param name: targetTeamId → targetTeamCode (it's a tricode, not a numeric ID)
- Remove unused 'teams' param from gameToEvent
- Replace bare throw strings with Error objects in readConfig
- Replace bare 'return' with throw in main flow
- Add rate limiting delay after each calendar insert (not just on delete)
- Remove dead downloadAndSave function (was only used by downloadAndSaveSchedule)
- Improve README with schedule caching docs and clearEvents usage
ada closed this pull request 2026-05-17 22:59:00 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
freemasen/nba-calendar-utility!1
No description provided.