@tatil/server-api 0.0.7 → 0.0.9

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/README.md CHANGED
@@ -95,6 +95,8 @@ NODE_ENV=production
95
95
 
96
96
  - Otomatik token yönetimi ve refresh
97
97
  - Request logging ile Reklog entegrasyonu
98
+ - Request/Response interceptor'lar ile otomatik logging
99
+ - Hata yönetimi ve HTTP 400+ durum kontrolü
98
100
  - `/api/v1` prefix otomatik eklenir
99
101
  - Cookie-based token storage
100
102
  - Next.js server components ile tam uyumlu
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.9",
4
4
  "description": "Tatilsepeti Server Api for Next.js server-side operations",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -8,22 +8,98 @@ const { API_URL } = process.env;
8
8
 
9
9
  class TatilsepetiApi {
10
10
  constructor() {
11
- this.instance = axios.create({
12
- baseURL: `${API_URL}/api`,
13
- });
14
-
11
+ this.defaultBaseURL = `${API_URL}/api`;
12
+ this.instances = new Map();
13
+ this.endpointURLMap = new Map();
14
+
15
+ // Default instance
16
+ this.instance = this.createInstance(this.defaultBaseURL);
17
+
15
18
  this.logger = RequestLogger();
16
19
  this.defaultHeaders = {
17
20
  'Content-Type': 'application/json',
18
21
  };
19
22
  }
20
23
 
24
+ // Dışarıdan mapping eklemek için
25
+ mapEndpoint(endpoint, rootURL) {
26
+ const baseURL = `${rootURL}/api`;
27
+ this.endpointURLMap.set(endpoint, baseURL);
28
+ return this;
29
+ }
30
+
31
+ createInstance(baseURL) {
32
+ const instance = axios.create({ baseURL });
33
+
34
+ instance.interceptors.request.use(
35
+ (config) => {
36
+ const { url, method } = config;
37
+ console.log(`[API] ${method} ${url}`);
38
+ return config;
39
+ },
40
+ (error) => {
41
+ return Promise.reject(error);
42
+ }
43
+ );
44
+
45
+ instance.interceptors.response.use(
46
+ (response) => response,
47
+ (error) => {
48
+ if (error.response) {
49
+ console.log(`[API ERROR] ${error.response.status} - ${error.config.url}`);
50
+ return error.response;
51
+ }
52
+
53
+ console.log(`[API ERROR] No response - ${error.config?.url}`);
54
+ return null;
55
+ }
56
+ );
57
+
58
+ return instance;
59
+ }
60
+
61
+ getInstance(endpoint) {
62
+ let rootKey;
63
+ let fullPath;
64
+
65
+ if (Array.isArray(endpoint)) {
66
+ rootKey = endpoint[0];
67
+ fullPath = endpoint.join('/');
68
+ } else {
69
+ const cleanEndpoint = endpoint.replace(/^\/+/, '');
70
+ rootKey = cleanEndpoint.split('/')[0];
71
+ fullPath = cleanEndpoint;
72
+ }
73
+
74
+ // Önce tam path'i kontrol et
75
+ let customURL = this.endpointURLMap.get(fullPath);
76
+
77
+ // Bulamazsa root key'i kontrol et
78
+ if (!customURL) {
79
+ customURL = this.endpointURLMap.get(rootKey);
80
+ }
81
+
82
+ if (!customURL) {
83
+ return this.instance;
84
+ }
85
+
86
+ if (!this.instances.has(customURL)) {
87
+ this.instances.set(customURL, this.createInstance(customURL));
88
+ }
89
+
90
+ return this.instances.get(customURL);
91
+ }
92
+
93
+ clearEndpoint(endpoint) {
94
+ return endpoint.replace(/\/\//g, '/');
95
+ }
96
+
21
97
  buildUrl(endpoint, version = 'v1') {
22
98
  if (Array.isArray(endpoint)) {
23
- return `/${version}/${endpoint.join('/')}`;
99
+ return this.clearEndpoint(`/${version}/${endpoint.join('/')}`);
24
100
  }
25
101
 
26
- return `/${version}/${endpoint}`;
102
+ return this.clearEndpoint(`/${version}/${endpoint}`);
27
103
  }
28
104
 
29
105
  setAuthCookies(accessToken, expiresAt) {
@@ -81,8 +157,9 @@ class TatilsepetiApi {
81
157
  async get(endpoint, params = {}, version = 'v1') {
82
158
  const url = this.buildUrl(endpoint, version);
83
159
  const logId = this.logger.start(url, 'GET');
160
+ const instance = this.getInstance(endpoint);
84
161
 
85
- const response = await this.instance.get(url, {
162
+ const response = await instance.get(url, {
86
163
  params,
87
164
  headers: await this.getAuthHeaders(),
88
165
  });
@@ -93,7 +170,17 @@ class TatilsepetiApi {
93
170
  response: 'No response from server',
94
171
  params,
95
172
  });
96
- return;
173
+ return null;
174
+ }
175
+
176
+ if (response.status >= 400) {
177
+ this.logger.end(logId, {
178
+ statusCode: response.status,
179
+ response: response.data || 'Server error',
180
+ params,
181
+ error: true,
182
+ });
183
+ return null;
97
184
  }
98
185
 
99
186
  if (!response.data) {
@@ -102,7 +189,7 @@ class TatilsepetiApi {
102
189
  response: 'No data in response',
103
190
  params,
104
191
  });
105
- return;
192
+ return null;
106
193
  }
107
194
 
108
195
  const { data } = response;
@@ -118,15 +205,10 @@ class TatilsepetiApi {
118
205
 
119
206
  async post(endpoint, body = {}, version = 'v1') {
120
207
  const url = this.buildUrl(endpoint, version);
121
-
122
-
123
- console.log(url, "urlurlurlurlurlurlurlurlurlurl");
124
-
125
-
126
-
127
208
  const logId = this.logger.start(url, 'POST');
209
+ const instance = this.getInstance(endpoint);
128
210
 
129
- const response = await this.instance.post(url, body, {
211
+ const response = await instance.post(url, body, {
130
212
  headers: await this.getAuthHeaders(),
131
213
  });
132
214
 
@@ -136,7 +218,17 @@ class TatilsepetiApi {
136
218
  response: 'No response from server',
137
219
  body,
138
220
  });
139
- return;
221
+ return null;
222
+ }
223
+
224
+ if (response.status >= 400) {
225
+ this.logger.end(logId, {
226
+ statusCode: response.status,
227
+ response: response.data || 'Server error',
228
+ body,
229
+ error: true,
230
+ });
231
+ return null;
140
232
  }
141
233
 
142
234
  if (!response.data) {
@@ -145,7 +237,7 @@ class TatilsepetiApi {
145
237
  response: 'No data in response',
146
238
  body,
147
239
  });
148
- return;
240
+ return null;
149
241
  }
150
242
 
151
243
  const { data } = response;
@@ -162,4 +254,4 @@ class TatilsepetiApi {
162
254
 
163
255
  const Tatilsepeti = new TatilsepetiApi();
164
256
 
165
- export default Tatilsepeti;
257
+ export default Tatilsepeti;