@art-ws/fastify-http-server 2.0.1

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.
@@ -0,0 +1,20 @@
1
+ import { type RouteDef } from "@art-ws/http-server";
2
+ import { type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify";
3
+ export declare function startHttpServer({ routes, listen, port, onPlugin, }: {
4
+ routes: RouteDef<FastifyRequest, FastifyReply, FastifyInstance>[];
5
+ listen: boolean;
6
+ port?: number;
7
+ onPlugin?: (options: {
8
+ fastify: FastifyInstance;
9
+ }) => Promise<void>;
10
+ }): Promise<{
11
+ port: number;
12
+ fastify: FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>> & {
13
+ __linterBrands: "SafePromiseLike";
14
+ };
15
+ } | {
16
+ fastify: FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>> & {
17
+ __linterBrands: "SafePromiseLike";
18
+ };
19
+ port?: undefined;
20
+ }>;
@@ -0,0 +1,115 @@
1
+ import { Injector } from "@art-ws/di";
2
+ import { CONTEXT, REQUEST, RESPONSE, } from "@art-ws/http-server";
3
+ import cors from "@fastify/cors";
4
+ import formbody from "@fastify/formbody";
5
+ import multipart from "@fastify/multipart";
6
+ import Fastify, {} from "fastify";
7
+ export async function startHttpServer({ routes, listen, port = 3000, onPlugin, }) {
8
+ const fastify = Fastify({
9
+ // https://fastify.dev/docs/latest/Reference/Server/#exposeheadroutes
10
+ exposeHeadRoutes: true,
11
+ });
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 },
19
+ });
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
+ fastify.setNotFoundHandler((_req, reply) => {
27
+ reply.code(404).type("text/json").send({ message: "Not Found" });
28
+ });
29
+ routes.forEach(({ method, path, handler }) => {
30
+ async function fastifyHandler(req, res) {
31
+ const req_id = req.headers["x-req-id"] ||
32
+ req.headers["x-request-id"] ||
33
+ req.id ||
34
+ "req-" + Date.now();
35
+ req.id = req_id;
36
+ const context = new FastifyHttpContext(new FastifyHttpReq(req), new FastifyHttpRes(res), new FastifyHttpServer(fastify));
37
+ const result = Injector.root.runScope(() => handler(context), {
38
+ onEnd: () => {
39
+ res.headers({
40
+ "x-req-id": req_id,
41
+ });
42
+ },
43
+ providers: [
44
+ { token: REQUEST, factory: () => req },
45
+ { token: RESPONSE, factory: () => res },
46
+ { token: CONTEXT, factory: () => context },
47
+ ],
48
+ });
49
+ return result;
50
+ }
51
+ fastify[method](path, fastifyHandler);
52
+ });
53
+ if (listen) {
54
+ try {
55
+ const host = "0.0.0.0";
56
+ await fastify.listen({ port, host });
57
+ console.log(`Listening on http://${host}:${port} ...`);
58
+ }
59
+ catch (err) {
60
+ fastify.log.error(err);
61
+ process.exit(1);
62
+ }
63
+ return { port, fastify };
64
+ }
65
+ else {
66
+ return {
67
+ fastify,
68
+ };
69
+ }
70
+ }
71
+ class FastifyHttpReq {
72
+ raw;
73
+ constructor(raw) {
74
+ this.raw = raw;
75
+ }
76
+ get query() {
77
+ return this.raw.query;
78
+ }
79
+ get body() {
80
+ return this.raw.body;
81
+ }
82
+ get headers() {
83
+ return this.raw.headers;
84
+ }
85
+ }
86
+ class FastifyHttpRes {
87
+ raw;
88
+ constructor(raw) {
89
+ this.raw = raw;
90
+ }
91
+ json(value) {
92
+ this.raw.send(value);
93
+ return this;
94
+ }
95
+ code(code) {
96
+ this.raw.status(code);
97
+ return this;
98
+ }
99
+ }
100
+ class FastifyHttpServer {
101
+ raw;
102
+ constructor(raw) {
103
+ this.raw = raw;
104
+ }
105
+ }
106
+ class FastifyHttpContext {
107
+ req;
108
+ res;
109
+ server;
110
+ constructor(req, res, server) {
111
+ this.req = req;
112
+ this.res = res;
113
+ this.server = server;
114
+ }
115
+ }
@@ -0,0 +1 @@
1
+ export * from "./fastify-http-server.js";
@@ -0,0 +1 @@
1
+ export * from "./fastify-http-server.js";
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@art-ws/fastify-http-server",
3
+ "version": "2.0.1",
4
+ "description": "Fastify Http server",
5
+ "license": "UNLICENSED",
6
+ "author": "Alexander Shagin",
7
+ "type": "module",
8
+ "main": "./dist/es/index.js",
9
+ "types": "dist/es",
10
+ "files": [
11
+ "./dist/*"
12
+ ],
13
+ "dependencies": {
14
+ "@fastify/cors": "11.0.1",
15
+ "@fastify/formbody": "8.0.2",
16
+ "@fastify/multipart": "9.0.3",
17
+ "fastify": "5.4.0",
18
+ "@art-ws/di": "2.0.1",
19
+ "@art-ws/http-server": "2.0.1"
20
+ },
21
+ "devDependencies": {
22
+ "eslint": "9.29.0",
23
+ "typescript": "5.6.2",
24
+ "vitest": "3.2.4",
25
+ "@art-ws/config-eslint": "2.0.1",
26
+ "@art-ws/config-ts": "2.0.4"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "clean": "rm -rf dist",
31
+ "clean:nm": "rm -rf node_modules",
32
+ "lint": "eslint .",
33
+ "test": "exit 0",
34
+ "test:watch": "vitest",
35
+ "watch": "tsc --watch"
36
+ }
37
+ }