@athenna/http 3.0.10 → 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 -76
  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 -223
  120. package/src/Context/Response.js +0 -137
  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 -417
  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 -1331
  151. package/src/index.js +0 -377
@@ -1,417 +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
- if (!response) {
364
- this.#swaggerOptions.response.default = response
365
-
366
- return this
367
- }
368
-
369
- this.#swaggerOptions.response[statusCode] = response
370
-
371
- return this
372
- }
373
-
374
- toJSON() {
375
- const json = {
376
- url: this.#getUrl(),
377
- methods: this.#methods,
378
- middlewares: this.#routeMiddlewares,
379
- helmetOptions: this.#helmetOptions,
380
- swaggerOptions: this.#swaggerOptions,
381
- rateLimitOptions: this.#rateLimitOptions,
382
- }
383
-
384
- if (Is.String(this.#handler)) {
385
- const [controller, method] = this.#handler.split('.')
386
-
387
- const dependency = ioc.safeUse(`App/Http/Controllers/${controller}`)
388
-
389
- if (!dependency[method]) {
390
- throw new UndefinedMethodException(method, controller)
391
- }
392
-
393
- json.handler = dependency[method].bind(dependency)
394
- } else {
395
- json.handler = this.#handler
396
- }
397
-
398
- return json
399
- }
400
-
401
- /**
402
- * Get the route url parsed with prefixes.
403
- *
404
- * @return {string}
405
- */
406
- #getUrl() {
407
- const url = removeSlashes(this.#url)
408
-
409
- const prefix = this.#prefixes
410
- .slice()
411
- .reverse()
412
- .map(p => removeSlashes(p))
413
- .join('')
414
-
415
- return prefix ? `${prefix}${url === '/' ? '' : url}` : url
416
- }
417
- }
@@ -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
- }