@athenna/http 3.2.1 → 3.4.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.
@@ -12,4 +12,16 @@ export declare class MakeControllerCommand extends BaseCommand {
12
12
  static signature(): string;
13
13
  static description(): string;
14
14
  handle(): Promise<void>;
15
+ /**
16
+ * Get the file path where it will be generated.
17
+ */
18
+ private getFilePath;
19
+ /**
20
+ * Get the destination path for the file that will be generated.
21
+ */
22
+ private getDestinationPath;
23
+ /**
24
+ * Get the import path that should be registered in RC file.
25
+ */
26
+ private getImportPath;
15
27
  }
@@ -13,6 +13,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
13
13
  return c > 3 && r && Object.defineProperty(target, key, r), r;
14
14
  };
15
15
  import { Path } from '@athenna/common';
16
+ import { sep, resolve, isAbsolute } from 'node:path';
16
17
  import { BaseCommand, Argument } from '@athenna/artisan';
17
18
  export class MakeControllerCommand extends BaseCommand {
18
19
  name;
@@ -25,15 +26,41 @@ export class MakeControllerCommand extends BaseCommand {
25
26
  async handle() {
26
27
  this.logger.simple('({bold,green} [ MAKING CONTROLLER ])\n');
27
28
  const file = await this.generator
28
- .path(Path.http(`Controllers/${this.name}.${Path.ext()}`))
29
+ .path(this.getFilePath())
29
30
  .template('controller')
30
31
  .setNameProperties(true)
31
32
  .make();
32
33
  this.logger.success(`Controller ({yellow} "${file.name}") successfully created.`);
33
- const importPath = `#app/Http/Controllers/${file.name}`;
34
+ const importPath = this.getImportPath(file.name);
34
35
  await this.rc.pushTo('controllers', importPath).save();
35
36
  this.logger.success(`Athenna RC updated: ({dim,yellow} [ controllers += "${importPath}" ])`);
36
37
  }
38
+ /**
39
+ * Get the file path where it will be generated.
40
+ */
41
+ getFilePath() {
42
+ return this.getDestinationPath().concat(`${sep}${this.name}.${Path.ext()}`);
43
+ }
44
+ /**
45
+ * Get the destination path for the file that will be generated.
46
+ */
47
+ getDestinationPath() {
48
+ let destination = Config.get('rc.commandsManifest.make:controller.destination', Path.http('Controllers'));
49
+ if (!isAbsolute(destination)) {
50
+ destination = resolve(Path.pwd(), destination);
51
+ }
52
+ return destination;
53
+ }
54
+ /**
55
+ * Get the import path that should be registered in RC file.
56
+ */
57
+ getImportPath(fileName) {
58
+ const destination = this.getDestinationPath();
59
+ return `${destination
60
+ .replace(Path.pwd(), '')
61
+ .replace(/\\/g, '/')
62
+ .replace('/', '#')}/${fileName}`;
63
+ }
37
64
  }
38
65
  __decorate([
39
66
  Argument({
@@ -12,4 +12,16 @@ export declare class MakeInterceptorCommand extends BaseCommand {
12
12
  static signature(): string;
13
13
  static description(): string;
14
14
  handle(): Promise<void>;
15
+ /**
16
+ * Get the file path where it will be generated.
17
+ */
18
+ private getFilePath;
19
+ /**
20
+ * Get the destination path for the file that will be generated.
21
+ */
22
+ private getDestinationPath;
23
+ /**
24
+ * Get the import path that should be registered in RC file.
25
+ */
26
+ private getImportPath;
15
27
  }
@@ -13,6 +13,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
13
13
  return c > 3 && r && Object.defineProperty(target, key, r), r;
14
14
  };
15
15
  import { Path } from '@athenna/common';
16
+ import { sep, resolve, isAbsolute } from 'node:path';
16
17
  import { BaseCommand, Argument } from '@athenna/artisan';
17
18
  export class MakeInterceptorCommand extends BaseCommand {
18
19
  name;
@@ -25,15 +26,41 @@ export class MakeInterceptorCommand extends BaseCommand {
25
26
  async handle() {
26
27
  this.logger.simple('({bold,green} [ MAKING INTERCEPTOR ])\n');
27
28
  const file = await this.generator
28
- .path(Path.http(`Interceptors/${this.name}.${Path.ext()}`))
29
+ .path(this.getFilePath())
29
30
  .template('interceptor')
30
31
  .setNameProperties(true)
31
32
  .make();
32
33
  this.logger.success(`Interceptor ({yellow} "${file.name}") successfully created.`);
33
- const importPath = `#app/Http/Interceptors/${file.name}`;
34
+ const importPath = this.getImportPath(file.name);
34
35
  await this.rc.pushTo('middlewares', importPath).save();
35
36
  this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
37
  }
38
+ /**
39
+ * Get the file path where it will be generated.
40
+ */
41
+ getFilePath() {
42
+ return this.getDestinationPath().concat(`${sep}${this.name}.${Path.ext()}`);
43
+ }
44
+ /**
45
+ * Get the destination path for the file that will be generated.
46
+ */
47
+ getDestinationPath() {
48
+ let destination = Config.get('rc.commandsManifest.make:interceptor.destination', Path.http('Interceptors'));
49
+ if (!isAbsolute(destination)) {
50
+ destination = resolve(Path.pwd(), destination);
51
+ }
52
+ return destination;
53
+ }
54
+ /**
55
+ * Get the import path that should be registered in RC file.
56
+ */
57
+ getImportPath(fileName) {
58
+ const destination = this.getDestinationPath();
59
+ return `${destination
60
+ .replace(Path.pwd(), '')
61
+ .replace(/\\/g, '/')
62
+ .replace('/', '#')}/${fileName}`;
63
+ }
37
64
  }
38
65
  __decorate([
39
66
  Argument({
@@ -12,4 +12,16 @@ export declare class MakeMiddlewareCommand extends BaseCommand {
12
12
  static signature(): string;
13
13
  static description(): string;
14
14
  handle(): Promise<void>;
15
+ /**
16
+ * Get the file path where it will be generated.
17
+ */
18
+ private getFilePath;
19
+ /**
20
+ * Get the destination path for the file that will be generated.
21
+ */
22
+ private getDestinationPath;
23
+ /**
24
+ * Get the import path that should be registered in RC file.
25
+ */
26
+ private getImportPath;
15
27
  }
@@ -13,6 +13,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
13
13
  return c > 3 && r && Object.defineProperty(target, key, r), r;
14
14
  };
15
15
  import { Path } from '@athenna/common';
16
+ import { sep, resolve, isAbsolute } from 'node:path';
16
17
  import { BaseCommand, Argument } from '@athenna/artisan';
17
18
  export class MakeMiddlewareCommand extends BaseCommand {
18
19
  name;
@@ -25,15 +26,41 @@ export class MakeMiddlewareCommand extends BaseCommand {
25
26
  async handle() {
26
27
  this.logger.simple('({bold,green} [ MAKING MIDDLEWARE ])\n');
27
28
  const file = await this.generator
28
- .path(Path.http(`Middlewares/${this.name}.${Path.ext()}`))
29
+ .path(this.getFilePath())
29
30
  .template('middleware')
30
31
  .setNameProperties(true)
31
32
  .make();
32
33
  this.logger.success(`Middleware ({yellow} "${file.name}") successfully created.`);
33
- const importPath = `#app/Http/Middlewares/${file.name}`;
34
+ const importPath = this.getImportPath(file.name);
34
35
  await this.rc.pushTo('middlewares', importPath).save();
35
36
  this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
37
  }
38
+ /**
39
+ * Get the file path where it will be generated.
40
+ */
41
+ getFilePath() {
42
+ return this.getDestinationPath().concat(`${sep}${this.name}.${Path.ext()}`);
43
+ }
44
+ /**
45
+ * Get the destination path for the file that will be generated.
46
+ */
47
+ getDestinationPath() {
48
+ let destination = Config.get('rc.commandsManifest.make:middleware.destination', Path.http('Middlewares'));
49
+ if (!isAbsolute(destination)) {
50
+ destination = resolve(Path.pwd(), destination);
51
+ }
52
+ return destination;
53
+ }
54
+ /**
55
+ * Get the import path that should be registered in RC file.
56
+ */
57
+ getImportPath(fileName) {
58
+ const destination = this.getDestinationPath();
59
+ return `${destination
60
+ .replace(Path.pwd(), '')
61
+ .replace(/\\/g, '/')
62
+ .replace('/', '#')}/${fileName}`;
63
+ }
37
64
  }
38
65
  __decorate([
39
66
  Argument({
@@ -12,4 +12,16 @@ export declare class MakeTerminatorCommand extends BaseCommand {
12
12
  static signature(): string;
13
13
  static description(): string;
14
14
  handle(): Promise<void>;
15
+ /**
16
+ * Get the file path where it will be generated.
17
+ */
18
+ private getFilePath;
19
+ /**
20
+ * Get the destination path for the file that will be generated.
21
+ */
22
+ private getDestinationPath;
23
+ /**
24
+ * Get the import path that should be registered in RC file.
25
+ */
26
+ private getImportPath;
15
27
  }
@@ -13,6 +13,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
13
13
  return c > 3 && r && Object.defineProperty(target, key, r), r;
14
14
  };
15
15
  import { Path } from '@athenna/common';
16
+ import { sep, resolve, isAbsolute } from 'node:path';
16
17
  import { BaseCommand, Argument } from '@athenna/artisan';
17
18
  export class MakeTerminatorCommand extends BaseCommand {
18
19
  name;
@@ -25,15 +26,41 @@ export class MakeTerminatorCommand extends BaseCommand {
25
26
  async handle() {
26
27
  this.logger.simple('({bold,green} [ MAKING TERMINATOR ])\n');
27
28
  const file = await this.generator
28
- .path(Path.http(`Terminators/${this.name}.${Path.ext()}`))
29
+ .path(this.getFilePath())
29
30
  .template('terminator')
30
31
  .setNameProperties(true)
31
32
  .make();
32
33
  this.logger.success(`Terminator ({yellow} "${file.name}") successfully created.`);
33
- const importPath = `#app/Http/Terminators/${file.name}`;
34
+ const importPath = this.getImportPath(file.name);
34
35
  await this.rc.pushTo('middlewares', importPath).save();
35
36
  this.logger.success(`Athenna RC updated: ({dim,yellow} [ middlewares += "${importPath}" ])`);
36
37
  }
38
+ /**
39
+ * Get the file path where it will be generated.
40
+ */
41
+ getFilePath() {
42
+ return this.getDestinationPath().concat(`${sep}${this.name}.${Path.ext()}`);
43
+ }
44
+ /**
45
+ * Get the destination path for the file that will be generated.
46
+ */
47
+ getDestinationPath() {
48
+ let destination = Config.get('rc.commandsManifest.make:terminator.destination', Path.http('Terminators'));
49
+ if (!isAbsolute(destination)) {
50
+ destination = resolve(Path.pwd(), destination);
51
+ }
52
+ return destination;
53
+ }
54
+ /**
55
+ * Get the import path that should be registered in RC file.
56
+ */
57
+ getImportPath(fileName) {
58
+ const destination = this.getDestinationPath();
59
+ return `${destination
60
+ .replace(Path.pwd(), '')
61
+ .replace(/\\/g, '/')
62
+ .replace('/', '#')}/${fileName}`;
63
+ }
37
64
  }
38
65
  __decorate([
39
66
  Argument({
@@ -8,8 +8,15 @@
8
8
  */
9
9
  import { BaseCommand } from '@athenna/artisan';
10
10
  export declare class RouteListCommand extends BaseCommand {
11
- routeFilePath: any;
12
11
  static signature(): string;
13
12
  static description(): string;
14
13
  handle(): Promise<void>;
14
+ /**
15
+ * Resolve the http routes file.
16
+ */
17
+ private resolveRoute;
18
+ /**
19
+ * Get the http kernel module from RC file or resolve the default one.
20
+ */
21
+ private getHttpKernel;
15
22
  }
@@ -11,7 +11,6 @@ import { Module } from '@athenna/common';
11
11
  import { BaseCommand } from '@athenna/artisan';
12
12
  import { Route, HttpKernel, HttpRouteProvider, HttpServerProvider } from '#src';
13
13
  export class RouteListCommand extends BaseCommand {
14
- routeFilePath = Env('HTTP_ROUTE_FILE_PATH', Path.routes('http.js'));
15
14
  static signature() {
16
15
  return 'route:list';
17
16
  }
@@ -22,10 +21,10 @@ export class RouteListCommand extends BaseCommand {
22
21
  this.logger.simple('({bold,green} [ LISTING ROUTES ])\n');
23
22
  new HttpServerProvider().register();
24
23
  new HttpRouteProvider().register();
25
- const kernel = new HttpKernel();
24
+ const kernel = new (await this.getHttpKernel())();
26
25
  await kernel.registerControllers();
27
26
  await kernel.registerMiddlewares();
28
- await Module.resolve(this.routeFilePath, Config.get('rc.meta'));
27
+ await this.resolveRoute();
29
28
  const routes = Route.list();
30
29
  const table = this.logger.table();
31
30
  table.head('Methods', 'Route', 'Name', 'Handler');
@@ -42,4 +41,19 @@ export class RouteListCommand extends BaseCommand {
42
41
  });
43
42
  table.render();
44
43
  }
44
+ /**
45
+ * Resolve the http routes file.
46
+ */
47
+ async resolveRoute() {
48
+ await Module.resolve(Config.get('rc.commandsManifest.route:list.route', '#routes/http'), Config.get('rc.meta'));
49
+ }
50
+ /**
51
+ * Get the http kernel module from RC file or resolve the default one.
52
+ */
53
+ async getHttpKernel() {
54
+ if (!Config.exists('rc.commandsManifest.route:list.kernel')) {
55
+ return HttpKernel;
56
+ }
57
+ return Module.resolve(Config.get('rc.commandsManifest.route:list.kernel'), Config.get('rc.meta'));
58
+ }
45
59
  }
@@ -0,0 +1,98 @@
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
+ import { Assert } from '@japa/assert';
10
+ import { InjectOptions } from 'fastify';
11
+ import { TestResponse } from '#src/Testing/Plugins/Request/TestResponse';
12
+ export declare class TestRequest {
13
+ /**
14
+ * Japa assert class instance.
15
+ */
16
+ assert: Assert;
17
+ /**
18
+ * Instantiate TestResponse class from API response.
19
+ */
20
+ createResponse(response: any): TestResponse;
21
+ /**
22
+ * Make a GET request to the given URL and options.
23
+ *
24
+ * @example
25
+ * ```js
26
+ * const response = await request.get('/users')
27
+ *
28
+ * response.assertStatusCode(200)
29
+ * ```
30
+ */
31
+ get(url: string, options?: InjectOptions): Promise<TestResponse>;
32
+ /**
33
+ * Make a HEAD request to the given URL and options.
34
+ *
35
+ * @example
36
+ * ```js
37
+ * const response = await request.head('/users')
38
+ *
39
+ * response.assertStatusCode(200)
40
+ * ```
41
+ */
42
+ head(url: string, options?: InjectOptions): Promise<TestResponse>;
43
+ /**
44
+ * Make a OPTIONS request to the given URL and options.
45
+ *
46
+ * @example
47
+ * ```js
48
+ * const response = await request.options('/users')
49
+ *
50
+ * response.assertStatusCode(200)
51
+ * ```
52
+ */
53
+ options(url: string, options?: InjectOptions): Promise<TestResponse>;
54
+ /**
55
+ * Make a POST request to the given URL and options.
56
+ *
57
+ * @example
58
+ * ```js
59
+ * const response = await request.post('/users')
60
+ *
61
+ * response.assertStatusCode(200)
62
+ * ```
63
+ */
64
+ post(url: string, options?: InjectOptions): Promise<TestResponse>;
65
+ /**
66
+ * Make a PUT request to the given URL and options.
67
+ *
68
+ * @example
69
+ * ```js
70
+ * const response = await request.put('/users')
71
+ *
72
+ * response.assertStatusCode(200)
73
+ * ```
74
+ */
75
+ put(url: string, options?: InjectOptions): Promise<TestResponse>;
76
+ /**
77
+ * Make a PATCH request to the given URL and options.
78
+ *
79
+ * @example
80
+ * ```js
81
+ * const response = await request.patch('/users')
82
+ *
83
+ * response.assertStatusCode(200)
84
+ * ```
85
+ */
86
+ patch(url: string, options?: InjectOptions): Promise<TestResponse>;
87
+ /**
88
+ * Make a DELETE request to the given URL and options.
89
+ *
90
+ * @example
91
+ * ```js
92
+ * const response = await request.delete('/users')
93
+ *
94
+ * response.assertStatusCode(200)
95
+ * ```
96
+ */
97
+ delete(url: string, options?: InjectOptions): Promise<TestResponse>;
98
+ }
@@ -0,0 +1,114 @@
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
+ import { Server } from '#src';
10
+ import { Assert } from '@japa/assert';
11
+ import { TestResponse } from '#src/Testing/Plugins/Request/TestResponse';
12
+ export class TestRequest {
13
+ /**
14
+ * Japa assert class instance.
15
+ */
16
+ assert = new Assert();
17
+ /**
18
+ * Instantiate TestResponse class from API response.
19
+ */
20
+ createResponse(response) {
21
+ return new TestResponse(this.assert, response);
22
+ }
23
+ /**
24
+ * Make a GET request to the given URL and options.
25
+ *
26
+ * @example
27
+ * ```js
28
+ * const response = await request.get('/users')
29
+ *
30
+ * response.assertStatusCode(200)
31
+ * ```
32
+ */
33
+ async get(url, options = {}) {
34
+ return Server.request({ url, method: 'GET', ...options }).then(res => this.createResponse(res));
35
+ }
36
+ /**
37
+ * Make a HEAD request to the given URL and options.
38
+ *
39
+ * @example
40
+ * ```js
41
+ * const response = await request.head('/users')
42
+ *
43
+ * response.assertStatusCode(200)
44
+ * ```
45
+ */
46
+ async head(url, options = {}) {
47
+ return Server.request({ url, method: 'HEAD', ...options }).then(res => this.createResponse(res));
48
+ }
49
+ /**
50
+ * Make a OPTIONS request to the given URL and options.
51
+ *
52
+ * @example
53
+ * ```js
54
+ * const response = await request.options('/users')
55
+ *
56
+ * response.assertStatusCode(200)
57
+ * ```
58
+ */
59
+ async options(url, options = {}) {
60
+ return Server.request({ url, method: 'OPTIONS', ...options }).then(res => this.createResponse(res));
61
+ }
62
+ /**
63
+ * Make a POST request to the given URL and options.
64
+ *
65
+ * @example
66
+ * ```js
67
+ * const response = await request.post('/users')
68
+ *
69
+ * response.assertStatusCode(200)
70
+ * ```
71
+ */
72
+ async post(url, options = {}) {
73
+ return Server.request({ url, method: 'POST', ...options }).then(res => this.createResponse(res));
74
+ }
75
+ /**
76
+ * Make a PUT request to the given URL and options.
77
+ *
78
+ * @example
79
+ * ```js
80
+ * const response = await request.put('/users')
81
+ *
82
+ * response.assertStatusCode(200)
83
+ * ```
84
+ */
85
+ async put(url, options = {}) {
86
+ return Server.request({ url, method: 'PUT', ...options }).then(res => this.createResponse(res));
87
+ }
88
+ /**
89
+ * Make a PATCH request to the given URL and options.
90
+ *
91
+ * @example
92
+ * ```js
93
+ * const response = await request.patch('/users')
94
+ *
95
+ * response.assertStatusCode(200)
96
+ * ```
97
+ */
98
+ async patch(url, options = {}) {
99
+ return Server.request({ url, method: 'PATCH', ...options }).then(res => this.createResponse(res));
100
+ }
101
+ /**
102
+ * Make a DELETE request to the given URL and options.
103
+ *
104
+ * @example
105
+ * ```js
106
+ * const response = await request.delete('/users')
107
+ *
108
+ * response.assertStatusCode(200)
109
+ * ```
110
+ */
111
+ async delete(url, options = {}) {
112
+ return Server.request({ url, method: 'DELETE', ...options }).then(res => this.createResponse(res));
113
+ }
114
+ }