@athenna/http 5.40.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 +1 -1
- package/src/router/Route.d.ts +14 -0
- package/src/router/Route.js +22 -0
- package/src/server/ServerImpl.js +10 -1
- package/src/types/router/RouteJson.d.ts +1 -0
package/package.json
CHANGED
package/src/router/Route.d.ts
CHANGED
|
@@ -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
|
*
|
package/src/router/Route.js
CHANGED
|
@@ -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,
|
package/src/server/ServerImpl.js
CHANGED
|
@@ -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;
|