-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiBaseService.ts
161 lines (154 loc) · 4.43 KB
/
ApiBaseService.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import axios, { AxiosError, Method } from "axios";
import {
BaseRequestModel,
NullableBaseRequestModel,
} from "../../datas/request-models/BaseRequestModel";
import { BaseResponseModel } from "../../datas/response-models/BaseResponseModel";
import {
BaseResultModel,
NullableBaseResultModel,
} from "../../datas/response-models/BaseResultModel";
import { ValidationErrorResponseModel } from "../../datas/response-models/ValidationErrorResponseModel";
import { ContentType } from "../../utils/enums/ContentTypes";
import { AuthManager } from "../../utils/helpers/AuthManager";
export abstract class ApiBaseService {
static BaseUrl: string |undefined = process.env.REACT_APP_API_URL;
static isBusy: boolean = false;
static async sendRequestBase<ResponseModel>(
method: Method,
path: string,
data?: any,
params?: URLSearchParams,
contentType?: string,
automaticUnauthRedirect: boolean = true
) {
this.isBusy = true;
let isUnauthorized: boolean = false;
var response = await axios
.request({
baseURL: this.BaseUrl,
url: path,
method: method,
headers: {
"Content-Type": contentType ?? ContentType.json,
Authorization: `Bearer ${AuthManager.getToken()}`,
},
data: data,
params: params,
})
.then((response) => {
return response.data as ResponseModel;
})
.catch((err: AxiosError<any, any>) => {
const errorStatus = err?.response?.status ?? 0;
const errorData = err?.response?.data;
isUnauthorized =
automaticUnauthRedirect &&
(errorStatus === 401 || errorStatus === 403);
if (errorData != null) {
const isBaseResponse = err instanceof AxiosError<ResponseModel, any>;
const isValidationError =
err instanceof AxiosError<ValidationErrorResponseModel, any>;
if (errorStatus === 400 && isValidationError) {
return BaseResponseModel.fromValidationErrors(
errorData as ValidationErrorResponseModel
);
} else if (isBaseResponse) {
return errorData as ResponseModel;
}
}
return BaseResponseModel.fromError(err?.message ?? "Undefined Error !");
});
if (isUnauthorized) {
AuthManager.logout();
}
this.isBusy = false;
return response;
}
static async sendRequest<
ReqModel extends NullableBaseRequestModel,
ResModel extends NullableBaseResultModel
>(
method: Method,
path: string,
data?: ReqModel,
params?: URLSearchParams,
contentType?: string,
automaticUnauthRedirect: boolean = true
): Promise<BaseResponseModel<ResModel | null>> {
return this.sendRequestBase<BaseResponseModel<ResModel | null>>(
method,
path,
data,
params,
contentType,
automaticUnauthRedirect
);
}
static async postRequest<
ReqModel extends BaseRequestModel,
ResModel extends BaseResultModel
>(
path: string,
data?: ReqModel,
contentType?: string,
automaticUnauthRedirect: boolean = true
): Promise<BaseResponseModel<ResModel | null>> {
return this.sendRequest<ReqModel, ResModel>(
"post",
path,
data,
undefined,
contentType,
automaticUnauthRedirect
);
}
static async putRequest<
ReqModel extends BaseRequestModel,
ResModel extends BaseResultModel
>(
path: string,
data?: ReqModel,
contentType?: string,
automaticUnauthRedirect: boolean = true
): Promise<BaseResponseModel<ResModel | null>> {
return this.sendRequest<ReqModel, ResModel>(
"put",
path,
data,
undefined,
contentType,
automaticUnauthRedirect
);
}
static async deleteRequest(
path: string,
id: string,
contentType?: string,
automaticUnauthRedirect: boolean = true
): Promise<BaseResponseModel<boolean | null>> {
return this.sendRequestBase<BaseResponseModel<boolean | null>>(
"delete",
path,
id,
undefined,
contentType,
automaticUnauthRedirect
);
}
static async getRequest<ResModel extends BaseResultModel>(
path: string,
params?: URLSearchParams,
contentType?: string,
automaticUnauthRedirect: boolean = true
): Promise<BaseResponseModel<ResModel | null>> {
return this.sendRequest<null, ResModel>(
"get",
path,
null,
params,
contentType,
automaticUnauthRedirect
);
}
}