fix: replace broken API config with centralized axios instance#268
fix: replace broken API config with centralized axios instance#268gautamsidhwani29 wants to merge 3 commits into
Conversation
Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Code Review
This pull request centralizes frontend API requests by introducing an Axios-based apiClient and an API_ENDPOINTS configuration object. It refactors components like Dashboard, Jobs, Pods, and Queues to use these new utilities instead of direct fetch or axios calls, improving consistency and error handling. The reviewer feedback suggests further improving this architecture by migrating all remaining hardcoded paths to the centralized configuration and moving URL encoding logic into the endpoint helper functions to reduce boilerplate and prevent bugs.
| throw new Error(errText); | ||
| } | ||
| const response = await apiClient.put( | ||
| `${API_ENDPOINTS.queues.list}/${updated.metadata.name}`, |
There was a problem hiding this comment.
| import React, { useCallback, useEffect, useMemo, useState } from "react"; | ||
| import { Box, Typography, useTheme } from "@mui/material"; | ||
| import axios from "axios"; | ||
| import apiClient from "../../config/api"; |
| apiClient.get("/jobs?limit=1000"), | ||
| apiClient.get("/queues?limit=1000"), | ||
| apiClient.get("/pods?limit=1000"), |
There was a problem hiding this comment.
Use the centralized API_ENDPOINTS constants instead of hardcoded strings.
| apiClient.get("/jobs?limit=1000"), | |
| apiClient.get("/queues?limit=1000"), | |
| apiClient.get("/pods?limit=1000"), | |
| apiClient.get(`${API_ENDPOINTS.jobs.list}?limit=1000`), | |
| apiClient.get(`${API_ENDPOINTS.queues.list}?limit=1000`), | |
| apiClient.get(`${API_ENDPOINTS.pods.list}?limit=1000`), |
| export const API_ENDPOINTS = { | ||
| jobs: { | ||
| list: `${API_CONFIG.baseURL}/api/jobs`, | ||
| detail: (namespace, name) => | ||
| `${API_CONFIG.baseURL}/jobs/${namespace}/${name}`, | ||
| list: '/jobs', | ||
| detail: (namespace, name) => `/jobs/${namespace}/${name}`, | ||
| }, | ||
| queues: { | ||
| list: `${API_CONFIG.baseURL}/api/queues`, | ||
| list: '/queues', | ||
| }, | ||
| pods: { | ||
| list: `${API_CONFIG.baseURL}/api/pods`, | ||
| list: '/pods', | ||
| }, | ||
| podgroups: { | ||
| list: `${API_CONFIG.baseURL}/api/podgroups`, | ||
| list: '/podgroups', | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
To fully leverage the centralized configuration, we should include all used API paths in API_ENDPOINTS. Additionally, moving encodeURIComponent into these helper functions ensures consistency and prevents bugs where developers might forget to encode URL parameters (as seen in some parts of this PR).
export const API_ENDPOINTS = {
jobs: {
list: '/jobs',
detail: (namespace, name) => `/jobs/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}`,
yaml: (namespace, name) => `/job/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/yaml`,
},
queues: {
list: '/queues',
all: '/all-queues',
detail: (name) => `/queues/${encodeURIComponent(name)}`,
},
pods: {
list: '/pods',
yaml: (namespace, name) => `/pod/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/yaml`,
},
namespaces: {
list: '/namespaces',
},
podgroups: {
list: '/podgroups',
},
};|
|
||
| try { | ||
| const response = await axios.get(`/api/jobs`, { | ||
| const response = await apiClient.get("/jobs", { |
| import DashboardHeader from "./DashboardHeader"; | ||
| import StatCardsContainer from "./StatCardsContainer"; | ||
| import ChartsContainer from "./ChartsContainer"; | ||
| import apiClient from "../../config/api"; |
| }, | ||
| }, | ||
| await apiClient.delete( | ||
| `${API_ENDPOINTS.queues.list}/${encodeURIComponent(queueToDelete)}` |
| @@ -1,15 +1,11 @@ | |||
| import apiClient from "../config/api"; | |||
|
|
||
| const data = await response.json(); | ||
|
|
||
| const response = await apiClient.get("/namespaces"); |
|
|
||
| const data = await response.json(); | ||
|
|
||
| const response = await apiClient.get("/all-queues"); |
…omponents Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
Problem
src/config/api.jswas created to centralize API configuration but was effectively dead code. It usedprocess.env.APP_SERVER_PORT(not supported in Vite), hardcodedhttp://localhost:3001as the base URL (breaking staging and production), and was never imported by any component.Solution
Rewrote
api.jsas a single shared axios instance withbaseURL: '/api'and migrated all directfetch/axioscalls across the frontend to use it.Fixes #263
Changes
process.env+ hardcoded localhost URL inapi.jswith a shared axios instance (baseURL: '/api')API_ENDPOINTSto use relative pathsfetch/axioscalls withapiClientin:Dashboard.jsxJobs.jsxJobTable.jsxPods.jsxQueueTable.jsxEditQueueDialog.jsxutils.js