@art-ws/fastify-http-server 2.0.2 → 2.0.4
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,9 +1,17 @@
|
|
1
1
|
import { type RouteDef } from "@art-ws/http-server";
|
2
2
|
import { type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify";
|
3
|
-
export declare function
|
3
|
+
export declare function createHttpServer({ routes, port, handlers, plugins, onPlugin, }: {
|
4
4
|
routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
|
5
|
-
listen: boolean;
|
6
5
|
port?: number;
|
6
|
+
handlers?: {
|
7
|
+
errorHandler?: boolean;
|
8
|
+
notFoundHandler?: boolean;
|
9
|
+
};
|
10
|
+
plugins?: {
|
11
|
+
formbody?: boolean;
|
12
|
+
multipart?: boolean;
|
13
|
+
cors?: boolean;
|
14
|
+
};
|
7
15
|
onPlugin?: (options: {
|
8
16
|
fastify: FastifyInstance;
|
9
17
|
}) => Promise<void>;
|
@@ -4,28 +4,39 @@ import cors from "@fastify/cors";
|
|
4
4
|
import formbody from "@fastify/formbody";
|
5
5
|
import multipart from "@fastify/multipart";
|
6
6
|
import Fastify, {} from "fastify";
|
7
|
-
export async function
|
7
|
+
export async function createHttpServer({ routes, port, handlers, plugins, onPlugin, }) {
|
8
8
|
const fastify = Fastify({
|
9
9
|
// https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
|
10
10
|
exposeHeadRoutes: true,
|
11
11
|
});
|
12
|
-
|
13
|
-
|
14
|
-
reply
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
if (handlers?.errorHandler) {
|
13
|
+
// https://fastify.dev/docs/latest/Reference/Errors/#uncaught-errors
|
14
|
+
fastify.setErrorHandler((err, _req, reply) => {
|
15
|
+
reply
|
16
|
+
.status(500)
|
17
|
+
.type("text/json")
|
18
|
+
.send({
|
19
|
+
error: { name: err.name, message: err.message, stack: err.stack },
|
20
|
+
});
|
19
21
|
});
|
20
|
-
}
|
22
|
+
}
|
23
|
+
if (handlers?.notFoundHandler) {
|
24
|
+
// https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler
|
25
|
+
fastify.setNotFoundHandler((_req, reply) => {
|
26
|
+
reply.code(404).type("text/json").send({ message: "Not Found" });
|
27
|
+
});
|
28
|
+
}
|
21
29
|
await onPlugin?.({ fastify });
|
22
|
-
|
23
|
-
|
30
|
+
if (plugins?.formbody) {
|
31
|
+
await fastify.register(formbody);
|
32
|
+
}
|
33
|
+
if (plugins?.multipart) {
|
34
|
+
await fastify.register(multipart);
|
35
|
+
}
|
24
36
|
// https://github.com/fastify/fastify-cors
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
});
|
37
|
+
if (plugins?.cors) {
|
38
|
+
await fastify.register(cors);
|
39
|
+
}
|
29
40
|
routes.forEach(({ method, path, handler }) => {
|
30
41
|
async function fastifyHandler(req, res) {
|
31
42
|
const req_id = req.headers["x-req-id"] ||
|
@@ -50,7 +61,7 @@ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }
|
|
50
61
|
}
|
51
62
|
fastify[method](path, fastifyHandler);
|
52
63
|
});
|
53
|
-
if (
|
64
|
+
if (port) {
|
54
65
|
try {
|
55
66
|
const host = "0.0.0.0";
|
56
67
|
await fastify.listen({ port, host });
|
@@ -58,7 +69,7 @@ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }
|
|
58
69
|
}
|
59
70
|
catch (err) {
|
60
71
|
fastify.log.error(err);
|
61
|
-
|
72
|
+
throw err;
|
62
73
|
}
|
63
74
|
return { port, fastify };
|
64
75
|
}
|
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.4",
|
4
4
|
"description": "Fastify Http server",
|
5
5
|
"license": "UNLICENSED",
|
6
6
|
"author": "Alexander Shagin",
|
@@ -30,6 +30,7 @@
|
|
30
30
|
"clean": "rm -rf dist",
|
31
31
|
"clean:nm": "rm -rf node_modules",
|
32
32
|
"lint": "eslint .",
|
33
|
+
"pub": "pnpm prepare && pnpm version patch && pnpm publish --access public --no-git-checks",
|
33
34
|
"test": "exit 0",
|
34
35
|
"test:watch": "vitest",
|
35
36
|
"watch": "tsc --watch"
|