@art-ws/fastify-http-server 2.0.3 → 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,8 +1,12 @@
|
|
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 createHttpServer({ routes, port, plugins, onPlugin, }: {
|
3
|
+
export declare function createHttpServer({ routes, port, handlers, plugins, onPlugin, }: {
|
4
4
|
routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
|
5
5
|
port?: number;
|
6
|
+
handlers?: {
|
7
|
+
errorHandler?: boolean;
|
8
|
+
notFoundHandler?: boolean;
|
9
|
+
};
|
6
10
|
plugins?: {
|
7
11
|
formbody?: boolean;
|
8
12
|
multipart?: boolean;
|
@@ -4,23 +4,28 @@ 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 createHttpServer({ routes, port, plugins, onPlugin, }) {
|
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
|
-
}
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
+
}
|
24
29
|
await onPlugin?.({ fastify });
|
25
30
|
if (plugins?.formbody) {
|
26
31
|
await fastify.register(formbody);
|
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",
|
@@ -22,14 +22,15 @@
|
|
22
22
|
"eslint": "9.29.0",
|
23
23
|
"typescript": "5.6.2",
|
24
24
|
"vitest": "3.2.4",
|
25
|
-
"@art-ws/config-
|
26
|
-
"@art-ws/config-
|
25
|
+
"@art-ws/config-eslint": "2.0.2",
|
26
|
+
"@art-ws/config-ts": "2.0.5"
|
27
27
|
},
|
28
28
|
"scripts": {
|
29
29
|
"build": "tsc",
|
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"
|