@athenna/http 5.24.0 → 5.26.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "5.24.0",
3
+ "version": "5.26.0",
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>",
@@ -82,7 +82,7 @@
82
82
  "@athenna/test": "^5.3.0",
83
83
  "@athenna/tsconfig": "^5.0.0",
84
84
  "@athenna/view": "^5.3.0",
85
- "@athenna/vite": "^5.12.0",
85
+ "@athenna/vite": "^5.13.0",
86
86
  "@fastify/cors": "^10.0.2",
87
87
  "@fastify/helmet": "^13.0.1",
88
88
  "@fastify/rate-limit": "^10.2.2",
@@ -13,7 +13,7 @@ import { Log } from '@athenna/logger';
13
13
  import { Config } from '@athenna/config';
14
14
  import { sep, isAbsolute, resolve } from 'node:path';
15
15
  import { Annotation } from '@athenna/ioc';
16
- import { File, Path, Exec, Module, String } from '@athenna/common';
16
+ import { File, Path, Module, String } from '@athenna/common';
17
17
  import { HttpExceptionHandler } from '#src/handlers/HttpExceptionHandler';
18
18
  const corsPlugin = await Module.safeImport('@fastify/cors');
19
19
  const helmetPlugin = await Module.safeImport('@fastify/helmet');
@@ -163,7 +163,7 @@ export class HttpKernel {
163
163
  */
164
164
  async registerControllers() {
165
165
  const controllers = Config.get('rc.controllers', []);
166
- await Exec.concurrently(controllers, async (path) => {
166
+ await controllers.athenna.concurrently(async (path) => {
167
167
  const Controller = await Module.resolve(path, this.getMeta());
168
168
  if (Annotation.isAnnotated(Controller)) {
169
169
  this.registerUsingMeta(Controller);
@@ -179,7 +179,7 @@ export class HttpKernel {
179
179
  */
180
180
  async registerMiddlewares() {
181
181
  const middlewares = Config.get('rc.middlewares', []);
182
- await Exec.concurrently(middlewares, async (path) => {
182
+ await middlewares.athenna.concurrently(async (path) => {
183
183
  const Middleware = await Module.resolve(path, this.getMeta());
184
184
  if (Annotation.isAnnotated(Middleware)) {
185
185
  this.registerUsingMeta(Middleware);
@@ -198,7 +198,7 @@ export class HttpKernel {
198
198
  */
199
199
  async registerNamedMiddlewares() {
200
200
  const namedMiddlewares = Config.get('rc.namedMiddlewares', {});
201
- await Exec.concurrently(Object.keys(namedMiddlewares), async (key) => {
201
+ await Object.keys(namedMiddlewares).athenna.concurrently(async (key) => {
202
202
  const Middleware = await Module.resolve(namedMiddlewares[key], this.getMeta());
203
203
  if (Annotation.isAnnotated(Middleware)) {
204
204
  this.registerUsingMeta(Middleware);
@@ -214,7 +214,7 @@ export class HttpKernel {
214
214
  */
215
215
  async registerGlobalMiddlewares() {
216
216
  const globalMiddlewares = Config.get('rc.globalMiddlewares', []);
217
- await Exec.concurrently(globalMiddlewares, async (path) => {
217
+ await globalMiddlewares.athenna.concurrently(async (path) => {
218
218
  const Middleware = await Module.resolve(path, this.getMeta());
219
219
  if (Annotation.isAnnotated(Middleware)) {
220
220
  this.registerUsingMeta(Middleware);
@@ -11,6 +11,10 @@ import { Macroable } from '@athenna/common';
11
11
  import type { InjectOptions } from '#src/types';
12
12
  import { TestResponse } from '#src/testing/plugins/request/TestResponse';
13
13
  export declare class TestRequest extends Macroable {
14
+ /**
15
+ * Headers that will be defined in all requests.
16
+ */
17
+ private headers;
14
18
  /**
15
19
  * Japa assert class instance.
16
20
  */
@@ -96,4 +100,16 @@ export declare class TestRequest extends Macroable {
96
100
  * ```
97
101
  */
98
102
  delete(url: string, options?: InjectOptions): Promise<TestResponse>;
103
+ /**
104
+ * Define the authorization access token into all requests.
105
+ *
106
+ * @example
107
+ * ```js
108
+ * const token ='Bearer ...'
109
+ * const response = await request.authorize(token).get('/users')
110
+ *
111
+ * response.assertStatusCode(200)
112
+ * ```
113
+ */
114
+ authorize(accessToken: string): this;
99
115
  }
@@ -13,6 +13,10 @@ import { TestResponse } from '#src/testing/plugins/request/TestResponse';
13
13
  export class TestRequest extends Macroable {
14
14
  constructor() {
15
15
  super(...arguments);
16
+ /**
17
+ * Headers that will be defined in all requests.
18
+ */
19
+ this.headers = {};
16
20
  /**
17
21
  * Japa assert class instance.
18
22
  */
@@ -35,7 +39,12 @@ export class TestRequest extends Macroable {
35
39
  * ```
36
40
  */
37
41
  async get(url, options = {}) {
38
- return Server.request({ url, method: 'GET', ...options }).then(res => this.createResponse(res));
42
+ return Server.request({
43
+ url,
44
+ method: 'GET',
45
+ ...this.headers,
46
+ ...options
47
+ }).then(res => this.createResponse(res));
39
48
  }
40
49
  /**
41
50
  * Make a HEAD request to the given URL and options.
@@ -48,7 +57,12 @@ export class TestRequest extends Macroable {
48
57
  * ```
49
58
  */
50
59
  async head(url, options = {}) {
51
- return Server.request({ url, method: 'HEAD', ...options }).then(res => this.createResponse(res));
60
+ return Server.request({
61
+ url,
62
+ method: 'HEAD',
63
+ ...this.headers,
64
+ ...options
65
+ }).then(res => this.createResponse(res));
52
66
  }
53
67
  /**
54
68
  * Make a OPTIONS request to the given URL and options.
@@ -61,7 +75,12 @@ export class TestRequest extends Macroable {
61
75
  * ```
62
76
  */
63
77
  async options(url, options = {}) {
64
- return Server.request({ url, method: 'OPTIONS', ...options }).then(res => this.createResponse(res));
78
+ return Server.request({
79
+ url,
80
+ method: 'OPTIONS',
81
+ ...this.headers,
82
+ ...options
83
+ }).then(res => this.createResponse(res));
65
84
  }
66
85
  /**
67
86
  * Make a POST request to the given URL and options.
@@ -74,7 +93,12 @@ export class TestRequest extends Macroable {
74
93
  * ```
75
94
  */
76
95
  async post(url, options = {}) {
77
- return Server.request({ url, method: 'POST', ...options }).then(res => this.createResponse(res));
96
+ return Server.request({
97
+ url,
98
+ method: 'POST',
99
+ ...this.headers,
100
+ ...options
101
+ }).then(res => this.createResponse(res));
78
102
  }
79
103
  /**
80
104
  * Make a PUT request to the given URL and options.
@@ -87,7 +111,12 @@ export class TestRequest extends Macroable {
87
111
  * ```
88
112
  */
89
113
  async put(url, options = {}) {
90
- return Server.request({ url, method: 'PUT', ...options }).then(res => this.createResponse(res));
114
+ return Server.request({
115
+ url,
116
+ method: 'PUT',
117
+ ...this.headers,
118
+ ...options
119
+ }).then(res => this.createResponse(res));
91
120
  }
92
121
  /**
93
122
  * Make a PATCH request to the given URL and options.
@@ -100,7 +129,12 @@ export class TestRequest extends Macroable {
100
129
  * ```
101
130
  */
102
131
  async patch(url, options = {}) {
103
- return Server.request({ url, method: 'PATCH', ...options }).then(res => this.createResponse(res));
132
+ return Server.request({
133
+ url,
134
+ method: 'PATCH',
135
+ ...this.headers,
136
+ ...options
137
+ }).then(res => this.createResponse(res));
104
138
  }
105
139
  /**
106
140
  * Make a DELETE request to the given URL and options.
@@ -113,6 +147,26 @@ export class TestRequest extends Macroable {
113
147
  * ```
114
148
  */
115
149
  async delete(url, options = {}) {
116
- return Server.request({ url, method: 'DELETE', ...options }).then(res => this.createResponse(res));
150
+ return Server.request({
151
+ url,
152
+ method: 'DELETE',
153
+ ...this.headers,
154
+ ...options
155
+ }).then(res => this.createResponse(res));
156
+ }
157
+ /**
158
+ * Define the authorization access token into all requests.
159
+ *
160
+ * @example
161
+ * ```js
162
+ * const token ='Bearer ...'
163
+ * const response = await request.authorize(token).get('/users')
164
+ *
165
+ * response.assertStatusCode(200)
166
+ * ```
167
+ */
168
+ authorize(accessToken) {
169
+ this.headers.authorization = accessToken;
170
+ return this;
117
171
  }
118
172
  }