@tatil/server-api 0.0.7 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tatil/server-api",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Tatilsepeti Server Api for Next.js server-side operations",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -8,22 +8,55 @@ const { API_URL } = process.env;
8
8
 
9
9
  class TatilsepetiApi {
10
10
  constructor() {
11
+ this.baseURL = `${API_URL}/api`;
11
12
  this.instance = axios.create({
12
- baseURL: `${API_URL}/api`,
13
+ baseURL: this.baseURL,
13
14
  });
14
15
 
15
16
  this.logger = RequestLogger();
16
17
  this.defaultHeaders = {
17
18
  'Content-Type': 'application/json',
18
19
  };
20
+
21
+ this.attachInterceptors();
22
+ }
23
+
24
+ attachInterceptors() {
25
+ this.instance.interceptors.request.use(
26
+ (config) => {
27
+ const { url, method } = config;
28
+ console.log(`[API] ${method} ${url}`);
29
+ return config;
30
+ },
31
+ (error) => {
32
+ return Promise.reject(error);
33
+ }
34
+ );
35
+
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
+ );
48
+ }
49
+
50
+ clearEndpoint(endpoint) {
51
+ return endpoint.replace(/\/\//g, '/');
19
52
  }
20
53
 
21
54
  buildUrl(endpoint, version = 'v1') {
22
55
  if (Array.isArray(endpoint)) {
23
- return `/${version}/${endpoint.join('/')}`;
56
+ return this.clearEndpoint(`/${version}/${endpoint.join('/')}`);
24
57
  }
25
58
 
26
- return `/${version}/${endpoint}`;
59
+ return this.clearEndpoint(`/${version}/${endpoint}`);
27
60
  }
28
61
 
29
62
  setAuthCookies(accessToken, expiresAt) {
@@ -93,7 +126,17 @@ class TatilsepetiApi {
93
126
  response: 'No response from server',
94
127
  params,
95
128
  });
96
- 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;
97
140
  }
98
141
 
99
142
  if (!response.data) {
@@ -102,7 +145,7 @@ class TatilsepetiApi {
102
145
  response: 'No data in response',
103
146
  params,
104
147
  });
105
- return;
148
+ return null;
106
149
  }
107
150
 
108
151
  const { data } = response;
@@ -118,12 +161,6 @@ class TatilsepetiApi {
118
161
 
119
162
  async post(endpoint, body = {}, version = 'v1') {
120
163
  const url = this.buildUrl(endpoint, version);
121
-
122
-
123
- console.log(url, "urlurlurlurlurlurlurlurlurlurl");
124
-
125
-
126
-
127
164
  const logId = this.logger.start(url, 'POST');
128
165
 
129
166
  const response = await this.instance.post(url, body, {
@@ -136,7 +173,17 @@ class TatilsepetiApi {
136
173
  response: 'No response from server',
137
174
  body,
138
175
  });
139
- 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;
140
187
  }
141
188
 
142
189
  if (!response.data) {
@@ -145,7 +192,7 @@ class TatilsepetiApi {
145
192
  response: 'No data in response',
146
193
  body,
147
194
  });
148
- return;
195
+ return null;
149
196
  }
150
197
 
151
198
  const { data } = response;
@@ -162,4 +209,4 @@ class TatilsepetiApi {
162
209
 
163
210
  const Tatilsepeti = new TatilsepetiApi();
164
211
 
165
- export default Tatilsepeti;
212
+ export default Tatilsepeti;