-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiService.ts
More file actions
135 lines (128 loc) · 3.95 KB
/
apiService.ts
File metadata and controls
135 lines (128 loc) · 3.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { getApiDomain } from "@/utils/domain";
import { ApplicationError } from "@/types/error";
export class ApiService {
private baseURL: string;
private defaultHeaders: HeadersInit;
constructor() {
this.baseURL = getApiDomain();
this.defaultHeaders = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
}
/**
* Helper function to check the response, parse JSON,
* and throw an error if the response is not OK.
*
* @param res - The response from fetch.
* @param errorMessage - A descriptive error message for this call.
* @returns Parsed JSON data.
* @throws ApplicationError if res.ok is false.
*/
private async processResponse<T>(
res: Response,
errorMessage: string,
): Promise<T> {
if (!res.ok) {
let errorDetail = res.statusText;
try {
const errorInfo = await res.json();
if (errorInfo?.message) {
errorDetail = errorInfo.message;
} else {
errorDetail = JSON.stringify(errorInfo);
}
} catch {
// If parsing fails, keep using res.statusText
}
const detailedMessage = `${errorMessage} (${res.status}: ${errorDetail})`;
const error: ApplicationError = new Error(
detailedMessage,
) as ApplicationError;
error.info = JSON.stringify(
{ status: res.status, statusText: res.statusText },
null,
2,
);
error.status = res.status;
throw error;
}
return res.headers.get("Content-Type")?.includes("application/json")
? res.json() as Promise<T>
: Promise.resolve(res as T);
}
/**
* GET request.
* @param endpoint - The API endpoint (e.g. "/users").
* @returns JSON data of type T.
*/
public async get<T>(endpoint: string, customHeaders? : { [key:string]: string}): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const headers={...this.defaultHeaders,...customHeaders};
const res = await fetch(url, {
method: "GET",
headers: headers,
});
return this.processResponse<T>(
res,
"An error occurred while fetching the data.\n",
);
}
/**
* POST request.
* @param endpoint - The API endpoint (e.g. "/users").
* @param data - The payload to post.
* @param customHeaders - Optional additional headers to include.
* @returns JSON data of type T.
*/
public async post<T>(endpoint: string, data: unknown, customHeaders?: { [key: string]: string }): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const headers = { ...this.defaultHeaders, ...customHeaders };
const res = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
});
return this.processResponse<T>(
res,
"An error occurred while posting the data.\n",
);
}
/**
* PUT request.
* @param endpoint - The API endpoint (e.g. "/users/123").
* @param data - The payload to update.
* @returns JSON data of type T.
*/
public async put<T>(endpoint: string, data: unknown, customHeaders? : { [key:string]: string}): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const headers={...this.defaultHeaders,...customHeaders};
const res = await fetch(url, {
method: "PUT",
headers: headers,
body: JSON.stringify(data),
});
return this.processResponse<T>(
res,
"An error occurred while updating the data.\n",
);
}
/**
* DELETE request.
* @param endpoint - The API endpoint (e.g. "/users/123").
* @param customHeaders - Optional additional headers to include.
* @returns JSON data of type T.
*/
public async delete<T>(endpoint: string, customHeaders?: { [key:string]: string}): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const headers={...this.defaultHeaders,...customHeaders};
const res = await fetch(url, {
method: "DELETE",
headers,
});
return this.processResponse<T>(
res,
"An error occurred while deleting the data.\n",
);
}
}