Skip to content

feat: add shared fetchJson helper for API calls - #62

Open
nicomiguelino wants to merge 9 commits into
mainfrom
feat/fetch-helper-utils
Open

feat: add shared fetchJson helper for API calls#62
nicomiguelino wants to merge 9 commits into
mainfrom
feat/fetch-helper-utils

Conversation

@nicomiguelino

@nicomiguelino nicomiguelino commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds fetchJson and fetchJsonOrDefault helpers in src/utils/http.ts to remove duplicated fetch boilerplate (checking response.ok, logging warnings, parsing JSON) across the library.
  • fetchJson throws a FetchJsonError on a non-ok response, letting callers catch and handle it however they need. fetchJsonOrDefault wraps it, logs a warning, and returns a caller-supplied fallback instead of throwing.
  • Both apply a default timeoutMs of 8 seconds (DEFAULT_TIMEOUT_MS) so requests are bounded by default, keeping the app inside the screenshotter's ~10s network-idle budget. Callers can override this by passing their own timeoutMs, including 0 or Infinity to disable the timeout entirely.
  • Refactors the three JSON-based call sites to use the new helpers:
    • oauth.ts getCredentials: now checks response.ok (previously it did not), throwing FetchJsonError on failure.
    • weather.ts fetchCurrentWeatherData and getCityInfo: swapped raw fetch + manual checks for fetchJsonOrDefault, which also removed a duplicated fallback branch in getCityInfo.
  • theme.ts fetchLogoImage was left untouched. It works with response.blob() / arrayBuffer() rather than JSON, so the new JSON-oriented helper is not a good fit there.

Test plan

  • npm run type-check
  • npm run lint
  • npm run test
  • npm run format:check
  • Added src/utils/http.test.ts covering success, non-ok response, network error, timeout, fallback behavior, the default timeout, and overriding the default timeout.

- Add fetchJson and fetchJsonOrDefault helpers in src/utils/http.ts that
  wrap fetch, check response.ok, and support an optional timeoutMs
- fetchJson throws a FetchJsonError on a non-ok response, fetchJsonOrDefault
  catches failures, logs a warning, and returns a caller-supplied fallback
- Refactor oauth.ts getCredentials, and weather.ts fetchCurrentWeatherData
  and getCityInfo to use the new helpers, removing duplicated fetch
  boilerplate
- Add tests for the new http utilities
@nicomiguelino nicomiguelino self-assigned this Jul 22, 2026
- Add DEFAULT_TIMEOUT_MS (8000ms) applied when timeoutMs is omitted
- Keep the timeout overridable, including disabling it with 0 or Infinity
- Add tests covering the default timeout and overriding it
- add HTTP Requests subsection under Core Functions
- cover FetchJsonError, fallback behavior, and the 8s default timeout with timeoutMs override examples
- attach parsed error payload to FetchJsonError as an optional body field
- add FetchJsonParseError for ok responses whose body is not valid JSON
- resolve ok responses with an empty body to undefined instead of throwing
- add test coverage for both new failure paths and fetchJsonOrDefault
- document the new behavior in the README HTTP Requests section
- Bump package.json version from 1.2.1 to 1.3.0
- Sync package-lock.json version to match
@nicomiguelino
nicomiguelino marked this pull request as ready for review July 23, 2026 04:32
@nicomiguelino
nicomiguelino requested a review from Copilot July 23, 2026 04:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Comment thread src/utils/http.ts
Comment thread src/utils/http.test.ts
Comment thread README.md
- fetchJsonOrDefault no longer forwards undefined from an ok, empty-body
  response when a non-undefined fallback is provided
- update JSDoc to document the empty-body fallback behavior
- add test covering array and object fallbacks on a 204 response
- restore console.warn spies via a shared afterEach instead of scattered
  per-test mockRestore calls, so a spy cannot leak into later tests
- rename repeated 'const settings' declarations in both HTTP Requests
  code blocks so each block is valid TypeScript on its own

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Comment thread src/utils/http.ts Outdated
Comment thread src/utils/http.ts
Comment thread src/utils/http.ts
…ined typing

- combine caller-provided AbortSignal with the timeout controller's signal
  via AbortSignal.any so either one cancels the request
- redact sensitive query params and truncate long bodies before
  console.warn logs a FetchJsonError or FetchJsonParseError in
  fetchJsonOrDefault, reusing sentry.ts's SENSITIVE_KEY_PATTERNS
- change fetchJson's return type to Promise<T | undefined> to reflect
  that an empty response body resolves to undefined at runtime
- update getCredentials in oauth.ts to throw when the OAuth token
  endpoint returns an empty body, keeping its own return type non-optional
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.

2 participants