@art-ws/fastify-http-server 2.0.14 → 2.0.16

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,7 +1,10 @@
1
1
  import { type RouteDef } from "@art-ws/http-server";
2
- import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify";
3
- export type { FastifyInstance, FastifyReply, FastifyRequest };
4
- export declare function createHttpServer({ routes, port, handlers, plugins, onPlugin, }: {
2
+ import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest, type RouteShorthandOptions as FastifyRouteShorthandOptions } from "fastify";
3
+ export type { FastifyInstance, FastifyReply, FastifyRequest, FastifyRouteShorthandOptions, };
4
+ export type FastifyHook = (options: {
5
+ fastify: FastifyInstance;
6
+ }) => Promise<void>;
7
+ export declare function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, }: {
5
8
  routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
6
9
  port?: number;
7
10
  handlers?: {
@@ -17,9 +20,10 @@ export declare function createHttpServer({ routes, port, handlers, plugins, onPl
17
20
  wildcard?: boolean;
18
21
  };
19
22
  };
20
- onPlugin?: (options: {
21
- fastify: FastifyInstance;
22
- }) => Promise<void>;
23
+ onWebsocket?: FastifyHook;
24
+ onPlugin?: FastifyHook;
25
+ onBeforeReady?: FastifyHook;
26
+ onAfterReady?: FastifyHook;
23
27
  }): Promise<{
24
28
  port: number;
25
29
  fastify: Fastify.FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, Fastify.FastifyBaseLogger, Fastify.FastifyTypeProviderDefault> & PromiseLike<Fastify.FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, Fastify.FastifyBaseLogger, Fastify.FastifyTypeProviderDefault>> & {
@@ -1,11 +1,12 @@
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";
3
4
  import cors from "@fastify/cors";
4
5
  import formbody from "@fastify/formbody";
5
6
  import multipart from "@fastify/multipart";
6
7
  import fastifyStatic from "@fastify/static";
7
8
  import Fastify, {} from "fastify";
8
- export async function createHttpServer({ routes, port, handlers, plugins, onPlugin, }) {
9
+ export async function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, }) {
9
10
  const fastify = Fastify({
10
11
  // https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
11
12
  exposeHeadRoutes: true,
@@ -13,11 +14,15 @@ export async function createHttpServer({ routes, port, handlers, plugins, onPlug
13
14
  if (handlers?.errorHandler) {
14
15
  // https://fastify.dev/docs/latest/Reference/Errors/#uncaught-errors
15
16
  fastify.setErrorHandler((err, _req, reply) => {
16
- reply
17
- .status(500)
18
- .type("text/json")
19
- .send({
20
- error: { name: err.name, message: err.message, stack: err.stack },
17
+ console.log("Error occurred:", err);
18
+ let status = 500;
19
+ const { code = "INTERNAL_SERVER_ERROR", message, name, stack } = err || {};
20
+ const ex = err;
21
+ if (ex?.status) {
22
+ status = ex.status;
23
+ }
24
+ reply.status(status).type("application/json").send({
25
+ error: { code, name, message, stack },
21
26
  });
22
27
  });
23
28
  }
@@ -33,6 +38,12 @@ export async function createHttpServer({ routes, port, handlers, plugins, onPlug
33
38
  reply.code(404).type("text/json").send({ message: "Not Found" });
34
39
  });
35
40
  }
41
+ // https://www.npmjs.com/package/@fastify/websocket
42
+ if (onWebsocket) {
43
+ await fastify.register(ws);
44
+ console.log("WebSocket plugin registered");
45
+ onWebsocket({ fastify });
46
+ }
36
47
  await onPlugin?.({ fastify });
37
48
  if (plugins?.formbody) {
38
49
  await fastify.register(formbody);
@@ -44,7 +55,7 @@ export async function createHttpServer({ routes, port, handlers, plugins, onPlug
44
55
  if (plugins?.cors) {
45
56
  await fastify.register(cors);
46
57
  }
47
- routes.forEach(({ method, path, handler }) => {
58
+ routes.forEach(({ method, path, handler, options }) => {
48
59
  async function fastifyHandler(req, res) {
49
60
  const req_id = req.headers["x-req-id"] ||
50
61
  req.headers["x-request-id"] ||
@@ -74,8 +85,13 @@ export async function createHttpServer({ routes, port, handlers, plugins, onPlug
74
85
  });
75
86
  return result;
76
87
  }
77
- fastify[method](path, fastifyHandler);
88
+ fastify[method](path, { ...(options || {}) }, fastifyHandler);
78
89
  });
90
+ if (onBeforeReady || onAfterReady) {
91
+ onBeforeReady?.({ fastify });
92
+ await fastify.ready();
93
+ onAfterReady?.({ fastify });
94
+ }
79
95
  if (port) {
80
96
  try {
81
97
  const host = "0.0.0.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/fastify-http-server",
3
- "version": "2.0.14",
3
+ "version": "2.0.16",
4
4
  "description": "Fastify Http server",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Alexander Shagin",
@@ -15,9 +15,10 @@
15
15
  "@fastify/formbody": "^8.0.2",
16
16
  "@fastify/multipart": "^9.0.3",
17
17
  "@fastify/static": "^8.2.0",
18
+ "@fastify/websocket": "^11.2.0",
18
19
  "fastify": "^5.5.0",
19
- "@art-ws/di": "2.0.19",
20
- "@art-ws/http-server": "2.0.11"
20
+ "@art-ws/di": "2.0.21",
21
+ "@art-ws/http-server": "2.0.13"
21
22
  },
22
23
  "devDependencies": {
23
24
  "eslint": "^9.34.0",