@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.
- package/build/Commands/MakeControllerCommand.d.ts +15 -0
- package/build/Commands/MakeControllerCommand.js +42 -0
- package/build/Commands/MakeInterceptorCommand.d.ts +15 -0
- package/build/Commands/MakeInterceptorCommand.js +42 -0
- package/build/Commands/MakeMiddlewareCommand.d.ts +15 -0
- package/build/Commands/MakeMiddlewareCommand.js +42 -0
- package/build/Commands/MakeTerminatorCommand.d.ts +15 -0
- package/build/Commands/MakeTerminatorCommand.js +42 -0
- package/build/Commands/RouteListCommand.d.ts +19 -0
- package/build/Commands/RouteListCommand.js +62 -0
- package/build/Context/Request.d.ts +144 -0
- package/build/Context/Request.js +219 -0
- package/build/Context/Response.d.ts +67 -0
- package/build/Context/Response.js +102 -0
- package/build/Contracts/InterceptorContract.d.ts +15 -0
- package/build/Contracts/InterceptorContract.js +9 -0
- package/build/Contracts/MiddlewareContract.d.ts +15 -0
- package/build/Contracts/MiddlewareContract.js +9 -0
- package/build/Contracts/TerminatorContract.d.ts +15 -0
- package/build/Contracts/TerminatorContract.js +9 -0
- package/build/Decorators/Controller.d.ts +14 -0
- package/build/Decorators/Controller.js +28 -0
- package/build/Decorators/Interceptor.d.ts +14 -0
- package/build/Decorators/Interceptor.js +37 -0
- package/build/Decorators/Middleware.d.ts +14 -0
- package/build/Decorators/Middleware.js +37 -0
- package/build/Decorators/Terminator.d.ts +14 -0
- package/build/Decorators/Terminator.js +37 -0
- package/build/Exceptions/BadGatewayException.d.ts +20 -0
- package/build/Exceptions/BadGatewayException.js +22 -0
- package/build/Exceptions/BadRequestException.d.ts +20 -0
- package/build/Exceptions/BadRequestException.js +22 -0
- package/build/Exceptions/ForbiddenException.d.ts +20 -0
- package/build/Exceptions/ForbiddenException.js +22 -0
- package/build/Exceptions/HttpException.d.ts +20 -0
- package/build/Exceptions/HttpException.js +27 -0
- package/build/Exceptions/InternalServerException.d.ts +20 -0
- package/build/Exceptions/InternalServerException.js +22 -0
- package/build/Exceptions/MethodNotAllowedException.d.ts +20 -0
- package/build/Exceptions/MethodNotAllowedException.js +22 -0
- package/build/Exceptions/NotAcceptableException.d.ts +20 -0
- package/build/Exceptions/NotAcceptableException.js +22 -0
- package/build/Exceptions/NotFoundException.d.ts +20 -0
- package/build/Exceptions/NotFoundException.js +22 -0
- package/build/Exceptions/NotImplementedException.d.ts +20 -0
- package/build/Exceptions/NotImplementedException.js +22 -0
- package/build/Exceptions/PayloadTooLargeException.d.ts +20 -0
- package/build/Exceptions/PayloadTooLargeException.js +22 -0
- package/build/Exceptions/RequestTimeoutException.d.ts +20 -0
- package/build/Exceptions/RequestTimeoutException.js +22 -0
- package/build/Exceptions/ServiceUnavailableException.d.ts +20 -0
- package/build/Exceptions/ServiceUnavailableException.js +22 -0
- package/build/Exceptions/UnauthorizedException.d.ts +20 -0
- package/build/Exceptions/UnauthorizedException.js +22 -0
- package/build/Exceptions/UndefinedMethodException.d.ts +12 -0
- package/build/Exceptions/UndefinedMethodException.js +19 -0
- package/build/Exceptions/UnprocessableEntityException.d.ts +20 -0
- package/build/Exceptions/UnprocessableEntityException.js +22 -0
- package/build/Facades/Route.d.ts +10 -0
- package/{src → build}/Facades/Route.js +2 -7
- package/build/Facades/Server.d.ts +10 -0
- package/{src → build}/Facades/Server.js +2 -7
- package/build/Handlers/FastifyHandler.d.ts +41 -0
- package/build/Handlers/FastifyHandler.js +116 -0
- package/build/Handlers/HttpExceptionHandler.d.ts +31 -0
- package/build/Handlers/HttpExceptionHandler.js +82 -0
- package/build/Kernels/HttpKernel.d.ts +73 -0
- package/build/Kernels/HttpKernel.js +221 -0
- package/build/Providers/HttpRouteProvider.d.ts +12 -0
- package/build/Providers/HttpRouteProvider.js +15 -0
- package/build/Providers/HttpServerProvider.d.ts +13 -0
- package/build/Providers/HttpServerProvider.js +25 -0
- package/build/Router/Route.d.ts +263 -0
- package/build/Router/Route.js +447 -0
- package/build/Router/RouteGroup.d.ts +96 -0
- package/build/Router/RouteGroup.js +123 -0
- package/build/Router/RouteResource.d.ts +108 -0
- package/build/Router/RouteResource.js +158 -0
- package/build/Router/Router.d.ts +103 -0
- package/build/Router/Router.js +188 -0
- package/build/Server/ServerImpl.d.ts +129 -0
- package/build/Server/ServerImpl.js +213 -0
- package/build/Types/Contexts/Context.d.ts +20 -0
- package/build/Types/Contexts/Context.js +9 -0
- package/build/Types/Contexts/ErrorContext.d.ts +21 -0
- package/build/Types/Contexts/ErrorContext.js +9 -0
- package/build/Types/Contexts/InterceptContext.d.ts +20 -0
- package/build/Types/Contexts/InterceptContext.js +9 -0
- package/build/Types/Contexts/TerminateContext.d.ts +21 -0
- package/build/Types/Contexts/TerminateContext.js +9 -0
- package/build/Types/Controllers/ControllerOptions.d.ts +25 -0
- package/build/Types/Controllers/ControllerOptions.js +9 -0
- package/build/Types/Middlewares/InterceptorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/InterceptorRouteType.js +9 -0
- package/build/Types/Middlewares/MiddlewareHandler.d.ts +20 -0
- package/build/Types/Middlewares/MiddlewareHandler.js +9 -0
- package/build/Types/Middlewares/MiddlewareOptions.d.ts +40 -0
- package/build/Types/Middlewares/MiddlewareOptions.js +9 -0
- package/build/Types/Middlewares/MiddlewareRouteType.d.ts +11 -0
- package/build/Types/Middlewares/MiddlewareRouteType.js +9 -0
- package/build/Types/Middlewares/TerminatorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/TerminatorRouteType.js +9 -0
- package/build/Types/Router/RouteHandler.d.ts +10 -0
- package/build/Types/Router/RouteHandler.js +9 -0
- package/build/Types/Router/RouteJSON.d.ts +21 -0
- package/build/Types/Router/RouteJSON.js +9 -0
- package/build/Types/Router/RouteResourceTypes.d.ts +9 -0
- package/build/Types/Router/RouteResourceTypes.js +9 -0
- package/build/index.d.ts +70 -0
- package/build/index.js +48 -0
- package/package.json +159 -77
- package/templates/controller.edge +8 -36
- package/templates/interceptor.edge +8 -0
- package/templates/middleware.edge +4 -36
- package/templates/terminator.edge +6 -0
- package/src/Commands/Make/Controller.js +0 -64
- package/src/Commands/Make/Middleware.js +0 -80
- package/src/Commands/Route/List.js +0 -107
- package/src/Context/Request.js +0 -268
- package/src/Context/Response.js +0 -173
- package/src/Exceptions/BadGatewayException.js +0 -31
- package/src/Exceptions/BadRequestException.js +0 -31
- package/src/Exceptions/ForbiddenException.js +0 -31
- package/src/Exceptions/HttpException.js +0 -33
- package/src/Exceptions/InternalServerException.js +0 -31
- package/src/Exceptions/MethodNotAllowedException.js +0 -31
- package/src/Exceptions/NotAcceptableException.js +0 -31
- package/src/Exceptions/NotFoundException.js +0 -31
- package/src/Exceptions/NotImplementedException.js +0 -31
- package/src/Exceptions/PayloadTooLargeException.js +0 -31
- package/src/Exceptions/RequestTimeoutException.js +0 -31
- package/src/Exceptions/ServiceUnavailableException.js +0 -31
- package/src/Exceptions/UnauthorizedException.js +0 -31
- package/src/Exceptions/UndefinedMethodException.js +0 -30
- package/src/Exceptions/UnprocessableEntityException.js +0 -31
- package/src/Handlers/FastifyHandler.js +0 -148
- package/src/Handlers/HttpExceptionHandler.js +0 -88
- package/src/Helpers/HttpLoader.js +0 -38
- package/src/Kernels/HttpKernel.js +0 -199
- package/src/Providers/ControllerProvider.js +0 -29
- package/src/Providers/HttpRouteProvider.js +0 -24
- package/src/Providers/HttpServerProvider.js +0 -37
- package/src/Providers/MiddlewareProvider.js +0 -29
- package/src/Router/Route.js +0 -411
- package/src/Router/RouteGroup.js +0 -119
- package/src/Router/RouteResource.js +0 -247
- package/src/Router/Router.js +0 -313
- package/src/Utils/isMiddlewareContract.js +0 -17
- package/src/Utils/removeSlashes.js +0 -30
- package/src/index.d.ts +0 -1395
- package/src/index.js +0 -377
package/src/Router/Route.js
DELETED
|
@@ -1,411 +0,0 @@
|
|
|
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
|
-
|
|
10
|
-
import { Is, Options, Route as RouteHelper } from '@athenna/common'
|
|
11
|
-
import { removeSlashes } from '#src/Utils/removeSlashes'
|
|
12
|
-
import { isMiddlewareContract } from '#src/Utils/isMiddlewareContract'
|
|
13
|
-
import { UndefinedMethodException } from '#src/Exceptions/UndefinedMethodException'
|
|
14
|
-
|
|
15
|
-
export class Route {
|
|
16
|
-
/**
|
|
17
|
-
* Route name for resources.
|
|
18
|
-
*
|
|
19
|
-
* @type {string}
|
|
20
|
-
*/
|
|
21
|
-
name
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Route url.
|
|
25
|
-
*
|
|
26
|
-
* @type {string}
|
|
27
|
-
*/
|
|
28
|
-
#url
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Sets if route should be registered or not.
|
|
32
|
-
*
|
|
33
|
-
* @type {boolean}
|
|
34
|
-
*/
|
|
35
|
-
#deleted
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The route handler closure.
|
|
39
|
-
*
|
|
40
|
-
* @type {string|any}
|
|
41
|
-
*/
|
|
42
|
-
#handler
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The methods of this route.
|
|
46
|
-
*
|
|
47
|
-
* @type {string[]}
|
|
48
|
-
*/
|
|
49
|
-
#methods
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The middlewares of this route.
|
|
53
|
-
*
|
|
54
|
-
* @type {{ handlers: any[], terminators: any[], interceptors: any[] }}
|
|
55
|
-
*/
|
|
56
|
-
#routeMiddlewares
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The prefixes of this route.
|
|
60
|
-
*
|
|
61
|
-
* @type {string[]}
|
|
62
|
-
*/
|
|
63
|
-
#prefixes
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Helmet options of this route.
|
|
67
|
-
*
|
|
68
|
-
* @type {any}
|
|
69
|
-
*/
|
|
70
|
-
#helmetOptions
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Swagger options of this route.
|
|
74
|
-
*
|
|
75
|
-
* @type {any}
|
|
76
|
-
*/
|
|
77
|
-
#swaggerOptions
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Rate limit options of this route.
|
|
81
|
-
*
|
|
82
|
-
* @type {any}
|
|
83
|
-
*/
|
|
84
|
-
#rateLimitOptions
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Creates a new instance of Route.
|
|
88
|
-
*
|
|
89
|
-
* @param {string} url
|
|
90
|
-
* @param {string[]} methods
|
|
91
|
-
* @param {string|any} handler
|
|
92
|
-
* @param {string} [name]
|
|
93
|
-
* @return {Route}
|
|
94
|
-
*/
|
|
95
|
-
constructor(url, methods, handler, name) {
|
|
96
|
-
this.#url = url
|
|
97
|
-
this.#deleted = false
|
|
98
|
-
this.#methods = methods
|
|
99
|
-
this.#prefixes = []
|
|
100
|
-
this.#handler = handler
|
|
101
|
-
this.#routeMiddlewares = { handlers: [], terminators: [], interceptors: [] }
|
|
102
|
-
|
|
103
|
-
this.#helmetOptions = {}
|
|
104
|
-
this.#swaggerOptions = {}
|
|
105
|
-
this.#rateLimitOptions = {}
|
|
106
|
-
|
|
107
|
-
RouteHelper.getParamsName(url).forEach(param => this.param(param))
|
|
108
|
-
|
|
109
|
-
if (name) {
|
|
110
|
-
this.name = name
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Set a prefix for the route.
|
|
116
|
-
*
|
|
117
|
-
* @param {string} prefix
|
|
118
|
-
* @return {Route}
|
|
119
|
-
*/
|
|
120
|
-
prefix(prefix) {
|
|
121
|
-
this.#prefixes.push(prefix)
|
|
122
|
-
|
|
123
|
-
return this
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Set a middleware for the route.
|
|
128
|
-
*
|
|
129
|
-
* @param {string|any} middleware
|
|
130
|
-
* @param {'handle'|'intercept'|'terminate'} [type]
|
|
131
|
-
* @param {boolean} [prepend]
|
|
132
|
-
* @return {Route}
|
|
133
|
-
*/
|
|
134
|
-
middleware(middleware, type = 'handle', prepend = false) {
|
|
135
|
-
const dictionary = {
|
|
136
|
-
handle: 'handlers',
|
|
137
|
-
terminate: 'terminators',
|
|
138
|
-
intercept: 'interceptors',
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const insertionType = prepend ? 'unshift' : 'push'
|
|
142
|
-
|
|
143
|
-
if (Is.String(middleware)) {
|
|
144
|
-
const mid =
|
|
145
|
-
ioc.use(`App/Http/Middlewares/Names/${middleware}`) ||
|
|
146
|
-
ioc.safeUse(`App/Http/Middlewares/${middleware}`)
|
|
147
|
-
|
|
148
|
-
if (mid.handle) {
|
|
149
|
-
this.#routeMiddlewares.handlers[insertionType](mid.handle.bind(mid))
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (mid.intercept) {
|
|
153
|
-
this.#routeMiddlewares.interceptors[insertionType](
|
|
154
|
-
mid.intercept.bind(mid),
|
|
155
|
-
)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (mid.terminate) {
|
|
159
|
-
this.#routeMiddlewares.terminators[insertionType](
|
|
160
|
-
mid.terminate.bind(mid),
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return this
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (isMiddlewareContract(middleware)) {
|
|
168
|
-
if (middleware.handle) {
|
|
169
|
-
this.#routeMiddlewares.handlers[insertionType](
|
|
170
|
-
middleware.handle.bind(middleware),
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (middleware.intercept) {
|
|
175
|
-
this.#routeMiddlewares.interceptors[insertionType](
|
|
176
|
-
middleware.intercept.bind(middleware),
|
|
177
|
-
)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (middleware.terminate) {
|
|
181
|
-
this.#routeMiddlewares.terminators[insertionType](
|
|
182
|
-
middleware.terminate.bind(middleware),
|
|
183
|
-
)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return this
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
this.#routeMiddlewares[dictionary[type]][insertionType](middleware)
|
|
190
|
-
|
|
191
|
-
return this
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Set up all helmet options for route.
|
|
196
|
-
*
|
|
197
|
-
* @param {any} options
|
|
198
|
-
* @param {boolean} [override]
|
|
199
|
-
* @return {Route}
|
|
200
|
-
*/
|
|
201
|
-
helmet(options, override = true) {
|
|
202
|
-
if (!override) {
|
|
203
|
-
this.#helmetOptions = Options.create(this.#helmetOptions, options)
|
|
204
|
-
|
|
205
|
-
return this
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
this.#helmetOptions = options
|
|
209
|
-
|
|
210
|
-
return this
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Set up all swagger options for route.
|
|
215
|
-
*
|
|
216
|
-
* @param {any} options
|
|
217
|
-
* @param {boolean} [override]
|
|
218
|
-
* @return {Route}
|
|
219
|
-
*/
|
|
220
|
-
swagger(options, override = true) {
|
|
221
|
-
if (!override) {
|
|
222
|
-
this.#swaggerOptions = Options.create(this.#swaggerOptions, options)
|
|
223
|
-
|
|
224
|
-
return this
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
this.#swaggerOptions = options
|
|
228
|
-
|
|
229
|
-
return this
|
|
230
|
-
}
|
|
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
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Set a summary for the route swagger docs.
|
|
253
|
-
*
|
|
254
|
-
* @param {string} summary
|
|
255
|
-
* @return {Route}
|
|
256
|
-
*/
|
|
257
|
-
summary(summary) {
|
|
258
|
-
this.#swaggerOptions.summary = summary
|
|
259
|
-
|
|
260
|
-
return this
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Set a description for the route swagger docs.
|
|
265
|
-
*
|
|
266
|
-
* @param {string} description
|
|
267
|
-
* @return {Route}
|
|
268
|
-
*/
|
|
269
|
-
description(description) {
|
|
270
|
-
this.#swaggerOptions.description = description
|
|
271
|
-
|
|
272
|
-
return this
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Set tags for the route swagger docs.
|
|
277
|
-
*
|
|
278
|
-
* @param {string} tags
|
|
279
|
-
* @return {Route}
|
|
280
|
-
*/
|
|
281
|
-
tags(...tags) {
|
|
282
|
-
if (!this.#swaggerOptions.tags) {
|
|
283
|
-
this.#swaggerOptions.tags = []
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
tags.forEach(tag => this.#swaggerOptions.tags.push(tag))
|
|
287
|
-
|
|
288
|
-
return this
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Set body param for the route swagger docs.
|
|
293
|
-
*
|
|
294
|
-
* @param {string} name
|
|
295
|
-
* @param {string} [type]
|
|
296
|
-
* @param {string} [description]
|
|
297
|
-
* @return {Route}
|
|
298
|
-
*/
|
|
299
|
-
body(name, type = 'string', description = '') {
|
|
300
|
-
if (!this.#swaggerOptions.body) {
|
|
301
|
-
this.#swaggerOptions.body = {}
|
|
302
|
-
this.#swaggerOptions.body.type = 'object'
|
|
303
|
-
this.#swaggerOptions.body.properties = {}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
this.#swaggerOptions.body.properties[name] = { type, description }
|
|
307
|
-
|
|
308
|
-
return this
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Set param for the route swagger docs.
|
|
313
|
-
*
|
|
314
|
-
* @param {string} name
|
|
315
|
-
* @param {string} [type]
|
|
316
|
-
* @param {string} [description]
|
|
317
|
-
* @return {Route}
|
|
318
|
-
*/
|
|
319
|
-
param(name, type = 'string', description = '') {
|
|
320
|
-
if (!this.#swaggerOptions.params) {
|
|
321
|
-
this.#swaggerOptions.params = {}
|
|
322
|
-
this.#swaggerOptions.params.type = 'object'
|
|
323
|
-
this.#swaggerOptions.params.properties = {}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
this.#swaggerOptions.params.properties[name] = { type, description }
|
|
327
|
-
|
|
328
|
-
return this
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Set query string for the route swagger docs.
|
|
333
|
-
*
|
|
334
|
-
* @param {string} name
|
|
335
|
-
* @param {string} [type]
|
|
336
|
-
* @param {string} [description]
|
|
337
|
-
* @return {Route}
|
|
338
|
-
*/
|
|
339
|
-
queryString(name, type = 'string', description = '') {
|
|
340
|
-
if (!this.#swaggerOptions.queryString) {
|
|
341
|
-
this.#swaggerOptions.querystring = {}
|
|
342
|
-
this.#swaggerOptions.querystring.type = 'object'
|
|
343
|
-
this.#swaggerOptions.querystring.properties = {}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
this.#swaggerOptions.querystring.properties[name] = { type, description }
|
|
347
|
-
|
|
348
|
-
return this
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Set response for the route swagger docs.
|
|
353
|
-
*
|
|
354
|
-
* @param {number|any} statusCode
|
|
355
|
-
* @param {any} [response]
|
|
356
|
-
* @return {Route}
|
|
357
|
-
*/
|
|
358
|
-
response(statusCode, response) {
|
|
359
|
-
if (!this.#swaggerOptions.response) {
|
|
360
|
-
this.#swaggerOptions.response = {}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
this.#swaggerOptions.response[statusCode] = response
|
|
364
|
-
|
|
365
|
-
return this
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
toJSON() {
|
|
369
|
-
const json = {
|
|
370
|
-
url: this.#getUrl(),
|
|
371
|
-
methods: this.#methods,
|
|
372
|
-
middlewares: this.#routeMiddlewares,
|
|
373
|
-
helmetOptions: this.#helmetOptions,
|
|
374
|
-
swaggerOptions: this.#swaggerOptions,
|
|
375
|
-
rateLimitOptions: this.#rateLimitOptions,
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (Is.String(this.#handler)) {
|
|
379
|
-
const [controller, method] = this.#handler.split('.')
|
|
380
|
-
|
|
381
|
-
const dependency = ioc.safeUse(`App/Http/Controllers/${controller}`)
|
|
382
|
-
|
|
383
|
-
if (!dependency[method]) {
|
|
384
|
-
throw new UndefinedMethodException(method, controller)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
json.handler = dependency[method].bind(dependency)
|
|
388
|
-
} else {
|
|
389
|
-
json.handler = this.#handler
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
return json
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Get the route url parsed with prefixes.
|
|
397
|
-
*
|
|
398
|
-
* @return {string}
|
|
399
|
-
*/
|
|
400
|
-
#getUrl() {
|
|
401
|
-
const url = removeSlashes(this.#url)
|
|
402
|
-
|
|
403
|
-
const prefix = this.#prefixes
|
|
404
|
-
.slice()
|
|
405
|
-
.reverse()
|
|
406
|
-
.map(p => removeSlashes(p))
|
|
407
|
-
.join('')
|
|
408
|
-
|
|
409
|
-
return prefix ? `${prefix}${url === '/' ? '' : url}` : url
|
|
410
|
-
}
|
|
411
|
-
}
|
package/src/Router/RouteGroup.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
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
|
-
|
|
10
|
-
import { RouteResource } from '#src/Router/RouteResource'
|
|
11
|
-
|
|
12
|
-
export class RouteGroup {
|
|
13
|
-
/**
|
|
14
|
-
* All routes registered in the group.
|
|
15
|
-
*
|
|
16
|
-
* @type {(Route | RouteResource | RouteGroup)[]}
|
|
17
|
-
*/
|
|
18
|
-
routes
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Creates a new instance of RouteGroup.
|
|
22
|
-
*
|
|
23
|
-
* @param {(Route | RouteResource | RouteGroup)[]} routes
|
|
24
|
-
*/
|
|
25
|
-
constructor(routes) {
|
|
26
|
-
this.routes = routes
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Set a prefix for the route group.
|
|
31
|
-
*
|
|
32
|
-
* @param {string} prefix
|
|
33
|
-
* @return {RouteGroup}
|
|
34
|
-
*/
|
|
35
|
-
prefix(prefix) {
|
|
36
|
-
this.routes.forEach(route => this.#invoke(route, 'prefix', [prefix]))
|
|
37
|
-
|
|
38
|
-
return this
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Set a middleware for the route group.
|
|
43
|
-
*
|
|
44
|
-
* @param {string|any} middleware
|
|
45
|
-
* @param {'handle'|'intercept'|'terminate'} type
|
|
46
|
-
* @param {boolean} prepend
|
|
47
|
-
* @return {RouteGroup}
|
|
48
|
-
*/
|
|
49
|
-
middleware(middleware, type = 'handle', prepend = false) {
|
|
50
|
-
this.routes.forEach(route => {
|
|
51
|
-
this.#invoke(route, 'middleware', [middleware, type, prepend])
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
return this
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Set up helmet options for route group.
|
|
59
|
-
*
|
|
60
|
-
* @param {any} options
|
|
61
|
-
* @return {RouteGroup}
|
|
62
|
-
*/
|
|
63
|
-
helmet(options) {
|
|
64
|
-
this.routes.forEach(route => {
|
|
65
|
-
this.#invoke(route, 'helmet', [options, false])
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
return this
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Set up swagger options for route group.
|
|
73
|
-
*
|
|
74
|
-
* @param {any} options
|
|
75
|
-
* @return {RouteGroup}
|
|
76
|
-
*/
|
|
77
|
-
swagger(options) {
|
|
78
|
-
this.routes.forEach(route => {
|
|
79
|
-
this.#invoke(route, 'swagger', [options, false])
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
return this
|
|
83
|
-
}
|
|
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
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Invoke a method from route.
|
|
101
|
-
*
|
|
102
|
-
* @param {import('./Route.js').Route|RouteResource|RouteGroup} route
|
|
103
|
-
* @param {string} method
|
|
104
|
-
* @param {any[]} params
|
|
105
|
-
*/
|
|
106
|
-
#invoke(route, method, params) {
|
|
107
|
-
if (route instanceof RouteResource) {
|
|
108
|
-
route.routes.forEach(child => this.#invoke(child, method, params))
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (route instanceof RouteGroup) {
|
|
113
|
-
route.routes.forEach(child => this.#invoke(child, method, params))
|
|
114
|
-
return
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
route[method](...params)
|
|
118
|
-
}
|
|
119
|
-
}
|