@athenna/http 1.3.9 → 1.4.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "1.3.9",
3
+ "version": "1.4.2",
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>",
@@ -41,7 +41,10 @@
41
41
  "./providers/ControllerProvider": "./src/Providers/ControllerProvider.js",
42
42
  "./providers/HttpRouteProvider": "./src/Providers/HttpRouteProvider.js",
43
43
  "./providers/HttpServerProvider": "./src/Providers/HttpServerProvider.js",
44
- "./providers/MiddlewareProvider": "./src/Providers/MiddlewareProvider.js"
44
+ "./providers/MiddlewareProvider": "./src/Providers/MiddlewareProvider.js",
45
+ "./commands/Route/List": "./src/Commands/Route/List.js",
46
+ "./commands/Make/Controller": "./src/Commands/Make/Controller.js",
47
+ "./commands/Make/Middleware": "./src/Commands/Make/Middleware.js"
45
48
  },
46
49
  "types": "./src/index.d.ts",
47
50
  "imports": {
@@ -49,6 +52,7 @@
49
52
  "#tests/*": "./tests/*.js"
50
53
  },
51
54
  "dependencies": {
55
+ "@athenna/artisan": "1.2.7",
52
56
  "@athenna/ioc": "1.2.0",
53
57
  "@athenna/logger": "1.2.5",
54
58
  "@secjs/utils": "1.9.4",
@@ -89,7 +93,11 @@
89
93
  "html"
90
94
  ],
91
95
  "report-dir": "./tests/Coverage",
92
- "check-coverage": true
96
+ "check-coverage": true,
97
+ "statements": "70",
98
+ "branches": "70",
99
+ "functions": "70",
100
+ "lines": "70"
93
101
  },
94
102
  "husky": {
95
103
  "hooks": {
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @athenna/artisan
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 { join } from 'node:path'
11
+ import { Folder, Path, String } from '@secjs/utils'
12
+ import { Artisan, Command, TemplateHelper } from '@athenna/artisan'
13
+
14
+ export class MakeController extends Command {
15
+ /**
16
+ * The name and signature of the console command.
17
+ */
18
+ signature = 'make:controller <name>'
19
+
20
+ /**
21
+ * The console command description.
22
+ */
23
+ description = 'Make a new controller file.'
24
+
25
+ /**
26
+ * Set additional flags in the commander instance.
27
+ * This method is executed when registering your command.
28
+ *
29
+ * @param {import('commander').Command} commander
30
+ * @return {import('commander').Command}
31
+ */
32
+ addFlags(commander) {
33
+ return commander.option(
34
+ '--no-lint',
35
+ 'Do not run eslint in the controller.',
36
+ true,
37
+ )
38
+ }
39
+
40
+ /**
41
+ * Execute the console command.
42
+ *
43
+ * @param {string} name
44
+ * @param {any} options
45
+ * @return {Promise<void>}
46
+ */
47
+ async handle(name, options) {
48
+ TemplateHelper.setTemplatesFolder(
49
+ new Folder(join(__dirname, '..', '..', '..', 'templates')).loadSync(),
50
+ )
51
+
52
+ const resource = 'Controller'
53
+ const subPath = Path.app(`Http/${String.pluralize(resource)}`)
54
+
55
+ this.simpleLog(
56
+ `[ MAKING ${resource.toUpperCase()} ]\n`,
57
+ 'rmNewLineStart',
58
+ 'bold',
59
+ 'green',
60
+ )
61
+
62
+ const file = await TemplateHelper.getResourceFile(name, resource, subPath)
63
+
64
+ this.success(`${resource} ({yellow} "${file.name}") successfully created.`)
65
+
66
+ if (options.lint) {
67
+ await Artisan.call(`eslint:fix ${file.path} --resource ${resource}`)
68
+ }
69
+
70
+ TemplateHelper.setOriginalTemplatesFolder()
71
+ }
72
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @athenna/artisan
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 { join } from 'node:path'
11
+ import { Folder, Path, String } from '@secjs/utils'
12
+ import { Artisan, Command, TemplateHelper } from '@athenna/artisan'
13
+
14
+ export class MakeMiddleware extends Command {
15
+ /**
16
+ * The name and signature of the console command.
17
+ */
18
+ signature = 'make:middleware <name>'
19
+
20
+ /**
21
+ * The console command description.
22
+ */
23
+ description = 'Make a new middleware file.'
24
+
25
+ /**
26
+ * Set additional flags in the commander instance.
27
+ * This method is executed when registering your command.
28
+ *
29
+ * @param {import('commander').Command} commander
30
+ * @return {import('commander').Command}
31
+ */
32
+ addFlags(commander) {
33
+ return commander
34
+ .option(
35
+ '--no-register',
36
+ 'Do not register the middleware inside Kernel.',
37
+ true,
38
+ )
39
+ .option('--no-lint', 'Do not run eslint in the middleware.', true)
40
+ }
41
+
42
+ /**
43
+ * Execute the console command.
44
+ *
45
+ * @param {string} name
46
+ * @param {any} options
47
+ * @return {Promise<void>}
48
+ */
49
+ async handle(name, options) {
50
+ TemplateHelper.setTemplatesFolder(
51
+ new Folder(join(__dirname, '..', '..', '..', 'templates')).loadSync(),
52
+ )
53
+
54
+ const resource = 'Middleware'
55
+ const subPath = Path.app(`Http/${String.pluralize(resource)}`)
56
+
57
+ this.simpleLog(
58
+ `[ MAKING ${resource.toUpperCase()} ]\n`,
59
+ 'rmNewLineStart',
60
+ 'bold',
61
+ 'green',
62
+ )
63
+
64
+ const file = await TemplateHelper.getResourceFile(name, resource, subPath)
65
+
66
+ this.success(`${resource} ({yellow} "${file.name}") successfully created.`)
67
+
68
+ if (options.lint) {
69
+ await Artisan.call(`eslint:fix ${file.path} --resource ${resource}`)
70
+ }
71
+
72
+ if (options.register) {
73
+ await TemplateHelper.replaceObjectProperty(
74
+ Path.http('Kernel.js'),
75
+ 'namedMiddlewares =',
76
+ file.name,
77
+ `#app/Http/Middlewares/${file.name}`,
78
+ )
79
+ }
80
+
81
+ TemplateHelper.setOriginalTemplatesFolder()
82
+ }
83
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @athenna/artisan
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 { pathToFileURL } from 'node:url'
11
+ import { Command } from '@athenna/artisan'
12
+ import { Exec, Path, String } from '@secjs/utils'
13
+
14
+ export class RouteList extends Command {
15
+ /**
16
+ * The name and signature of the console command.
17
+ */
18
+ signature = 'route:list'
19
+
20
+ /**
21
+ * The console command description.
22
+ */
23
+ description = 'List all the routes of your application.'
24
+
25
+ /**
26
+ * Set additional flags in the commander instance.
27
+ * This method is executed when registering your command.
28
+ *
29
+ * @param {import('commander').Command} commander
30
+ * @return {import('commander').Command}
31
+ */
32
+ addFlags(commander) {
33
+ return commander.option(
34
+ '-m, --middleware',
35
+ 'List the middlewares of each route.',
36
+ false,
37
+ )
38
+ }
39
+
40
+ /**
41
+ * Execute the console command.
42
+ *
43
+ * @param {any} options
44
+ * @return {Promise<void>}
45
+ */
46
+ async handle(options) {
47
+ this.simpleLog('[ ROUTE LISTING ]', 'rmNewLineStart', 'bold', 'green')
48
+
49
+ const Route = ioc.safeUse('Athenna/Core/HttpRoute')
50
+
51
+ const Kernel = await Exec.getModule(
52
+ import(pathToFileURL(Path.http('Kernel.js')).href),
53
+ )
54
+
55
+ const kernel = new Kernel()
56
+
57
+ await kernel.registerCors()
58
+ await kernel.registerRateLimit()
59
+ await kernel.registerMiddlewares()
60
+ await kernel.registerErrorHandler()
61
+ await kernel.registerLogMiddleware()
62
+ await kernel.registerRequestIdMiddleware()
63
+
64
+ const routePath = Path.routes('http.js')
65
+
66
+ await import(routePath)
67
+ const routes = Route.listRoutes()
68
+
69
+ const header = ['Method', 'Route', 'Handler']
70
+ const rows = []
71
+
72
+ if (options.middleware) header.push('Middlewares')
73
+
74
+ routes.forEach(route => {
75
+ const row = [route.methods.join('|'), route.url, 'Closure']
76
+
77
+ if (options.middleware) {
78
+ let middlewares = ''
79
+
80
+ Object.keys(route.middlewares).forEach(key => {
81
+ if (route.middlewares[key].length) {
82
+ const number = route.middlewares[key].length
83
+
84
+ if (middlewares) {
85
+ middlewares = middlewares + '\n'
86
+ }
87
+
88
+ middlewares = middlewares + `${String.toPascalCase(key)}: ${number}`
89
+ }
90
+ })
91
+
92
+ if (!middlewares) middlewares = 'Not found'
93
+
94
+ row.push(middlewares)
95
+ }
96
+
97
+ rows.push(row)
98
+ })
99
+
100
+ this.logTable({ head: header }, ...rows)
101
+ }
102
+ }
@@ -64,11 +64,11 @@ export class HttpExceptionHandler {
64
64
  return
65
65
  }
66
66
 
67
- const prettyError = await new Exception(
68
- body.message,
69
- body.statusCode,
70
- body.code,
71
- ).prettify()
67
+ const exception = new Exception(body.message, body.statusCode, body.code)
68
+
69
+ exception.stack = body.stack
70
+
71
+ const prettyError = await exception.prettify()
72
72
 
73
73
  Log.channel('exception').error(prettyError.concat('\n'))
74
74
  }
@@ -0,0 +1,14 @@
1
+ export class HttpCommandsLoader {
2
+ /**
3
+ * Return all commands from http package.
4
+ *
5
+ * @return {any[]}
6
+ */
7
+ static loadCommands() {
8
+ return [
9
+ import('#src/Commands/Route/List.js'),
10
+ import('#src/Commands/Make/Controller.js'),
11
+ import('#src/Commands/Make/Middleware.js'),
12
+ ]
13
+ }
14
+ }
package/src/index.d.ts CHANGED
@@ -578,6 +578,15 @@ export interface ResponseContract {
578
578
  getFastifyResponse(): FastifyReply
579
579
  }
580
580
 
581
+ export class HttpCommandsLoader {
582
+ /**
583
+ * Return all commands from http package.
584
+ *
585
+ * @return {any[]}
586
+ */
587
+ static loadCommands(): any[]
588
+ }
589
+
581
590
  export interface NextContract {
582
591
  (...params: any[]): void
583
592
  }
package/src/index.js CHANGED
@@ -19,6 +19,11 @@ export * from './Facades/Server.js'
19
19
  export * from './Kernels/HttpKernel.js'
20
20
  export * from './Handlers/HttpExceptionHandler.js'
21
21
 
22
+ export * from './Commands/Route/List.js'
23
+ export * from './Commands/Make/Controller.js'
24
+ export * from './Commands/Make/Middleware.js'
25
+ export * from './Helpers/HttpCommandsLoader.js'
26
+
22
27
  export class Http {
23
28
  /**
24
29
  * Holds the fastify server instance.