@athenna/http 1.7.9 → 1.8.1
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 +1 -1
- package/src/Handlers/FastifyHandler.js +9 -7
- package/src/Router/Route.js +28 -0
- package/src/Router/RouteGroup.js +14 -0
- package/src/Router/RouteResource.js +29 -2
- package/src/Router/Router.js +1 -0
- package/src/index.d.ts +34 -0
package/package.json
CHANGED
|
@@ -25,25 +25,27 @@ export class FastifyHandler {
|
|
|
25
25
|
|
|
26
26
|
if (!req.data) req.data = {}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const isJson = Is.Json(payload)
|
|
29
29
|
|
|
30
|
-
if (
|
|
31
|
-
|
|
30
|
+
if (isJson) {
|
|
31
|
+
payload = JSON.parse(payload)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
payload = await handler({
|
|
35
35
|
request,
|
|
36
36
|
response,
|
|
37
|
-
body,
|
|
37
|
+
body: payload,
|
|
38
38
|
status: res.statusCode,
|
|
39
39
|
params: req.params,
|
|
40
40
|
queries: req.query,
|
|
41
41
|
data: req.data,
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
if (
|
|
44
|
+
if (isJson) {
|
|
45
|
+
payload = JSON.stringify(payload)
|
|
46
|
+
}
|
|
45
47
|
|
|
46
|
-
return
|
|
48
|
+
return payload
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
|
package/src/Router/Route.js
CHANGED
|
@@ -76,6 +76,13 @@ export class Route {
|
|
|
76
76
|
*/
|
|
77
77
|
#swaggerOptions
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Rate limit options of this route.
|
|
81
|
+
*
|
|
82
|
+
* @type {any}
|
|
83
|
+
*/
|
|
84
|
+
#rateLimitOptions
|
|
85
|
+
|
|
79
86
|
/**
|
|
80
87
|
* Creates a new instance of Route.
|
|
81
88
|
*
|
|
@@ -95,6 +102,7 @@ export class Route {
|
|
|
95
102
|
|
|
96
103
|
this.#helmetOptions = {}
|
|
97
104
|
this.#swaggerOptions = {}
|
|
105
|
+
this.#rateLimitOptions = {}
|
|
98
106
|
|
|
99
107
|
RouteHelper.getParamsName(url).forEach(param => this.param(param))
|
|
100
108
|
|
|
@@ -221,6 +229,25 @@ export class Route {
|
|
|
221
229
|
return this
|
|
222
230
|
}
|
|
223
231
|
|
|
232
|
+
/**
|
|
233
|
+
* Set up all rate limit options for route.
|
|
234
|
+
*
|
|
235
|
+
* @param {any} options
|
|
236
|
+
* @param {boolean} [override]
|
|
237
|
+
* @return {Route}
|
|
238
|
+
*/
|
|
239
|
+
rateLimit(options, override = true) {
|
|
240
|
+
if (!override) {
|
|
241
|
+
this.#rateLimitOptions = Options.create(this.#rateLimitOptions, options)
|
|
242
|
+
|
|
243
|
+
return this
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.#rateLimitOptions = options
|
|
247
|
+
|
|
248
|
+
return this
|
|
249
|
+
}
|
|
250
|
+
|
|
224
251
|
/**
|
|
225
252
|
* Set a summary for the route swagger docs.
|
|
226
253
|
*
|
|
@@ -351,6 +378,7 @@ export class Route {
|
|
|
351
378
|
middlewares: this.#routeMiddlewares,
|
|
352
379
|
helmetOptions: this.#helmetOptions,
|
|
353
380
|
swaggerOptions: this.#swaggerOptions,
|
|
381
|
+
rateLimitOptions: this.#rateLimitOptions,
|
|
354
382
|
}
|
|
355
383
|
|
|
356
384
|
if (Is.String(this.#handler)) {
|
package/src/Router/RouteGroup.js
CHANGED
|
@@ -82,6 +82,20 @@ export class RouteGroup {
|
|
|
82
82
|
return this
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Set up rate limit options for route group.
|
|
87
|
+
*
|
|
88
|
+
* @param {any} options
|
|
89
|
+
* @return {RouteGroup}
|
|
90
|
+
*/
|
|
91
|
+
rateLimit(options) {
|
|
92
|
+
this.routes.forEach(route => {
|
|
93
|
+
this.#invoke(route, 'rateLimit', [options, false])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
|
|
85
99
|
/**
|
|
86
100
|
* Invoke a method from route.
|
|
87
101
|
*
|
|
@@ -106,7 +106,7 @@ export class RouteResource {
|
|
|
106
106
|
*/
|
|
107
107
|
helmet(action, options) {
|
|
108
108
|
if (!options) {
|
|
109
|
-
this.routes.forEach(route => route.helmet(
|
|
109
|
+
this.routes.forEach(route => route.helmet(action))
|
|
110
110
|
|
|
111
111
|
return this
|
|
112
112
|
}
|
|
@@ -133,7 +133,7 @@ export class RouteResource {
|
|
|
133
133
|
*/
|
|
134
134
|
swagger(action, options) {
|
|
135
135
|
if (!options) {
|
|
136
|
-
this.routes.forEach(route => route.swagger(
|
|
136
|
+
this.routes.forEach(route => route.swagger(action))
|
|
137
137
|
|
|
138
138
|
return this
|
|
139
139
|
}
|
|
@@ -151,6 +151,33 @@ export class RouteResource {
|
|
|
151
151
|
return this
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Set up rate limit options for route resource method.
|
|
156
|
+
*
|
|
157
|
+
* @param {string|any} action
|
|
158
|
+
* @param {any} [options]
|
|
159
|
+
* @return {RouteResource}
|
|
160
|
+
*/
|
|
161
|
+
rateLimit(action, options) {
|
|
162
|
+
if (!options) {
|
|
163
|
+
this.routes.forEach(route => route.rateLimit(action))
|
|
164
|
+
|
|
165
|
+
return this
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const resourceName = `${this.#resourceName}.${action}`
|
|
169
|
+
|
|
170
|
+
this.routes.forEach(route => {
|
|
171
|
+
if (route.name !== resourceName) {
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
route.rateLimit(options)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
return this
|
|
179
|
+
}
|
|
180
|
+
|
|
154
181
|
/**
|
|
155
182
|
* Create the route.
|
|
156
183
|
*
|
package/src/Router/Router.js
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { Exception } from '@athenna/common'
|
|
|
12
12
|
import { OpenAPIV2, OpenAPIV3 } from 'openapi-types'
|
|
13
13
|
import { FastifyHelmetOptions } from '@fastify/helmet'
|
|
14
14
|
import { FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
|
|
15
|
+
import { RateLimitOptions } from '@fastify/rate-limit'
|
|
15
16
|
|
|
16
17
|
export const Server: Facade & Http
|
|
17
18
|
export const Route: Facade & Router.Router
|
|
@@ -390,6 +391,14 @@ declare module Router {
|
|
|
390
391
|
*/
|
|
391
392
|
swagger(options: FastifySwaggerSchema): this
|
|
392
393
|
|
|
394
|
+
/**
|
|
395
|
+
* Set up all rate limit options for route.
|
|
396
|
+
*
|
|
397
|
+
* @param {any} options
|
|
398
|
+
* @return {Route}
|
|
399
|
+
*/
|
|
400
|
+
rateLimit(options: RateLimitOptions): this
|
|
401
|
+
|
|
393
402
|
/**
|
|
394
403
|
* Set a summary for the route swagger docs.
|
|
395
404
|
*
|
|
@@ -550,6 +559,23 @@ declare module Router {
|
|
|
550
559
|
* @return {RouteResource}
|
|
551
560
|
*/
|
|
552
561
|
swagger(action: string, options: FastifySwaggerSchema): this
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Set up rate limit options for route resource method.
|
|
565
|
+
*
|
|
566
|
+
* @param {RateLimitOptions} options
|
|
567
|
+
* @return {RouteResource}
|
|
568
|
+
*/
|
|
569
|
+
rateLimit(options: RateLimitOptions): this
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Set up rate limit options for route resource method.
|
|
573
|
+
*
|
|
574
|
+
* @param {string} action
|
|
575
|
+
* @param {RateLimitOptions} options
|
|
576
|
+
* @return {RouteResource}
|
|
577
|
+
*/
|
|
578
|
+
rateLimit(action: string, options: RateLimitOptions): this
|
|
553
579
|
}
|
|
554
580
|
|
|
555
581
|
export class RouteGroup {
|
|
@@ -604,6 +630,14 @@ declare module Router {
|
|
|
604
630
|
* @return {RouteGroup}
|
|
605
631
|
*/
|
|
606
632
|
swagger(options: FastifySwaggerSchema): this
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Set up rate limit options for route group.
|
|
636
|
+
*
|
|
637
|
+
* @param {any} options
|
|
638
|
+
* @return {RouteGroup}
|
|
639
|
+
*/
|
|
640
|
+
rateLimit(options: RateLimitOptions): this
|
|
607
641
|
}
|
|
608
642
|
|
|
609
643
|
export class Router {
|