@tatil/server-api 0.0.1 → 0.0.3
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 +180 -0
- package/index.js +1 -0
- package/package.json +6 -2
- package/src/utils/apiResponse.js +25 -0
- package/src/utils/auth.js +3 -6
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# @tatil/server-api
|
|
2
|
+
|
|
3
|
+
Tatilsepeti server-side API package. Next.js server components, route handlers ve middleware için token yönetimli API çağrıları sağlar.
|
|
4
|
+
|
|
5
|
+
## Kurulum
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @tatil/server-api
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Kullanım
|
|
12
|
+
|
|
13
|
+
### Tatilsepeti API
|
|
14
|
+
|
|
15
|
+
#### GET İsteği
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { Tatilsepeti } from '@tatil/server-api';
|
|
19
|
+
|
|
20
|
+
const data = await Tatilsepeti.get('/tour/list', {
|
|
21
|
+
page: 1,
|
|
22
|
+
pageSize: 20,
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
#### POST İsteği
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { Tatilsepeti } from '@tatil/server-api';
|
|
30
|
+
|
|
31
|
+
const result = await Tatilsepeti.post('/hotel/list', {
|
|
32
|
+
checkinDate: '2026-01-17',
|
|
33
|
+
checkoutDate: '2026-01-21',
|
|
34
|
+
pageNumber: 1,
|
|
35
|
+
pageSize: 20,
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### TokenManager
|
|
40
|
+
|
|
41
|
+
Token expire kontrolü ve refresh işlemleri için:
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { TokenManager } from '@tatil/server-api';
|
|
45
|
+
|
|
46
|
+
// Token expire kontrolü
|
|
47
|
+
const isExpired = TokenManager.isTokenExpired(tokenValue, tokenExpireValue);
|
|
48
|
+
|
|
49
|
+
// Token yenileme (gerekirse)
|
|
50
|
+
const { token, tokenExpire, refreshed } = await TokenManager.refreshTokenIfNeeded(
|
|
51
|
+
tokenValue,
|
|
52
|
+
tokenExpireValue
|
|
53
|
+
);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Response Helper
|
|
57
|
+
|
|
58
|
+
Server response oluşturma:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
import { Tatilsepeti, createResponse, createErrorResponse } from '@tatil/server-api';
|
|
62
|
+
|
|
63
|
+
// Başarılı response
|
|
64
|
+
return createResponse('success', data, 200);
|
|
65
|
+
|
|
66
|
+
// Hata response
|
|
67
|
+
return createErrorResponse('Bir hata oluştu', 400);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Veya Tatilsepeti instance üzerinden:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
import { Tatilsepeti } from '@tatil/server-api';
|
|
74
|
+
|
|
75
|
+
// Başarılı response
|
|
76
|
+
return Tatilsepeti.createResponse(data, 200);
|
|
77
|
+
|
|
78
|
+
// Hata response
|
|
79
|
+
return Tatilsepeti.createErrorResponse('Bir hata oluştu', 500);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Environment Variables
|
|
83
|
+
|
|
84
|
+
Bu paket aşağıdaki environment değişkenlerini gerektirir:
|
|
85
|
+
|
|
86
|
+
```env
|
|
87
|
+
API_URL=https://api.example.com
|
|
88
|
+
API_USERNAME=your_username
|
|
89
|
+
API_PASSWORD=your_password
|
|
90
|
+
REKLOG_KEY=your_reklog_key
|
|
91
|
+
NODE_ENV=production
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Özellikler
|
|
95
|
+
|
|
96
|
+
- Otomatik token yönetimi ve refresh
|
|
97
|
+
- Request logging ile Reklog entegrasyonu
|
|
98
|
+
- `/api/v1` prefix otomatik eklenir
|
|
99
|
+
- Cookie-based token storage
|
|
100
|
+
- Next.js server components ile tam uyumlu
|
|
101
|
+
|
|
102
|
+
## Örnek Kullanım (Next.js)
|
|
103
|
+
|
|
104
|
+
### Server Component
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
import { Tatilsepeti } from '@tatil/server-api';
|
|
108
|
+
|
|
109
|
+
export default async function Page() {
|
|
110
|
+
const tours = await Tatilsepeti.get('/tour/list', { page: 1 });
|
|
111
|
+
|
|
112
|
+
return <div>{/* ... */}</div>;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Route Handler
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
import { Tatilsepeti } from '@tatil/server-api';
|
|
120
|
+
|
|
121
|
+
export async function GET(request) {
|
|
122
|
+
const data = await Tatilsepeti.get('/tour/list');
|
|
123
|
+
|
|
124
|
+
if (!data) {
|
|
125
|
+
return Tatilsepeti.createErrorResponse('Failed to fetch', 500);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return Tatilsepeti.createResponse(data);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Middleware
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
import { TokenManager } from '@tatil/server-api';
|
|
136
|
+
import { NextResponse } from 'next/server';
|
|
137
|
+
|
|
138
|
+
export async function middleware(request) {
|
|
139
|
+
const token = request.cookies.get('token');
|
|
140
|
+
const tokenExpire = request.cookies.get('token_expire');
|
|
141
|
+
|
|
142
|
+
const isExpired = TokenManager.isTokenExpired(
|
|
143
|
+
token?.value,
|
|
144
|
+
tokenExpire?.value
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
if (isExpired) {
|
|
148
|
+
const { token: newToken } = await TokenManager.refreshTokenIfNeeded(
|
|
149
|
+
token?.value,
|
|
150
|
+
tokenExpire?.value
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const response = NextResponse.next();
|
|
154
|
+
response.cookies.set('token', newToken);
|
|
155
|
+
return response;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return NextResponse.next();
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## API Metotları
|
|
163
|
+
|
|
164
|
+
| Metot | Parametreler | Açıklama |
|
|
165
|
+
|-------|--------------|----------|
|
|
166
|
+
| `get(endpoint, params)` | endpoint: string/array, params: object | GET isteği |
|
|
167
|
+
| `post(endpoint, body)` | endpoint: string/array, body: object | POST isteği |
|
|
168
|
+
| `createResponse(data, status)` | data: any, status: number | Response objesi oluşturur |
|
|
169
|
+
| `createErrorResponse(message, status)` | message: string, status: number | Hata response'u oluşturur |
|
|
170
|
+
|
|
171
|
+
## TokenManager Metotları
|
|
172
|
+
|
|
173
|
+
| Metot | Parametreler | Açıklama |
|
|
174
|
+
|-------|--------------|----------|
|
|
175
|
+
| `isTokenExpired(token, tokenExpire)` | token: string, tokenExpire: string | Token expire kontrolü |
|
|
176
|
+
| `refreshTokenIfNeeded(token, tokenExpire)` | token: string, tokenExpire: string | Token yeniler (gerekirse) |
|
|
177
|
+
|
|
178
|
+
## Lisans
|
|
179
|
+
|
|
180
|
+
MIT
|
package/index.js
CHANGED
|
@@ -2,3 +2,4 @@ export { default as Tatilsepeti } from './src/utils/tatilsepeti';
|
|
|
2
2
|
export { default as TokenManager } from './src/utils/tokenManager';
|
|
3
3
|
export { getNewToken } from './src/utils/auth';
|
|
4
4
|
export { RequestLogger } from './src/utils/reklog';
|
|
5
|
+
export { createResponse, createErrorResponse } from './src/utils/apiResponse';
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tatil/server-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Tatilsepeti Server Api for Next.js server-side operations",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"next": ">=15"
|
|
11
|
+
},
|
|
9
12
|
"dependencies": {
|
|
10
13
|
"axios": "^1.13.2",
|
|
11
14
|
"moment": "^2.29.4",
|
|
12
|
-
"reklog-request": "^0.0.22"
|
|
15
|
+
"reklog-request": "^0.0.22",
|
|
16
|
+
"next": "15.5.7"
|
|
13
17
|
}
|
|
14
18
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create standardized API response
|
|
5
|
+
* @param {string} status - Response status ("success" | "error")
|
|
6
|
+
* @param {Object} data - Response data
|
|
7
|
+
* @param {number} httpStatus - HTTP status code (default: 200)
|
|
8
|
+
* @returns {NextResponse}
|
|
9
|
+
*/
|
|
10
|
+
export const createResponse = (status, data, httpStatus = 200) => {
|
|
11
|
+
return NextResponse.json(
|
|
12
|
+
{ status, data },
|
|
13
|
+
{ status: httpStatus }
|
|
14
|
+
);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create standardized error response
|
|
19
|
+
* @param {string} message - Error message
|
|
20
|
+
* @param {number} httpStatus - HTTP status code (default: 400)
|
|
21
|
+
* @returns {NextResponse}
|
|
22
|
+
*/
|
|
23
|
+
export const createErrorResponse = (message, httpStatus = 400) => {
|
|
24
|
+
return createResponse('error', { message }, httpStatus);
|
|
25
|
+
};
|
package/src/utils/auth.js
CHANGED
|
@@ -14,14 +14,11 @@ export const getNewToken = async () => {
|
|
|
14
14
|
},
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
const body = {
|
|
18
|
-
grant_type: 'password',
|
|
19
|
-
username: API_USERNAME,
|
|
20
|
-
password: API_PASSWORD,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
17
|
const req = await fetch(`${API_URL}/token`, options);
|
|
18
|
+
if (!req) return;
|
|
19
|
+
|
|
24
20
|
const { access_token, expires_in } = await req.json();
|
|
21
|
+
if (!access_token) return;
|
|
25
22
|
|
|
26
23
|
return { access_token, expires_in };
|
|
27
24
|
};
|