create middleware block and update proxy file also make token stored in cookes and up date user page to can show user details also fixed delete user error and delete car error and broken corse inginx image block now we can uplode image to server and but it in my app
This commit is contained in:
55
lib/api.ts
55
lib/api.ts
@@ -3,17 +3,15 @@ const DEFAULT_BASE_URL = "/api/proxy";
|
||||
export const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_API_BASE_URL || DEFAULT_BASE_URL;
|
||||
|
||||
function normalizePath(path: string) {
|
||||
function normalizePath(path: string): string {
|
||||
return path.startsWith("/") ? path : `/${path}`;
|
||||
}
|
||||
|
||||
// ─── API REQUESTS ─────────────────────────────
|
||||
export async function requestJson<T>(
|
||||
path: string, init: RequestInit = {}, token: string | null, p0: number,
|
||||
path: string,
|
||||
init: RequestInit = {},
|
||||
token: string | null = null,
|
||||
): Promise<T> {
|
||||
const token =
|
||||
typeof window !== "undefined" ? localStorage.getItem("auth_token") : null;
|
||||
|
||||
const headers = new Headers(init.headers || {});
|
||||
|
||||
if (!headers.has("Content-Type") && !(init.body instanceof FormData)) {
|
||||
@@ -39,7 +37,6 @@ path: string, init: RequestInit = {}, token: string | null, p0: number,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
// try to parse error message from response, fallback to status text
|
||||
const json = await response.json().catch(() => null);
|
||||
const message =
|
||||
json?.message ||
|
||||
@@ -52,51 +49,9 @@ path: string, init: RequestInit = {}, token: string | null, p0: number,
|
||||
} catch (error) {
|
||||
attempt += 1;
|
||||
if (attempt >= maxAttempts) throw error;
|
||||
// exponential back-off: 600ms, 1200ms
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, 2 ** attempt * 300),
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 2 ** attempt * 300));
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Request failed after retries.");
|
||||
}
|
||||
|
||||
// ─── Types ───────────────────────────────────
|
||||
export type DashboardSummaryResponse = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
data: {
|
||||
stats: {
|
||||
clients: number;
|
||||
orders: number;
|
||||
trips: number;
|
||||
cars: number;
|
||||
drivers: number;
|
||||
};
|
||||
alerts: {
|
||||
expiringCars: Array<Record<string, unknown>>;
|
||||
expiringDrivers: Array<Record<string, unknown>>;
|
||||
upcomingMaint: Array<Record<string, unknown>>;
|
||||
};
|
||||
activeTrips: Array<{
|
||||
id: string;
|
||||
tripNumber: string;
|
||||
title: string;
|
||||
progress: number;
|
||||
}>;
|
||||
accountSecurity: {
|
||||
lastLogin: string | null;
|
||||
requestMeta: Record<string, unknown> | null;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// ─── API calls ───────────────────────────────
|
||||
export async function getDashboardSummary() {
|
||||
return requestJson<DashboardSummaryResponse>("/dashboard/summary");
|
||||
}
|
||||
|
||||
export async function getHealth() {
|
||||
return requestJson<{ status: string; message: string }>("/health");
|
||||
}
|
||||
Reference in New Issue
Block a user