-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.service.ts
79 lines (68 loc) · 2.4 KB
/
http.service.ts
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
import { IAnalyticsService } from "src/layers/application/interfaces/services/analytics.service.interface";
import { ICrashlyticsService } from "src/layers/application/interfaces/services/crashlytics.service.interface";
import { IHttpService } from "src/layers/application/interfaces/services/http.service.interface";
import BaseError from "src/layers/domain/errors/base-error";
export default class HttpService implements IHttpService {
private readonly baseUrl: string = "https://api.github.com";
constructor(
private readonly crashlyticsService: ICrashlyticsService,
private readonly analyticsService: IAnalyticsService,
) { }
private async request<T>(
url: string,
method: string,
body?: Record<string, unknown>,
params?: Record<string, string>,
): Promise<T> {
return this.analyticsService.setAnalytics(
{
name: `${method} | ${url}`,
from: HttpService.name,
},
async () => {
const queryParams = params
? "?" + new URLSearchParams(params).toString()
: "";
const options: RequestInit = {
method,
headers: {
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
};
try {
const response = await fetch(
`${this.baseUrl}${url}${queryParams}`,
options,
);
if (!response.ok) {
throw new BaseError({
type: response.type,
status: response.status,
message: response.statusText,
});
}
const data: T = await response.json();
return data;
} catch (error) {
this.crashlyticsService.reportError(error, {
from: HttpService.name,
});
throw new BaseError({ message: `Error: ${error}` });
}
},
);
}
async get<T>(url: string, params?: Record<string, string>): Promise<T> {
return this.request<T>(url, "GET", undefined, params);
}
async post<T>(url: string, body: Record<string, unknown>): Promise<T> {
return this.request<T>(url, "POST", body);
}
async put<T>(url: string, body: Record<string, unknown>): Promise<T> {
return this.request<T>(url, "PUT", body);
}
async delete<T>(url: string, params?: Record<string, string>): Promise<T> {
return this.request<T>(url, "DELETE", undefined, params);
}
}