Skip to content

refactor: typed HTTPError and response-shape detection in apiPull#50

Merged
robrechtme merged 1 commit into
mainfrom
refactor/typed-http-error-and-shape-detection
May 5, 2026
Merged

refactor: typed HTTPError and response-shape detection in apiPull#50
robrechtme merged 1 commit into
mainfrom
refactor/typed-http-error-and-shape-detection

Conversation

@robrechtme
Copy link
Copy Markdown
Owner

@robrechtme robrechtme commented May 5, 2026

Summary

  • Add an HTTPError class with typed status / statusText fields. fetchApi throws it on non-2xx responses; handleAsyncErrors now matches the 401 access-key hint by error instanceof HTTPError && error.status === 401 instead of regex-matching the message string (which broke when Loco varied the wording between Unauthorized and Authorization Required).
  • apiPull decides whether to wrap the /export/all.json payload based on the response itself — if every top-level key matches a known locale code, it's already locale-keyed and we leave it alone. Single-locale flat responses still get wrapped under the project's locale code. Same number of API calls (still 2 in parallel), more robust against locale-list races and future Loco shape changes.
  • Tests now assert instanceof HTTPError + .status instead of message-substring matches.

Smoke-tested end-to-end against a live multi-locale Loco project: pull, status (clean + dirty), push, and 401-with-invalid-key — all behave as expected.

Test plan

  • pnpm build && pnpm test && pnpm lint && pnpm format:check all pass
  • loco-cli pull against a multi-locale project still returns the locale-keyed shape
  • loco-cli pull against a single-locale project still wraps the flat response under the locale code
  • loco-cli pull with an invalid access key prints the friendly hint and exits 1
  • loco-cli push still uploads per-locale and surfaces Loco's response message

- Add `HTTPError` class with `status` / `statusText` fields. `fetchApi`
  throws it on non-2xx responses; `handleAsyncErrors` matches the 401
  hint by `error instanceof HTTPError && error.status === 401` instead
  of regex-matching the message string (which broke when Loco varied
  the wording between `Unauthorized` and `Authorization Required`).
- `apiPull` decides whether to wrap the `/export/all.json` payload
  based on the response itself (top-level keys all match known locale
  codes ⇒ already locale-keyed) instead of inferring purely from the
  separate `/locales` count. Same number of API calls, more robust
  against locale-list races and future Loco shape changes.
- Tests assert `instanceof HTTPError` + `.status` instead of message
  substring match.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
loco-cli Ready Ready Preview, Comment May 5, 2026 9:14am

@robrechtme robrechtme merged commit 7b3a533 into main May 5, 2026
3 checks passed
@robrechtme robrechtme deleted the refactor/typed-http-error-and-shape-detection branch May 5, 2026 09:18
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.

1 participant