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

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,4 +1,4 @@
1
- import { type RouteDef } from "@art-ws/http-server";
1
+ import { type HttpContext, type RouteDef } from "@art-ws/http-server";
2
2
  import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest, type RouteShorthandOptions as FastifyRouteShorthandOptions } from "fastify";
3
3
  export type { FastifyInstance, FastifyReply, FastifyRequest, FastifyRouteShorthandOptions, };
4
4
  export type FastifyHook = (options: {
@@ -10,6 +10,10 @@ export declare function createHttpServer({ routes, port, handlers, plugins, onWe
10
10
  handlers?: {
11
11
  errorHandler?: boolean;
12
12
  notFoundHandler?: boolean;
13
+ preHandler: (o?: {
14
+ context: HttpContext;
15
+ route: unknown;
16
+ }) => Promise<void>;
13
17
  };
14
18
  plugins?: {
15
19
  formbody?: boolean;
@@ -1,10 +1,10 @@
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";
4
3
  import cors from "@fastify/cors";
5
4
  import formbody from "@fastify/formbody";
6
5
  import multipart from "@fastify/multipart";
7
6
  import fastifyStatic from "@fastify/static";
7
+ import ws from "@fastify/websocket";
8
8
  import Fastify, {} from "fastify";
9
9
  export async function createHttpServer({ routes, port, handlers, plugins, onWebsocket, onPlugin, onBeforeReady, onAfterReady, onAfterListen, }) {
10
10
  const fastify = Fastify({
@@ -34,8 +34,11 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
34
34
  }
35
35
  if (handlers?.notFoundHandler) {
36
36
  // https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler
37
- fastify.setNotFoundHandler((_req, reply) => {
38
- reply.code(404).type("text/json").send({ message: "Not Found" });
37
+ fastify.setNotFoundHandler((req, reply) => {
38
+ reply
39
+ .status(404)
40
+ .type("application/json")
41
+ .send({ message: "Not Found", url: req.url });
39
42
  });
40
43
  }
41
44
  // https://www.npmjs.com/package/@fastify/websocket
@@ -55,19 +58,25 @@ export async function createHttpServer({ routes, port, handlers, plugins, onWebs
55
58
  if (plugins?.cors) {
56
59
  await fastify.register(cors);
57
60
  }
58
- routes.forEach(({ method, path, handler, options }) => {
61
+ routes.forEach((route) => {
62
+ const { method, path, handler, options } = route;
59
63
  async function fastifyHandler(req, res) {
60
64
  const req_id = req.headers["x-req-id"] ||
61
65
  req.headers["x-request-id"] ||
62
66
  req.id ||
63
- "req-" + Date.now();
67
+ "req-" + Date.now() + "-" + Math.random().toString(36).substring(2, 8);
64
68
  req.id = req_id;
65
69
  let resolveFunc;
66
70
  const promise = new Promise((resolve) => {
67
71
  resolveFunc = resolve;
68
72
  });
69
73
  const context = new FastifyHttpContext(promise, new FastifyHttpReq(req), new FastifyHttpRes(res), new FastifyHttpServer(fastify));
70
- const result = await getRootInjector().runScope(() => handler(context), {
74
+ const result = await getRootInjector().runScope(async () => {
75
+ if (handlers?.preHandler) {
76
+ await handlers.preHandler({ context, route });
77
+ }
78
+ return handler(context);
79
+ }, {
71
80
  onEnd: ({ injector }) => {
72
81
  res.headers({
73
82
  "x-req-id": req_id,
@@ -116,6 +125,15 @@ class FastifyHttpReq {
116
125
  constructor(raw) {
117
126
  this.raw = raw;
118
127
  }
128
+ get url() {
129
+ return this.raw.url;
130
+ }
131
+ get method() {
132
+ return this.raw.method;
133
+ }
134
+ get params() {
135
+ return this.raw.params;
136
+ }
119
137
  get query() {
120
138
  return this.raw.query;
121
139
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/fastify-http-server",
3
- "version": "2.0.18",
3
+ "version": "2.0.20",
4
4
  "description": "Fastify Http server",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Alexander Shagin",
@@ -17,8 +17,9 @@
17
17
  "@fastify/static": "^8.2.0",
18
18
  "@fastify/websocket": "^11.2.0",
19
19
  "fastify": "^5.5.0",
20
- "@art-ws/di": "2.0.22",
21
- "@art-ws/http-server": "2.0.15"
20
+ "fastify-multer": "^2.0.3",
21
+ "@art-ws/http-server": "2.0.17",
22
+ "@art-ws/di": "2.0.24"
22
23
  },
23
24
  "devDependencies": {
24
25
  "eslint": "^9.34.0",