balda-js 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.
Files changed (65) hide show
  1. package/package.json +1 -6
  2. package/.husky/pre-commit +0 -19
  3. package/.nvmrc +0 -1
  4. package/docs/README.md +0 -135
  5. package/docs/blog/authors.yml +0 -6
  6. package/docs/blog/tags.yml +0 -4
  7. package/docs/cli.md +0 -109
  8. package/docs/docs/core-concepts/controllers.md +0 -393
  9. package/docs/docs/core-concepts/middleware.md +0 -302
  10. package/docs/docs/core-concepts/request-response.md +0 -486
  11. package/docs/docs/core-concepts/routing.md +0 -388
  12. package/docs/docs/core-concepts/server.md +0 -332
  13. package/docs/docs/cron/overview.md +0 -70
  14. package/docs/docs/examples/rest-api.md +0 -595
  15. package/docs/docs/getting-started/configuration.md +0 -168
  16. package/docs/docs/getting-started/installation.md +0 -125
  17. package/docs/docs/getting-started/quick-start.md +0 -273
  18. package/docs/docs/intro.md +0 -46
  19. package/docs/docs/plugins/cookie.md +0 -424
  20. package/docs/docs/plugins/cors.md +0 -295
  21. package/docs/docs/plugins/file.md +0 -382
  22. package/docs/docs/plugins/helmet.md +0 -388
  23. package/docs/docs/plugins/json.md +0 -338
  24. package/docs/docs/plugins/log.md +0 -592
  25. package/docs/docs/plugins/overview.md +0 -390
  26. package/docs/docs/plugins/rate-limiter.md +0 -347
  27. package/docs/docs/plugins/static.md +0 -352
  28. package/docs/docs/plugins/swagger.md +0 -411
  29. package/docs/docs/plugins/urlencoded.md +0 -76
  30. package/docs/docs/testing/examples.md +0 -384
  31. package/docs/docs/testing/mock-server.md +0 -311
  32. package/docs/docs/testing/overview.md +0 -76
  33. package/docs/docusaurus.config.ts +0 -144
  34. package/docs/intro.md +0 -78
  35. package/docs/package.json +0 -46
  36. package/docs/sidebars.ts +0 -72
  37. package/docs/static/.nojekyll +0 -0
  38. package/docs/static/img/docusaurus-social-card.jpg +0 -0
  39. package/docs/static/img/docusaurus.png +0 -0
  40. package/docs/static/img/favicon.ico +0 -0
  41. package/docs/static/img/logo.svg +0 -1
  42. package/docs/static/img/undraw_docusaurus_mountain.svg +0 -37
  43. package/docs/static/img/undraw_docusaurus_react.svg +0 -170
  44. package/docs/static/img/undraw_docusaurus_tree.svg +0 -40
  45. package/docs/tsconfig.json +0 -8
  46. package/speed_test.sh +0 -3
  47. package/test/benchmark/index.ts +0 -17
  48. package/test/cli/cli.ts +0 -7
  49. package/test/commands/test.ts +0 -42
  50. package/test/controllers/file_upload.ts +0 -29
  51. package/test/controllers/urlencoded.ts +0 -13
  52. package/test/controllers/users.ts +0 -111
  53. package/test/cron/index.ts +0 -6
  54. package/test/cron/test_cron.ts +0 -8
  55. package/test/cron/test_cron_imported.ts +0 -8
  56. package/test/native_env.ts +0 -16
  57. package/test/resources/test.txt +0 -1
  58. package/test/server/index.ts +0 -3
  59. package/test/server/instance.ts +0 -63
  60. package/test/suite/upload.test.ts +0 -23
  61. package/test/suite/urlencoded.test.ts +0 -23
  62. package/test/suite/users.test.ts +0 -76
  63. package/todo.md +0 -9
  64. package/tsconfig.json +0 -24
  65. package/vitest.config.ts +0 -17
@@ -1,592 +0,0 @@
1
- ---
2
- sidebar_position: 8
3
- ---
4
-
5
- # Log Plugin
6
-
7
- The Log plugin provides comprehensive request and response logging for your Balda.js application. It automatically logs HTTP requests and responses with configurable detail levels and can be used both globally and on specific routes.
8
-
9
- ## Features
10
-
11
- - **Request Logging**: Log incoming requests with customizable detail
12
- - **Response Logging**: Log outgoing responses with status and body
13
- - **Configurable Detail**: Choose what information to log
14
- - **Global and Route-Level**: Apply logging globally or to specific routes
15
- - **Structured Logging**: JSON-formatted logs for easy parsing
16
- - **Performance Monitoring**: Track request timing and performance
17
-
18
- ## Basic Configuration
19
-
20
- ### Simple Setup
21
-
22
- ```typescript
23
- import { Server } from 'balda-js';
24
-
25
- const server = new Server({
26
- port: 3000,
27
- plugins: {
28
- log: {
29
- logRequest: true,
30
- logResponse: true
31
- }
32
- }
33
- });
34
- ```
35
-
36
- ### Detailed Configuration
37
-
38
- ```typescript
39
- const server = new Server({
40
- port: 3000,
41
- plugins: {
42
- log: {
43
- logRequest: true,
44
- logResponse: true,
45
- requestPayload: {
46
- method: true,
47
- url: true,
48
- ip: true,
49
- headers: true,
50
- body: false
51
- },
52
- responsePayload: {
53
- status: true,
54
- headers: false,
55
- body: false
56
- }
57
- }
58
- }
59
- });
60
- ```
61
-
62
- ## Configuration Options
63
-
64
- ### Basic Options
65
-
66
- ```typescript
67
- log: {
68
- logRequest: true, // Enable request logging (default: true)
69
- logResponse: true, // Enable response logging (default: true)
70
- requestPayload: {
71
- method: true, // Log HTTP method (default: true)
72
- url: true, // Log request URL (default: true)
73
- ip: true, // Log client IP (default: true)
74
- headers: true, // Log request headers (default: true)
75
- body: false // Log request body (default: false)
76
- },
77
- responsePayload: {
78
- status: true, // Log response status (default: true)
79
- headers: false, // Log response headers (default: false)
80
- body: false // Log response body (default: false)
81
- }
82
- }
83
- ```
84
-
85
- ### Advanced Configuration
86
-
87
- ```typescript
88
- log: {
89
- logRequest: true,
90
- logResponse: true,
91
- requestPayload: {
92
- method: true,
93
- url: true,
94
- ip: true,
95
- headers: false, // Don't log headers for privacy
96
- body: false
97
- },
98
- responsePayload: {
99
- status: true,
100
- headers: false,
101
- body: false
102
- },
103
- pinoOptions: {
104
- level: 'info',
105
- prettyPrint: true
106
- }
107
- }
108
- ```
109
-
110
- ## Usage Examples
111
-
112
- ### Global Logging
113
-
114
- ```typescript
115
- const server = new Server({
116
- port: 3000,
117
- plugins: {
118
- log: {
119
- logRequest: true,
120
- logResponse: true,
121
- requestPayload: {
122
- method: true,
123
- url: true,
124
- ip: true,
125
- headers: false,
126
- body: false
127
- },
128
- responsePayload: {
129
- status: true,
130
- headers: false,
131
- body: false
132
- }
133
- }
134
- }
135
- });
136
-
137
- @controller('/api')
138
- export class ApiController {
139
- @get('/users')
140
- async getUsers(req: Request, res: Response) {
141
- // All requests and responses will be logged automatically
142
- const users = await getUsers();
143
- res.json(users);
144
- }
145
- }
146
- ```
147
-
148
- ### Route-Level Logging
149
-
150
- ```typescript
151
- @controller('/api')
152
- export class ApiController {
153
- @get('/public', {
154
- middleware: [log({
155
- logRequest: true,
156
- logResponse: true,
157
- requestPayload: {
158
- method: true,
159
- url: true,
160
- ip: true,
161
- headers: false,
162
- body: false
163
- }
164
- })]
165
- })
166
- async publicEndpoint(req: Request, res: Response) {
167
- // Only this route will be logged
168
- res.json({ message: 'Public endpoint' });
169
- }
170
-
171
- @get('/private')
172
- async privateEndpoint(req: Request, res: Response) {
173
- // This route won't be logged
174
- res.json({ message: 'Private endpoint' });
175
- }
176
- }
177
- ```
178
-
179
- ### Different Logging Levels
180
-
181
- ```typescript
182
- @controller('/api')
183
- export class LoggingController {
184
- @get('/minimal', {
185
- middleware: [log({
186
- logRequest: true,
187
- logResponse: true,
188
- requestPayload: {
189
- method: true,
190
- url: true,
191
- ip: false,
192
- headers: false,
193
- body: false
194
- },
195
- responsePayload: {
196
- status: true,
197
- headers: false,
198
- body: false
199
- }
200
- })]
201
- })
202
- async minimalLogging(req: Request, res: Response) {
203
- res.json({ message: 'Minimal logging' });
204
- }
205
-
206
- @get('/detailed', {
207
- middleware: [log({
208
- logRequest: true,
209
- logResponse: true,
210
- requestPayload: {
211
- method: true,
212
- url: true,
213
- ip: true,
214
- headers: true,
215
- body: true
216
- },
217
- responsePayload: {
218
- status: true,
219
- headers: true,
220
- body: true
221
- }
222
- })]
223
- })
224
- async detailedLogging(req: Request, res: Response) {
225
- res.json({ message: 'Detailed logging' });
226
- }
227
- }
228
- ```
229
-
230
- ### Conditional Logging
231
-
232
- ```typescript
233
- @controller('/api')
234
- export class ConditionalController {
235
- @get('/conditional', {
236
- middleware: [log({
237
- logRequest: true,
238
- logResponse: (req, res) => {
239
- // Only log responses with status >= 400
240
- return res.responseStatus >= 400;
241
- },
242
- requestPayload: {
243
- method: true,
244
- url: true,
245
- ip: true,
246
- headers: false,
247
- body: false
248
- },
249
- responsePayload: {
250
- status: true,
251
- headers: false,
252
- body: true // Log body for errors
253
- }
254
- })]
255
- })
256
- async conditionalLogging(req: Request, res: Response) {
257
- if (Math.random() > 0.5) {
258
- res.status(500).json({ error: 'Random error' });
259
- } else {
260
- res.json({ message: 'Success' });
261
- }
262
- }
263
- }
264
- ```
265
-
266
- ## Log Output Examples
267
-
268
- ### Request Log
269
-
270
- ```json
271
- {
272
- "level": "info",
273
- "time": "2024-01-15T10:30:00.000Z",
274
- "type": "request",
275
- "requestId": "req-123",
276
- "method": "POST",
277
- "url": "/api/users",
278
- "ip": "192.168.1.100",
279
- "headers": {
280
- "content-type": "application/json",
281
- "authorization": "Bearer token123"
282
- },
283
- "body": {
284
- "name": "John Doe",
285
- "email": "john@example.com"
286
- }
287
- }
288
- ```
289
-
290
- ### Response Log
291
-
292
- ```json
293
- {
294
- "level": "info",
295
- "time": "2024-01-15T10:30:01.000Z",
296
- "type": "response",
297
- "requestId": "req-123",
298
- "status": 201,
299
- "headers": {
300
- "content-type": "application/json"
301
- },
302
- "body": {
303
- "id": 123,
304
- "name": "John Doe",
305
- "email": "john@example.com"
306
- }
307
- }
308
- ```
309
-
310
- ### Minimal Log
311
-
312
- ```json
313
- {
314
- "level": "info",
315
- "time": "2024-01-15T10:30:00.000Z",
316
- "type": "request",
317
- "requestId": "req-123",
318
- "method": "GET",
319
- "url": "/api/users"
320
- }
321
- ```
322
-
323
- ## Environment-Based Configuration
324
-
325
- ### Development vs Production
326
-
327
- ```typescript
328
- const isProduction = process.env.NODE_ENV === 'production';
329
-
330
- const server = new Server({
331
- port: 3000,
332
- plugins: {
333
- log: {
334
- logRequest: true,
335
- logResponse: true,
336
- requestPayload: {
337
- method: true,
338
- url: true,
339
- ip: true,
340
- headers: !isProduction, // Don't log headers in production
341
- body: !isProduction // Don't log bodies in production
342
- },
343
- responsePayload: {
344
- status: true,
345
- headers: false,
346
- body: !isProduction // Don't log response bodies in production
347
- },
348
- pinoOptions: {
349
- level: isProduction ? 'info' : 'debug',
350
- prettyPrint: !isProduction
351
- }
352
- }
353
- }
354
- });
355
- ```
356
-
357
- ### Different Logging for Different Routes
358
-
359
- ```typescript
360
- @controller('/api')
361
- export class DifferentLoggingController {
362
- @get('/public', {
363
- middleware: [log({
364
- logRequest: true,
365
- logResponse: true,
366
- requestPayload: {
367
- method: true,
368
- url: true,
369
- ip: true,
370
- headers: false,
371
- body: false
372
- }
373
- })]
374
- })
375
- async publicEndpoint(req: Request, res: Response) {
376
- res.json({ message: 'Public endpoint' });
377
- }
378
-
379
- @get('/admin', {
380
- middleware: [log({
381
- logRequest: true,
382
- logResponse: true,
383
- requestPayload: {
384
- method: true,
385
- url: true,
386
- ip: true,
387
- headers: true,
388
- body: true
389
- },
390
- responsePayload: {
391
- status: true,
392
- headers: true,
393
- body: true
394
- }
395
- })]
396
- })
397
- async adminEndpoint(req: Request, res: Response) {
398
- res.json({ message: 'Admin endpoint' });
399
- }
400
- }
401
- ```
402
-
403
- ## Performance Monitoring
404
-
405
- ### Request Timing
406
-
407
- ```typescript
408
- @controller('/api')
409
- export class PerformanceController {
410
- @get('/slow', {
411
- middleware: [log({
412
- logRequest: true,
413
- logResponse: true,
414
- requestPayload: {
415
- method: true,
416
- url: true,
417
- ip: true,
418
- headers: false,
419
- body: false
420
- },
421
- responsePayload: {
422
- status: true,
423
- headers: false,
424
- body: false
425
- }
426
- })]
427
- })
428
- async slowEndpoint(req: Request, res: Response) {
429
- // Simulate slow operation
430
- await new Promise(resolve => setTimeout(resolve, 2000));
431
- res.json({ message: 'Slow response' });
432
- }
433
- }
434
- ```
435
-
436
- ## Security Considerations
437
-
438
- ### Sensitive Data Filtering
439
-
440
- ```typescript
441
- @controller('/api')
442
- export class SecureController {
443
- @post('/login', {
444
- middleware: [log({
445
- logRequest: true,
446
- logResponse: true,
447
- requestPayload: {
448
- method: true,
449
- url: true,
450
- ip: true,
451
- headers: false, // Don't log auth headers
452
- body: false // Don't log passwords
453
- },
454
- responsePayload: {
455
- status: true,
456
- headers: false,
457
- body: false // Don't log tokens
458
- }
459
- })]
460
- })
461
- async login(req: Request, res: Response) {
462
- // Authentication logic
463
- res.json({ token: 'secret-token' });
464
- }
465
- }
466
- ```
467
-
468
- ## Integration with Other Plugins
469
-
470
- ### With Error Handling
471
-
472
- ```typescript
473
- @controller('/api')
474
- export class ErrorController {
475
- @get('/error', {
476
- middleware: [log({
477
- logRequest: true,
478
- logResponse: true,
479
- requestPayload: {
480
- method: true,
481
- url: true,
482
- ip: true,
483
- headers: false,
484
- body: false
485
- },
486
- responsePayload: {
487
- status: true,
488
- headers: false,
489
- body: true // Log error responses
490
- }
491
- })]
492
- })
493
- async errorEndpoint(req: Request, res: Response) {
494
- try {
495
- throw new Error('Something went wrong');
496
- } catch (error) {
497
- res.status(500).json({ error: error.message });
498
- }
499
- }
500
- }
501
- ```
502
-
503
- ## Best Practices
504
-
505
- ### Logging Recommendations
506
-
507
- 1. **Don't Log Sensitive Data**: Avoid logging passwords, tokens, PII
508
- 2. **Use Appropriate Detail Levels**: Different levels for different environments
509
- 3. **Monitor Log Volume**: Be aware of log storage and performance impact
510
- 4. **Structured Logging**: Use JSON format for easy parsing
511
- 5. **Request ID Tracking**: Use request IDs to correlate logs
512
-
513
- ### Configuration Tips
514
-
515
- ```typescript
516
- // Production configuration
517
- const productionLogConfig = {
518
- logRequest: true,
519
- logResponse: true,
520
- requestPayload: {
521
- method: true,
522
- url: true,
523
- ip: true,
524
- headers: false, // Privacy
525
- body: false // Security
526
- },
527
- responsePayload: {
528
- status: true,
529
- headers: false,
530
- body: false // Security
531
- },
532
- pinoOptions: {
533
- level: 'info',
534
- prettyPrint: false
535
- }
536
- };
537
-
538
- // Development configuration
539
- const developmentLogConfig = {
540
- logRequest: true,
541
- logResponse: true,
542
- requestPayload: {
543
- method: true,
544
- url: true,
545
- ip: true,
546
- headers: true,
547
- body: true
548
- },
549
- responsePayload: {
550
- status: true,
551
- headers: true,
552
- body: true
553
- },
554
- pinoOptions: {
555
- level: 'debug',
556
- prettyPrint: true
557
- }
558
- };
559
- ```
560
-
561
- ### Monitoring and Alerting
562
-
563
- ```typescript
564
- @controller('/api')
565
- export class MonitoredController {
566
- @get('/monitored', {
567
- middleware: [log({
568
- logRequest: true,
569
- logResponse: true,
570
- requestPayload: {
571
- method: true,
572
- url: true,
573
- ip: true,
574
- headers: false,
575
- body: false
576
- },
577
- responsePayload: {
578
- status: true,
579
- headers: false,
580
- body: false
581
- },
582
- onError: (error, req, res) => {
583
- // Send alert for errors
584
- alertService.sendAlert(`Error in ${req.url}: ${error.message}`);
585
- }
586
- })]
587
- })
588
- async monitoredEndpoint(req: Request, res: Response) {
589
- res.json({ message: 'Monitored endpoint' });
590
- }
591
- }
592
- ```