@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,31 @@
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 { ErrorContext } from '#src';
10
+ export declare class HttpExceptionHandler {
11
+ /**
12
+ * Error codes that should be ignored from logging.
13
+ */
14
+ get ignoreCodes(): string[];
15
+ /**
16
+ * Error statuses that should be ignored from logging.
17
+ */
18
+ get ignoreStatuses(): number[];
19
+ /**
20
+ * The exception handler of all request handlers.
21
+ */
22
+ handle({ error, response }: ErrorContext): Promise<void>;
23
+ /**
24
+ * Return a boolean indicating if the error can be logged or not.
25
+ */
26
+ private canBeLogged;
27
+ /**
28
+ * Returns a boolean indicating if the error is an internal server error.
29
+ */
30
+ private isInternalServerError;
31
+ }
@@ -0,0 +1,82 @@
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 { Log } from '@athenna/logger';
10
+ import { String } from '@athenna/common';
11
+ export class HttpExceptionHandler {
12
+ /**
13
+ * Error codes that should be ignored from logging.
14
+ */
15
+ get ignoreCodes() {
16
+ return [];
17
+ }
18
+ /**
19
+ * Error statuses that should be ignored from logging.
20
+ */
21
+ get ignoreStatuses() {
22
+ return [];
23
+ }
24
+ /**
25
+ * The exception handler of all request handlers.
26
+ */
27
+ async handle({ error, response }) {
28
+ const body = {
29
+ statusCode: error.statusCode || error.status || 500,
30
+ code: String.toSnakeCase(error.code || error.name).toUpperCase(),
31
+ name: error.name,
32
+ message: error.message,
33
+ stack: error.stack,
34
+ };
35
+ if (error.help) {
36
+ body.help = error.help;
37
+ }
38
+ const isInternalServerError = this.isInternalServerError(error);
39
+ const isDebugMode = Config.is('app.debug', true);
40
+ if (isInternalServerError && !isDebugMode) {
41
+ body.code = 'E_INTERNAL_SERVER';
42
+ body.name = 'InternalServerException';
43
+ body.message = 'An internal server exception has occurred.';
44
+ delete body.stack;
45
+ }
46
+ response.status(body.statusCode).send(body);
47
+ if (!this.canBeLogged(error)) {
48
+ return;
49
+ }
50
+ if (!error.prettify) {
51
+ error = error.toAthennaException();
52
+ }
53
+ Log.channelOrVanilla('exception').error((await error.prettify()).concat('\n'));
54
+ }
55
+ /**
56
+ * Return a boolean indicating if the error can be logged or not.
57
+ */
58
+ canBeLogged(error) {
59
+ if (this.ignoreCodes.includes(error.code)) {
60
+ return false;
61
+ }
62
+ if (this.ignoreStatuses.includes(error.status || error.statusCode)) {
63
+ return false;
64
+ }
65
+ return true;
66
+ }
67
+ /**
68
+ * Returns a boolean indicating if the error is an internal server error.
69
+ */
70
+ isInternalServerError(error) {
71
+ return [
72
+ 'Error',
73
+ 'URIError',
74
+ 'TypeError',
75
+ 'EvalError',
76
+ 'RangeError',
77
+ 'SyntaxError',
78
+ 'InternalError',
79
+ 'ReferenceError',
80
+ ].includes(error.name);
81
+ }
82
+ }
@@ -0,0 +1,73 @@
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 'reflect-metadata';
10
+ export declare class HttpKernel {
11
+ /**
12
+ * Register the @fastify/cors plugin in the Http server.
13
+ */
14
+ registerCors(): Promise<void>;
15
+ /**
16
+ * Register the @fastify/helmet plugin in the Http server.
17
+ */
18
+ registerHelmet(): Promise<void>;
19
+ /**
20
+ * Register the @fastify/swagger plugin in the Http server.
21
+ */
22
+ registerSwagger(): Promise<void>;
23
+ /**
24
+ * Register the @fastify/rate-limit plugin in the Http server.
25
+ */
26
+ registerRateLimit(): Promise<void>;
27
+ /**
28
+ * Register the cls-rtracer plugin in the Http server.
29
+ */
30
+ registerRTracer(): Promise<void>;
31
+ /**
32
+ * Register the global log terminator in the Http server.
33
+ */
34
+ registerLoggerTerminator(): Promise<void>;
35
+ /**
36
+ * Register all the controllers found inside "rc.controllers" config
37
+ * inside the service provider.
38
+ */
39
+ registerControllers(): Promise<void>;
40
+ /**
41
+ * Register all the middlewares found inside "rc.middlewares" config
42
+ * inside the service provider. Also register if "rc.namedMiddlewares"
43
+ * and "rc.globalMiddlewares" exists.
44
+ */
45
+ registerMiddlewares(): Promise<void>;
46
+ /**
47
+ * Register all the named middlewares found inside "rc.namedMiddlewares"
48
+ * property.
49
+ */
50
+ registerNamedMiddlewares(): Promise<void>;
51
+ /**
52
+ * Register all the named middlewares found inside "rc.globalMiddlewares"
53
+ * property.
54
+ */
55
+ registerGlobalMiddlewares(): Promise<void>;
56
+ /**
57
+ * Register the exception handler for all request handlers.
58
+ */
59
+ registerExceptionHandler(path?: string): Promise<void>;
60
+ /**
61
+ * Fabricate the named middlewares aliases.
62
+ */
63
+ private getNamedMiddlewareAlias;
64
+ /**
65
+ * Fabricate the global middlewares alias and resolve the handler and
66
+ * server methods.
67
+ */
68
+ private getGlobalMiddlewareAliasAndHandler;
69
+ /**
70
+ * Resolve the import path by meta URL and import it.
71
+ */
72
+ private resolvePathAndImport;
73
+ }
@@ -0,0 +1,221 @@
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 'reflect-metadata';
10
+ import { Server } from '#src';
11
+ import { resolve } from 'node:path';
12
+ import { Log } from '@athenna/logger';
13
+ import { pathToFileURL } from 'node:url';
14
+ import { Config } from '@athenna/config';
15
+ import { Is, Module } from '@athenna/common';
16
+ import { HttpExceptionHandler } from '#src/Handlers/HttpExceptionHandler';
17
+ export class HttpKernel {
18
+ /**
19
+ * Register the @fastify/cors plugin in the Http server.
20
+ */
21
+ async registerCors() {
22
+ if (!Config.exists('http.cors')) {
23
+ return;
24
+ }
25
+ await Server.plugin(import('@fastify/cors'), Config.get('http.cors'));
26
+ }
27
+ /**
28
+ * Register the @fastify/helmet plugin in the Http server.
29
+ */
30
+ async registerHelmet() {
31
+ if (!Config.exists('http.helmet')) {
32
+ return;
33
+ }
34
+ await Server.plugin(import('@fastify/helmet'), Config.get('http.helmet'));
35
+ }
36
+ /**
37
+ * Register the @fastify/swagger plugin in the Http server.
38
+ */
39
+ async registerSwagger() {
40
+ if (!Config.exists('http.swagger')) {
41
+ return;
42
+ }
43
+ await Server.plugin(import('@fastify/swagger'), Config.get('http.swagger.configurations'));
44
+ await Server.plugin(import('@fastify/swagger-ui'), Config.get('http.swagger.ui'));
45
+ }
46
+ /**
47
+ * Register the @fastify/rate-limit plugin in the Http server.
48
+ */
49
+ async registerRateLimit() {
50
+ if (!Config.exists('http.rateLimit')) {
51
+ return;
52
+ }
53
+ await Server.plugin(import('@fastify/rate-limit'), Config.get('http.rateLimit'));
54
+ }
55
+ /**
56
+ * Register the cls-rtracer plugin in the Http server.
57
+ */
58
+ async registerRTracer() {
59
+ if (!Config.exists('http.rTracer')) {
60
+ return;
61
+ }
62
+ const rTracer = await import('cls-rtracer');
63
+ Server.middleware(async (ctx) => (ctx.data.traceId = rTracer.id()));
64
+ await Server.plugin(rTracer.fastifyPlugin, Config.get('http.rTracer'));
65
+ }
66
+ /**
67
+ * Register the global log terminator in the Http server.
68
+ */
69
+ async registerLoggerTerminator() {
70
+ if (!Config.exists('http.logger') || Config.is('http.logger', false)) {
71
+ return;
72
+ }
73
+ Server.terminate(ctx => Log.channelOrVanilla('request').info(ctx));
74
+ }
75
+ /**
76
+ * Register all the controllers found inside "rc.controllers" config
77
+ * inside the service provider.
78
+ */
79
+ async registerControllers() {
80
+ const controllers = Config.get('rc.controllers', []);
81
+ const promises = controllers.map(path => this.resolvePathAndImport(path).then(Controller => {
82
+ if (Reflect.hasMetadata('provider:registered', Controller)) {
83
+ return;
84
+ }
85
+ const createCamelAlias = false;
86
+ const alias = `App/Http/Controllers/${Controller.name}`;
87
+ ioc.bind(alias, Controller, createCamelAlias);
88
+ }));
89
+ await Promise.all(promises);
90
+ }
91
+ /**
92
+ * Register all the middlewares found inside "rc.middlewares" config
93
+ * inside the service provider. Also register if "rc.namedMiddlewares"
94
+ * and "rc.globalMiddlewares" exists.
95
+ */
96
+ async registerMiddlewares() {
97
+ await this.registerNamedMiddlewares();
98
+ await this.registerGlobalMiddlewares();
99
+ if (Config.exists('rc.middlewares')) {
100
+ await Promise.all(Config.get('rc.middlewares').map(this.resolvePathAndImport));
101
+ }
102
+ }
103
+ /**
104
+ * Register all the named middlewares found inside "rc.namedMiddlewares"
105
+ * property.
106
+ */
107
+ async registerNamedMiddlewares() {
108
+ const namedMiddlewares = Config.get('rc.namedMiddlewares');
109
+ if (Is.Empty(namedMiddlewares)) {
110
+ return;
111
+ }
112
+ const promises = Object.keys(namedMiddlewares).map(key => this.resolvePathAndImport(namedMiddlewares[key]).then(Middleware => {
113
+ if (Reflect.hasMetadata('provider:registered', Middleware)) {
114
+ return;
115
+ }
116
+ const createCamelAlias = false;
117
+ const { alias, namedAlias } = this.getNamedMiddlewareAlias(key, Middleware);
118
+ ioc.bind(alias, Middleware, createCamelAlias).alias(namedAlias, alias);
119
+ }));
120
+ await Promise.all(promises);
121
+ }
122
+ /**
123
+ * Register all the named middlewares found inside "rc.globalMiddlewares"
124
+ * property.
125
+ */
126
+ async registerGlobalMiddlewares() {
127
+ const globalMiddlewares = Config.get('rc.globalMiddlewares');
128
+ if (Is.Empty(globalMiddlewares)) {
129
+ return;
130
+ }
131
+ const promises = globalMiddlewares.map(path => this.resolvePathAndImport(path).then(Middleware => {
132
+ if (Reflect.hasMetadata('provider:registered', Middleware)) {
133
+ return;
134
+ }
135
+ const createCamelAlias = false;
136
+ const { alias, handler, serverMethod } = this.getGlobalMiddlewareAliasAndHandler(Middleware);
137
+ ioc.bind(alias, Middleware, createCamelAlias);
138
+ Server[serverMethod](ioc.safeUse(alias)[handler]);
139
+ }));
140
+ await Promise.all(promises);
141
+ }
142
+ /**
143
+ * Register the exception handler for all request handlers.
144
+ */
145
+ async registerExceptionHandler(path) {
146
+ if (!path) {
147
+ const handler = new HttpExceptionHandler();
148
+ Server.setErrorHandler(handler.handle.bind(handler));
149
+ return;
150
+ }
151
+ const Handler = await this.resolvePathAndImport(path);
152
+ const handler = new Handler();
153
+ Server.setErrorHandler(handler.handle.bind(handler));
154
+ }
155
+ /**
156
+ * Fabricate the named middlewares aliases.
157
+ */
158
+ getNamedMiddlewareAlias(name, Middleware) {
159
+ const middleware = new Middleware();
160
+ if (middleware.handle) {
161
+ return {
162
+ alias: `App/Http/Middlewares/${Middleware.name}`,
163
+ namedAlias: `App/Http/Middlewares/Names/${name}`,
164
+ };
165
+ }
166
+ if (middleware.intercept) {
167
+ return {
168
+ alias: `App/Http/Interceptors/${Middleware.name}`,
169
+ namedAlias: `App/Http/Interceptors/Names/${name}`,
170
+ };
171
+ }
172
+ if (middleware.terminate) {
173
+ return {
174
+ alias: `App/Http/Terminators/${Middleware.name}`,
175
+ namedAlias: `App/Http/Terminators/Names/${name}`,
176
+ };
177
+ }
178
+ }
179
+ /**
180
+ * Fabricate the global middlewares alias and resolve the handler and
181
+ * server methods.
182
+ */
183
+ getGlobalMiddlewareAliasAndHandler(Middleware) {
184
+ const middleware = new Middleware();
185
+ if (middleware.handle) {
186
+ return {
187
+ handler: 'handle',
188
+ serverMethod: 'middleware',
189
+ alias: `App/Http/Middlewares/${Middleware.name}`,
190
+ };
191
+ }
192
+ if (middleware.intercept) {
193
+ return {
194
+ handler: 'intercept',
195
+ serverMethod: 'intercept',
196
+ alias: `App/Http/Interceptors/${Middleware.name}`,
197
+ };
198
+ }
199
+ if (middleware.terminate) {
200
+ return {
201
+ handler: 'terminate',
202
+ serverMethod: 'terminate',
203
+ alias: `App/Http/Terminators/${Middleware.name}`,
204
+ };
205
+ }
206
+ }
207
+ /**
208
+ * Resolve the import path by meta URL and import it.
209
+ */
210
+ resolvePathAndImport(path) {
211
+ if (path.includes('./') || path.includes('../')) {
212
+ path = resolve(path);
213
+ }
214
+ if (!path.startsWith('#')) {
215
+ path = pathToFileURL(path).href;
216
+ }
217
+ return import.meta
218
+ .resolve(path, Config.get('rc.meta'))
219
+ .then(meta => Module.get(import(`${meta}?version=${Math.random()}`)));
220
+ }
221
+ }
@@ -0,0 +1,12 @@
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 { ServiceProvider } from '@athenna/ioc';
10
+ export declare class HttpRouteProvider extends ServiceProvider {
11
+ register(): void;
12
+ }
@@ -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 { Router } from '#src/Router/Router';
10
+ import { ServiceProvider } from '@athenna/ioc';
11
+ export class HttpRouteProvider extends ServiceProvider {
12
+ register() {
13
+ this.container.instance('Athenna/Core/HttpRoute', new Router(), false);
14
+ }
15
+ }
@@ -0,0 +1,13 @@
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 { ServiceProvider } from '@athenna/ioc';
10
+ export declare class HttpServerProvider extends ServiceProvider {
11
+ register(): void;
12
+ shutdown(): Promise<void>;
13
+ }
@@ -0,0 +1,25 @@
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 { ServerImpl } from '#src/Server/ServerImpl';
10
+ import { ServiceProvider } from '@athenna/ioc';
11
+ export class HttpServerProvider extends ServiceProvider {
12
+ register() {
13
+ this.container.instance('Athenna/Core/HttpServer', new ServerImpl(), false);
14
+ }
15
+ async shutdown() {
16
+ const Server = this.container.use('Athenna/Core/HttpServer');
17
+ if (!Server) {
18
+ return;
19
+ }
20
+ if (!Server.isListening) {
21
+ return;
22
+ }
23
+ await Server.close();
24
+ }
25
+ }