@athenna/http 3.0.11 → 3.1.0

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 (151) hide show
  1. package/build/Commands/MakeControllerCommand.d.ts +15 -0
  2. package/build/Commands/MakeControllerCommand.js +42 -0
  3. package/build/Commands/MakeInterceptorCommand.d.ts +15 -0
  4. package/build/Commands/MakeInterceptorCommand.js +42 -0
  5. package/build/Commands/MakeMiddlewareCommand.d.ts +15 -0
  6. package/build/Commands/MakeMiddlewareCommand.js +42 -0
  7. package/build/Commands/MakeTerminatorCommand.d.ts +15 -0
  8. package/build/Commands/MakeTerminatorCommand.js +42 -0
  9. package/build/Commands/RouteListCommand.d.ts +19 -0
  10. package/build/Commands/RouteListCommand.js +62 -0
  11. package/build/Context/Request.d.ts +144 -0
  12. package/build/Context/Request.js +219 -0
  13. package/build/Context/Response.d.ts +67 -0
  14. package/build/Context/Response.js +102 -0
  15. package/build/Contracts/InterceptorContract.d.ts +15 -0
  16. package/build/Contracts/InterceptorContract.js +9 -0
  17. package/build/Contracts/MiddlewareContract.d.ts +15 -0
  18. package/build/Contracts/MiddlewareContract.js +9 -0
  19. package/build/Contracts/TerminatorContract.d.ts +15 -0
  20. package/build/Contracts/TerminatorContract.js +9 -0
  21. package/build/Decorators/Controller.d.ts +14 -0
  22. package/build/Decorators/Controller.js +28 -0
  23. package/build/Decorators/Interceptor.d.ts +14 -0
  24. package/build/Decorators/Interceptor.js +37 -0
  25. package/build/Decorators/Middleware.d.ts +14 -0
  26. package/build/Decorators/Middleware.js +37 -0
  27. package/build/Decorators/Terminator.d.ts +14 -0
  28. package/build/Decorators/Terminator.js +37 -0
  29. package/build/Exceptions/BadGatewayException.d.ts +20 -0
  30. package/build/Exceptions/BadGatewayException.js +22 -0
  31. package/build/Exceptions/BadRequestException.d.ts +20 -0
  32. package/build/Exceptions/BadRequestException.js +22 -0
  33. package/build/Exceptions/ForbiddenException.d.ts +20 -0
  34. package/build/Exceptions/ForbiddenException.js +22 -0
  35. package/build/Exceptions/HttpException.d.ts +20 -0
  36. package/build/Exceptions/HttpException.js +27 -0
  37. package/build/Exceptions/InternalServerException.d.ts +20 -0
  38. package/build/Exceptions/InternalServerException.js +22 -0
  39. package/build/Exceptions/MethodNotAllowedException.d.ts +20 -0
  40. package/build/Exceptions/MethodNotAllowedException.js +22 -0
  41. package/build/Exceptions/NotAcceptableException.d.ts +20 -0
  42. package/build/Exceptions/NotAcceptableException.js +22 -0
  43. package/build/Exceptions/NotFoundException.d.ts +20 -0
  44. package/build/Exceptions/NotFoundException.js +22 -0
  45. package/build/Exceptions/NotImplementedException.d.ts +20 -0
  46. package/build/Exceptions/NotImplementedException.js +22 -0
  47. package/build/Exceptions/PayloadTooLargeException.d.ts +20 -0
  48. package/build/Exceptions/PayloadTooLargeException.js +22 -0
  49. package/build/Exceptions/RequestTimeoutException.d.ts +20 -0
  50. package/build/Exceptions/RequestTimeoutException.js +22 -0
  51. package/build/Exceptions/ServiceUnavailableException.d.ts +20 -0
  52. package/build/Exceptions/ServiceUnavailableException.js +22 -0
  53. package/build/Exceptions/UnauthorizedException.d.ts +20 -0
  54. package/build/Exceptions/UnauthorizedException.js +22 -0
  55. package/build/Exceptions/UndefinedMethodException.d.ts +12 -0
  56. package/build/Exceptions/UndefinedMethodException.js +19 -0
  57. package/build/Exceptions/UnprocessableEntityException.d.ts +20 -0
  58. package/build/Exceptions/UnprocessableEntityException.js +22 -0
  59. package/build/Facades/Route.d.ts +10 -0
  60. package/{src → build}/Facades/Route.js +2 -7
  61. package/build/Facades/Server.d.ts +10 -0
  62. package/{src → build}/Facades/Server.js +2 -7
  63. package/build/Handlers/FastifyHandler.d.ts +41 -0
  64. package/build/Handlers/FastifyHandler.js +116 -0
  65. package/build/Handlers/HttpExceptionHandler.d.ts +31 -0
  66. package/build/Handlers/HttpExceptionHandler.js +82 -0
  67. package/build/Kernels/HttpKernel.d.ts +73 -0
  68. package/build/Kernels/HttpKernel.js +221 -0
  69. package/build/Providers/HttpRouteProvider.d.ts +12 -0
  70. package/build/Providers/HttpRouteProvider.js +15 -0
  71. package/build/Providers/HttpServerProvider.d.ts +13 -0
  72. package/build/Providers/HttpServerProvider.js +25 -0
  73. package/build/Router/Route.d.ts +263 -0
  74. package/build/Router/Route.js +447 -0
  75. package/build/Router/RouteGroup.d.ts +96 -0
  76. package/build/Router/RouteGroup.js +123 -0
  77. package/build/Router/RouteResource.d.ts +108 -0
  78. package/build/Router/RouteResource.js +158 -0
  79. package/build/Router/Router.d.ts +103 -0
  80. package/build/Router/Router.js +188 -0
  81. package/build/Server/ServerImpl.d.ts +129 -0
  82. package/build/Server/ServerImpl.js +213 -0
  83. package/build/Types/Contexts/Context.d.ts +20 -0
  84. package/build/Types/Contexts/Context.js +9 -0
  85. package/build/Types/Contexts/ErrorContext.d.ts +21 -0
  86. package/build/Types/Contexts/ErrorContext.js +9 -0
  87. package/build/Types/Contexts/InterceptContext.d.ts +20 -0
  88. package/build/Types/Contexts/InterceptContext.js +9 -0
  89. package/build/Types/Contexts/TerminateContext.d.ts +21 -0
  90. package/build/Types/Contexts/TerminateContext.js +9 -0
  91. package/build/Types/Controllers/ControllerOptions.d.ts +25 -0
  92. package/build/Types/Controllers/ControllerOptions.js +9 -0
  93. package/build/Types/Middlewares/InterceptorRouteType.d.ts +11 -0
  94. package/build/Types/Middlewares/InterceptorRouteType.js +9 -0
  95. package/build/Types/Middlewares/MiddlewareHandler.d.ts +20 -0
  96. package/build/Types/Middlewares/MiddlewareHandler.js +9 -0
  97. package/build/Types/Middlewares/MiddlewareOptions.d.ts +40 -0
  98. package/build/Types/Middlewares/MiddlewareOptions.js +9 -0
  99. package/build/Types/Middlewares/MiddlewareRouteType.d.ts +11 -0
  100. package/build/Types/Middlewares/MiddlewareRouteType.js +9 -0
  101. package/build/Types/Middlewares/TerminatorRouteType.d.ts +11 -0
  102. package/build/Types/Middlewares/TerminatorRouteType.js +9 -0
  103. package/build/Types/Router/RouteHandler.d.ts +10 -0
  104. package/build/Types/Router/RouteHandler.js +9 -0
  105. package/build/Types/Router/RouteJSON.d.ts +21 -0
  106. package/build/Types/Router/RouteJSON.js +9 -0
  107. package/build/Types/Router/RouteResourceTypes.d.ts +9 -0
  108. package/build/Types/Router/RouteResourceTypes.js +9 -0
  109. package/build/index.d.ts +70 -0
  110. package/build/index.js +48 -0
  111. package/package.json +159 -77
  112. package/templates/controller.edge +8 -36
  113. package/templates/interceptor.edge +8 -0
  114. package/templates/middleware.edge +4 -36
  115. package/templates/terminator.edge +6 -0
  116. package/src/Commands/Make/Controller.js +0 -64
  117. package/src/Commands/Make/Middleware.js +0 -80
  118. package/src/Commands/Route/List.js +0 -107
  119. package/src/Context/Request.js +0 -268
  120. package/src/Context/Response.js +0 -173
  121. package/src/Exceptions/BadGatewayException.js +0 -31
  122. package/src/Exceptions/BadRequestException.js +0 -31
  123. package/src/Exceptions/ForbiddenException.js +0 -31
  124. package/src/Exceptions/HttpException.js +0 -33
  125. package/src/Exceptions/InternalServerException.js +0 -31
  126. package/src/Exceptions/MethodNotAllowedException.js +0 -31
  127. package/src/Exceptions/NotAcceptableException.js +0 -31
  128. package/src/Exceptions/NotFoundException.js +0 -31
  129. package/src/Exceptions/NotImplementedException.js +0 -31
  130. package/src/Exceptions/PayloadTooLargeException.js +0 -31
  131. package/src/Exceptions/RequestTimeoutException.js +0 -31
  132. package/src/Exceptions/ServiceUnavailableException.js +0 -31
  133. package/src/Exceptions/UnauthorizedException.js +0 -31
  134. package/src/Exceptions/UndefinedMethodException.js +0 -30
  135. package/src/Exceptions/UnprocessableEntityException.js +0 -31
  136. package/src/Handlers/FastifyHandler.js +0 -148
  137. package/src/Handlers/HttpExceptionHandler.js +0 -88
  138. package/src/Helpers/HttpLoader.js +0 -38
  139. package/src/Kernels/HttpKernel.js +0 -199
  140. package/src/Providers/ControllerProvider.js +0 -29
  141. package/src/Providers/HttpRouteProvider.js +0 -24
  142. package/src/Providers/HttpServerProvider.js +0 -37
  143. package/src/Providers/MiddlewareProvider.js +0 -29
  144. package/src/Router/Route.js +0 -411
  145. package/src/Router/RouteGroup.js +0 -119
  146. package/src/Router/RouteResource.js +0 -247
  147. package/src/Router/Router.js +0 -313
  148. package/src/Utils/isMiddlewareContract.js +0 -17
  149. package/src/Utils/removeSlashes.js +0 -30
  150. package/src/index.d.ts +0 -1395
  151. package/src/index.js +0 -377
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { BaseCommand } from '@athenna/artisan';
10
+ export declare class MakeControllerCommand extends BaseCommand {
11
+ name: string;
12
+ static signature(): string;
13
+ static description(): string;
14
+ handle(): Promise<void>;
15
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ import { Path } from '@athenna/common';
16
+ import { BaseCommand, Argument } from '@athenna/artisan';
17
+ export class MakeControllerCommand extends BaseCommand {
18
+ name;
19
+ static signature() {
20
+ return 'make:controller';
21
+ }
22
+ static description() {
23
+ return 'Make a new controller file.';
24
+ }
25
+ async handle() {
26
+ this.logger.simple('({bold,green} [ MAKING CONTROLLER ])\n');
27
+ const file = await this.generator
28
+ .path(Path.http(`Controllers/${this.name}.${Path.ext()}`))
29
+ .template('artisan::controller')
30
+ .setNameProperties(true)
31
+ .make();
32
+ this.logger.success(`Controller ({yellow} "${file.name}") successfully created.`);
33
+ const importPath = `#app/Http/Controllers/${file.name}`;
34
+ await this.rc.pushTo('controllers', importPath).save();
35
+ this.logger.success(`Athenna RC updated: ({dim,yellow} [ controllers += "${importPath}" ])`);
36
+ }
37
+ }
38
+ __decorate([
39
+ Argument({
40
+ description: 'The controller name.',
41
+ })
42
+ ], MakeControllerCommand.prototype, "name", void 0);
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { BaseCommand } from '@athenna/artisan';
10
+ export declare class MakeInterceptorCommand extends BaseCommand {
11
+ name: string;
12
+ static signature(): string;
13
+ static description(): string;
14
+ handle(): Promise<void>;
15
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ import { Path } from '@athenna/common';
16
+ import { BaseCommand, Argument } from '@athenna/artisan';
17
+ export class MakeInterceptorCommand extends BaseCommand {
18
+ name;
19
+ static signature() {
20
+ return 'make:interceptor';
21
+ }
22
+ static description() {
23
+ return 'Make a new interceptor file.';
24
+ }
25
+ async handle() {
26
+ this.logger.simple('({bold,green} [ MAKING INTERCEPTOR ])\n');
27
+ const file = await this.generator
28
+ .path(Path.http(`Interceptors/${this.name}.${Path.ext()}`))
29
+ .template('artisan::interceptor')
30
+ .setNameProperties(true)
31
+ .make();
32
+ this.logger.success(`Interceptor ({yellow} "${file.name}") successfully created.`);
33
+ const importPath = `#app/Http/Interceptors/${file.name}`;
34
+ await this.rc.pushTo('middlewares', importPath).save();
35
+ this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
+ }
37
+ }
38
+ __decorate([
39
+ Argument({
40
+ description: 'The interceptor name.',
41
+ })
42
+ ], MakeInterceptorCommand.prototype, "name", void 0);
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { BaseCommand } from '@athenna/artisan';
10
+ export declare class MakeMiddlewareCommand extends BaseCommand {
11
+ name: string;
12
+ static signature(): string;
13
+ static description(): string;
14
+ handle(): Promise<void>;
15
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ import { Path } from '@athenna/common';
16
+ import { BaseCommand, Argument } from '@athenna/artisan';
17
+ export class MakeMiddlewareCommand extends BaseCommand {
18
+ name;
19
+ static signature() {
20
+ return 'make:middleware';
21
+ }
22
+ static description() {
23
+ return 'Make a new middleware file.';
24
+ }
25
+ async handle() {
26
+ this.logger.simple('({bold,green} [ MAKING MIDDLEWARE ])\n');
27
+ const file = await this.generator
28
+ .path(Path.http(`Middlewares/${this.name}.${Path.ext()}`))
29
+ .template('artisan::middleware')
30
+ .setNameProperties(true)
31
+ .make();
32
+ this.logger.success(`Middleware ({yellow} "${file.name}") successfully created.`);
33
+ const importPath = `#app/Http/Middlewares/${file.name}`;
34
+ await this.rc.pushTo('middlewares', importPath).save();
35
+ this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
+ }
37
+ }
38
+ __decorate([
39
+ Argument({
40
+ description: 'The middleware name.',
41
+ })
42
+ ], MakeMiddlewareCommand.prototype, "name", void 0);
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { BaseCommand } from '@athenna/artisan';
10
+ export declare class MakeTerminatorCommand extends BaseCommand {
11
+ name: string;
12
+ static signature(): string;
13
+ static description(): string;
14
+ handle(): Promise<void>;
15
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ import { Path } from '@athenna/common';
16
+ import { BaseCommand, Argument } from '@athenna/artisan';
17
+ export class MakeTerminatorCommand extends BaseCommand {
18
+ name;
19
+ static signature() {
20
+ return 'make:terminator';
21
+ }
22
+ static description() {
23
+ return 'Make a new terminator file.';
24
+ }
25
+ async handle() {
26
+ this.logger.simple('({bold,green} [ MAKING TERMINATOR ])\n');
27
+ const file = await this.generator
28
+ .path(Path.http(`Terminators/${this.name}.${Path.ext()}`))
29
+ .template('artisan::terminator')
30
+ .setNameProperties(true)
31
+ .make();
32
+ this.logger.success(`Terminator ({yellow} "${file.name}") successfully created.`);
33
+ const importPath = `#app/Http/Terminators/${file.name}`;
34
+ await this.rc.pushTo('middlewares', importPath).save();
35
+ this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
+ }
37
+ }
38
+ __decorate([
39
+ Argument({
40
+ description: 'The terminator name.',
41
+ })
42
+ ], MakeTerminatorCommand.prototype, "name", void 0);
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { BaseCommand } from '@athenna/artisan';
10
+ export declare class RouteListCommand extends BaseCommand {
11
+ routeFilePath: any;
12
+ static signature(): string;
13
+ static description(): string;
14
+ handle(): Promise<void>;
15
+ /**
16
+ * Resolve the import path by meta URL and import it.
17
+ */
18
+ private resolveRoutePathAndImport;
19
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { resolve } from 'node:path';
10
+ import { pathToFileURL } from 'node:url';
11
+ import { Config } from '@athenna/config';
12
+ import { Module } from '@athenna/common';
13
+ import { BaseCommand } from '@athenna/artisan';
14
+ import { Route, HttpKernel, HttpRouteProvider, HttpServerProvider } from '#src';
15
+ export class RouteListCommand extends BaseCommand {
16
+ routeFilePath = Env('HTTP_ROUTE_FILE_PATH', Path.routes('http.js'));
17
+ static signature() {
18
+ return 'route:list';
19
+ }
20
+ static description() {
21
+ return 'List all the http routes registered in your application.';
22
+ }
23
+ async handle() {
24
+ this.logger.simple('({bold,green} [ LISTING ROUTES ])\n');
25
+ new HttpServerProvider().register();
26
+ new HttpRouteProvider().register();
27
+ const kernel = new HttpKernel();
28
+ await kernel.registerControllers();
29
+ await kernel.registerMiddlewares();
30
+ await this.resolveRoutePathAndImport();
31
+ const routes = Route.list();
32
+ const table = this.logger.table();
33
+ table.head('Methods', 'Route', 'Name', 'Handler');
34
+ routes.forEach(route => {
35
+ if (route.deleted) {
36
+ return;
37
+ }
38
+ table.row([
39
+ route.methods.join('|'),
40
+ route.url,
41
+ route.name || 'Not found',
42
+ route.handler.name || 'closure',
43
+ ]);
44
+ });
45
+ table.render();
46
+ }
47
+ /**
48
+ * Resolve the import path by meta URL and import it.
49
+ */
50
+ resolveRoutePathAndImport() {
51
+ if (this.routeFilePath.includes('./') ||
52
+ this.routeFilePath.includes('../')) {
53
+ this.routeFilePath = resolve(this.routeFilePath);
54
+ }
55
+ if (!this.routeFilePath.startsWith('#')) {
56
+ this.routeFilePath = pathToFileURL(this.routeFilePath).href;
57
+ }
58
+ return import.meta
59
+ .resolve(this.routeFilePath, Config.get('rc.meta'))
60
+ .then(meta => Module.get(import(meta)));
61
+ }
62
+ }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { FastifyRequest } from 'fastify';
10
+ export declare class Request {
11
+ /**
12
+ * The fastify request object.
13
+ */
14
+ request: FastifyRequest;
15
+ constructor(request: FastifyRequest);
16
+ /**
17
+ * Get the request id.
18
+ *
19
+ * @example 12345
20
+ */
21
+ get id(): string;
22
+ /**
23
+ * Get the request ip.
24
+ *
25
+ * @example 192.168.0.1
26
+ */
27
+ get ip(): string;
28
+ /**
29
+ * Get the request hostname.
30
+ *
31
+ * @example localhost
32
+ */
33
+ get hostname(): string;
34
+ /**
35
+ * Get the request protocol.
36
+ *
37
+ * @example http
38
+ */
39
+ get protocol(): 'http' | 'https';
40
+ /**
41
+ * Get the request method.
42
+ *
43
+ * @example GET
44
+ */
45
+ get method(): string;
46
+ /**
47
+ * Get the base url from request.
48
+ *
49
+ * @example /users/1
50
+ */
51
+ get baseUrl(): string;
52
+ /**
53
+ * Get the base url with host and port info from request.
54
+ *
55
+ * @example http://localhost:3030/users/1
56
+ */
57
+ get baseHostUrl(): string;
58
+ /**
59
+ * Get the route url from request.
60
+ *
61
+ * @example /users/:id
62
+ */
63
+ get routeUrl(): string;
64
+ /**
65
+ * Get the route url with host and port info from request.
66
+ *
67
+ * @example http://localhost:3030/users/:id
68
+ */
69
+ get routeHostUrl(): string;
70
+ /**
71
+ * Get the original url from request.
72
+ *
73
+ * @example /users/1?query=true
74
+ */
75
+ get originalUrl(): string;
76
+ /**
77
+ * Get the original url with host and port info from request.
78
+ *
79
+ * @example /users/1?query=true
80
+ */
81
+ get originalHostUrl(): string;
82
+ /**
83
+ * Get all body from request.
84
+ */
85
+ get body(): any | any[];
86
+ /**
87
+ * Get all params from request.
88
+ */
89
+ get params(): any;
90
+ /**
91
+ * Get all queries from request.
92
+ */
93
+ get queries(): any;
94
+ /**
95
+ * Get all headers from request.
96
+ */
97
+ get headers(): any;
98
+ /**
99
+ * Get the server port.
100
+ */
101
+ get port(): number;
102
+ /**
103
+ * Get the server version.
104
+ */
105
+ get version(): string;
106
+ /**
107
+ * Get a value from the request params or the default value.
108
+ */
109
+ param(param: string, defaultValue?: any): any;
110
+ /**
111
+ * Get a value from the request query param or the default value.
112
+ */
113
+ query(query: string, defaultValue?: any): any;
114
+ /**
115
+ * Get a value from the request header or the default value.
116
+ */
117
+ header(header: string, defaultValue?: any): any;
118
+ /**
119
+ * Get only the selected values from the request body.
120
+ */
121
+ only(keys: string[]): any;
122
+ /**
123
+ * Get all the values from the request body except the selected ones.
124
+ */
125
+ except(keys: string[]): any;
126
+ /**
127
+ * Get a value from the request body or the default value.
128
+ */
129
+ input(key: string, defaultValue?: any): any;
130
+ /**
131
+ * Get a value from the request body or the default value.
132
+ */
133
+ payload(key: string, defaultValue?: any): any;
134
+ /**
135
+ * Add the hostname and port to the url.
136
+ */
137
+ private getHostUrlFor;
138
+ /**
139
+ * Get the address info of the server. This method will return the
140
+ * port used to listen the server, the family (IPv4, IPv6) and the
141
+ * server address (127.0.0.1).
142
+ */
143
+ private getAddressInfo;
144
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Is, Json } from '@athenna/common';
10
+ export class Request {
11
+ /**
12
+ * The fastify request object.
13
+ */
14
+ request;
15
+ constructor(request) {
16
+ this.request = request;
17
+ }
18
+ /**
19
+ * Get the request id.
20
+ *
21
+ * @example 12345
22
+ */
23
+ get id() {
24
+ return this.request.id;
25
+ }
26
+ /**
27
+ * Get the request ip.
28
+ *
29
+ * @example 192.168.0.1
30
+ */
31
+ get ip() {
32
+ return this.request.ip;
33
+ }
34
+ /**
35
+ * Get the request hostname.
36
+ *
37
+ * @example localhost
38
+ */
39
+ get hostname() {
40
+ return this.request.hostname;
41
+ }
42
+ /**
43
+ * Get the request protocol.
44
+ *
45
+ * @example http
46
+ */
47
+ get protocol() {
48
+ return this.request.protocol;
49
+ }
50
+ /**
51
+ * Get the request method.
52
+ *
53
+ * @example GET
54
+ */
55
+ get method() {
56
+ return this.request.method;
57
+ }
58
+ /**
59
+ * Get the base url from request.
60
+ *
61
+ * @example /users/1
62
+ */
63
+ get baseUrl() {
64
+ return this.request.url.split('?')[0];
65
+ }
66
+ /**
67
+ * Get the base url with host and port info from request.
68
+ *
69
+ * @example http://localhost:3030/users/1
70
+ */
71
+ get baseHostUrl() {
72
+ return this.getHostUrlFor(this.baseUrl);
73
+ }
74
+ /**
75
+ * Get the route url from request.
76
+ *
77
+ * @example /users/:id
78
+ */
79
+ get routeUrl() {
80
+ return this.request.routerPath;
81
+ }
82
+ /**
83
+ * Get the route url with host and port info from request.
84
+ *
85
+ * @example http://localhost:3030/users/:id
86
+ */
87
+ get routeHostUrl() {
88
+ return this.getHostUrlFor(this.routeUrl);
89
+ }
90
+ /**
91
+ * Get the original url from request.
92
+ *
93
+ * @example /users/1?query=true
94
+ */
95
+ get originalUrl() {
96
+ return this.request.url;
97
+ }
98
+ /**
99
+ * Get the original url with host and port info from request.
100
+ *
101
+ * @example /users/1?query=true
102
+ */
103
+ get originalHostUrl() {
104
+ return this.getHostUrlFor(this.originalUrl);
105
+ }
106
+ /**
107
+ * Get all body from request.
108
+ */
109
+ get body() {
110
+ return this.request.body || {};
111
+ }
112
+ /**
113
+ * Get all params from request.
114
+ */
115
+ get params() {
116
+ return this.request.params || {};
117
+ }
118
+ /**
119
+ * Get all queries from request.
120
+ */
121
+ get queries() {
122
+ return this.request.query || {};
123
+ }
124
+ /**
125
+ * Get all headers from request.
126
+ */
127
+ get headers() {
128
+ return this.request.headers || {};
129
+ }
130
+ /**
131
+ * Get the server port.
132
+ */
133
+ get port() {
134
+ return this.getAddressInfo().port;
135
+ }
136
+ /**
137
+ * Get the server version.
138
+ */
139
+ get version() {
140
+ return this.request.server.version;
141
+ }
142
+ /**
143
+ * Get a value from the request params or the default value.
144
+ */
145
+ param(param, defaultValue) {
146
+ return this.params[param] || defaultValue;
147
+ }
148
+ /**
149
+ * Get a value from the request query param or the default value.
150
+ */
151
+ query(query, defaultValue) {
152
+ return this.queries[query] || defaultValue;
153
+ }
154
+ /**
155
+ * Get a value from the request header or the default value.
156
+ */
157
+ header(header, defaultValue) {
158
+ return this.headers[header] || defaultValue;
159
+ }
160
+ /**
161
+ * Get only the selected values from the request body.
162
+ */
163
+ only(keys) {
164
+ const body = {};
165
+ Object.keys(this.body).forEach(key => {
166
+ if (!keys.includes(key)) {
167
+ return;
168
+ }
169
+ body[key] = this.body[key];
170
+ });
171
+ return body;
172
+ }
173
+ /**
174
+ * Get all the values from the request body except the selected ones.
175
+ */
176
+ except(keys) {
177
+ const body = {};
178
+ Object.keys(this.body).forEach(key => {
179
+ if (keys.includes(key)) {
180
+ return;
181
+ }
182
+ body[key] = this.body[key];
183
+ });
184
+ return body;
185
+ }
186
+ /**
187
+ * Get a value from the request body or the default value.
188
+ */
189
+ input(key, defaultValue) {
190
+ return this.payload(key, defaultValue);
191
+ }
192
+ /**
193
+ * Get a value from the request body or the default value.
194
+ */
195
+ payload(key, defaultValue) {
196
+ return Json.get(this.body, key, defaultValue);
197
+ }
198
+ /**
199
+ * Add the hostname and port to the url.
200
+ */
201
+ getHostUrlFor(url) {
202
+ let { address, port } = this.getAddressInfo();
203
+ if (address === '::1') {
204
+ address = '127.0.0.1';
205
+ }
206
+ if (!Is.Ip(address) && address !== 'localhost') {
207
+ return `${this.protocol}://${address}${url}`;
208
+ }
209
+ return `${this.protocol}://${address}:${port}${url}`;
210
+ }
211
+ /**
212
+ * Get the address info of the server. This method will return the
213
+ * port used to listen the server, the family (IPv4, IPv6) and the
214
+ * server address (127.0.0.1).
215
+ */
216
+ getAddressInfo() {
217
+ return this.request.server.server.address();
218
+ }
219
+ }