@athenna/http 5.39.0 → 5.41.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.39.0",
3
+ "version": "5.41.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>",
@@ -26,7 +26,7 @@ export class HttpExceptionHandler {
26
26
  */
27
27
  async handle({ error, response }) {
28
28
  let code = error.code;
29
- if (error.code === undefined) {
29
+ if (!Is.String(code) || Is.Undefined(code)) {
30
30
  code = error.name || 'E_INTERNAL_SERVER';
31
31
  }
32
32
  const body = {
@@ -15,6 +15,7 @@ export declare class Route extends Macroable {
15
15
  */
16
16
  route: {
17
17
  url?: string;
18
+ data?: Record<string, any>;
18
19
  methods: HTTPMethods[];
19
20
  handler?: RequestHandler;
20
21
  name?: string;
@@ -42,6 +43,19 @@ export declare class Route extends Macroable {
42
43
  * ```
43
44
  */
44
45
  vanillaOptions(options: Partial<RouteOptions>): Route;
46
+ /**
47
+ * Define data values for the route. Data values will be available in the
48
+ * request context.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * Route.data({ permission: 'post:create' }).post('/posts', ({ data }) => {
53
+ * console.log(data.permission)
54
+ * })
55
+ * ```
56
+ *
57
+ */
58
+ data(data: Record<string, any> | string, value?: any): Route;
45
59
  /**
46
60
  * Set a prefix for the route.
47
61
  *
@@ -16,6 +16,7 @@ export class Route extends Macroable {
16
16
  this.route = {
17
17
  url,
18
18
  methods,
19
+ data: {},
19
20
  deleted: false,
20
21
  prefixes: [],
21
22
  middlewares: {
@@ -74,6 +75,26 @@ export class Route extends Macroable {
74
75
  };
75
76
  return this;
76
77
  }
78
+ /**
79
+ * Define data values for the route. Data values will be available in the
80
+ * request context.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * Route.data({ permission: 'post:create' }).post('/posts', ({ data }) => {
85
+ * console.log(data.permission)
86
+ * })
87
+ * ```
88
+ *
89
+ */
90
+ data(data, value) {
91
+ if (Is.String(data)) {
92
+ this.route.data[data] = value;
93
+ return this;
94
+ }
95
+ this.route.data = data;
96
+ return this;
97
+ }
77
98
  /**
78
99
  * Set a prefix for the route.
79
100
  *
@@ -468,6 +489,7 @@ export class Route extends Macroable {
468
489
  toJSON() {
469
490
  return {
470
491
  url: this.getUrl(),
492
+ data: this.route.data,
471
493
  methods: this.route.methods,
472
494
  name: this.route.name,
473
495
  deleted: this.route.deleted,
@@ -7,7 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import fastify from 'fastify';
10
- import { Options, Macroable } from '@athenna/common';
10
+ import { Options, Macroable, Is } from '@athenna/common';
11
11
  import { FastifyHandler } from '#src/handlers/FastifyHandler';
12
12
  export class ServerImpl extends Macroable {
13
13
  constructor(options) {
@@ -188,6 +188,9 @@ export class ServerImpl extends Macroable {
188
188
  }
189
189
  const { middlewares, interceptors, terminators } = options.middlewares;
190
190
  const route = {
191
+ onSend: [],
192
+ preHandler: [],
193
+ onResponse: [],
191
194
  url: options.url,
192
195
  method: options.methods,
193
196
  handler: FastifyHandler.request(options.handler)
@@ -201,6 +204,12 @@ export class ServerImpl extends Macroable {
201
204
  if (terminators.length) {
202
205
  route.onResponse = terminators.map(t => FastifyHandler.terminate(t));
203
206
  }
207
+ if (options.data && Is.Array(route.preHandler)) {
208
+ route.preHandler?.unshift((req, _, done) => {
209
+ req.data = options.data;
210
+ done();
211
+ });
212
+ }
204
213
  this.fastify.route({ ...route, ...options.fastify });
205
214
  }
206
215
  /**
@@ -10,6 +10,7 @@ import type { HTTPMethods, RouteOptions } from 'fastify';
10
10
  import type { RequestHandler, MiddlewareRecord } from '#src/types';
11
11
  export type RouteJson = {
12
12
  url: string;
13
+ data?: Record<string, any>;
13
14
  methods?: HTTPMethods[];
14
15
  name?: string;
15
16
  deleted?: boolean;