refactor: replace custom API fetch logic with requestJson utility across multiple pages
Updated the API fetching mechanism in various components to utilize the new requestJson function for improved consistency and error handling. This change affects the alerts, dashboard, data collection, filters, gre, network map, probes, recursive routes, route optimizer, servers, settings, traffic, and uptime pages.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
export class ApiClientError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number,
|
||||
public readonly payload?: unknown,
|
||||
) {
|
||||
super(message)
|
||||
this.name = "ApiClientError"
|
||||
}
|
||||
}
|
||||
|
||||
function trimBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.replace(/\/$/, "")
|
||||
}
|
||||
|
||||
export async function requestJson<T>(
|
||||
baseUrl: string,
|
||||
path: string,
|
||||
init?: RequestInit,
|
||||
): Promise<T> {
|
||||
const hasBody = init?.body != null
|
||||
const res = await fetch(trimBaseUrl(baseUrl) + path, {
|
||||
...init,
|
||||
headers: {
|
||||
...(hasBody ? { "Content-Type": "application/json" } : {}),
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
})
|
||||
|
||||
if (res.status === 204) return undefined as T
|
||||
|
||||
const payload = await res.json().catch(() => undefined)
|
||||
if (!res.ok) {
|
||||
const msg =
|
||||
typeof payload === "object" &&
|
||||
payload !== null &&
|
||||
"error" in payload &&
|
||||
typeof (payload as { error?: unknown }).error === "string"
|
||||
? (payload as { error: string }).error
|
||||
: res.statusText
|
||||
throw new ApiClientError(msg, res.status, payload)
|
||||
}
|
||||
|
||||
return payload as T
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import {
|
||||
serverListSchema,
|
||||
serverReadSchema,
|
||||
testConnectionSchema,
|
||||
type ServerCreate,
|
||||
type ServerRead,
|
||||
type ServerUpdate,
|
||||
type TestConnectionRequest,
|
||||
} from "@/packages/contracts/src/servers"
|
||||
import { requestJson } from "@/shared/api/http-client"
|
||||
|
||||
type TestConnectionResponse = {
|
||||
success: boolean
|
||||
latencyMs?: number
|
||||
identity?: string
|
||||
version?: string
|
||||
boardName?: string
|
||||
uptime?: string
|
||||
message: string
|
||||
}
|
||||
|
||||
export async function listServers(baseUrl: string): Promise<ServerRead[]> {
|
||||
const payload = await requestJson<unknown>(baseUrl, "/api/servers")
|
||||
return serverListSchema.parse(payload)
|
||||
}
|
||||
|
||||
export async function getServer(baseUrl: string, id: string): Promise<ServerRead> {
|
||||
const payload = await requestJson<unknown>(baseUrl, `/api/servers/${id}`)
|
||||
return serverReadSchema.parse(payload)
|
||||
}
|
||||
|
||||
export async function createServer(baseUrl: string, data: ServerCreate): Promise<ServerRead> {
|
||||
const payload = await requestJson<unknown>(baseUrl, "/api/servers", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return serverReadSchema.parse(payload)
|
||||
}
|
||||
|
||||
export async function updateServer(baseUrl: string, id: string, data: ServerUpdate): Promise<ServerRead> {
|
||||
const payload = await requestJson<unknown>(baseUrl, `/api/servers/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return serverReadSchema.parse(payload)
|
||||
}
|
||||
|
||||
export async function deleteServer(baseUrl: string, id: string): Promise<void> {
|
||||
await requestJson<void>(baseUrl, `/api/servers/${id}`, { method: "DELETE" })
|
||||
}
|
||||
|
||||
export async function pollServer(baseUrl: string, id: string): Promise<void> {
|
||||
await requestJson<unknown>(baseUrl, `/api/servers/${id}/poll`, { method: "POST" })
|
||||
}
|
||||
|
||||
export async function testServerConnection(
|
||||
baseUrl: string,
|
||||
payload: TestConnectionRequest,
|
||||
): Promise<TestConnectionResponse> {
|
||||
testConnectionSchema.parse(payload)
|
||||
return requestJson<TestConnectionResponse>(baseUrl, "/api/servers/test-connection", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user