Repository navigation
fix: add root Vercel API rewrite and robust frontend error handling - #70
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR adds a Vercel edge rewrite for /api/* requests at the repo root and updates the frontend jobsService to produce more user-friendly errors when the backend/proxy returns non-JSON responses.
Changes:
- Added
vercel.jsonrewrite rules to forward/api/*to the external backend host. - Introduced a
readPayloadhelper infrontend/src/services/jobsService.tsto handle non-JSON error bodies more gracefully. - Added a unit test covering the “non-JSON error response” scenario for
fetchKeywords.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| vercel.json | Adds root-level /api/* rewrite to external backend. |
| frontend/src/services/jobsService.ts | Centralizes response payload parsing with JSON/text fallback for error handling. |
| frontend/tests/unit/utils/jobsService.test.ts | Adds coverage for readable errors when the response isn’t JSON. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| async function readPayload(response: Response): Promise<Record<string, unknown>> { | ||
| const contentType = response.headers?.get?.("content-type") ?? ""; | ||
|
|
||
| if (!contentType || contentType.includes("application/json")) { | ||
| try { | ||
| const payload = await response.json(); | ||
| return payload && typeof payload === "object" ? (payload as Record<string, unknown>) : {}; | ||
| } catch { | ||
| // Fallback below for non-JSON bodies returned by proxies/platforms. | ||
| } | ||
| } | ||
|
|
||
| if (typeof response.text === "function") { | ||
| const text = await response.text(); | ||
| return text ? { message: text } : {}; | ||
| } |
There was a problem hiding this comment.
readPayload tenta response.json() e, se isso falhar, faz fallback para response.text(). Em fetch, o body do Response é um stream que só pode ser consumido uma vez; se json() já leu o body (mesmo lançando por JSON inválido ou body vazio), text() vai falhar com erro de stream já lido. Para tornar o fallback realmente robusto, use response.clone() antes de ler, ou leia text() uma vez e faça JSON.parse condicionalmente (mantendo o texto para a mensagem de erro).
| "rewrites": [ | ||
| { | ||
| "source": "/api/(.*)", | ||
| "destination": "http://jobsglobalscraper.ddns.net/api/$1" |
There was a problem hiding this comment.
O rewrite aponta para http://jobsglobalscraper.ddns.net/... (HTTP). Isso remove TLS entre a infraestrutura da Vercel e a API e também pode ser bloqueado/alterado por redirecionamentos. Como o código/testes já usam https://jobsglobalscraper.ddns.net/ como base (ex.: testes de VITE_API_BASE_URL), alinhe o rewrite para https://... (ou documente/garanta explicitamente que o backend só serve HTTP).
| "destination": "http://jobsglobalscraper.ddns.net/api/$1" | |
| "destination": "https://jobsglobalscraper.ddns.net/api/$1" |
No description provided.