forked from zolfagharipour/Matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
60 lines (53 loc) · 1.61 KB
/
api.ts
File metadata and controls
60 lines (53 loc) · 1.61 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
import { ApiResponse } from '@/types/api';
import { ApiError } from '../errors/ApiError';
export interface RequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
body?: object | BodyInit;
headers?: Record<string, string>;
}
export async function apiFetch<T>(endpoint: string, options: RequestOptions = {}): Promise<T> {
const headers: Record<string, string> = options.headers ?? {};
const method = options.method ?? 'GET';
let response: Response;
let json: ApiResponse<T>;
let body: BodyInit | undefined;
if (method !== 'GET') {
const b = options.body;
if (b instanceof FormData || b instanceof Blob || typeof b === 'string') {
body = b;
} else {
headers['Content-Type'] = 'application/json';
body = JSON.stringify(b ?? {});
}
}
try {
response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}${endpoint}`, {
method: method,
headers: headers,
body: body,
credentials: 'include',
});
} catch {
throw new ApiError({ code: 'NETWORK_ERROR', message: 'Failed to connect to server.', status: 0, cause: 'network' });
}
try {
json = (await response.json()) as ApiResponse<T>;
} catch {
throw new ApiError({
code: 'INVALID_JSON',
message: 'Invalid JSON response from server.',
status: response.status,
cause: 'json',
});
}
if (!json.success) {
throw new ApiError({
code: json?.error?.code ?? 'API_ERROR',
message: json?.message ?? response.statusText,
details: json?.error?.details,
status: response.status,
cause: 'api',
});
}
return json.data;
}