-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe47-api-plugin-basic.js
More file actions
328 lines (290 loc) · 11.4 KB
/
Copy pathe47-api-plugin-basic.js
File metadata and controls
328 lines (290 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Example 47 - API Plugin Basic Usage
*
* Demonstrates how to expose s3db.js resources as REST API endpoints
* using the API Plugin with automatic versioning and validation.
*
* Prerequisites:
* ```bash
* pnpm add s3db.js
* ```
*
* Features demonstrated:
* - Automatic REST endpoint generation for resources
* - Versioned API paths (e.g., /v1/cars, /v1/cars)
* - CORS support
* - Request logging
* - Response compression
* - Schema validation
* - Rate limiting
*
* Endpoints created:
* - GET /v1/cars → List all cars (with optional filtering via query params)
* - GET /v1/cars/:id → Get car by ID
* - POST /v1/cars → Create new car
* - PUT /v1/cars/:id → Update car (full)
* - PATCH /v1/cars/:id → Update car (partial)
* - DELETE /v1/cars/:id → Delete car
* - HEAD /v1/cars → Get statistics (count, version, schema fields in headers)
* - OPTIONS /v1/cars → Get resource metadata (schema, endpoints, allowed methods)
*/
import { Database } from '../../src/database.class.js';
import { ApiPlugin } from '../../src/plugins/api.plugin.js';
async function main() {
console.log('🚀 API Plugin Example - Basic Usage\n');
// Initialize database
const database = new Database({
bucket: 'api-plugin-example',
useFakeS3: true
});
await database.connect();
// Create cars resource
console.log('Creating cars resource...');
const cars = await database.createResource({
name: 'cars',
attributes: {
id: 'string|required',
brand: 'string|required|minlength:2',
model: 'string|required',
year: 'number|required|min:1900|max:2025',
price: 'number|required|min:0',
color: 'string|optional',
inStock: 'boolean|default:true',
features: 'array|items:string|optional'
},
behavior: 'body-overflow',
timestamps: true
});
console.log('✅ Cars resource created\n');
// Add API Plugin
console.log('Starting API server...');
const apiPlugin = new ApiPlugin({
port: 3000,
host: '0.0.0.0',
verbose: true,
// API Documentation
docs: {
enabled: true,
title: 's3db.js Cars API',
version: '1.0.0',
description: 'Example REST API for managing cars inventory'
},
// Enable production features
cors: {
enabled: true,
origin: '*',
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
},
rateLimit: {
enabled: true,
windowMs: 60000, // 1 minute
maxRequests: 100 // 100 requests per minute
},
logging: {
enabled: true,
format: ':method :path :status :response-time ms'
},
compression: {
enabled: true,
threshold: 1024 // Compress responses > 1KB
},
validation: {
enabled: true,
validateOnInsert: true,
validateOnUpdate: true
},
// Configure resources
resources: {
cars: {
auth: false, // Public access (no authentication)
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
}
}
});
await database.usePlugin(apiPlugin);
console.log('\n✅ API server running on http://localhost:3000');
console.log('\n📋 Available endpoints:');
console.log(' GET http://localhost:3000/v1/cars');
console.log(' GET http://localhost:3000/v1/cars/:id');
console.log(' POST http://localhost:3000/v1/cars');
console.log(' PUT http://localhost:3000/v1/cars/:id');
console.log(' PATCH http://localhost:3000/v1/cars/:id');
console.log(' DELETE http://localhost:3000/v1/cars/:id');
console.log(' HEAD http://localhost:3000/v1/cars');
console.log(' OPTIONS http://localhost:3000/v1/cars');
console.log('\n🏥 Health Check endpoints (Kubernetes):');
console.log(' GET http://localhost:3000/health - Generic health');
console.log(' GET http://localhost:3000/health/live - Liveness probe');
console.log(' GET http://localhost:3000/health/ready - Readiness probe');
console.log('\n📚 Interactive API Documentation:');
console.log(' http://localhost:3000/docs - API Documentation (Redoc)');
console.log(' http://localhost:3000/openapi.json - OpenAPI spec');
// Add some sample data
console.log('\n📝 Adding sample data...');
await cars.insert({
id: 'car-1',
brand: 'Toyota',
model: 'Corolla',
year: 2023,
price: 25000,
color: 'white',
inStock: true,
features: ['bluetooth', 'backup camera', 'cruise control']
});
await cars.insert({
id: 'car-2',
brand: 'Honda',
model: 'Civic',
year: 2024,
price: 28000,
color: 'blue',
inStock: true,
features: ['sunroof', 'leather seats', 'navigation']
});
await cars.insert({
id: 'car-3',
brand: 'Tesla',
model: 'Model 3',
year: 2024,
price: 45000,
color: 'black',
inStock: false,
features: ['autopilot', 'electric', 'premium audio']
});
console.log('✅ Sample data added (3 cars)\n');
// Test API calls using fetch (simulating HTTP requests)
console.log('🧪 Testing API endpoints...\n');
try {
// Test 1: List all cars
console.log('1️⃣ GET /v1/cars');
const listResponse = await fetch('http://localhost:3000/v1/cars');
const listData = await listResponse.json();
console.log(` Status: ${listResponse.status}`);
console.log(` Found: ${listData.data.length} cars`);
console.log(` Total: ${listData.pagination.total}\n`);
// Test 2: Get single car
console.log('2️⃣ GET /v1/cars/car-1');
const getResponse = await fetch('http://localhost:3000/v1/cars/car-1');
const getData = await getResponse.json();
console.log(` Status: ${getResponse.status}`);
console.log(` Car: ${getData.data.brand} ${getData.data.model}\n`);
// Test 3: Create new car
console.log('3️⃣ POST /v1/cars');
const createResponse = await fetch('http://localhost:3000/v1/cars', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: 'car-4',
brand: 'Ford',
model: 'Mustang',
year: 2024,
price: 55000,
color: 'red',
features: ['v8 engine', 'performance package']
})
});
const createData = await createResponse.json();
console.log(` Status: ${createResponse.status}`);
console.log(` Created: ${createData.data.id}\n`);
// Test 4: Update car (partial)
console.log('4️⃣ PATCH /v1/cars/car-1');
const updateResponse = await fetch('http://localhost:3000/v1/cars/car-1', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
price: 24000,
inStock: false
})
});
const updateData = await updateResponse.json();
console.log(` Status: ${updateResponse.status}`);
console.log(` Updated price: $${updateData.data.price}\n`);
// Test 5: Filter cars via query string
console.log('5️⃣ GET /v1/cars?inStock=true (filtering via query string)');
const filterResponse = await fetch('http://localhost:3000/v1/cars?inStock=true');
const filterData = await filterResponse.json();
console.log(` Status: ${filterResponse.status}`);
console.log(` In stock: ${filterData.data.length} cars\n`);
// Test 6: Get statistics with HEAD
console.log('6️⃣ HEAD /v1/cars (get statistics)');
const headResponse = await fetch('http://localhost:3000/v1/cars', {
method: 'HEAD'
});
console.log(` Status: ${headResponse.status}`);
console.log(` X-Total-Count: ${headResponse.headers.get('X-Total-Count')}`);
console.log(` X-Resource-Version: ${headResponse.headers.get('X-Resource-Version')}`);
console.log(` X-Schema-Fields: ${headResponse.headers.get('X-Schema-Fields')}\n`);
// Test 7: Get metadata with OPTIONS
console.log('7️⃣ OPTIONS /v1/cars (get resource metadata)');
const optionsResponse = await fetch('http://localhost:3000/v1/cars', {
method: 'OPTIONS'
});
const optionsData = await optionsResponse.json();
console.log(` Status: ${optionsResponse.status}`);
console.log(` Resource: ${optionsData.resource}`);
console.log(` Version: ${optionsData.version}`);
console.log(` Total records: ${optionsData.totalRecords}`);
console.log(` Allowed methods: ${optionsData.allowedMethods.join(', ')}`);
console.log(` Schema fields: ${optionsData.schema.length}\n`);
// Test 8: Validation error
console.log('8️⃣ POST /v1/cars (invalid data)');
const errorResponse = await fetch('http://localhost:3000/v1/cars', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: 'car-5',
brand: 'X', // Too short (min:2)
year: 1800 // Too old (min:1900)
})
});
const errorData = await errorResponse.json();
console.log(` Status: ${errorResponse.status}`);
console.log(` Errors: ${errorData.error.details.errors.length}`);
errorData.error.details.errors.forEach(err => {
console.log(` - ${err.field}: ${err.message}`);
});
console.log('');
// Test 9: Health checks (Kubernetes probes)
console.log('9️⃣ Health Check Endpoints:');
// Liveness probe
const liveResponse = await fetch('http://localhost:3000/health/live');
const liveData = await liveResponse.json();
console.log(` /health/live: ${liveResponse.status} - ${liveData.data.status}`);
// Readiness probe
const readyResponse = await fetch('http://localhost:3000/health/ready');
const readyData = await readyResponse.json();
console.log(` /health/ready: ${readyResponse.status} - ${readyData.data.status}`);
console.log(` Database connected: ${readyData.data.database.connected}`);
console.log(` Resources loaded: ${readyData.data.database.resources}`);
// Generic health
const healthResponse = await fetch('http://localhost:3000/health');
const healthData = await healthResponse.json();
console.log(` /health: ${healthResponse.status} - ${healthData.data.status}`);
console.log(` Uptime: ${Math.floor(healthData.data.uptime)}s\n`);
} catch (err) {
console.error('Error testing API:', err.message);
}
console.log('\n✅ All tests completed!');
console.log('\n💡 Try these commands in another terminal:');
console.log(' curl http://localhost:3000/v1/cars');
console.log(' curl http://localhost:3000/v1/cars/car-1');
console.log(' curl http://localhost:3000/health/live');
console.log(' curl http://localhost:3000/health/ready');
console.log(' curl -I http://localhost:3000/v1/cars # HEAD request');
console.log('\n📖 Or open these URLs in your browser:');
console.log(' http://localhost:3000/ - API info');
console.log(' http://localhost:3000/docs - Interactive docs (Swagger UI)');
console.log('\n☸️ Kubernetes Health Probes:');
console.log(' http://localhost:3000/health/live - Liveness (restarts pod if fails)');
console.log(' http://localhost:3000/health/ready - Readiness (removes from LB if fails)');
console.log('\n⏸️ Server is running. Press Ctrl+C to stop.');
// Keep process alive
process.on('SIGINT', async () => {
console.log('\n\n🛑 Stopping server...');
await apiPlugin.stop();
await database.disconnect();
console.log('✅ Server stopped');
process.exit(0);
});
}
main().catch(console.error);