@athenna/http 3.0.5 → 3.0.7
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.
- package/package.json +10 -9
- package/src/Commands/Route/List.js +1 -1
- package/src/Handlers/HttpExceptionHandler.js +13 -15
- package/src/Kernels/HttpKernel.js +27 -19
- package/src/Providers/ControllerProvider.js +1 -1
- package/src/index.d.ts +33 -18
- package/src/index.js +27 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/http",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.7",
|
|
4
4
|
"description": "The Athenna Http server. Built on top of fastify.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "João Lenon <lenon@athenna.io>",
|
|
@@ -54,24 +54,25 @@
|
|
|
54
54
|
"#tests/*": "./tests/*.js"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
+
"fastify": "4.9.2"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@athenna/artisan": "3.0.7",
|
|
61
|
+
"@athenna/common": "3.0.1",
|
|
62
|
+
"@athenna/config": "3.0.3",
|
|
63
|
+
"@athenna/ioc": "3.0.1",
|
|
64
|
+
"@athenna/logger": "3.0.6",
|
|
57
65
|
"@fastify/cors": "8.1.1",
|
|
58
66
|
"@fastify/helmet": "10.0.2",
|
|
59
67
|
"@fastify/rate-limit": "7.5.0",
|
|
60
68
|
"@fastify/swagger": "8.1.0",
|
|
61
69
|
"@fastify/swagger-ui": "1.1.0",
|
|
62
|
-
"fastify": "4.9.2"
|
|
63
|
-
},
|
|
64
|
-
"devDependencies": {
|
|
65
|
-
"@athenna/artisan": "3.0.5",
|
|
66
|
-
"@athenna/common": "3.0.0",
|
|
67
|
-
"@athenna/config": "3.0.2",
|
|
68
|
-
"@athenna/ioc": "3.0.0",
|
|
69
|
-
"@athenna/logger": "3.0.3",
|
|
70
70
|
"@japa/assert": "1.3.4",
|
|
71
71
|
"@japa/run-failed-tests": "1.0.7",
|
|
72
72
|
"@japa/runner": "2.0.7",
|
|
73
73
|
"@japa/spec-reporter": "1.1.12",
|
|
74
74
|
"c8": "7.11.2",
|
|
75
|
+
"cls-rtracer": "2.6.2",
|
|
75
76
|
"commitizen": "4.2.5",
|
|
76
77
|
"cross-env": "7.0.3",
|
|
77
78
|
"cz-conventional-changelog": "3.3.0",
|
|
@@ -60,11 +60,11 @@ export class RouteList extends Command {
|
|
|
60
60
|
const kernel = new Kernel()
|
|
61
61
|
|
|
62
62
|
await kernel.registerCors()
|
|
63
|
+
await kernel.registerTracer()
|
|
63
64
|
await kernel.registerRateLimit()
|
|
64
65
|
await kernel.registerMiddlewares()
|
|
65
66
|
await kernel.registerErrorHandler()
|
|
66
67
|
await kernel.registerLogMiddleware()
|
|
67
|
-
await kernel.registerRequestIdMiddleware()
|
|
68
68
|
|
|
69
69
|
const routePath = Path.routes(`http.${Path.ext()}`)
|
|
70
70
|
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { Log } from '@athenna/logger'
|
|
11
10
|
import { Config } from '@athenna/config'
|
|
12
|
-
import {
|
|
11
|
+
import { String } from '@athenna/common'
|
|
12
|
+
import { Log, Logger } from '@athenna/logger'
|
|
13
13
|
|
|
14
14
|
export class HttpExceptionHandler {
|
|
15
15
|
/**
|
|
@@ -61,8 +61,6 @@ export class HttpExceptionHandler {
|
|
|
61
61
|
delete body.stack
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
response.status(statusCode).send(body)
|
|
65
|
-
|
|
66
64
|
if (
|
|
67
65
|
this.ignoreCodes.includes(code) ||
|
|
68
66
|
this.ignoreStatuses.includes(statusCode)
|
|
@@ -70,20 +68,20 @@ export class HttpExceptionHandler {
|
|
|
70
68
|
return
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
71
|
+
const logger = Config.exists('logging.channels.exception')
|
|
72
|
+
? Log.channel('exception')
|
|
73
|
+
: Logger.getConsoleLogger({
|
|
74
|
+
level: 'trace',
|
|
75
|
+
streamType: 'stderr',
|
|
76
|
+
formatter: 'none',
|
|
77
|
+
})
|
|
77
78
|
|
|
78
|
-
|
|
79
|
+
if (!error.prettify) {
|
|
80
|
+
error = error.toAthennaException()
|
|
79
81
|
}
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
exception.stack = error.stack
|
|
84
|
-
|
|
85
|
-
const prettyError = await exception.prettify()
|
|
83
|
+
logger.error((await error.prettify()).concat('\n'))
|
|
86
84
|
|
|
87
|
-
|
|
85
|
+
return response.status(statusCode).send(body)
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import
|
|
11
|
-
import { Config } from '@athenna/config'
|
|
12
|
-
import { Module, Path, Uuid } from '@athenna/common'
|
|
10
|
+
import rTracer from 'cls-rtracer'
|
|
13
11
|
|
|
12
|
+
import { Log, Logger } from '@athenna/logger'
|
|
13
|
+
import { Config } from '@athenna/config'
|
|
14
14
|
import { Server } from '#src/Facades/Server'
|
|
15
|
+
import { Module, Path } from '@athenna/common'
|
|
15
16
|
|
|
16
17
|
export class HttpKernel {
|
|
17
18
|
/**
|
|
@@ -100,6 +101,21 @@ export class HttpKernel {
|
|
|
100
101
|
await Server.registerCors(Config.get('http.cors'))
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Register the rTracer plugin.
|
|
106
|
+
*
|
|
107
|
+
* @return {Promise<void>}
|
|
108
|
+
*/
|
|
109
|
+
async registerTracer() {
|
|
110
|
+
if (Config.is('http.noTracer', true)) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Server.use(async ctx => (ctx.data.traceId = rTracer.id()))
|
|
115
|
+
|
|
116
|
+
await Server.registerTracer(Config.get('http.tracer'))
|
|
117
|
+
}
|
|
118
|
+
|
|
103
119
|
/**
|
|
104
120
|
* Register helmet plugin.
|
|
105
121
|
*
|
|
@@ -169,22 +185,14 @@ export class HttpKernel {
|
|
|
169
185
|
}
|
|
170
186
|
|
|
171
187
|
Server.use(async ctx => {
|
|
172
|
-
|
|
188
|
+
const logger = Config.exists('logging.channels.request')
|
|
189
|
+
? Log.channel('request')
|
|
190
|
+
: Logger.getConsoleLogger({
|
|
191
|
+
level: 'trace',
|
|
192
|
+
formatter: 'none',
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
logger.info(ctx)
|
|
173
196
|
}, 'terminate')
|
|
174
197
|
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Register the requestId handle middleware.
|
|
178
|
-
*
|
|
179
|
-
* @return {Promise<void>}
|
|
180
|
-
*/
|
|
181
|
-
async registerRequestIdMiddleware() {
|
|
182
|
-
if (Config.is('http.noRequestId', true)) {
|
|
183
|
-
return
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
Server.use(async ctx => {
|
|
187
|
-
ctx.data.requestId = Uuid.generate('ath')
|
|
188
|
-
}, 'handle')
|
|
189
|
-
}
|
|
190
198
|
}
|
|
@@ -23,7 +23,7 @@ export class ControllerProvider extends ServiceProvider {
|
|
|
23
23
|
const controllers = await Module.getAllFromWithAlias(path, subAlias)
|
|
24
24
|
|
|
25
25
|
controllers.forEach(({ alias, module }) => {
|
|
26
|
-
this.container.
|
|
26
|
+
this.container.bind(alias, module)
|
|
27
27
|
})
|
|
28
28
|
}
|
|
29
29
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -11,8 +11,8 @@ import { Facade } from '@athenna/ioc'
|
|
|
11
11
|
import { Exception, Json } from '@athenna/common'
|
|
12
12
|
import { OpenAPIV2, OpenAPIV3 } from 'openapi-types'
|
|
13
13
|
import { FastifyHelmetOptions } from '@fastify/helmet'
|
|
14
|
-
import { FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
|
|
15
14
|
import { RateLimitOptions } from '@fastify/rate-limit'
|
|
15
|
+
import { FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
|
|
16
16
|
|
|
17
17
|
export const Server: typeof Facade & Http
|
|
18
18
|
export const Route: typeof Facade & Router.Router
|
|
@@ -70,6 +70,13 @@ export class HttpKernel {
|
|
|
70
70
|
*/
|
|
71
71
|
registerCors(): Promise<void>
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Register the rTracer plugin.
|
|
75
|
+
*
|
|
76
|
+
* @return {Promise<void>}
|
|
77
|
+
*/
|
|
78
|
+
registerTracer(): Promise<void>
|
|
79
|
+
|
|
73
80
|
/**
|
|
74
81
|
* Register helmet plugin.
|
|
75
82
|
*
|
|
@@ -104,13 +111,6 @@ export class HttpKernel {
|
|
|
104
111
|
* @return {Promise<void>}
|
|
105
112
|
*/
|
|
106
113
|
registerLogMiddleware(): Promise<void>
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Register the requestId handle middleware.
|
|
110
|
-
*
|
|
111
|
-
* @return {Promise<void>}
|
|
112
|
-
*/
|
|
113
|
-
registerRequestIdMiddleware(): Promise<void>
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
export class HttpExceptionHandler {
|
|
@@ -150,7 +150,7 @@ export class Http {
|
|
|
150
150
|
*
|
|
151
151
|
* @param {import('fastify').FastifyPluginCallback<import('fastify').FastifyPluginOptions>} plugin
|
|
152
152
|
* @param {import('fastify').FastifyRegisterOptions<import('fastify').FastifyPluginOptions>} [options]
|
|
153
|
-
* @return {Http}
|
|
153
|
+
* @return {Promise<Http>}
|
|
154
154
|
*/
|
|
155
155
|
register(
|
|
156
156
|
plugin: import('fastify').FastifyPluginCallback<
|
|
@@ -159,41 +159,49 @@ export class Http {
|
|
|
159
159
|
options?: import('fastify').FastifyRegisterOptions<
|
|
160
160
|
import('fastify').FastifyPluginOptions
|
|
161
161
|
>,
|
|
162
|
-
): Http
|
|
162
|
+
): Promise<Http>
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
165
|
* Register the cors plugin to fastify server.
|
|
166
166
|
*
|
|
167
167
|
* @param {import('@fastify/cors').FastifyCorsOptions} [options]
|
|
168
|
-
* @return {Http}
|
|
168
|
+
* @return {Promise<Http>}
|
|
169
169
|
*/
|
|
170
|
-
registerCors(options?: import('@fastify/cors').FastifyCorsOptions): Http
|
|
170
|
+
registerCors(options?: import('@fastify/cors').FastifyCorsOptions): Promise<Http>
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Register the rTracer plugin.
|
|
174
|
+
*
|
|
175
|
+
* @param {import('cls-rtracer').IFastifyOptions} options
|
|
176
|
+
* @return {Promise<Http>}
|
|
177
|
+
*/
|
|
178
|
+
registerTracer(options?: import('cls-rtracer').IFastifyOptions): Promise<Http>
|
|
171
179
|
|
|
172
180
|
/**
|
|
173
181
|
* Register the helmet plugin to fastify server.
|
|
174
182
|
*
|
|
175
183
|
* @param {import('@fastify/helmet').FastifyHelmetOptions} [options]
|
|
176
|
-
* @return {Http}
|
|
184
|
+
* @return {Promise<Http>}
|
|
177
185
|
*/
|
|
178
|
-
registerHelmet(options?: import('@fastify/helmet').FastifyHelmetOptions): Http
|
|
186
|
+
registerHelmet(options?: import('@fastify/helmet').FastifyHelmetOptions): Promise<Http>
|
|
179
187
|
|
|
180
188
|
/**
|
|
181
189
|
* Register the swagger plugin to fastify server.
|
|
182
190
|
*
|
|
183
191
|
* @param {import('@fastify/swagger').SwaggerOptions} [options]
|
|
184
|
-
* @return {Http}
|
|
192
|
+
* @return {Promise<Http>}
|
|
185
193
|
*/
|
|
186
|
-
registerSwagger(options?: import('@fastify/swagger').SwaggerOptions): Http
|
|
194
|
+
registerSwagger(options?: import('@fastify/swagger').SwaggerOptions): Promise<Http>
|
|
187
195
|
|
|
188
196
|
/**
|
|
189
197
|
* Register the rate limit plugin to fastify server.
|
|
190
198
|
*
|
|
191
199
|
* @param {import('@fastify/rate-limit').RateLimitOptions} [options]
|
|
192
|
-
* @return {Http}
|
|
200
|
+
* @return {Promise<Http>}
|
|
193
201
|
*/
|
|
194
202
|
registerRateLimit(
|
|
195
203
|
options?: import('@fastify/rate-limit').RateLimitOptions,
|
|
196
|
-
): Http
|
|
204
|
+
): Promise<Http>
|
|
197
205
|
|
|
198
206
|
/**
|
|
199
207
|
* Get the fastify server instance.
|
|
@@ -217,6 +225,13 @@ export class Http {
|
|
|
217
225
|
*/
|
|
218
226
|
getPort(): number
|
|
219
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Get the server http version.
|
|
230
|
+
*
|
|
231
|
+
* @return {string}
|
|
232
|
+
*/
|
|
233
|
+
getVersion(): string
|
|
234
|
+
|
|
220
235
|
/**
|
|
221
236
|
* Print all routes registered.
|
|
222
237
|
*
|
package/src/index.js
CHANGED
|
@@ -7,10 +7,9 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { Options } from '@athenna/common'
|
|
11
|
-
|
|
12
10
|
import fastify from 'fastify'
|
|
13
11
|
|
|
12
|
+
import { Options } from '@athenna/common'
|
|
14
13
|
import { FastifyHandler } from '#src/Handlers/FastifyHandler'
|
|
15
14
|
|
|
16
15
|
export * from './Facades/Route.js'
|
|
@@ -83,7 +82,7 @@ export class Http {
|
|
|
83
82
|
*
|
|
84
83
|
* @param {any} plugin
|
|
85
84
|
* @param {any} [options]
|
|
86
|
-
* @return {Http}
|
|
85
|
+
* @return {Promise<Http>}
|
|
87
86
|
*/
|
|
88
87
|
async register(plugin, options = {}) {
|
|
89
88
|
await this.#server.register(plugin, options)
|
|
@@ -95,17 +94,29 @@ export class Http {
|
|
|
95
94
|
* Register the cors plugin to fastify server.
|
|
96
95
|
*
|
|
97
96
|
* @param {import('@fastify/cors').FastifyCorsOptions} [options]
|
|
98
|
-
* @return {Http}
|
|
97
|
+
* @return {Promise<Http>}
|
|
99
98
|
*/
|
|
100
99
|
async registerCors(options) {
|
|
101
100
|
return this.register(import('@fastify/cors'), options)
|
|
102
101
|
}
|
|
103
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Register the rTracer plugin to fastify server.
|
|
105
|
+
*
|
|
106
|
+
* @param {import('cls-rtracer').IFastifyOptions} [options]
|
|
107
|
+
* @return {Promise<Http>}
|
|
108
|
+
*/
|
|
109
|
+
async registerTracer(options) {
|
|
110
|
+
const rTracer = await import('cls-rtracer')
|
|
111
|
+
|
|
112
|
+
return this.register(rTracer.fastifyPlugin, options)
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
/**
|
|
105
116
|
* Register the helmet plugin to fastify server.
|
|
106
117
|
*
|
|
107
118
|
* @param {import('@fastify/helmet').FastifyHelmetOptions} [options]
|
|
108
|
-
* @return {Http}
|
|
119
|
+
* @return {Promise<Http>}
|
|
109
120
|
*/
|
|
110
121
|
async registerHelmet(options) {
|
|
111
122
|
return this.register(import('@fastify/helmet'), options)
|
|
@@ -118,7 +129,7 @@ export class Http {
|
|
|
118
129
|
* ui: import('@fastify/swagger-ui').FastifySwaggerUiOptions,
|
|
119
130
|
* configurations: import('@fastify/swagger').SwaggerOptions
|
|
120
131
|
* }} [options]
|
|
121
|
-
* @return {Http}
|
|
132
|
+
* @return {Promise<Http>}
|
|
122
133
|
*/
|
|
123
134
|
async registerSwagger(options = {}) {
|
|
124
135
|
await this.register(import('@fastify/swagger'), options.configurations)
|
|
@@ -130,7 +141,7 @@ export class Http {
|
|
|
130
141
|
* Register the rate limit plugin to fastify server.
|
|
131
142
|
*
|
|
132
143
|
* @param {import('@fastify/rate-limit').RateLimitOptions} [options]
|
|
133
|
-
* @return {Http}
|
|
144
|
+
* @return {Promise<Http>}
|
|
134
145
|
*/
|
|
135
146
|
async registerRateLimit(options) {
|
|
136
147
|
return this.register(import('@fastify/rate-limit'), options)
|
|
@@ -164,6 +175,15 @@ export class Http {
|
|
|
164
175
|
return this.#server.server.address().port
|
|
165
176
|
}
|
|
166
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Get the server http version.
|
|
180
|
+
*
|
|
181
|
+
* @return {string}
|
|
182
|
+
*/
|
|
183
|
+
getVersion() {
|
|
184
|
+
return this.#server.version
|
|
185
|
+
}
|
|
186
|
+
|
|
167
187
|
/**
|
|
168
188
|
* Print all routes registered.
|
|
169
189
|
*
|