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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
import axios, { Axios, AxiosError, AxiosResponse } from "axios";
import { Base64 } from "js-base64";
import { identity } from "lodash";
import { BehaviorSubject, Observable } from "rxjs";
export { axios };
export const apiBaseUrl = "/api";
export class HttpNetworkError extends Error {
constructor(public innerError?: AxiosError) {
super();
}
}
export class HttpForbiddenError extends Error {
constructor(public innerError?: AxiosError) {
super();
}
}
export class HttpNotFoundError extends Error {
constructor(public innerError?: AxiosError) {
super();
}
}
function convertNetworkError(error: AxiosError): never {
if (error.isAxiosError && error.response == null) {
throw new HttpNetworkError(error);
} else {
throw error;
}
}
function convertForbiddenError(error: AxiosError): never {
if (
error.isAxiosError &&
error.response != null &&
(error.response.status == 401 || error.response.status == 403)
) {
throw new HttpForbiddenError(error);
} else {
throw error;
}
}
function convertNotFoundError(error: AxiosError): never {
if (
error.isAxiosError &&
error.response != null &&
error.response.status == 404
) {
throw new HttpNotFoundError(error);
} else {
throw error;
}
}
export function configureAxios(axios: Axios): void {
axios.interceptors.response.use(identity, convertNetworkError);
axios.interceptors.response.use(identity, convertForbiddenError);
axios.interceptors.response.use(identity, convertNotFoundError);
}
configureAxios(axios);
const tokenSubject = new BehaviorSubject<string | null>(null);
export function getHttpToken(): string | null {
return tokenSubject.value;
}
export function setHttpToken(token: string | null): void {
tokenSubject.next(token);
if (token == null) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete axios.defaults.headers.common["Authorization"];
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
}
export const token$: Observable<string | null> = tokenSubject.asObservable();
export function base64(blob: Blob | string): Promise<string> {
if (typeof blob === "string") {
return Promise.resolve(Base64.encode(blob));
}
return new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onload = function () {
resolve((reader.result as string).replace(/^data:.*;base64,/, ""));
};
reader.readAsDataURL(blob);
});
}
export function extractStatusCode(error: AxiosError): number | null {
if (error.isAxiosError) {
const code = error?.response?.status;
if (typeof code === "number") {
return code;
}
}
return null;
}
export interface CommonErrorResponse {
code: number;
message: string;
}
export function extractErrorCode(
error: AxiosError<CommonErrorResponse>
): number | null {
if (error.isAxiosError) {
const code = error.response?.data?.code;
if (typeof code === "number") {
return code;
}
}
return null;
}
export class NotModified {}
export interface BlobWithEtag {
data: Blob;
etag: string;
}
export function extractResponseData<T>(res: AxiosResponse<T>): T {
return res.data;
}
export function catchIfStatusCodeIs<
TResult,
TErrorHandlerResult extends TResult | PromiseLike<TResult> | null | undefined
>(
statusCode: number,
errorHandler: (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult
): (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult {
return (error: AxiosError<CommonErrorResponse>) => {
if (extractStatusCode(error) == statusCode) {
return errorHandler(error);
} else {
throw error;
}
};
}
export function convertToIfStatusCodeIs<NewError>(
statusCode: number,
newErrorType: {
new (innerError: AxiosError): NewError;
}
): (error: AxiosError<CommonErrorResponse>) => never {
return catchIfStatusCodeIs(statusCode, (error) => {
throw new newErrorType(error);
});
}
export function catchIfErrorCodeIs<
TResult,
TErrorHandlerResult extends TResult | PromiseLike<TResult> | null | undefined
>(
errorCode: number,
errorHandler: (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult
): (error: AxiosError<CommonErrorResponse>) => TErrorHandlerResult {
return (error: AxiosError<CommonErrorResponse>) => {
if (extractErrorCode(error) == errorCode) {
return errorHandler(error);
} else {
throw error;
}
};
}
export function convertToIfErrorCodeIs<NewError>(
errorCode: number,
newErrorType: {
new (innerError: AxiosError): NewError;
}
): (error: AxiosError<CommonErrorResponse>) => never {
return catchIfErrorCodeIs(errorCode, (error) => {
throw new newErrorType(error);
});
}
export function convertToNotModified(
error: AxiosError<CommonErrorResponse>
): NotModified {
if (
error.isAxiosError &&
error.response != null &&
error.response.status == 304
) {
return new NotModified();
} else {
throw error;
}
}
export function convertToBlobWithEtag(res: AxiosResponse<Blob>): BlobWithEtag {
return {
data: res.data,
etag: (res.headers as Record<"etag", string>)["etag"],
};
}
export function extractEtag(res: AxiosResponse): string {
return (res.headers as Record<"etag", string>)["etag"];
}
export interface Page<T> {
pageNumber: number;
pageSize: number;
totalPageCount: number;
totalCount: number;
items: T[];
}
|