-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
72 lines (59 loc) · 1.72 KB
/
Copy pathapi.ts
File metadata and controls
72 lines (59 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { getToken } from "@/lib/auth";
const DEFAULT_API_BASE =
process.env.NEXT_PUBLIC_API_BASE ?? "http://localhost:8080";
function normalizeBase(base: string) {
return base.replace(/\/+$/, "");
}
export function apiUrl(path: string) {
const base = normalizeBase(DEFAULT_API_BASE);
const normalized = path.startsWith("/") ? path : `/${path}`;
return `${base}${normalized}`;
}
type JsonBody = Record<string, unknown> | unknown[];
type FetchOptions = Omit<RequestInit, "body"> & {
body?: JsonBody | string | null;
};
export async function fetchJson<T>(
path: string,
options: FetchOptions = {},
): Promise<T> {
const headers = new Headers(options.headers);
const token = getToken();
if (token) {
headers.set("Authorization", `Bearer ${token}`);
}
let resolvedBody: BodyInit | undefined;
const body = options.body;
const isStringBody = typeof body === "string";
if (body !== null && body !== undefined) {
if (isStringBody) {
resolvedBody = body;
} else {
headers.set("Content-Type", "application/json");
resolvedBody = JSON.stringify(body);
}
}
const response = await fetch(apiUrl(path), {
...options,
headers,
body: resolvedBody,
});
if (!response.ok) {
const errorText = await response.text();
const message = errorText || `Request failed with ${response.status}`;
throw new Error(message);
}
if (response.status === 204) {
return undefined as T;
}
const contentType = response.headers.get("content-type") ?? "";
const rawText = await response.text();
if (contentType.includes("application/json")) {
return JSON.parse(rawText) as T;
}
try {
return JSON.parse(rawText) as T;
} catch {
return rawText as T;
}
}