@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
|
@@ -1,247 +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 { Route } from '#src/Router/Route'
|
|
11
|
-
import { Is, String } from '@athenna/common'
|
|
12
|
-
|
|
13
|
-
export class RouteResource {
|
|
14
|
-
/**
|
|
15
|
-
* All routes registered in the resource.
|
|
16
|
-
*
|
|
17
|
-
* @type {Route[]}
|
|
18
|
-
*/
|
|
19
|
-
routes
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The resource name.
|
|
23
|
-
*
|
|
24
|
-
* @type {string}
|
|
25
|
-
*/
|
|
26
|
-
#resource
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The resource controller.
|
|
30
|
-
*
|
|
31
|
-
* @type {any}
|
|
32
|
-
*/
|
|
33
|
-
#controller
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The resource name.
|
|
37
|
-
*
|
|
38
|
-
* @type {string}
|
|
39
|
-
*/
|
|
40
|
-
#resourceName
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Creates a new instance of RouteResource.
|
|
44
|
-
*
|
|
45
|
-
* @param {string} resource
|
|
46
|
-
* @param {any} controller
|
|
47
|
-
* @return {RouteResource}
|
|
48
|
-
*/
|
|
49
|
-
constructor(resource, controller) {
|
|
50
|
-
this.routes = []
|
|
51
|
-
|
|
52
|
-
this.#resource = resource
|
|
53
|
-
this.#controller = controller
|
|
54
|
-
|
|
55
|
-
this.#resourceName = this.#resource
|
|
56
|
-
.split('.')
|
|
57
|
-
.map(string => String.toSnakeCase(string))
|
|
58
|
-
.join('.')
|
|
59
|
-
|
|
60
|
-
this.#buildRoutes()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Set a middleware for the route group.
|
|
65
|
-
*
|
|
66
|
-
* @param {string|any} middleware
|
|
67
|
-
* @param {'handle'|'intercept'|'terminate'} type
|
|
68
|
-
* @return {RouteResource}
|
|
69
|
-
*/
|
|
70
|
-
middleware(middleware, type = 'handle') {
|
|
71
|
-
this.routes.forEach(route => route.middleware(middleware, type))
|
|
72
|
-
|
|
73
|
-
return this
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Register only the methods in the array.
|
|
78
|
-
*
|
|
79
|
-
* @param {string} names
|
|
80
|
-
* @return {RouteResource}
|
|
81
|
-
*/
|
|
82
|
-
only(...names) {
|
|
83
|
-
this.#filter(names, true).forEach(route => (route.deleted = true))
|
|
84
|
-
|
|
85
|
-
return this
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Register all methods except the methods in the array.
|
|
90
|
-
*
|
|
91
|
-
* @param {string} names
|
|
92
|
-
* @return {RouteResource}
|
|
93
|
-
*/
|
|
94
|
-
except(...names) {
|
|
95
|
-
this.#filter(names, false).forEach(route => (route.deleted = true))
|
|
96
|
-
|
|
97
|
-
return this
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Set up helmet options for route resource.
|
|
102
|
-
*
|
|
103
|
-
* @param {string|any} action
|
|
104
|
-
* @param {any} [options]
|
|
105
|
-
* @return {RouteResource}
|
|
106
|
-
*/
|
|
107
|
-
helmet(action, options) {
|
|
108
|
-
if (!options) {
|
|
109
|
-
this.routes.forEach(route => route.helmet(action))
|
|
110
|
-
|
|
111
|
-
return this
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const resourceName = `${this.#resourceName}.${action}`
|
|
115
|
-
|
|
116
|
-
this.routes.forEach(route => {
|
|
117
|
-
if (route.name !== resourceName) {
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
route.helmet(options)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
return this
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Set up swagger options for route resource method.
|
|
129
|
-
*
|
|
130
|
-
* @param {string|any} action
|
|
131
|
-
* @param {any} [options]
|
|
132
|
-
* @return {RouteResource}
|
|
133
|
-
*/
|
|
134
|
-
swagger(action, options) {
|
|
135
|
-
if (!options) {
|
|
136
|
-
this.routes.forEach(route => route.swagger(action))
|
|
137
|
-
|
|
138
|
-
return this
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const resourceName = `${this.#resourceName}.${action}`
|
|
142
|
-
|
|
143
|
-
this.routes.forEach(route => {
|
|
144
|
-
if (route.name !== resourceName) {
|
|
145
|
-
return
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
route.swagger(options)
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
return this
|
|
152
|
-
}
|
|
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
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Create the route.
|
|
183
|
-
*
|
|
184
|
-
* @param {string} url
|
|
185
|
-
* @param {string[]} methods
|
|
186
|
-
* @param {string} action
|
|
187
|
-
* @return {void}
|
|
188
|
-
*/
|
|
189
|
-
#makeRoute(url, methods, action) {
|
|
190
|
-
let handler = ''
|
|
191
|
-
|
|
192
|
-
if (Is.String(this.#controller)) {
|
|
193
|
-
handler = `${this.#controller}.${action}`
|
|
194
|
-
} else {
|
|
195
|
-
handler = this.#controller[action]
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const route = new Route(
|
|
199
|
-
url,
|
|
200
|
-
methods,
|
|
201
|
-
handler,
|
|
202
|
-
`${this.#resourceName}.${action}`,
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
this.routes.push(route)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Build all the routes of the resource.
|
|
210
|
-
*
|
|
211
|
-
* @return {void}
|
|
212
|
-
*/
|
|
213
|
-
#buildRoutes() {
|
|
214
|
-
this.#resource = this.#resource.replace(/^\//, '').replace(/\/$/, '')
|
|
215
|
-
|
|
216
|
-
const resourceTokens = this.#resource.split('.')
|
|
217
|
-
const mainResource = resourceTokens.pop()
|
|
218
|
-
|
|
219
|
-
const fullUrl = `${resourceTokens
|
|
220
|
-
.map(
|
|
221
|
-
string =>
|
|
222
|
-
`${string}/:${String.toSnakeCase(String.singularize(string))}_id`,
|
|
223
|
-
)
|
|
224
|
-
.join('/')}/${mainResource}`
|
|
225
|
-
|
|
226
|
-
this.#makeRoute(fullUrl, ['HEAD', 'GET'], 'index')
|
|
227
|
-
this.#makeRoute(fullUrl, ['POST'], 'store')
|
|
228
|
-
this.#makeRoute(`${fullUrl}/:id`, ['HEAD', 'GET'], 'show')
|
|
229
|
-
this.#makeRoute(`${fullUrl}/:id`, ['PUT', 'PATCH'], 'update')
|
|
230
|
-
this.#makeRoute(`${fullUrl}/:id`, ['DELETE'], 'delete')
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Filter the routes by name.
|
|
235
|
-
*
|
|
236
|
-
* @param {string[]} names
|
|
237
|
-
* @param {boolean} inverse
|
|
238
|
-
* @return {Route[]}
|
|
239
|
-
*/
|
|
240
|
-
#filter(names, inverse) {
|
|
241
|
-
return this.routes.filter(route => {
|
|
242
|
-
const match = names.find(name => route.name.endsWith(name))
|
|
243
|
-
|
|
244
|
-
return inverse ? !match : match
|
|
245
|
-
})
|
|
246
|
-
}
|
|
247
|
-
}
|
package/src/Router/Router.js
DELETED
|
@@ -1,313 +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 } from '@athenna/common'
|
|
11
|
-
|
|
12
|
-
import { Route } from '#src/Router/Route'
|
|
13
|
-
import { Server } from '#src/Facades/Server'
|
|
14
|
-
import { RouteGroup } from '#src/Router/RouteGroup'
|
|
15
|
-
import { RouteResource } from '#src/Router/RouteResource'
|
|
16
|
-
|
|
17
|
-
export class Router {
|
|
18
|
-
/**
|
|
19
|
-
* All routes registered.
|
|
20
|
-
*
|
|
21
|
-
* @type {(Route | RouteResource | RouteGroup)[]}
|
|
22
|
-
*/
|
|
23
|
-
routes
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Route groups opened.
|
|
27
|
-
*
|
|
28
|
-
* @type {RouteGroup[]}
|
|
29
|
-
*/
|
|
30
|
-
#openedGroups
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The controller instance.
|
|
34
|
-
*
|
|
35
|
-
* @type {any}
|
|
36
|
-
*/
|
|
37
|
-
#controllerInstance
|
|
38
|
-
|
|
39
|
-
constructor() {
|
|
40
|
-
this.routes = []
|
|
41
|
-
this.#openedGroups = []
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* List the routes registered.
|
|
46
|
-
*
|
|
47
|
-
* @return {any}
|
|
48
|
-
*/
|
|
49
|
-
listRoutes() {
|
|
50
|
-
return this.toRoutesJSON(this.routes)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Set the controller instance.
|
|
55
|
-
*
|
|
56
|
-
* @param {any} controller
|
|
57
|
-
* @return {Router}
|
|
58
|
-
*/
|
|
59
|
-
controller(controller) {
|
|
60
|
-
this.#controllerInstance = controller
|
|
61
|
-
|
|
62
|
-
return this
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Register a new route.
|
|
67
|
-
*
|
|
68
|
-
* @param {string} url
|
|
69
|
-
* @param {string[]} methods
|
|
70
|
-
* @param {string|any} handler
|
|
71
|
-
* @return {Route}
|
|
72
|
-
*/
|
|
73
|
-
route(url, methods, handler) {
|
|
74
|
-
if (
|
|
75
|
-
this.#controllerInstance &&
|
|
76
|
-
Is.String(handler) &&
|
|
77
|
-
!handler.includes('.')
|
|
78
|
-
) {
|
|
79
|
-
handler = this.#controllerInstance[handler]
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const route = new Route(url, methods, handler)
|
|
83
|
-
const openedGroup = this.#getRecentGroup()
|
|
84
|
-
|
|
85
|
-
if (openedGroup) {
|
|
86
|
-
openedGroup.routes.push(route)
|
|
87
|
-
} else {
|
|
88
|
-
this.routes.push(route)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return route
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Register a new vanila route using fastify options
|
|
96
|
-
* directly.
|
|
97
|
-
*
|
|
98
|
-
* @param {import('fastify').RouteOptions} options
|
|
99
|
-
* @return {void}
|
|
100
|
-
*/
|
|
101
|
-
vanilaRoute(options) {
|
|
102
|
-
Server.getFastify().route(options)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Creates a new route group.
|
|
107
|
-
*
|
|
108
|
-
* @param {() => void} callback
|
|
109
|
-
* @return {RouteGroup}
|
|
110
|
-
*/
|
|
111
|
-
group(callback) {
|
|
112
|
-
const group = new RouteGroup([])
|
|
113
|
-
|
|
114
|
-
const openedGroup = this.#getRecentGroup()
|
|
115
|
-
|
|
116
|
-
if (openedGroup) {
|
|
117
|
-
openedGroup.routes.push(group)
|
|
118
|
-
} else {
|
|
119
|
-
this.routes.push(group)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this.#openedGroups.push(group)
|
|
123
|
-
callback()
|
|
124
|
-
|
|
125
|
-
this.#openedGroups.pop()
|
|
126
|
-
|
|
127
|
-
return group
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Creates a new route resource.
|
|
132
|
-
*
|
|
133
|
-
* @param {string} resource
|
|
134
|
-
* @param {any} controller
|
|
135
|
-
* @return {RouteResource}
|
|
136
|
-
*/
|
|
137
|
-
resource(resource, controller) {
|
|
138
|
-
const resourceInstance = new RouteResource(resource, controller)
|
|
139
|
-
const openedGroup = this.#getRecentGroup()
|
|
140
|
-
|
|
141
|
-
if (openedGroup) {
|
|
142
|
-
openedGroup.routes.push(resourceInstance)
|
|
143
|
-
} else {
|
|
144
|
-
this.routes.push(resourceInstance)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return resourceInstance
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Creates a new redirect route.
|
|
152
|
-
*
|
|
153
|
-
* @param {string} url
|
|
154
|
-
* @param {string} redirectTo
|
|
155
|
-
* @param {number} [status]
|
|
156
|
-
* @return {Route}
|
|
157
|
-
*/
|
|
158
|
-
redirect(url, redirectTo, status = 302) {
|
|
159
|
-
return this.any(url, ({ response }) =>
|
|
160
|
-
response.redirectTo(redirectTo, status),
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Register a new get method route.
|
|
166
|
-
*
|
|
167
|
-
* @param {string} url
|
|
168
|
-
* @param {string|any} handler
|
|
169
|
-
* @return {Route}
|
|
170
|
-
*/
|
|
171
|
-
get(url, handler) {
|
|
172
|
-
return this.route(url, ['GET', 'HEAD'], handler)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Register a new head method route.
|
|
177
|
-
*
|
|
178
|
-
* @param {string} url
|
|
179
|
-
* @param {string|any} handler
|
|
180
|
-
* @return {Route}
|
|
181
|
-
*/
|
|
182
|
-
head(url, handler) {
|
|
183
|
-
return this.route(url, ['HEAD'], handler)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Register a new post method route.
|
|
188
|
-
*
|
|
189
|
-
* @param {string} url
|
|
190
|
-
* @param {string|any} handler
|
|
191
|
-
* @return {Route}
|
|
192
|
-
*/
|
|
193
|
-
post(url, handler) {
|
|
194
|
-
return this.route(url, ['POST'], handler)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Register a new put method route.
|
|
199
|
-
*
|
|
200
|
-
* @param {string} url
|
|
201
|
-
* @param {string|any} handler
|
|
202
|
-
* @return {Route}
|
|
203
|
-
*/
|
|
204
|
-
put(url, handler) {
|
|
205
|
-
return this.route(url, ['PUT'], handler)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Register a new patch method route.
|
|
210
|
-
*
|
|
211
|
-
* @param {string} url
|
|
212
|
-
* @param {string|any} handler
|
|
213
|
-
* @return {Route}
|
|
214
|
-
*/
|
|
215
|
-
patch(url, handler) {
|
|
216
|
-
return this.route(url, ['PATCH'], handler)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Register a new delete method route.
|
|
221
|
-
*
|
|
222
|
-
* @param {string} url
|
|
223
|
-
* @param {string|any} handler
|
|
224
|
-
* @return {Route}
|
|
225
|
-
*/
|
|
226
|
-
delete(url, handler) {
|
|
227
|
-
return this.route(url, ['HEAD'], handler)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Register a new options method route.
|
|
232
|
-
*
|
|
233
|
-
* @param {string} url
|
|
234
|
-
* @param {string|any} handler
|
|
235
|
-
* @return {Route}
|
|
236
|
-
*/
|
|
237
|
-
options(url, handler) {
|
|
238
|
-
return this.route(url, ['OPTIONS'], handler)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Register a new route with all methods.
|
|
243
|
-
*
|
|
244
|
-
* @param {string} url
|
|
245
|
-
* @param {string|any} handler
|
|
246
|
-
* @return {Route}
|
|
247
|
-
*/
|
|
248
|
-
any(url, handler) {
|
|
249
|
-
return this.route(
|
|
250
|
-
url,
|
|
251
|
-
['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
|
|
252
|
-
handler,
|
|
253
|
-
)
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Register all the routes inside the Server.
|
|
258
|
-
*
|
|
259
|
-
* @param {string} url
|
|
260
|
-
* @param {string|any} handler
|
|
261
|
-
* @return {Route}
|
|
262
|
-
*/
|
|
263
|
-
register() {
|
|
264
|
-
this.toRoutesJSON(this.routes).forEach(route => {
|
|
265
|
-
route.methods.forEach(method => {
|
|
266
|
-
Server[method.toLowerCase()](
|
|
267
|
-
route.url,
|
|
268
|
-
route.handler,
|
|
269
|
-
route.middlewares,
|
|
270
|
-
{
|
|
271
|
-
helmet: route.helmetOptions,
|
|
272
|
-
schema: route.swaggerOptions,
|
|
273
|
-
config: { rateLimit: route.rateLimitOptions },
|
|
274
|
-
},
|
|
275
|
-
)
|
|
276
|
-
})
|
|
277
|
-
})
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Transform the routes to JSON Object.
|
|
282
|
-
*
|
|
283
|
-
* @param {any[]} [routes]
|
|
284
|
-
*/
|
|
285
|
-
toRoutesJSON(routes) {
|
|
286
|
-
return routes.reduce((list, route) => {
|
|
287
|
-
if (route instanceof RouteGroup) {
|
|
288
|
-
list = list.concat(this.toRoutesJSON(route.routes))
|
|
289
|
-
return list
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (route instanceof RouteResource) {
|
|
293
|
-
list = list.concat(this.toRoutesJSON(route.routes))
|
|
294
|
-
return list
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (!route.deleted) {
|
|
298
|
-
list.push(route.toJSON())
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return list
|
|
302
|
-
}, [])
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Returns the recent group.
|
|
307
|
-
*
|
|
308
|
-
* @return {RouteGroup}
|
|
309
|
-
*/
|
|
310
|
-
#getRecentGroup() {
|
|
311
|
-
return this.#openedGroups[this.#openedGroups.length - 1]
|
|
312
|
-
}
|
|
313
|
-
}
|
|
@@ -1,17 +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
|
-
/**
|
|
11
|
-
* Verify if the middleware implements the middleware contract.
|
|
12
|
-
*
|
|
13
|
-
* @param {any} object
|
|
14
|
-
*/
|
|
15
|
-
export function isMiddlewareContract(object) {
|
|
16
|
-
return 'handle' in object || 'intercept' in object || 'terminate' in object
|
|
17
|
-
}
|
|
@@ -1,30 +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 } from '@athenna/common'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Remove additional slashes from url.
|
|
14
|
-
*
|
|
15
|
-
* @param {string|string[]} url
|
|
16
|
-
* @return {string|string[]}
|
|
17
|
-
*/
|
|
18
|
-
export function removeSlashes(url) {
|
|
19
|
-
if (url === '/') {
|
|
20
|
-
return url
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const matcher = url => `/${url.replace(/^\//, '').replace(/\/$/, '')}`
|
|
24
|
-
|
|
25
|
-
if (Is.Array(url)) {
|
|
26
|
-
return url.map(u => matcher(u))
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return matcher(url)
|
|
30
|
-
}
|