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

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,12 +1,20 @@
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;
9
13
  cors?: boolean;
14
+ static?: {
15
+ root: string;
16
+ wildcard?: boolean;
17
+ };
10
18
  };
11
19
  onPlugin?: (options: {
12
20
  fastify: FastifyInstance;
@@ -3,24 +3,36 @@ import { CONTEXT, REQUEST, RESPONSE, } from "@art-ws/http-server";
3
3
  import cors from "@fastify/cors";
4
4
  import formbody from "@fastify/formbody";
5
5
  import multipart from "@fastify/multipart";
6
+ import fastifyStatic from "@fastify/static";
6
7
  import Fastify, {} from "fastify";
7
- export async function createHttpServer({ routes, port, plugins, onPlugin, }) {
8
+ export async function createHttpServer({ routes, port, handlers, plugins, onPlugin, }) {
8
9
  const fastify = Fastify({
9
10
  // https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
10
11
  exposeHeadRoutes: true,
11
12
  });
12
- // https://fastify.dev/docs/latest/Reference/Errors/#uncaught-errors
13
- fastify.setErrorHandler((err, _req, reply) => {
14
- reply
15
- .status(500)
16
- .type("text/json")
17
- .send({
18
- error: { name: err.name, message: err.message, stack: err.stack },
13
+ if (handlers?.errorHandler) {
14
+ // https://fastify.dev/docs/latest/Reference/Errors/#uncaught-errors
15
+ 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 },
21
+ });
19
22
  });
20
- });
21
- fastify.setNotFoundHandler((_req, reply) => {
22
- reply.code(404).type("text/json").send({ message: "Not Found" });
23
- });
23
+ }
24
+ if (plugins?.static?.root) {
25
+ fastify.register(fastifyStatic, {
26
+ root: plugins.static.root,
27
+ wildcard: !!plugins.static.wildcard,
28
+ });
29
+ }
30
+ if (handlers?.notFoundHandler) {
31
+ // https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler
32
+ fastify.setNotFoundHandler((_req, reply) => {
33
+ reply.code(404).type("text/json").send({ message: "Not Found" });
34
+ });
35
+ }
24
36
  await onPlugin?.({ fastify });
25
37
  if (plugins?.formbody) {
26
38
  await fastify.register(formbody);
@@ -40,7 +52,7 @@ export async function createHttpServer({ routes, port, plugins, onPlugin, }) {
40
52
  "req-" + Date.now();
41
53
  req.id = req_id;
42
54
  const context = new FastifyHttpContext(new FastifyHttpReq(req), new FastifyHttpRes(res), new FastifyHttpServer(fastify));
43
- const result = Injector.root.runScope(() => handler(context), {
55
+ const result = await Injector.root.runScope(() => handler(context), {
44
56
  onEnd: () => {
45
57
  res.headers({
46
58
  "x-req-id": req_id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/fastify-http-server",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Fastify Http server",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Alexander Shagin",
@@ -14,6 +14,7 @@
14
14
  "@fastify/cors": "11.0.1",
15
15
  "@fastify/formbody": "8.0.2",
16
16
  "@fastify/multipart": "9.0.3",
17
+ "@fastify/static": "8.2.0",
17
18
  "fastify": "5.4.0",
18
19
  "@art-ws/di": "2.0.2",
19
20
  "@art-ws/http-server": "2.0.2"
@@ -22,14 +23,15 @@
22
23
  "eslint": "9.29.0",
23
24
  "typescript": "5.6.2",
24
25
  "vitest": "3.2.4",
25
- "@art-ws/config-ts": "2.0.5",
26
- "@art-ws/config-eslint": "2.0.2"
26
+ "@art-ws/config-eslint": "2.0.2",
27
+ "@art-ws/config-ts": "2.0.5"
27
28
  },
28
29
  "scripts": {
29
30
  "build": "tsc",
30
31
  "clean": "rm -rf dist",
31
32
  "clean:nm": "rm -rf node_modules",
32
33
  "lint": "eslint .",
34
+ "pub": "pnpm prepare && pnpm version patch && pnpm publish --access public --no-git-checks",
33
35
  "test": "exit 0",
34
36
  "test:watch": "vitest",
35
37
  "watch": "tsc --watch"