@avleon/core 0.0.26 → 0.0.28

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 (41) hide show
  1. package/README.md +601 -561
  2. package/package.json +38 -6
  3. package/src/application.ts +104 -125
  4. package/src/authentication.ts +16 -16
  5. package/src/cache.ts +91 -91
  6. package/src/collection.test.ts +71 -0
  7. package/src/collection.ts +344 -254
  8. package/src/config.test.ts +35 -0
  9. package/src/config.ts +85 -42
  10. package/src/constants.ts +1 -1
  11. package/src/container.ts +54 -54
  12. package/src/controller.ts +125 -127
  13. package/src/decorators.ts +27 -27
  14. package/src/environment-variables.ts +53 -46
  15. package/src/exceptions/http-exceptions.ts +86 -86
  16. package/src/exceptions/index.ts +1 -1
  17. package/src/exceptions/system-exception.ts +35 -34
  18. package/src/file-storage.ts +206 -206
  19. package/src/helpers.ts +324 -328
  20. package/src/icore.ts +66 -90
  21. package/src/index.ts +30 -30
  22. package/src/interfaces/avleon-application.ts +32 -40
  23. package/src/logger.ts +72 -72
  24. package/src/map-types.ts +159 -159
  25. package/src/middleware.ts +119 -98
  26. package/src/multipart.ts +116 -116
  27. package/src/openapi.ts +372 -372
  28. package/src/params.ts +111 -111
  29. package/src/queue.ts +126 -126
  30. package/src/response.ts +74 -74
  31. package/src/results.ts +30 -30
  32. package/src/route-methods.ts +186 -186
  33. package/src/swagger-schema.ts +213 -213
  34. package/src/testing.ts +220 -220
  35. package/src/types/app-builder.interface.ts +18 -19
  36. package/src/types/application.interface.ts +7 -9
  37. package/src/utils/hash.ts +8 -5
  38. package/src/utils/index.ts +2 -2
  39. package/src/utils/optional-require.ts +50 -50
  40. package/src/validation.ts +160 -156
  41. package/src/validator-extend.ts +25 -25
package/README.md CHANGED
@@ -1,561 +1,601 @@
1
- # Avleon Core Framework
2
-
3
- ## Overview
4
-
5
- Avleon is a powerful, TypeScript-based web framework built on top of Fastify, designed to simplify API development with a focus on decorators, dependency injection, and OpenAPI documentation. It provides a robust set of tools for building scalable, maintainable web applications with minimal boilerplate code.
6
-
7
- ## Table of Contents
8
-
9
- - [Features](#features)
10
- - [Installation](#installation)
11
- - [Quick Start](#quick-start)
12
- - [Core Concepts](#core-concepts)
13
- - [Application Creation](#application-creation)
14
- - [Controllers](#controllers)
15
- - [Route Methods](#route-methods)
16
- - [Parameter Decorators](#parameter-decorators)
17
- - [Response Handling](#response-handling)
18
- - [Middleware](#middleware)
19
- - [Authentication & Authorization](#authentication--authorization)
20
- - [Validation](#validation)
21
- - [OpenAPI Documentation](#openapi-documentation)
22
- - [Advanced Features](#advanced-features)
23
- - [Database Integration](#database-integration)
24
- - [File Uploads](#file-uploads)
25
- - [Static Files](#static-files)
26
- - [Testing](#testing)
27
- - [Configuration](#configuration)
28
- - [Route Mapping](#route-mapping)
29
- - [mapGet](#mapget)
30
- - [mapPost](#mappost)
31
- - [mapPut](#mapput)
32
- - [mapDelete](#mapdelete)
33
-
34
- ## Features
35
-
36
- - **Decorator-based API Development**: Define controllers, routes, and middleware using TypeScript decorators
37
- - **Dependency Injection**: Built-in DI system using TypeDI for service management
38
- - **OpenAPI/Swagger Integration**: Automatic API documentation generation with support for both Swagger UI and Scalar
39
- - **Validation**: Request validation with support for class-validator and custom validation rules
40
- - **Middleware System**: Flexible middleware architecture for request processing
41
- - **Response Handling**: Standardized response formats with HTTP status codes
42
- - **File Upload**: Built-in support for multipart file uploads with file validation
43
- - **Authentication & Authorization**: Middleware for securing your API endpoints
44
- - **Database Integration**: Support for TypeORM for database operations
45
- - **Queue System**: Background job processing capabilities
46
- - **Environment Configuration**: Environment variable management
47
- - **Logging**: Integrated logging with Pino
48
- - **Testing**: Built-in testing utilities for API endpoints
49
-
50
- ## Installation
51
-
52
- ```bash
53
- npm install @avleon/core
54
- # or
55
- yarn add @avleon/core
56
- # or
57
- pnpm add @avleon/core
58
- ```
59
-
60
- ## Quick Start
61
-
62
- ```typescript
63
- import { Avleon, ApiController, Get, Results } from '@avleon/core';
64
-
65
- // Define a controller
66
- @ApiController('/hello')
67
- class HelloController {
68
- @Get('/')
69
- async sayHello() {
70
- return Results.Ok({ message: 'Hello, Avleon!' });
71
- }
72
- }
73
-
74
- // Create and start the application
75
- const app = new Avleon({
76
- controllers: [HelloController],
77
- openapi: {
78
- info: {
79
- title: 'My Avleon API',
80
- version: '1.0.0',
81
- },
82
- },
83
- });
84
-
85
- app.start();
86
- ```
87
-
88
- ## Core Concepts
89
-
90
- ### Application Creation
91
-
92
- Avleon provides a builder pattern for creating applications:
93
-
94
- ```typescript
95
- import { Builder } from '@avleon/core';
96
-
97
- // Create an application using the builder
98
- const builder = Builder.createAppBuilder();
99
- const app = builder.build();
100
-
101
- // Configure and start the application
102
- app.useCors();
103
- app.mapControllers([UserController]);
104
- app.start(3000);
105
- ```
106
-
107
- ### Controllers
108
-
109
- Controllers are the entry points for your API requests. They are defined using the `@ApiController` decorator:
110
-
111
- ```typescript
112
- @ApiController('/users')
113
- class UserController {
114
- // Route handlers go here
115
- }
116
- ```
117
-
118
- You can also specify controller options:
119
-
120
- ```typescript
121
- @ApiController({
122
- path: '/users',
123
- name: 'User Management',
124
- version: '1.0.0',
125
- since: '2024-01-01'
126
- })
127
- class UserController {
128
- // Route handlers go here
129
- }
130
- ```
131
-
132
- ### Route Methods
133
-
134
- Define HTTP methods using decorators:
135
-
136
- ```typescript
137
- @Get('/')
138
- async getUsers() {
139
- // Handle GET request
140
- }
141
-
142
- @Post('/')
143
- async createUser(@Body() user: UserDto) {
144
- // Handle POST request
145
- }
146
-
147
- @Put('/:id')
148
- async updateUser(@Param('id') id: string, @Body() user: UserDto) {
149
- // Handle PUT request
150
- }
151
-
152
- @Delete('/:id')
153
- async deleteUser(@Param('id') id: string) {
154
- // Handle DELETE request
155
- }
156
- ```
157
-
158
- ### Parameter Decorators
159
-
160
- Extract data from requests using parameter decorators:
161
-
162
- ```typescript
163
- @Get('/:id')
164
- async getUser(
165
- @Param('id') id: string,
166
- @Query('include') include: string,
167
- @Header('authorization') token: string,
168
- @Body() data: UserDto
169
- ) {
170
- // Access route parameters, query strings, headers, and request body
171
- }
172
- ```
173
-
174
- You can also access the current user and files:
175
-
176
- ```typescript
177
- @Post('/upload')
178
- async uploadFile(
179
- @User() currentUser: any,
180
- @File() file: any
181
- ) {
182
- // Access the current user and uploaded file
183
- }
184
-
185
- @Post('/upload-multiple')
186
- async uploadFiles(
187
- @Files() files: any[]
188
- ) {
189
- // Access multiple uploaded files
190
- }
191
- ```
192
-
193
- ### Response Handling
194
-
195
- Return standardized responses using the `Results` class:
196
-
197
- ```typescript
198
- @Get('/:id')
199
- async getUser(@Param('id') id: string) {
200
- const user = await this.userService.findById(id);
201
-
202
- if (!user) {
203
- return Results.NotFound('User not found');
204
- }
205
-
206
- return Results.Ok(user);
207
- }
208
- ```
209
-
210
- ### Middleware
211
-
212
- Create and apply middleware for cross-cutting concerns:
213
-
214
- ```typescript
215
- @Middleware
216
- class LoggingMiddleware extends AppMiddleware {
217
- async invoke(req: IRequest) {
218
- console.log(`Request: ${req.method} ${req.url}`);
219
- return req;
220
- }
221
- }
222
-
223
- @UseMiddleware(LoggingMiddleware)
224
- @ApiController('/users')
225
- class UserController {
226
- // Controller methods
227
- }
228
- ```
229
-
230
- You can also apply middleware to specific routes:
231
-
232
- ```typescript
233
- @ApiController('/users')
234
- class UserController {
235
- @UseMiddleware(LoggingMiddleware)
236
- @Get('/')
237
- async getUsers() {
238
- // Only this route will use the LoggingMiddleware
239
- }
240
- }
241
- ```
242
-
243
- ### Authentication & Authorization
244
-
245
- Secure your API with authentication and authorization:
246
-
247
- ```typescript
248
- @Authorize
249
- class JwtAuthMiddleware extends AuthorizeMiddleware {
250
- authorize(roles: string[]) {
251
- return async (req: IRequest) => {
252
- // Implement JWT authentication logic
253
- return req;
254
- };
255
- }
256
- }
257
-
258
- @Authorized()
259
- @ApiController('/admin')
260
- class AdminController {
261
- // Protected controller methods
262
- }
263
-
264
- // Or protect specific routes with roles
265
- @ApiController('/admin')
266
- class AdminController {
267
- @Authorized(['admin'])
268
- @Get('/')
269
- async adminDashboard() {
270
- // Only users with 'admin' role can access this
271
- }
272
- }
273
- ```
274
-
275
- ### Validation
276
-
277
- Validate request data using class-validator:
278
-
279
- ```typescript
280
- class UserDto {
281
- @IsString()
282
- @IsNotEmpty()
283
- name: string;
284
-
285
- @IsEmail()
286
- email: string;
287
-
288
- @IsNumber()
289
- @Min(0)
290
- @Max(120)
291
- age: number;
292
- }
293
-
294
- @Post('/')
295
- async createUser(@Body() user: UserDto) {
296
- // User data is automatically validated
297
- return Results.Created(user);
298
- }
299
- ```
300
-
301
- You can also use custom validation rules:
302
-
303
- ```typescript
304
- class UserDto {
305
- @Validate({
306
- type: 'string',
307
- required: true,
308
- message: 'Name is required'
309
- })
310
- name: string;
311
-
312
- @Validate({
313
- type: 'number',
314
- min: 0,
315
- max: 120,
316
- message: 'Age must be between 0 and 120'
317
- })
318
- age: number;
319
- }
320
- ```
321
-
322
- ### OpenAPI Documentation
323
-
324
- Generate API documentation automatically:
325
-
326
- ```typescript
327
- const app = new Avleon({
328
- controllers: [UserController],
329
- openapi: {
330
- info: {
331
- title: 'User API',
332
- version: '1.0.0',
333
- description: 'API for managing users',
334
- },
335
- servers: [
336
- {
337
- url: 'http://localhost:3000',
338
- description: 'Development server',
339
- },
340
- ],
341
- },
342
- });
343
- ```
344
-
345
- You can also customize the OpenAPI UI:
346
-
347
- ```typescript
348
- app.useOpenApi(OpenApiConfig, (config) => {
349
- // Modify the OpenAPI configuration
350
- config.info.title = 'Custom API Title';
351
- return config;
352
- });
353
-
354
- // Or use Swagger UI with custom options
355
- app.useSwagger({
356
- routePrefix: '/api-docs',
357
- ui: 'default', // or 'scalar' for Scalar UI
358
- theme: {
359
- // Custom theme options
360
- }
361
- });
362
- ```
363
-
364
- ## Advanced Features
365
-
366
- ### Database Integration
367
-
368
- Connect to databases using TypeORM:
369
-
370
- ```typescript
371
- const app = new Avleon({
372
- controllers: [UserController],
373
- database: {
374
- type: 'postgres',
375
- host: 'localhost',
376
- port: 5432,
377
- username: 'postgres',
378
- password: 'password',
379
- database: 'avleon',
380
- entities: [User],
381
- synchronize: true,
382
- },
383
- });
384
- ```
385
-
386
- Or use the builder pattern:
387
-
388
- ```typescript
389
- const builder = Builder.createAppBuilder();
390
- builder.addDataSource(DatabaseConfig);
391
- const app = builder.build();
392
- ```
393
-
394
- ### File Uploads
395
-
396
- Handle file uploads with multipart support:
397
-
398
- ```typescript
399
- // Configure multipart file uploads
400
- app.useMultipart({
401
- destination: path.join(process.cwd(), 'uploads'),
402
- limits: {
403
- fileSize: 5 * 1024 * 1024 // 5MB
404
- }
405
- });
406
-
407
- // In your controller
408
- @Post('/upload')
409
- async uploadFile(@File() file: any) {
410
- // Process uploaded file
411
- return Results.Ok({ filename: file.filename });
412
- }
413
- ```
414
-
415
- ### Static Files
416
-
417
- Serve static files:
418
-
419
- ```typescript
420
- app.useStaticFiles({
421
- path: path.join(process.cwd(), 'public'),
422
- prefix: '/static/'
423
- });
424
- ```
425
-
426
- ### Testing
427
-
428
- Test your API endpoints with the built-in testing utilities:
429
-
430
- ```typescript
431
- import { TestBuilder } from '@avleon/core';
432
-
433
- const testBuilder = TestBuilder.createBuilder();
434
- const app = testBuilder.getTestApplication({
435
- controllers: [UserController]
436
- });
437
-
438
- // Test your API endpoints
439
- const response = await app.get('/users');
440
- expect(response.statusCode).toBe(200);
441
- ```
442
-
443
- ## Configuration
444
-
445
- Configure your application with environment variables:
446
-
447
- ```typescript
448
- // .env
449
- PORT=3000
450
- DATABASE_URL=postgres://user:password@localhost:5432/db
451
-
452
- // app.ts
453
- import { Environment } from '@avleon/core';
454
-
455
- const env = new Environment();
456
- env.load();
457
-
458
- const app = new Avleon({
459
- controllers: [UserController],
460
- env: {
461
- port: 'PORT',
462
- databaseUrl: 'DATABASE_URL',
463
- },
464
- });
465
- ```
466
-
467
- ## Route Mapping
468
-
469
- Avleon provides several methods for mapping routes in your application:
470
-
471
- ### mapGet
472
-
473
- The `mapGet` method is used to define GET routes in your application. It takes a path string and a handler function as parameters.
474
-
475
- ```typescript
476
- app.mapGet("/users", async (req, res) => {
477
- // Handle GET request to /users
478
- return { users: [] };
479
- });
480
- ```
481
-
482
- ### mapPost
483
-
484
- The `mapPost` method is used to define POST routes in your application. It takes a path string and a handler function as parameters.
485
-
486
- ```typescript
487
- app.mapPost("/users", async (req, res) => {
488
- // Handle POST request to /users
489
- const userData = req.body;
490
- // Process user data
491
- return { success: true };
492
- });
493
- ```
494
-
495
- ### mapPut
496
-
497
- The `mapPut` method is used to define PUT routes in your application. It takes a path string and a handler function as parameters.
498
-
499
- ```typescript
500
- app.mapPut("/users/:id", async (req, res) => {
501
- // Handle PUT request to /users/:id
502
- const userId = req.params.id;
503
- const userData = req.body;
504
- // Update user data
505
- return { success: true };
506
- });
507
- ```
508
-
509
- ### mapDelete
510
-
511
- The `mapDelete` method is used to define DELETE routes in your application. It takes a path string and a handler function as parameters.
512
-
513
- ```typescript
514
- app.mapDelete("/users/:id", async (req, res) => {
515
- // Handle DELETE request to /users/:id
516
- const userId = req.params.id;
517
- // Delete user
518
- return { success: true };
519
- });
520
- ```
521
-
522
- Each of these methods returns a route object that can be used to add middleware or Swagger documentation to the route.
523
-
524
- ```typescript
525
- app.mapGet("/users", async (req, res) => {
526
- // Handler function
527
- })
528
- .useMiddleware([AuthMiddleware])
529
- .useSwagger({
530
- summary: "Get all users",
531
- description: "Retrieves a list of all users",
532
- tags: ["users"],
533
- response: {
534
- 200: {
535
- description: "Successful response",
536
- content: {
537
- "application/json": {
538
- schema: {
539
- type: "array",
540
- items: {
541
- type: "object",
542
- properties: {
543
- id: { type: "string" },
544
- name: { type: "string" }
545
- }
546
- }
547
- }
548
- }
549
- }
550
- }
551
- }
552
- });
553
- ```
554
-
555
- ## License
556
-
557
- ISC
558
-
559
- ## Author
560
-
561
- Tareq Hossain - [GitHub](https://github.com/xtareq)
1
+ # AvleonJs
2
+
3
+ ## ⚠️ WARNING: NOT FOR PRODUCTION USE
4
+
5
+ > **🚧 This project is in active development.**
6
+ >
7
+ > It is **not stable** and **not ready** for live environments.
8
+ > Use **only for testing, experimentation, or internal evaluation**.
9
+ >
10
+ > ####❗ Risks of using this in production:
11
+ >
12
+ > - 🔄 Breaking changes may be introduced at any time
13
+ > - 🧪 Features are experimental and may be unstable
14
+ > - 🔐 Security has not been audited
15
+ > - 💥 Potential for data loss or critical errors
16
+ >
17
+ > **Please do not deploy this in production environments.**
18
+
19
+ ## Overview
20
+
21
+ Avleon is a powerful, TypeScript-based web framework built on top of Fastify, designed to simplify API development with a focus on decorators, dependency injection, and OpenAPI documentation. It provides a robust set of tools for building scalable, maintainable web applications with minimal boilerplate code.
22
+
23
+ ## Table of Contents
24
+
25
+ - [Features](#features)
26
+ - [Installation](#installation)
27
+ - [Quick Start](#quick-start)
28
+ - [Route Based](#route-based)
29
+ - [Controller Based](#controller-based)
30
+ - [Core Concepts](#core-concepts)
31
+ - [Application Creation](#application-creation)
32
+ - [Controllers](#controllers)
33
+ - [Route Methods](#route-methods)
34
+ - [Parameter Decorators](#parameter-decorators)
35
+ - [Response Handling](#response-handling)
36
+ - [Middleware](#middleware)
37
+ - [Authentication & Authorization](#authentication--authorization)
38
+ - [Validation](#validation)
39
+ - [OpenAPI Documentation](#openapi-documentation)
40
+ - [Advanced Features](#advanced-features)
41
+ - [Database Integration](#database-integration)
42
+ - [File Uploads](#file-uploads)
43
+ - [Static Files](#static-files)
44
+ - [Testing](#testing)
45
+ - [Configuration](#configuration)
46
+ - [Route Mapping](#route-mapping)
47
+ - [mapGet](#mapget)
48
+ - [mapPost](#mappost)
49
+ - [mapPut](#mapput)
50
+ - [mapDelete](#mapdelete)
51
+
52
+ ## Features
53
+
54
+ - **Decorator-based API Development**: Define controllers, routes, and middleware using TypeScript decorators
55
+ - **Dependency Injection**: Built-in DI system using TypeDI for service management
56
+ - **OpenAPI/Swagger Integration**: Automatic API documentation generation with support for both Swagger UI and Scalar
57
+ - **Validation**: Request validation with support for class-validator and custom validation rules
58
+ - **Middleware System**: Flexible middleware architecture for request processing
59
+ - **Response Handling**: Standardized response formats with HTTP status codes
60
+ - **File Upload**: Built-in support for multipart file uploads with file validation
61
+ - **Authentication & Authorization**: Middleware for securing your API endpoints
62
+ - **Database Integration**: Support for TypeORM for database operations
63
+ - **Queue System**: Background job processing capabilities
64
+ - **Environment Configuration**: Environment variable management
65
+ - **Logging**: Integrated logging with Pino
66
+ - **Testing**: Built-in testing utilities for API endpoints
67
+
68
+ ## Installation
69
+
70
+ ```bash
71
+ npm install @avleon/core
72
+ # or
73
+ yarn add @avleon/core
74
+ # or
75
+ pnpm add @avleon/core
76
+ ```
77
+
78
+ ## Quick Start
79
+
80
+ ### Route Based
81
+
82
+ ```typescript
83
+ import { Avleon, ApiController, Get, Results } from "@avleon/core";
84
+
85
+ const app = Avleon.createApplication();
86
+ app.mapGet("/", () => "Hello, Avleon");
87
+ app.run(); // or app.run(3000);
88
+ ```
89
+
90
+ ### Controller Based
91
+
92
+ ```typescript
93
+ import { Avleon, ApiController, Get, Results } from "@avleon/core";
94
+
95
+ // Define a controller
96
+ @ApiController
97
+ class HelloController {
98
+ @Get()
99
+ sayHello() {
100
+ return "Hello, Avleon!";
101
+ }
102
+ }
103
+
104
+ // Create and start the application
105
+ const app = Avleon.createApplication();
106
+ app.useControllers([HelloController]);
107
+ app.run();
108
+ ```
109
+
110
+ ## Core Concepts
111
+
112
+ ### Application Creation
113
+
114
+ Avleon provides a builder pattern for creating applications:
115
+
116
+ ```typescript
117
+ import { Avleon } from "@avleon/core";
118
+
119
+ // Create an application
120
+ const app = Avleon.createApplication();
121
+
122
+ // Configure and run the application
123
+ app.useCors();
124
+ app.useControllers([UserController]);
125
+ app.run(3000);
126
+ ```
127
+
128
+ ### Controllers
129
+
130
+ Controllers are the entry points for your API requests. They are defined using the `@ApiController` decorator:
131
+
132
+ ```typescript
133
+ @ApiController("/users")
134
+ class UserController {
135
+ // Route handlers go here
136
+ }
137
+ ```
138
+
139
+ ### Route Methods
140
+
141
+ Define HTTP methods using decorators:
142
+
143
+ ```typescript
144
+ @Get('/')
145
+ async getUsers() {
146
+ // Handle GET request
147
+ }
148
+
149
+ @Post('/')
150
+ async createUser(@Body() user: UserDto) {
151
+ // Handle POST request
152
+ }
153
+
154
+ @Put('/:id')
155
+ async updateUser(@Param('id') id: string, @Body() user: UserDto) {
156
+ // Handle PUT request
157
+ }
158
+
159
+ @Delete('/:id')
160
+ async deleteUser(@Param('id') id: string) {
161
+ // Handle DELETE request
162
+ }
163
+ ```
164
+
165
+ ### Parameter Decorators
166
+
167
+ Extract data from requests using parameter decorators:
168
+
169
+ ```typescript
170
+ @Get('/:id')
171
+ async getUser(
172
+ @Param('id') id: string,
173
+ @Query('include') include: string,
174
+ @Header('authorization') token: string,
175
+ @Body() data: UserDto
176
+ ) {
177
+ // Access route parameters, query strings, headers, and request body
178
+ }
179
+ ```
180
+
181
+ <!-- You can also access the current user and files:
182
+
183
+ ```typescript
184
+ @Post('/upload')
185
+ async uploadFile(
186
+ @User() currentUser: any,
187
+ @File() file: any
188
+ ) {
189
+ // Access the current user and uploaded file
190
+ }
191
+
192
+ @Post('/upload-multiple')
193
+ async uploadFiles(
194
+ @Files() files: any[]
195
+ ) {
196
+ // Access multiple uploaded files
197
+ }
198
+ ``` -->
199
+
200
+ ### Error Handling
201
+
202
+ Return standardized responses using the `HttpResponse` and `HttpExceptions` class:
203
+
204
+ ```typescript
205
+ @Get('/:id')
206
+ async getUser(@Param('id') id: string) {
207
+ const user = await this.userService.findById(id);
208
+
209
+ if (!user) {
210
+ throw HttpExceptions.NotFound('User not found');
211
+ }
212
+
213
+ return HttpResponse.Ok(user);
214
+ }
215
+ ```
216
+
217
+ ### Middleware
218
+
219
+ Create and apply middleware for cross-cutting concerns:
220
+
221
+ ```typescript
222
+ @Middleware
223
+ class LoggingMiddleware extends AppMiddleware {
224
+ async invoke(req: IRequest) {
225
+ console.log(`Request: ${req.method} ${req.url}`);
226
+ return req;
227
+ }
228
+ }
229
+
230
+ @UseMiddleware(LoggingMiddleware)
231
+ @ApiController("/users")
232
+ class UserController {
233
+ // Controller methods
234
+ }
235
+ ```
236
+
237
+ You can also apply middleware to specific routes:
238
+
239
+ ```typescript
240
+ @ApiController("/users")
241
+ class UserController {
242
+ @UseMiddleware(LoggingMiddleware)
243
+ @Get("/")
244
+ async getUsers() {
245
+ // Only this route will use the LoggingMiddleware
246
+ }
247
+ }
248
+ ```
249
+
250
+ ### Authentication & Authorization
251
+
252
+ Secure your API with authentication and authorization:
253
+
254
+ ```typescript
255
+ @Authorize
256
+ class JwtAuthorization extends AuthorizeMiddleware {
257
+ authorize(roles: string[]) {
258
+ return async (req: IRequest) => {
259
+ // Implement JWT authentication logic
260
+ return req;
261
+ };
262
+ }
263
+ }
264
+ ```
265
+
266
+ Now register the authrization class to our app by `useAuthorization` function;
267
+
268
+ ```typescript
269
+ app.useAuthorization(JwtAuthorization);
270
+ ```
271
+
272
+ Then you have access the `AuthUser` on class lavel or method lavel depending on how you use the `@Authorized()` decorator.
273
+
274
+ ```typescript
275
+ // admin.controller.ts
276
+ @Authorized()
277
+ @ApiController("/admin")
278
+ class AdminController {
279
+ // Protected controller methods
280
+
281
+ // protected controller has access to AuthUser in each route method
282
+ @Get()
283
+ async account(@AuthUser() user: User) {
284
+ ///
285
+ }
286
+ }
287
+
288
+ // Or protect specific routes with roles
289
+ @ApiController("/admin")
290
+ class AdminController {
291
+ @Authorized({
292
+ roles: ["admin"],
293
+ })
294
+ @Get("/")
295
+ async adminDashboard() {
296
+ // Only users with 'admin' role can access this
297
+ }
298
+ }
299
+ ```
300
+
301
+ ### Validation
302
+
303
+ Validate request data using class-validator:
304
+
305
+ ```typescript
306
+ class UserDto {
307
+ @IsString()
308
+ @IsNotEmpty()
309
+ name: string;
310
+
311
+ @IsEmail()
312
+ email: string;
313
+
314
+ @IsNumber()
315
+ @Min(0)
316
+ @Max(120)
317
+ age: number;
318
+ }
319
+
320
+ @Post('/')
321
+ async createUser(@Body() user: UserDto) {
322
+ // User data is automatically validated
323
+ return user;
324
+ }
325
+ ```
326
+
327
+ You can also use custom validation rules:
328
+
329
+ ```typescript
330
+ class UserDto {
331
+ @Validate({
332
+ type: "string",
333
+ required: true,
334
+ message: "Name is required",
335
+ })
336
+ name: string;
337
+
338
+ @Validate({
339
+ type: "number",
340
+ min: 0,
341
+ max: 120,
342
+ message: "Age must be between 0 and 120",
343
+ })
344
+ age: number;
345
+ }
346
+ ```
347
+
348
+ ### OpenAPI Documentation
349
+
350
+ Generate API documentation automatically:
351
+
352
+ ```typescript
353
+ const app = new Avleon({
354
+ controllers: [UserController],
355
+ openapi: {
356
+ info: {
357
+ title: "User API",
358
+ version: "1.0.0",
359
+ description: "API for managing users",
360
+ },
361
+ servers: [
362
+ {
363
+ url: "http://localhost:3000",
364
+ description: "Development server",
365
+ },
366
+ ],
367
+ },
368
+ });
369
+ ```
370
+
371
+ You can also customize the OpenAPI UI:
372
+
373
+ ```typescript
374
+ app.useOpenApi(OpenApiConfig, (config) => {
375
+ // Modify the OpenAPI configuration
376
+ config.info.title = "Custom API Title";
377
+ return config;
378
+ });
379
+ ```
380
+
381
+ ## Advanced Features
382
+
383
+ ### Database Integration
384
+
385
+ Connect to databases using TypeORM:
386
+
387
+ ```typescript
388
+ const app = Avleon.createApplication();
389
+ app.useDataSource({
390
+ type: "postgres",
391
+ host: "localhost",
392
+ port: 5432,
393
+ username: "postgres",
394
+ password: "password",
395
+ database: "avleon",
396
+ entities: [User],
397
+ synchronize: true,
398
+ });
399
+ ```
400
+
401
+ Or use the config class:
402
+
403
+ ```typescript
404
+ // datasource.config.ts
405
+ import { Config, IConfig } from "@avleon/core";
406
+
407
+ @Config
408
+ export class DataSourceConfig implements IConfig {
409
+ // config method is mendatory
410
+ // config method has access to environment variables by default
411
+ config(env: Environment) {
412
+ return {
413
+ type: env.get("type") || "postgres",
414
+ host: "localhost",
415
+ port: 5432,
416
+ username: "postgres",
417
+ password: "password",
418
+ database: "avleon",
419
+ entities: [User],
420
+ synchronize: true,
421
+ };
422
+ }
423
+ }
424
+ ```
425
+
426
+ ```typescript
427
+ // app.ts
428
+ const app = Avleon.createApplication();
429
+ app.useDataSource(DataSourceConfig);
430
+ // ... other impments
431
+ ```
432
+
433
+ ### File Uploads
434
+
435
+ Handle file uploads with multipart support:
436
+
437
+ ```typescript
438
+ // Configure multipart file uploads
439
+ app.useMultipart({
440
+ destination: path.join(process.cwd(), 'uploads'),
441
+ limits: {
442
+ fileSize: 5 * 1024 * 1024 // 5MB
443
+ }
444
+ });
445
+
446
+ // In your controller
447
+ @Post('/upload')
448
+ async uploadFile(@File() file: any) {
449
+ // Process uploaded file
450
+ return HttpResponse.Ok({ filename: file.filename });
451
+ }
452
+ ```
453
+
454
+ ### Static Files
455
+
456
+ Serve static files:
457
+
458
+ ```typescript
459
+ app.useStaticFiles({
460
+ path: path.join(process.cwd(), "public"),
461
+ prefix: "/static/",
462
+ });
463
+ ```
464
+
465
+ ### Testing
466
+
467
+ Test your API endpoints with the built-in testing utilities:
468
+
469
+ ```typescript
470
+ import { TestBuilder } from "@avleon/core";
471
+
472
+ const testBuilder = TestBuilder.createBuilder();
473
+ const app = testBuilder.getTestApplication({
474
+ controllers: [UserController],
475
+ });
476
+
477
+ // Test your API endpoints
478
+ const response = await app.get("/users");
479
+ expect(response.statusCode).toBe(200);
480
+ ```
481
+
482
+ ## Configuration
483
+
484
+ Configure your application with environment variables:
485
+
486
+ ```typescript
487
+ // .env
488
+ PORT=3000
489
+ DATABASE_URL=postgres://user:password@localhost:5432/db
490
+
491
+ // app.ts
492
+ import { Environment } from '@avleon/core';
493
+
494
+ const env = new Environment();
495
+ env.load();
496
+
497
+ const app = new Avleon({
498
+ controllers: [UserController],
499
+ env: {
500
+ port: 'PORT',
501
+ databaseUrl: 'DATABASE_URL',
502
+ },
503
+ });
504
+ ```
505
+
506
+ ## Route Mapping
507
+
508
+ Avleon provides several methods for mapping routes in your application:
509
+
510
+ ### mapGet
511
+
512
+ The `mapGet` method is used to define GET routes in your application. It takes a path string and a handler function as parameters.
513
+
514
+ ```typescript
515
+ app.mapGet("/users", async (req, res) => {
516
+ // Handle GET request to /users
517
+ return { users: [] };
518
+ });
519
+ ```
520
+
521
+ ### mapPost
522
+
523
+ The `mapPost` method is used to define POST routes in your application. It takes a path string and a handler function as parameters.
524
+
525
+ ```typescript
526
+ app.mapPost("/users", async (req, res) => {
527
+ // Handle POST request to /users
528
+ const userData = req.body;
529
+ // Process user data
530
+ return { success: true };
531
+ });
532
+ ```
533
+
534
+ ### mapPut
535
+
536
+ The `mapPut` method is used to define PUT routes in your application. It takes a path string and a handler function as parameters.
537
+
538
+ ```typescript
539
+ app.mapPut("/users/:id", async (req, res) => {
540
+ // Handle PUT request to /users/:id
541
+ const userId = req.params.id;
542
+ const userData = req.body;
543
+ // Update user data
544
+ return { success: true };
545
+ });
546
+ ```
547
+
548
+ ### mapDelete
549
+
550
+ The `mapDelete` method is used to define DELETE routes in your application. It takes a path string and a handler function as parameters.
551
+
552
+ ```typescript
553
+ app.mapDelete("/users/:id", async (req, res) => {
554
+ // Handle DELETE request to /users/:id
555
+ const userId = req.params.id;
556
+ // Delete user
557
+ return { success: true };
558
+ });
559
+ ```
560
+
561
+ Each of these methods returns a route object that can be used to add middleware or Swagger documentation to the route.
562
+
563
+ ```typescript
564
+ app
565
+ .mapGet("/users", async (req, res) => {
566
+ // Handler function
567
+ })
568
+ .useMiddleware([AuthMiddleware])
569
+ .useSwagger({
570
+ summary: "Get all users",
571
+ description: "Retrieves a list of all users",
572
+ tags: ["users"],
573
+ response: {
574
+ 200: {
575
+ description: "Successful response",
576
+ content: {
577
+ "application/json": {
578
+ schema: {
579
+ type: "array",
580
+ items: {
581
+ type: "object",
582
+ properties: {
583
+ id: { type: "string" },
584
+ name: { type: "string" },
585
+ },
586
+ },
587
+ },
588
+ },
589
+ },
590
+ },
591
+ },
592
+ });
593
+ ```
594
+
595
+ ## License
596
+
597
+ ISC
598
+
599
+ ## Author
600
+
601
+ Tareq Hossain - [GitHub](https://github.com/xtareq)