@tatil/server-api 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/tatilsepeti.js +50 -36
package/package.json
CHANGED
package/src/utils/tatilsepeti.js
CHANGED
|
@@ -8,9 +8,9 @@ const { API_URL } = process.env;
|
|
|
8
8
|
|
|
9
9
|
class TatilsepetiApi {
|
|
10
10
|
constructor() {
|
|
11
|
-
this.baseURL = API_URL
|
|
11
|
+
this.baseURL = `${API_URL}/api`;
|
|
12
12
|
this.instance = axios.create({
|
|
13
|
-
baseURL:
|
|
13
|
+
baseURL: this.baseURL,
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
this.logger = RequestLogger();
|
|
@@ -22,45 +22,41 @@ class TatilsepetiApi {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
attachInterceptors() {
|
|
25
|
-
this.instance.interceptors.
|
|
26
|
-
(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
data: response.data,
|
|
31
|
-
});
|
|
32
|
-
return response;
|
|
25
|
+
this.instance.interceptors.request.use(
|
|
26
|
+
(config) => {
|
|
27
|
+
const { url, method } = config;
|
|
28
|
+
console.log(`[API] ${method} ${url}`);
|
|
29
|
+
return config;
|
|
33
30
|
},
|
|
34
31
|
(error) => {
|
|
35
|
-
console.error('[API RESPONSE ERROR]', {
|
|
36
|
-
url: error.config?.url,
|
|
37
|
-
status: error.response?.status,
|
|
38
|
-
data: error.response?.data,
|
|
39
|
-
});
|
|
40
32
|
return Promise.reject(error);
|
|
41
33
|
}
|
|
42
34
|
);
|
|
43
|
-
}
|
|
44
35
|
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
this.instance.interceptors.response.use(
|
|
37
|
+
(response) => response,
|
|
38
|
+
(error) => {
|
|
39
|
+
if (error.response) {
|
|
40
|
+
console.log(`[API ERROR] ${error.response.status} - ${error.config.url}`);
|
|
41
|
+
return error.response;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log(`[API ERROR] No response - ${error.config?.url}`);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
);
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
return
|
|
50
|
+
clearEndpoint(endpoint) {
|
|
51
|
+
return endpoint.replace(/\/\//g, '/');
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
buildUrl(endpoint, version = 'v1') {
|
|
54
|
-
|
|
55
55
|
if (Array.isArray(endpoint)) {
|
|
56
|
-
return this.
|
|
56
|
+
return this.clearEndpoint(`/${version}/${endpoint.join('/')}`);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
getEndpointPath(endpoint) {
|
|
63
|
-
return Array.isArray(endpoint) ? this.joinEndpoint(endpoint) : endpoint;
|
|
59
|
+
return this.clearEndpoint(`/${version}/${endpoint}`);
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
setAuthCookies(accessToken, expiresAt) {
|
|
@@ -116,9 +112,8 @@ class TatilsepetiApi {
|
|
|
116
112
|
}
|
|
117
113
|
|
|
118
114
|
async get(endpoint, params = {}, version = 'v1') {
|
|
119
|
-
const endpointPath = this.getEndpointPath(endpoint);
|
|
120
|
-
const logId = this.logger.start(endpointPath, 'GET');
|
|
121
115
|
const url = this.buildUrl(endpoint, version);
|
|
116
|
+
const logId = this.logger.start(url, 'GET');
|
|
122
117
|
|
|
123
118
|
const response = await this.instance.get(url, {
|
|
124
119
|
params,
|
|
@@ -131,7 +126,17 @@ class TatilsepetiApi {
|
|
|
131
126
|
response: 'No response from server',
|
|
132
127
|
params,
|
|
133
128
|
});
|
|
134
|
-
return;
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (response.status >= 400) {
|
|
133
|
+
this.logger.end(logId, {
|
|
134
|
+
statusCode: response.status,
|
|
135
|
+
response: response.data || 'Server error',
|
|
136
|
+
params,
|
|
137
|
+
error: true,
|
|
138
|
+
});
|
|
139
|
+
return null;
|
|
135
140
|
}
|
|
136
141
|
|
|
137
142
|
if (!response.data) {
|
|
@@ -140,7 +145,7 @@ class TatilsepetiApi {
|
|
|
140
145
|
response: 'No data in response',
|
|
141
146
|
params,
|
|
142
147
|
});
|
|
143
|
-
return;
|
|
148
|
+
return null;
|
|
144
149
|
}
|
|
145
150
|
|
|
146
151
|
const { data } = response;
|
|
@@ -155,9 +160,8 @@ class TatilsepetiApi {
|
|
|
155
160
|
}
|
|
156
161
|
|
|
157
162
|
async post(endpoint, body = {}, version = 'v1') {
|
|
158
|
-
const endpointPath = this.getEndpointPath(endpoint);
|
|
159
|
-
const logId = this.logger.start(endpointPath, 'POST');
|
|
160
163
|
const url = this.buildUrl(endpoint, version);
|
|
164
|
+
const logId = this.logger.start(url, 'POST');
|
|
161
165
|
|
|
162
166
|
const response = await this.instance.post(url, body, {
|
|
163
167
|
headers: await this.getAuthHeaders(),
|
|
@@ -169,7 +173,17 @@ class TatilsepetiApi {
|
|
|
169
173
|
response: 'No response from server',
|
|
170
174
|
body,
|
|
171
175
|
});
|
|
172
|
-
return;
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (response.status >= 400) {
|
|
180
|
+
this.logger.end(logId, {
|
|
181
|
+
statusCode: response.status,
|
|
182
|
+
response: response.data || 'Server error',
|
|
183
|
+
body,
|
|
184
|
+
error: true,
|
|
185
|
+
});
|
|
186
|
+
return null;
|
|
173
187
|
}
|
|
174
188
|
|
|
175
189
|
if (!response.data) {
|
|
@@ -178,7 +192,7 @@ class TatilsepetiApi {
|
|
|
178
192
|
response: 'No data in response',
|
|
179
193
|
body,
|
|
180
194
|
});
|
|
181
|
-
return;
|
|
195
|
+
return null;
|
|
182
196
|
}
|
|
183
197
|
|
|
184
198
|
const { data } = response;
|
|
@@ -195,4 +209,4 @@ class TatilsepetiApi {
|
|
|
195
209
|
|
|
196
210
|
const Tatilsepeti = new TatilsepetiApi();
|
|
197
211
|
|
|
198
|
-
export default Tatilsepeti;
|
|
212
|
+
export default Tatilsepeti;
|