@art-ws/fastify-http-server 2.0.17 → 2.0.19
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.
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type RouteDef } from "@art-ws/http-server";
|
1
|
+
import { type HttpContext, type RouteDef } from "@art-ws/http-server";
|
2
2
|
import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest, type RouteShorthandOptions as FastifyRouteShorthandOptions } from "fastify";
|
3
3
|
export type { FastifyInstance, FastifyReply, FastifyRequest, FastifyRouteShorthandOptions, };
|
4
4
|
export type FastifyHook = (options: {
|
@@ -10,6 +10,7 @@ export declare function createHttpServer({ routes, port, handlers, plugins, onWe
|
|
10
10
|
handlers?: {
|
11
11
|
errorHandler?: boolean;
|
12
12
|
notFoundHandler?: boolean;
|
13
|
+
preHandler: (context?: HttpContext) => Promise<void>;
|
13
14
|
};
|
14
15
|
plugins?: {
|
15
16
|
formbody?: boolean;
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { getRootInjector } from "@art-ws/di";
|
2
2
|
import { CONTEXT, REQUEST, REQUEST_ID, RESPONSE, } from "@art-ws/http-server";
|
3
|
-
import ws from "@fastify/websocket";
|
4
3
|
import cors from "@fastify/cors";
|
5
4
|
import formbody from "@fastify/formbody";
|
6
5
|
import multipart from "@fastify/multipart";
|
7
6
|
import fastifyStatic from "@fastify/static";
|
7
|
+
import ws from "@fastify/websocket";
|
8
8
|
import Fastify, {} from "fastify";
|
9
9
|
export async function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
|
10
10
|
const fastify = Fastify({
|
@@ -34,8 +34,11 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
|
|
34
34
|
}
|
35
35
|
if (handlers?.notFoundHandler) {
|
36
36
|
// https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler
|
37
|
-
fastify.setNotFoundHandler((
|
38
|
-
reply
|
37
|
+
fastify.setNotFoundHandler((req, reply) => {
|
38
|
+
reply
|
39
|
+
.status(404)
|
40
|
+
.type("application/json")
|
41
|
+
.send({ message: "Not Found", url: req.url });
|
39
42
|
});
|
40
43
|
}
|
41
44
|
// https://www.npmjs.com/package/@fastify/websocket
|
@@ -67,7 +70,12 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
|
|
67
70
|
resolveFunc = resolve;
|
68
71
|
});
|
69
72
|
const context = new FastifyHttpContext(promise, new FastifyHttpReq(req), new FastifyHttpRes(res), new FastifyHttpServer(fastify));
|
70
|
-
const result = await getRootInjector().runScope(() =>
|
73
|
+
const result = await getRootInjector().runScope(async () => {
|
74
|
+
if (handlers?.preHandler) {
|
75
|
+
await handlers.preHandler(context);
|
76
|
+
}
|
77
|
+
return handler(context);
|
78
|
+
}, {
|
71
79
|
onEnd: ({ injector }) => {
|
72
80
|
res.headers({
|
73
81
|
"x-req-id": req_id,
|
@@ -116,6 +124,15 @@ class FastifyHttpReq {
|
|
116
124
|
constructor(raw) {
|
117
125
|
this.raw = raw;
|
118
126
|
}
|
127
|
+
get url() {
|
128
|
+
return this.raw.url;
|
129
|
+
}
|
130
|
+
get method() {
|
131
|
+
return this.raw.method;
|
132
|
+
}
|
133
|
+
get params() {
|
134
|
+
return this.raw.params;
|
135
|
+
}
|
119
136
|
get query() {
|
120
137
|
return this.raw.query;
|
121
138
|
}
|
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.19",
|
4
4
|
"description": "Fastify Http server",
|
5
5
|
"license": "UNLICENSED",
|
6
6
|
"author": "Alexander Shagin",
|
@@ -17,15 +17,16 @@
|
|
17
17
|
"@fastify/static": "^8.2.0",
|
18
18
|
"@fastify/websocket": "^11.2.0",
|
19
19
|
"fastify": "^5.5.0",
|
20
|
-
"
|
21
|
-
"@art-ws/
|
20
|
+
"fastify-multer": "^2.0.3",
|
21
|
+
"@art-ws/http-server": "2.0.16",
|
22
|
+
"@art-ws/di": "2.0.23"
|
22
23
|
},
|
23
24
|
"devDependencies": {
|
24
25
|
"eslint": "^9.34.0",
|
25
26
|
"typescript": "^5.9.2",
|
26
27
|
"vitest": "^3.2.4",
|
27
|
-
"@art-ws/config-
|
28
|
-
"@art-ws/config-
|
28
|
+
"@art-ws/config-eslint": "2.0.3",
|
29
|
+
"@art-ws/config-ts": "2.0.6"
|
29
30
|
},
|
30
31
|
"scripts": {
|
31
32
|
"build": "tsc",
|