@art-ws/fastify-http-server 2.0.1 → 2.0.3

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,13 @@
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 startHttpServer({ routes, listen, port, onPlugin, }: {
3
+ export declare function createHttpServer({ routes, port, plugins, onPlugin, }: {
4
4
  routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
5
- listen: boolean;
6
5
  port?: number;
6
+ plugins?: {
7
+ formbody?: boolean;
8
+ multipart?: boolean;
9
+ cors?: boolean;
10
+ };
7
11
  onPlugin?: (options: {
8
12
  fastify: FastifyInstance;
9
13
  }) => Promise<void>;
@@ -4,7 +4,7 @@ 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 startHttpServer({ routes, listen, port = 3000, onPlugin, }) {
7
+ export async function createHttpServer({ routes, port, plugins, onPlugin, }) {
8
8
  const fastify = Fastify({
9
9
  // https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
10
10
  exposeHeadRoutes: true,
@@ -18,14 +18,20 @@ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }
18
18
  error: { name: err.name, message: err.message, stack: err.stack },
19
19
  });
20
20
  });
21
- await onPlugin?.({ fastify });
22
- await fastify.register(formbody);
23
- await fastify.register(multipart);
24
- // https://github.com/fastify/fastify-cors
25
- await fastify.register(cors);
26
21
  fastify.setNotFoundHandler((_req, reply) => {
27
22
  reply.code(404).type("text/json").send({ message: "Not Found" });
28
23
  });
24
+ await onPlugin?.({ fastify });
25
+ if (plugins?.formbody) {
26
+ await fastify.register(formbody);
27
+ }
28
+ if (plugins?.multipart) {
29
+ await fastify.register(multipart);
30
+ }
31
+ // https://github.com/fastify/fastify-cors
32
+ if (plugins?.cors) {
33
+ await fastify.register(cors);
34
+ }
29
35
  routes.forEach(({ method, path, handler }) => {
30
36
  async function fastifyHandler(req, res) {
31
37
  const req_id = req.headers["x-req-id"] ||
@@ -50,7 +56,7 @@ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }
50
56
  }
51
57
  fastify[method](path, fastifyHandler);
52
58
  });
53
- if (listen) {
59
+ if (port) {
54
60
  try {
55
61
  const host = "0.0.0.0";
56
62
  await fastify.listen({ port, host });
@@ -58,7 +64,7 @@ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }
58
64
  }
59
65
  catch (err) {
60
66
  fastify.log.error(err);
61
- process.exit(1);
67
+ throw err;
62
68
  }
63
69
  return { port, fastify };
64
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/fastify-http-server",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Fastify Http server",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Alexander Shagin",
@@ -15,15 +15,15 @@
15
15
  "@fastify/formbody": "8.0.2",
16
16
  "@fastify/multipart": "9.0.3",
17
17
  "fastify": "5.4.0",
18
- "@art-ws/di": "2.0.1",
19
- "@art-ws/http-server": "2.0.1"
18
+ "@art-ws/di": "2.0.2",
19
+ "@art-ws/http-server": "2.0.2"
20
20
  },
21
21
  "devDependencies": {
22
22
  "eslint": "9.29.0",
23
23
  "typescript": "5.6.2",
24
24
  "vitest": "3.2.4",
25
- "@art-ws/config-eslint": "2.0.1",
26
- "@art-ws/config-ts": "2.0.4"
25
+ "@art-ws/config-ts": "2.0.5",
26
+ "@art-ws/config-eslint": "2.0.2"
27
27
  },
28
28
  "scripts": {
29
29
  "build": "tsc",