Skip to content

fix(forex): fetch BSP exchange rates directly from the browser - #752

Open
irishmicoletcando wants to merge 1 commit into
bettergovph:mainfrom
irishmicoletcando:fix/forex-direct-bsp
Open

irishmicoletcando wants to merge 1 commit into
bettergovph:mainfrom
irishmicoletcando:fix/forex-direct-bsp

Conversation

@irishmicoletcando

@irishmicoletcando irishmicoletcando commented Sep 21, 2026

Copy link
Copy Markdown

Summary

The Foreign Exchange Rates page (/data/forex) and the homepage forex ticker were failing with API request failed with status 404. The frontend fetched from https://api.bettergov.ph/forex, a Cloudflare Worker that reads exchange rates from a KV store. That KV store was never being populated: the scheduled Worker that fills it fetches BSP directly, and BSP's WAF returns 403 Forbidden to the Cloudflare Workers runtime — so the key stayed empty and every read returned 404.

This PR removes the KV/Worker dependency for forex entirely and fetches the official BSP exchange-rate list directly from the browser. BSP serves Access-Control-Allow-Origin: * and its WAF allows real browser clients (only datacenter/worker runtimes are blocked), so no proxy, backend, KV store, or scheduled job is required.

Before / After

Before (404) After (live BSP data)
Before — API request failed with status 404 After — USD ₱62.6700 with currency list, converter, and ticker

Root cause

  1. Frontend calls https://api.bettergov.ph/forex (KV-only Worker endpoint).
  2. The endpoint reads bsp_exchange_rates from KV; the key is empty, so it returns 404.
  3. The key is empty because the scheduled Worker that populates it fetches BSP from the Workers runtime, which BSP's WAF blocks with 403 (curl and real browsers get 200 for the identical request).

Changes

  • src/lib/forex.ts — fetch the BSP OData exchange-rate endpoint directly, request JSON explicitly (BSP defaults to XML), and map value[]ForexRate. Rows BSP reports as N/A (e.g. KWD) parse to NaN, so they are filtered out rather than rendered as a blank rate.
  • src/lib/api.tsfetchWithCache now accepts an optional RequestInit so the forex call can send Accept: application/json. Signature is backward-compatible; existing callers are unaffected.

No changes to the ForexRate shape or the 1-hour client cache, so Ticker, InfoWidgets, and the forex page work unchanged.

Test plan

  • /data/forex renders the currency list, rate card, and converter with live BSP data (USD ₱62.6700; ₱1000 → $15.96).
  • Homepage forex ticker shows live rates.
  • N/A currencies (e.g. KWD) are omitted — no blank rows.
  • tsc --noEmit passes; no new console errors on load.

Notes

  • The before/after screenshots are hosted on a separate pr-assets/forex-direct-bsp branch on the fork, so they are not part of this PR's diff.

@vercel

vercel Bot commented Sep 21, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
bettergov Ready Ready Preview Sep 21, 2026 12:04pm UTC

This branch was successfully deployed

1 active deployment
Preview 7256b3c5 Deployed Sep 21, 2026 by vercel[bot]
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