fix: Update API fetch calls to use absolute URLs for improved reliability

This commit is contained in:
GitHub Copilot
2025-04-24 17:47:29 -04:00
parent 01e8090fc2
commit 4991290699
2 changed files with 10 additions and 4 deletions

View File

@@ -9,3 +9,6 @@ A web application for managing financial transactions across multiple bank accou
* **Layout:** Features a two-column dashboard design with a sidebar for account selection/actions and a main area for transaction display. * **Layout:** Features a two-column dashboard design with a sidebar for account selection/actions and a main area for transaction display.
* **Data Management:** Currently uses an in-memory data store (`src/data/store.ts`) accessed via API endpoints (`src/pages/api/`). * **Data Management:** Currently uses an in-memory data store (`src/data/store.ts`) accessed via API endpoints (`src/pages/api/`).
* **Key Features (Implemented & Planned):** Account switching, transaction listing, adding, editing, and deleting transactions. * **Key Features (Implemented & Planned):** Account switching, transaction listing, adding, editing, and deleting transactions.
## Logs
This app is currently deployed using Cloudflare Pages. The logs can be viewed with the `npx wrangler pages deployment tail --project-name finance` command. T

View File

@@ -9,8 +9,11 @@ export interface Props {
transactions: Transaction[]; transactions: Transaction[];
} }
// Fetch accounts from API // Get the base URL from the incoming request
const accountsResponse = await fetch('/api/accounts'); // Use relative path const baseUrl = new URL(Astro.request.url).origin;
// Fetch accounts from API using absolute URL
const accountsResponse = await fetch(`${baseUrl}/api/accounts`);
const accounts: Account[] = await accountsResponse.json(); const accounts: Account[] = await accountsResponse.json();
// Initialize with first account or empty account if none exist // Initialize with first account or empty account if none exist
@@ -21,10 +24,10 @@ const initialAccount: Account = accounts[0] || {
balance: 0 balance: 0
}; };
// Fetch initial transactions if we have an account // Fetch initial transactions if we have an account, using absolute URL
let initialTransactions: Transaction[] = []; let initialTransactions: Transaction[] = [];
if (initialAccount.id) { if (initialAccount.id) {
const transactionsResponse = await fetch(`/api/accounts/${initialAccount.id}/transactions`); // Use relative path const transactionsResponse = await fetch(`${baseUrl}/api/accounts/${initialAccount.id}/transactions`);
initialTransactions = await transactionsResponse.json(); initialTransactions = await transactionsResponse.json();
} }
--- ---