@tatil/server-api 0.0.8 → 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 +2 -0
- package/package.json +1 -1
- package/src/utils/tatilsepeti.js +56 -11
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
package/src/utils/tatilsepeti.js
CHANGED
|
@@ -8,21 +8,30 @@ const { API_URL } = process.env;
|
|
|
8
8
|
|
|
9
9
|
class TatilsepetiApi {
|
|
10
10
|
constructor() {
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
|
|
16
18
|
this.logger = RequestLogger();
|
|
17
19
|
this.defaultHeaders = {
|
|
18
20
|
'Content-Type': 'application/json',
|
|
19
21
|
};
|
|
22
|
+
}
|
|
20
23
|
|
|
21
|
-
|
|
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;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
createInstance(baseURL) {
|
|
32
|
+
const instance = axios.create({ baseURL });
|
|
33
|
+
|
|
34
|
+
instance.interceptors.request.use(
|
|
26
35
|
(config) => {
|
|
27
36
|
const { url, method } = config;
|
|
28
37
|
console.log(`[API] ${method} ${url}`);
|
|
@@ -33,7 +42,7 @@ class TatilsepetiApi {
|
|
|
33
42
|
}
|
|
34
43
|
);
|
|
35
44
|
|
|
36
|
-
|
|
45
|
+
instance.interceptors.response.use(
|
|
37
46
|
(response) => response,
|
|
38
47
|
(error) => {
|
|
39
48
|
if (error.response) {
|
|
@@ -45,6 +54,40 @@ class TatilsepetiApi {
|
|
|
45
54
|
return null;
|
|
46
55
|
}
|
|
47
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);
|
|
48
91
|
}
|
|
49
92
|
|
|
50
93
|
clearEndpoint(endpoint) {
|
|
@@ -114,8 +157,9 @@ class TatilsepetiApi {
|
|
|
114
157
|
async get(endpoint, params = {}, version = 'v1') {
|
|
115
158
|
const url = this.buildUrl(endpoint, version);
|
|
116
159
|
const logId = this.logger.start(url, 'GET');
|
|
160
|
+
const instance = this.getInstance(endpoint);
|
|
117
161
|
|
|
118
|
-
const response = await
|
|
162
|
+
const response = await instance.get(url, {
|
|
119
163
|
params,
|
|
120
164
|
headers: await this.getAuthHeaders(),
|
|
121
165
|
});
|
|
@@ -162,8 +206,9 @@ class TatilsepetiApi {
|
|
|
162
206
|
async post(endpoint, body = {}, version = 'v1') {
|
|
163
207
|
const url = this.buildUrl(endpoint, version);
|
|
164
208
|
const logId = this.logger.start(url, 'POST');
|
|
209
|
+
const instance = this.getInstance(endpoint);
|
|
165
210
|
|
|
166
|
-
const response = await
|
|
211
|
+
const response = await instance.post(url, body, {
|
|
167
212
|
headers: await this.getAuthHeaders(),
|
|
168
213
|
});
|
|
169
214
|
|