@athenna/http 4.33.0 → 4.34.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": "4.33.0",
3
+ "version": "4.34.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>",
@@ -74,7 +74,7 @@
74
74
  },
75
75
  "devDependencies": {
76
76
  "@athenna/artisan": "^4.42.0",
77
- "@athenna/common": "^4.38.0",
77
+ "@athenna/common": "^4.39.0",
78
78
  "@athenna/config": "^4.21.0",
79
79
  "@athenna/ioc": "^4.20.0",
80
80
  "@athenna/logger": "^4.20.0",
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @athenna/ioc
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 { Exception } from '@athenna/common';
10
+ export declare class NotFoundValidatorException extends Exception {
11
+ constructor(alias: string, namedAlias: string);
12
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/ioc
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 { Exception } from '@athenna/common';
10
+ export class NotFoundValidatorException extends Exception {
11
+ constructor(alias, namedAlias) {
12
+ super({
13
+ status: 500,
14
+ code: 'E_NOT_FOUND_VALIDATOR_ERROR',
15
+ message: `The validator with ${namedAlias} named alias and ${alias} alias has not been found inside the container.`,
16
+ help: `Remember to register the validator in your .athennarc.json file.`
17
+ });
18
+ }
19
+ }
@@ -50,6 +50,11 @@ export declare class Route {
50
50
  * ```
51
51
  */
52
52
  prefix(prefix: string): Route;
53
+ /**
54
+ * Set a named validator, validator closure or a MiddlewareContract implementation
55
+ * in the route.
56
+ */
57
+ validator(validator: MiddlewareRouteType, prepend?: boolean): Route;
53
58
  /**
54
59
  * Set a named middleware, middleware closure or a MiddlewareContract implementation
55
60
  * in the route.
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { Is, Options, Route as RouteHelper } from '@athenna/common';
10
10
  import { UndefinedMethodException } from '#src/exceptions/UndefinedMethodException';
11
+ import { NotFoundValidatorException } from '#src/exceptions/NotFoundValidatorException';
11
12
  import { NotFoundMiddlewareException } from '#src/exceptions/NotFoundMiddlewareException';
12
13
  export class Route {
13
14
  constructor(url, methods, handler) {
@@ -84,6 +85,31 @@ export class Route {
84
85
  this.route.prefixes.push(prefix);
85
86
  return this;
86
87
  }
88
+ /**
89
+ * Set a named validator, validator closure or a MiddlewareContract implementation
90
+ * in the route.
91
+ */
92
+ validator(validator, prepend = false) {
93
+ const insertionType = prepend ? 'unshift' : 'push';
94
+ if (Is.String(validator)) {
95
+ const namedAlias = `App/Validators/Names/${validator}`;
96
+ const alias = `App/Validators/${validator}`;
97
+ if (!ioc.has(namedAlias) && !ioc.has(alias)) {
98
+ throw new NotFoundValidatorException(alias, namedAlias);
99
+ }
100
+ this.route.middlewares.middlewares[insertionType]((...args) => {
101
+ const mid = ioc.use(namedAlias) || ioc.safeUse(alias);
102
+ return mid.handle.bind(mid)(...args);
103
+ });
104
+ return this;
105
+ }
106
+ if (Is.Function(validator)) {
107
+ this.route.middlewares.middlewares[insertionType](validator);
108
+ return this;
109
+ }
110
+ this.route.middlewares.middlewares[insertionType](validator.handle.bind(validator));
111
+ return this;
112
+ }
87
113
  /**
88
114
  * Set a named middleware, middleware closure or a MiddlewareContract implementation
89
115
  * in the route.
@@ -37,6 +37,17 @@ export declare class RouteGroup {
37
37
  * ```
38
38
  */
39
39
  name(name: string): RouteGroup;
40
+ /**
41
+ * Add a validator to all routes in the group.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * Route.group(() => {
46
+ *
47
+ * }).validator('auth')
48
+ * ```
49
+ */
50
+ validator(validator: MiddlewareRouteType): RouteGroup;
40
51
  /**
41
52
  * Add a middleware to all routes in the group.
42
53
  *
@@ -40,6 +40,20 @@ export class RouteGroup {
40
40
  this.routes.forEach(route => this.invoke(route, 'name', [name]));
41
41
  return this;
42
42
  }
43
+ /**
44
+ * Add a validator to all routes in the group.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * Route.group(() => {
49
+ *
50
+ * }).validator('auth')
51
+ * ```
52
+ */
53
+ validator(validator) {
54
+ this.routes.forEach(route => this.invoke(route, 'validator', [validator, true]));
55
+ return this;
56
+ }
43
57
  /**
44
58
  * Add a middleware to all routes in the group.
45
59
  *
@@ -22,6 +22,15 @@ export declare class RouteResource {
22
22
  */
23
23
  controller: any;
24
24
  constructor(resource: string, controller: any);
25
+ /**
26
+ * Set a validator for the route resource.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * Route.resource('/test', 'TestController').validator('auth')
31
+ * ```
32
+ */
33
+ validator(validator: MiddlewareRouteType, prepend?: boolean): RouteResource;
25
34
  /**
26
35
  * Set a middleware for the route resource.
27
36
  *
@@ -18,6 +18,18 @@ export class RouteResource {
18
18
  this.controller = controller;
19
19
  this.buildRoutes();
20
20
  }
21
+ /**
22
+ * Set a validator for the route resource.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * Route.resource('/test', 'TestController').validator('auth')
27
+ * ```
28
+ */
29
+ validator(validator, prepend) {
30
+ this.routes.forEach(route => route.validator(validator, prepend));
31
+ return this;
32
+ }
21
33
  /**
22
34
  * Set a middleware for the route resource.
23
35
  *