@art-ws/fastify-http-server 2.0.19 → 2.0.21
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.
@@ -4,13 +4,28 @@ export type { FastifyInstance, FastifyReply, FastifyRequest, FastifyRouteShortha
|
|
4
4
|
export type FastifyHook = (options: {
|
5
5
|
fastify: FastifyInstance;
|
6
6
|
}) => Promise<void>;
|
7
|
-
export
|
7
|
+
export type FastifyRouteHook<P = unknown> = (options: {
|
8
|
+
context: HttpContext;
|
9
|
+
route: P;
|
10
|
+
}) => Promise<void>;
|
11
|
+
export type FastifyLifecycleHook<Req = unknown, Res = unknown, P = unknown> = (options: {
|
12
|
+
req?: Req;
|
13
|
+
res: Res;
|
14
|
+
payload?: P;
|
15
|
+
}) => Promise<void>;
|
16
|
+
export declare function createHttpServer({ routes, port, handlers, plugins, lifecycleHooks, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }: {
|
8
17
|
routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
|
9
18
|
port?: number;
|
10
19
|
handlers?: {
|
11
20
|
errorHandler?: boolean;
|
12
21
|
notFoundHandler?: boolean;
|
13
|
-
preHandler:
|
22
|
+
preHandler: FastifyRouteHook;
|
23
|
+
};
|
24
|
+
lifecycleHooks?: {
|
25
|
+
onRequest?: FastifyLifecycleHook;
|
26
|
+
preHandler?: FastifyLifecycleHook;
|
27
|
+
onSend?: FastifyLifecycleHook;
|
28
|
+
onResponse?: FastifyLifecycleHook;
|
14
29
|
};
|
15
30
|
plugins?: {
|
16
31
|
formbody?: boolean;
|
@@ -6,7 +6,7 @@ import multipart from "@fastify/multipart";
|
|
6
6
|
import fastifyStatic from "@fastify/static";
|
7
7
|
import ws from "@fastify/websocket";
|
8
8
|
import Fastify, {} from "fastify";
|
9
|
-
export async function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
|
9
|
+
export async function createHttpServer({ routes, port, handlers, plugins, lifecycleHooks, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
|
10
10
|
const fastify = Fastify({
|
11
11
|
// https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
|
12
12
|
exposeHeadRoutes: true,
|
@@ -58,12 +58,33 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
|
|
58
58
|
if (plugins?.cors) {
|
59
59
|
await fastify.register(cors);
|
60
60
|
}
|
61
|
-
|
61
|
+
if (lifecycleHooks?.onRequest) {
|
62
|
+
fastify.addHook("onRequest", async (req, res) => {
|
63
|
+
await lifecycleHooks.onRequest({ req, res });
|
64
|
+
});
|
65
|
+
}
|
66
|
+
if (lifecycleHooks?.preHandler) {
|
67
|
+
fastify.addHook("preHandler", async (req, res) => {
|
68
|
+
await lifecycleHooks.preHandler({ req, res });
|
69
|
+
});
|
70
|
+
}
|
71
|
+
if (lifecycleHooks?.onSend) {
|
72
|
+
fastify.addHook("onSend", async (req, res, payload) => {
|
73
|
+
await lifecycleHooks.onSend({ req, res, payload });
|
74
|
+
});
|
75
|
+
}
|
76
|
+
if (lifecycleHooks?.onResponse) {
|
77
|
+
fastify.addHook("onResponse", async (res) => {
|
78
|
+
await lifecycleHooks.onResponse({ res });
|
79
|
+
});
|
80
|
+
}
|
81
|
+
routes.forEach((route) => {
|
82
|
+
const { method, path, handler, options } = route;
|
62
83
|
async function fastifyHandler(req, res) {
|
63
84
|
const req_id = req.headers["x-req-id"] ||
|
64
85
|
req.headers["x-request-id"] ||
|
65
86
|
req.id ||
|
66
|
-
"req-" + Date.now();
|
87
|
+
"req-" + Date.now() + "-" + Math.random().toString(36).substring(2, 8);
|
67
88
|
req.id = req_id;
|
68
89
|
let resolveFunc;
|
69
90
|
const promise = new Promise((resolve) => {
|
@@ -72,7 +93,7 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
|
|
72
93
|
const context = new FastifyHttpContext(promise, new FastifyHttpReq(req), new FastifyHttpRes(res), new FastifyHttpServer(fastify));
|
73
94
|
const result = await getRootInjector().runScope(async () => {
|
74
95
|
if (handlers?.preHandler) {
|
75
|
-
await handlers.preHandler(context);
|
96
|
+
await handlers.preHandler({ context, route });
|
76
97
|
}
|
77
98
|
return handler(context);
|
78
99
|
}, {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@art-ws/fastify-http-server",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.21",
|
4
4
|
"description": "Fastify Http server",
|
5
5
|
"license": "UNLICENSED",
|
6
6
|
"author": "Alexander Shagin",
|
@@ -18,8 +18,8 @@
|
|
18
18
|
"@fastify/websocket": "^11.2.0",
|
19
19
|
"fastify": "^5.5.0",
|
20
20
|
"fastify-multer": "^2.0.3",
|
21
|
-
"@art-ws/http-server": "2.0.
|
22
|
-
"@art-ws/di": "2.0.
|
21
|
+
"@art-ws/http-server": "2.0.18",
|
22
|
+
"@art-ws/di": "2.0.25"
|
23
23
|
},
|
24
24
|
"devDependencies": {
|
25
25
|
"eslint": "^9.34.0",
|