@art-ws/fastify-http-server 2.0.20 → 2.0.22

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.
@@ -4,16 +4,28 @@ export type { FastifyInstance, FastifyReply, FastifyRequest, FastifyRouteShortha
4
4
  export type FastifyHook = (options: {
5
5
  fastify: FastifyInstance;
6
6
  }) => Promise<void>;
7
- export declare function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }: {
7
+ export type FastifyRouteHook<P = unknown> = (options: {
8
+ context: HttpContext;
9
+ route: P;
10
+ }) => Promise<void>;
11
+ export type FastifyLifecycleHook<Req = unknown, Res = unknown, P = unknown> = (options: {
12
+ req?: Req;
13
+ res: Res;
14
+ payload?: P;
15
+ }) => Promise<void>;
16
+ export declare function createHttpServer({ routes, port, handlers, plugins, lifecycleHooks, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }: {
8
17
  routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
9
18
  port?: number;
10
19
  handlers?: {
11
20
  errorHandler?: boolean;
12
21
  notFoundHandler?: boolean;
13
- preHandler: (o?: {
14
- context: HttpContext;
15
- route: unknown;
16
- }) => Promise<void>;
22
+ preHandler: FastifyRouteHook;
23
+ };
24
+ lifecycleHooks?: {
25
+ onRequest?: FastifyLifecycleHook;
26
+ preHandler?: FastifyLifecycleHook;
27
+ onSend?: FastifyLifecycleHook;
28
+ onResponse?: FastifyLifecycleHook;
17
29
  };
18
30
  plugins?: {
19
31
  formbody?: boolean;
@@ -6,7 +6,7 @@ import multipart from "@fastify/multipart";
6
6
  import fastifyStatic from "@fastify/static";
7
7
  import ws from "@fastify/websocket";
8
8
  import Fastify, {} from "fastify";
9
- export async function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
9
+ export async function createHttpServer({ routes, port, handlers, plugins, lifecycleHooks, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
10
10
  const fastify = Fastify({
11
11
  // https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
12
12
  exposeHeadRoutes: true,
@@ -58,6 +58,26 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
58
58
  if (plugins?.cors) {
59
59
  await fastify.register(cors);
60
60
  }
61
+ if (lifecycleHooks?.onRequest) {
62
+ fastify.addHook("onRequest", async (req, res) => {
63
+ await lifecycleHooks.onRequest({ req, res });
64
+ });
65
+ }
66
+ if (lifecycleHooks?.preHandler) {
67
+ fastify.addHook("preHandler", async (req, res) => {
68
+ await lifecycleHooks.preHandler({ req, res });
69
+ });
70
+ }
71
+ if (lifecycleHooks?.onSend) {
72
+ fastify.addHook("onSend", async (req, res, payload) => {
73
+ await lifecycleHooks.onSend({ req, res, payload });
74
+ });
75
+ }
76
+ if (lifecycleHooks?.onResponse) {
77
+ fastify.addHook("onResponse", async (res) => {
78
+ await lifecycleHooks.onResponse({ res });
79
+ });
80
+ }
61
81
  routes.forEach((route) => {
62
82
  const { method, path, handler, options } = route;
63
83
  async function fastifyHandler(req, res) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/fastify-http-server",
3
- "version": "2.0.20",
3
+ "version": "2.0.22",
4
4
  "description": "Fastify Http server",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Alexander Shagin",
@@ -18,8 +18,8 @@
18
18
  "@fastify/websocket": "^11.2.0",
19
19
  "fastify": "^5.5.0",
20
20
  "fastify-multer": "^2.0.3",
21
- "@art-ws/http-server": "2.0.17",
22
- "@art-ws/di": "2.0.24"
21
+ "@art-ws/di": "2.0.26",
22
+ "@art-ws/http-server": "2.0.19"
23
23
  },
24
24
  "devDependencies": {
25
25
  "eslint": "^9.34.0",