-
Notifications
You must be signed in to change notification settings - Fork 8
/
http.ts
133 lines (111 loc) · 3.04 KB
/
http.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
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosError,
AxiosResponse,
AxiosRequestHeaders,
} from "axios";
import { getApiKey, getFrontendApiBaseUrl } from "../config";
import { ApiResponseResult } from "../types";
import { HTTP_STATUS_CODES } from "../app.constants";
import { ApiError } from "./error";
interface CustomAxiosRequestHeaders extends Partial<AxiosRequestHeaders> {
}
const headers: CustomAxiosRequestHeaders = {
Accept: "application/json",
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Credentials": "true",
"X-Requested-With": "XMLHttpRequest",
};
export interface ConfigOptions {
sessionId?: string;
clientSessionId?: string;
validationStatues?: number[];
sourceIp?: string;
persistentSessionId?: string;
baseURL?: string;
userLanguage?: string;
reauthenticate?: boolean;
}
export function createApiResponse<T>(
response: AxiosResponse,
status: number[] = [HTTP_STATUS_CODES.OK]
): ApiResponseResult<T> {
return {
success: status.includes(response.status),
data: response.data,
};
}
export function getRequestConfig(options: ConfigOptions): AxiosRequestConfig {
const config: AxiosRequestConfig = {
headers: {
"X-API-Key": getApiKey(),
},
proxy: false,
};
if (options.sessionId) {
config.headers["Session-Id"] = options.sessionId;
}
if (options.clientSessionId) {
config.headers["Client-Session-Id"] = options.clientSessionId;
}
if (options.sourceIp) {
config.headers["X-Forwarded-For"] = options.sourceIp;
}
if (options.validationStatues) {
config.validateStatus = function (status: number) {
return options.validationStatues.includes(status);
};
}
if (options.persistentSessionId) {
config.headers["di-persistent-session-id"] = options.persistentSessionId;
}
if (options.baseURL) {
config.baseURL = options.baseURL;
}
if (options.reauthenticate) {
config.headers["Reauthenticate"] = options.reauthenticate;
}
if (options.userLanguage) {
config.headers["User-Language"] = options.userLanguage;
}
return config;
}
export class Http {
private instance: AxiosInstance;
get client(): AxiosInstance {
return this.instance || this.initHttp();
}
private static handleError(error: AxiosError) {
let apiError;
if (error.response && error.response.data) {
apiError = new ApiError(
error.message,
error.response.status,
error.response.data as string
);
} else {
apiError = new ApiError(error.message);
}
return Promise.reject(apiError);
}
private initHttp() {
const http = axios.create({
baseURL: getFrontendApiBaseUrl(),
headers: headers,
validateStatus: (status) => {
return (
status >= HTTP_STATUS_CODES.OK &&
status <= HTTP_STATUS_CODES.BAD_REQUEST
);
},
});
http.interceptors.response.use(
(response) => response,
(error) => Http.handleError(error)
);
this.instance = http;
return http;
}
}
export const http = new Http();