@lithia-js/core 1.0.0-canary.0

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.
Files changed (91) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +13 -0
  3. package/LICENSE +21 -0
  4. package/README.md +60 -0
  5. package/dist/config.d.ts +101 -0
  6. package/dist/config.js +113 -0
  7. package/dist/config.js.map +1 -0
  8. package/dist/context/event-context.d.ts +53 -0
  9. package/dist/context/event-context.js +42 -0
  10. package/dist/context/event-context.js.map +1 -0
  11. package/dist/context/index.d.ts +16 -0
  12. package/dist/context/index.js +29 -0
  13. package/dist/context/index.js.map +1 -0
  14. package/dist/context/lithia-context.d.ts +47 -0
  15. package/dist/context/lithia-context.js +43 -0
  16. package/dist/context/lithia-context.js.map +1 -0
  17. package/dist/context/route-context.d.ts +74 -0
  18. package/dist/context/route-context.js +42 -0
  19. package/dist/context/route-context.js.map +1 -0
  20. package/dist/env.d.ts +1 -0
  21. package/dist/env.js +32 -0
  22. package/dist/env.js.map +1 -0
  23. package/dist/errors.d.ts +51 -0
  24. package/dist/errors.js +80 -0
  25. package/dist/errors.js.map +1 -0
  26. package/dist/hooks/dependency-hooks.d.ts +105 -0
  27. package/dist/hooks/dependency-hooks.js +96 -0
  28. package/dist/hooks/dependency-hooks.js.map +1 -0
  29. package/dist/hooks/event-hooks.d.ts +61 -0
  30. package/dist/hooks/event-hooks.js +70 -0
  31. package/dist/hooks/event-hooks.js.map +1 -0
  32. package/dist/hooks/index.d.ts +41 -0
  33. package/dist/hooks/index.js +59 -0
  34. package/dist/hooks/index.js.map +1 -0
  35. package/dist/hooks/route-hooks.d.ts +154 -0
  36. package/dist/hooks/route-hooks.js +174 -0
  37. package/dist/hooks/route-hooks.js.map +1 -0
  38. package/dist/lib.d.ts +10 -0
  39. package/dist/lib.js +30 -0
  40. package/dist/lib.js.map +1 -0
  41. package/dist/lithia.d.ts +447 -0
  42. package/dist/lithia.js +649 -0
  43. package/dist/lithia.js.map +1 -0
  44. package/dist/logger.d.ts +11 -0
  45. package/dist/logger.js +55 -0
  46. package/dist/logger.js.map +1 -0
  47. package/dist/module-loader.d.ts +12 -0
  48. package/dist/module-loader.js +78 -0
  49. package/dist/module-loader.js.map +1 -0
  50. package/dist/server/event-processor.d.ts +195 -0
  51. package/dist/server/event-processor.js +253 -0
  52. package/dist/server/event-processor.js.map +1 -0
  53. package/dist/server/http-server.d.ts +196 -0
  54. package/dist/server/http-server.js +295 -0
  55. package/dist/server/http-server.js.map +1 -0
  56. package/dist/server/middlewares/validation.d.ts +12 -0
  57. package/dist/server/middlewares/validation.js +34 -0
  58. package/dist/server/middlewares/validation.js.map +1 -0
  59. package/dist/server/request-processor.d.ts +400 -0
  60. package/dist/server/request-processor.js +652 -0
  61. package/dist/server/request-processor.js.map +1 -0
  62. package/dist/server/request.d.ts +73 -0
  63. package/dist/server/request.js +207 -0
  64. package/dist/server/request.js.map +1 -0
  65. package/dist/server/response.d.ts +69 -0
  66. package/dist/server/response.js +173 -0
  67. package/dist/server/response.js.map +1 -0
  68. package/package.json +46 -0
  69. package/src/config.ts +212 -0
  70. package/src/context/event-context.ts +66 -0
  71. package/src/context/index.ts +32 -0
  72. package/src/context/lithia-context.ts +59 -0
  73. package/src/context/route-context.ts +89 -0
  74. package/src/env.ts +31 -0
  75. package/src/errors.ts +96 -0
  76. package/src/hooks/dependency-hooks.ts +122 -0
  77. package/src/hooks/event-hooks.ts +69 -0
  78. package/src/hooks/index.ts +58 -0
  79. package/src/hooks/route-hooks.ts +177 -0
  80. package/src/lib.ts +27 -0
  81. package/src/lithia.ts +777 -0
  82. package/src/logger.ts +66 -0
  83. package/src/module-loader.ts +45 -0
  84. package/src/server/event-processor.ts +344 -0
  85. package/src/server/http-server.ts +371 -0
  86. package/src/server/middlewares/validation.ts +46 -0
  87. package/src/server/request-processor.ts +860 -0
  88. package/src/server/request.ts +247 -0
  89. package/src/server/response.ts +204 -0
  90. package/tsconfig.build.tsbuildinfo +1 -0
  91. package/tsconfig.json +8 -0
@@ -0,0 +1,196 @@
1
+ import { type Server } from "node:http";
2
+ import { type Server as HttpsServer } from "node:https";
3
+ import { Server as SocketIOServer } from "socket.io";
4
+ import type { LithiaOptions } from "../config";
5
+ import type { Lithia } from "../lithia";
6
+ /**
7
+ * Configuration used to create the HTTP server.
8
+ *
9
+ * This interface defines the settings required to initialize and run
10
+ * the Lithia HTTP server, including network bindings and optional SSL.
11
+ */
12
+ export interface HttpServerConfig {
13
+ /**
14
+ * Port to listen on.
15
+ *
16
+ * @default 3000
17
+ */
18
+ port: number;
19
+ /**
20
+ * Hostname or IP address to bind the server to.
21
+ *
22
+ * @default "localhost"
23
+ */
24
+ host: string;
25
+ /**
26
+ * Optional SSL/TLS configuration for HTTPS.
27
+ *
28
+ * If provided, the server will use HTTPS instead of HTTP.
29
+ */
30
+ ssl?: {
31
+ /** Path to the private key file or key content. */
32
+ key: string;
33
+ /** Path to the certificate file or certificate content. */
34
+ cert: string;
35
+ /** Optional passphrase for the private key. */
36
+ passphrase?: string;
37
+ };
38
+ }
39
+ /**
40
+ * Lightweight HTTP server wrapper used by Lithia.
41
+ *
42
+ * This class manages the lifecycle of a Node.js HTTP/HTTPS server and
43
+ * integrates it with Lithia's request processing pipeline and Socket.IO
44
+ * event handling. It provides a clean abstraction over the underlying
45
+ * server infrastructure.
46
+ *
47
+ * @remarks
48
+ * The server supports both HTTP and HTTPS protocols, WebSocket connections
49
+ * via Socket.IO, and graceful shutdown with connection tracking.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const config = { port: 3000, host: 'localhost' };
54
+ * const server = new HttpServer(config, lithia);
55
+ * await server.listen();
56
+ * ```
57
+ */
58
+ export declare class HttpServer {
59
+ private lithia;
60
+ /** The underlying Node.js HTTP or HTTPS server instance. */
61
+ private server?;
62
+ /** Socket.IO server instance for real-time event handling. */
63
+ private io?;
64
+ /** Server configuration (port, host, SSL). */
65
+ private config;
66
+ /** Processor responsible for handling HTTP requests. */
67
+ private requestProcessor;
68
+ /** Processor responsible for handling Socket.IO events. */
69
+ private eventProcessor;
70
+ /** Set of active socket connections for graceful shutdown tracking. */
71
+ private sockets;
72
+ /**
73
+ * Creates a new HTTP server instance.
74
+ *
75
+ * @param config - Server configuration including port, host, and optional SSL
76
+ * @param lithia - The main Lithia application instance
77
+ */
78
+ constructor(config: HttpServerConfig, lithia: Lithia);
79
+ get socketIO(): SocketIOServer | undefined;
80
+ /**
81
+ * Creates the HTTP request handler function.
82
+ *
83
+ * This handler processes all incoming HTTP requests, including the internal
84
+ * `/_lithia` health check endpoint and regular application routes.
85
+ *
86
+ * @returns An async request handler compatible with Node.js http.Server
87
+ * @private
88
+ */
89
+ private createRequestHandler;
90
+ /**
91
+ * Creates the underlying HTTP or HTTPS server instance.
92
+ *
93
+ * Chooses between HTTP and HTTPS based on the SSL configuration.
94
+ * Sets up connection tracking for graceful shutdown.
95
+ *
96
+ * @returns The created server instance
97
+ * @private
98
+ */
99
+ private createBaseServer;
100
+ /**
101
+ * Initializes the Socket.IO server with CORS configuration.
102
+ *
103
+ * Creates a Socket.IO instance attached to the HTTP server and configures
104
+ * it with CORS settings from the Lithia options.
105
+ *
106
+ * @param server - The HTTP/HTTPS server to attach Socket.IO to
107
+ * @returns The configured Socket.IO server instance
108
+ * @private
109
+ */
110
+ private initializeSocketIO;
111
+ /**
112
+ * Sets up Socket.IO event listeners for all registered Lithia events.
113
+ *
114
+ * This method configures handlers for:
115
+ * - Connection events (when a client connects)
116
+ * - Disconnection events (when a client disconnects)
117
+ * - Custom application events (defined in the Lithia app)
118
+ *
119
+ * @private
120
+ */
121
+ private setupSocketIOEventListeners;
122
+ /**
123
+ * Creates (or returns) the underlying Node.js server instance.
124
+ *
125
+ * This method lazily initializes the server and all its dependencies:
126
+ * - HTTP/HTTPS server
127
+ * - Socket.IO server
128
+ * - Event listeners
129
+ *
130
+ * Subsequent calls return the same server instance.
131
+ *
132
+ * @returns The server instance (HTTP or HTTPS depending on configuration)
133
+ */
134
+ create(): Promise<Server | HttpsServer>;
135
+ /**
136
+ * Starts the server and begins listening for connections.
137
+ *
138
+ * This method will create the server if it hasn't been created yet,
139
+ * then bind it to the configured host and port. The promise resolves
140
+ * when the server is successfully listening.
141
+ *
142
+ * @throws {Error} If the server fails to start or if the port is already in use
143
+ * @returns A promise that resolves when the server is listening
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * await server.listen();
148
+ * // Server is now accepting connections
149
+ * ```
150
+ */
151
+ listen(): Promise<void>;
152
+ /**
153
+ * Gracefully shuts down the server.
154
+ *
155
+ * This method performs a clean shutdown by:
156
+ * 1. Closing the Socket.IO server and all WebSocket connections
157
+ * 2. Stopping the HTTP/HTTPS server from accepting new connections
158
+ * 3. Destroying all active socket connections
159
+ * 4. Clearing the socket tracking set
160
+ *
161
+ * @throws {Error} If an error occurs during shutdown
162
+ * @returns A promise that resolves when the server is fully closed
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * await server.close();
167
+ * // Server is now fully shut down
168
+ * ```
169
+ */
170
+ close(): Promise<void>;
171
+ }
172
+ /**
173
+ * Creates an HTTP server instance from Lithia configuration.
174
+ *
175
+ * This factory function is the primary way to create an HttpServer from
176
+ * a complete Lithia configuration object. It extracts the necessary HTTP
177
+ * settings and instantiates the server.
178
+ *
179
+ * @param opts - Configuration object
180
+ * @param opts.options - The complete Lithia options including HTTP settings
181
+ * @param opts.lithia - The main Lithia application instance
182
+ * @returns A configured HttpServer instance ready to be started
183
+ *
184
+ * @example
185
+ * ```typescript
186
+ * const server = createHttpServerFromConfig({
187
+ * options: lithiaOptions,
188
+ * lithia: lithiaInstance
189
+ * });
190
+ * await server.listen();
191
+ * ```
192
+ */
193
+ export declare function createHttpServerFromConfig(opts: {
194
+ options: LithiaOptions;
195
+ lithia: Lithia;
196
+ }): HttpServer;
@@ -0,0 +1,295 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpServer = void 0;
4
+ exports.createHttpServerFromConfig = createHttpServerFromConfig;
5
+ const node_http_1 = require("node:http");
6
+ const node_https_1 = require("node:https");
7
+ const socket_io_1 = require("socket.io");
8
+ const logger_1 = require("../logger");
9
+ const event_processor_1 = require("./event-processor");
10
+ const request_1 = require("./request");
11
+ const request_processor_1 = require("./request-processor");
12
+ const response_1 = require("./response");
13
+ /**
14
+ * Lightweight HTTP server wrapper used by Lithia.
15
+ *
16
+ * This class manages the lifecycle of a Node.js HTTP/HTTPS server and
17
+ * integrates it with Lithia's request processing pipeline and Socket.IO
18
+ * event handling. It provides a clean abstraction over the underlying
19
+ * server infrastructure.
20
+ *
21
+ * @remarks
22
+ * The server supports both HTTP and HTTPS protocols, WebSocket connections
23
+ * via Socket.IO, and graceful shutdown with connection tracking.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const config = { port: 3000, host: 'localhost' };
28
+ * const server = new HttpServer(config, lithia);
29
+ * await server.listen();
30
+ * ```
31
+ */
32
+ class HttpServer {
33
+ lithia;
34
+ /** The underlying Node.js HTTP or HTTPS server instance. */
35
+ server;
36
+ /** Socket.IO server instance for real-time event handling. */
37
+ io;
38
+ /** Server configuration (port, host, SSL). */
39
+ config;
40
+ /** Processor responsible for handling HTTP requests. */
41
+ requestProcessor;
42
+ /** Processor responsible for handling Socket.IO events. */
43
+ eventProcessor;
44
+ /** Set of active socket connections for graceful shutdown tracking. */
45
+ sockets = new Set();
46
+ /**
47
+ * Creates a new HTTP server instance.
48
+ *
49
+ * @param config - Server configuration including port, host, and optional SSL
50
+ * @param lithia - The main Lithia application instance
51
+ */
52
+ constructor(config, lithia) {
53
+ this.lithia = lithia;
54
+ this.config = config;
55
+ this.requestProcessor = new request_processor_1.RequestProcessor(lithia, this);
56
+ this.eventProcessor = new event_processor_1.EventProcessor(lithia);
57
+ }
58
+ get socketIO() {
59
+ return this.io;
60
+ }
61
+ /**
62
+ * Creates the HTTP request handler function.
63
+ *
64
+ * This handler processes all incoming HTTP requests, including the internal
65
+ * `/_lithia` health check endpoint and regular application routes.
66
+ *
67
+ * @returns An async request handler compatible with Node.js http.Server
68
+ * @private
69
+ */
70
+ createRequestHandler() {
71
+ return async (req, res) => {
72
+ try {
73
+ // Handle /_lithia internal health check endpoint
74
+ const url = req.url || "/";
75
+ if (url === "/_lithia") {
76
+ res.writeHead(200, { "Content-Type": "application/json" });
77
+ res.end(JSON.stringify({ ok: true, ts: Date.now() }));
78
+ return;
79
+ }
80
+ // Process request through the Lithia pipeline
81
+ const lithiaReq = new request_1.LithiaRequest(req, this.lithia);
82
+ const lithiaRes = new response_1.LithiaResponse(res);
83
+ await this.requestProcessor.processRequest(lithiaReq, lithiaRes);
84
+ }
85
+ catch (err) {
86
+ logger_1.logger.error("HttpServer request handler error:", err);
87
+ try {
88
+ res.writeHead(500, { "Content-Type": "text/plain" });
89
+ res.end("Internal Server Error");
90
+ }
91
+ catch (_) {
92
+ // Ignore errors when attempting to send error response
93
+ }
94
+ }
95
+ };
96
+ }
97
+ /**
98
+ * Creates the underlying HTTP or HTTPS server instance.
99
+ *
100
+ * Chooses between HTTP and HTTPS based on the SSL configuration.
101
+ * Sets up connection tracking for graceful shutdown.
102
+ *
103
+ * @returns The created server instance
104
+ * @private
105
+ */
106
+ createBaseServer() {
107
+ const handler = this.createRequestHandler();
108
+ const server = this.config.ssl
109
+ ? (0, node_https_1.createServer)(this.config.ssl, handler)
110
+ : (0, node_http_1.createServer)(handler);
111
+ // Track connections for graceful shutdown
112
+ server.on("connection", (socket) => {
113
+ this.sockets.add(socket);
114
+ socket.on("close", () => {
115
+ this.sockets.delete(socket);
116
+ });
117
+ });
118
+ return server;
119
+ }
120
+ /**
121
+ * Initializes the Socket.IO server with CORS configuration.
122
+ *
123
+ * Creates a Socket.IO instance attached to the HTTP server and configures
124
+ * it with CORS settings from the Lithia options.
125
+ *
126
+ * @param server - The HTTP/HTTPS server to attach Socket.IO to
127
+ * @returns The configured Socket.IO server instance
128
+ * @private
129
+ */
130
+ initializeSocketIO(server) {
131
+ return new socket_io_1.Server(server, {
132
+ cors: {
133
+ origin: this.lithia.options.http.cors?.origin || "*",
134
+ methods: this.lithia.options.http.cors?.methods || ["GET", "POST"],
135
+ credentials: this.lithia.options.http.cors?.credentials ?? true,
136
+ },
137
+ });
138
+ }
139
+ /**
140
+ * Sets up Socket.IO event listeners for all registered Lithia events.
141
+ *
142
+ * This method configures handlers for:
143
+ * - Connection events (when a client connects)
144
+ * - Disconnection events (when a client disconnects)
145
+ * - Custom application events (defined in the Lithia app)
146
+ *
147
+ * @private
148
+ */
149
+ setupSocketIOEventListeners() {
150
+ if (!this.io)
151
+ return;
152
+ const events = this.lithia.getEvents();
153
+ const connectionEvent = events.find((e) => e.name === "connection");
154
+ const disconnectEvent = events.find((e) => e.name === "disconnect");
155
+ this.io.on("connection", async (socket) => {
156
+ // Handle connection event
157
+ if (connectionEvent) {
158
+ await this.eventProcessor.processEvent(socket, connectionEvent);
159
+ }
160
+ // Handle disconnect event
161
+ socket.on("disconnect", async () => {
162
+ if (disconnectEvent) {
163
+ await this.eventProcessor.processEvent(socket, disconnectEvent);
164
+ }
165
+ });
166
+ // Handle all custom events
167
+ socket.onAny(async (eventName, ...args) => {
168
+ const event = events.find((e) => e.name === eventName);
169
+ if (event) {
170
+ await this.eventProcessor.processEvent(socket, event, ...args);
171
+ }
172
+ });
173
+ });
174
+ }
175
+ /**
176
+ * Creates (or returns) the underlying Node.js server instance.
177
+ *
178
+ * This method lazily initializes the server and all its dependencies:
179
+ * - HTTP/HTTPS server
180
+ * - Socket.IO server
181
+ * - Event listeners
182
+ *
183
+ * Subsequent calls return the same server instance.
184
+ *
185
+ * @returns The server instance (HTTP or HTTPS depending on configuration)
186
+ */
187
+ async create() {
188
+ if (this.server)
189
+ return this.server;
190
+ this.server = this.createBaseServer();
191
+ this.io = this.initializeSocketIO(this.server);
192
+ this.setupSocketIOEventListeners();
193
+ return this.server;
194
+ }
195
+ /**
196
+ * Starts the server and begins listening for connections.
197
+ *
198
+ * This method will create the server if it hasn't been created yet,
199
+ * then bind it to the configured host and port. The promise resolves
200
+ * when the server is successfully listening.
201
+ *
202
+ * @throws {Error} If the server fails to start or if the port is already in use
203
+ * @returns A promise that resolves when the server is listening
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * await server.listen();
208
+ * // Server is now accepting connections
209
+ * ```
210
+ */
211
+ async listen() {
212
+ if (!this.server)
213
+ await this.create();
214
+ return new Promise((resolve, reject) => {
215
+ if (!this.server)
216
+ return reject(new Error("Server not created"));
217
+ // If already listening, resolve immediately
218
+ if (this.server.listening)
219
+ return resolve();
220
+ this.server.listen(this.config.port, this.config.host, () => {
221
+ logger_1.logger.event(`Server listening on http://${this.config.host}:${this.config.port}`);
222
+ resolve();
223
+ });
224
+ this.server.on("error", (err) => {
225
+ logger_1.logger.error("Server error:", err);
226
+ reject(err);
227
+ });
228
+ });
229
+ }
230
+ /**
231
+ * Gracefully shuts down the server.
232
+ *
233
+ * This method performs a clean shutdown by:
234
+ * 1. Closing the Socket.IO server and all WebSocket connections
235
+ * 2. Stopping the HTTP/HTTPS server from accepting new connections
236
+ * 3. Destroying all active socket connections
237
+ * 4. Clearing the socket tracking set
238
+ *
239
+ * @throws {Error} If an error occurs during shutdown
240
+ * @returns A promise that resolves when the server is fully closed
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * await server.close();
245
+ * // Server is now fully shut down
246
+ * ```
247
+ */
248
+ async close() {
249
+ if (!this.server)
250
+ return;
251
+ // Close Socket.IO server
252
+ await this.io?.close();
253
+ // Close HTTP/HTTPS server
254
+ this.server.close((err) => {
255
+ if (err)
256
+ throw err;
257
+ });
258
+ // Destroy all active connections
259
+ for (const socket of this.sockets) {
260
+ socket.destroy();
261
+ }
262
+ this.sockets.clear();
263
+ }
264
+ }
265
+ exports.HttpServer = HttpServer;
266
+ /**
267
+ * Creates an HTTP server instance from Lithia configuration.
268
+ *
269
+ * This factory function is the primary way to create an HttpServer from
270
+ * a complete Lithia configuration object. It extracts the necessary HTTP
271
+ * settings and instantiates the server.
272
+ *
273
+ * @param opts - Configuration object
274
+ * @param opts.options - The complete Lithia options including HTTP settings
275
+ * @param opts.lithia - The main Lithia application instance
276
+ * @returns A configured HttpServer instance ready to be started
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * const server = createHttpServerFromConfig({
281
+ * options: lithiaOptions,
282
+ * lithia: lithiaInstance
283
+ * });
284
+ * await server.listen();
285
+ * ```
286
+ */
287
+ function createHttpServerFromConfig(opts) {
288
+ const cfg = {
289
+ port: opts.options.http.port,
290
+ host: opts.options.http.host,
291
+ ssl: opts.options.http.ssl,
292
+ };
293
+ return new HttpServer(cfg, opts.lithia);
294
+ }
295
+ //# sourceMappingURL=http-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-server.js","sourceRoot":"","sources":["../../src/server/http-server.ts"],"names":[],"mappings":";;;AAuWA,gEAWC;AAlXD,yCAKmB;AACnB,2CAGoB;AAEpB,yCAAqD;AAGrD,sCAAmC;AACnC,uDAAmD;AACnD,uCAA0C;AAC1C,2DAAuD;AACvD,yCAA4C;AAsC5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAa,UAAU;IA2Bb;IA1BT,4DAA4D;IACpD,MAAM,CAAwB;IAEtC,8DAA8D;IACtD,EAAE,CAAkB;IAE5B,8CAA8C;IACtC,MAAM,CAAmB;IAEjC,wDAAwD;IAChD,gBAAgB,CAAmB;IAE3C,2DAA2D;IACnD,cAAc,CAAiB;IAEvC,uEAAuE;IAC/D,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAEpC;;;;;OAKG;IACH,YACC,MAAwB,EAChB,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;QAEtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,gBAAgB,GAAG,IAAI,oCAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC3D,IAAI,CAAC,cAAc,GAAG,IAAI,gCAAc,CAAC,MAAM,C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yB;QACzB,MAAM,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC;QAEvB,0BAA0B;QAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACzB,IAAI,GAAG;gBAAE,MAAM,GAAG,CAAC;QACpB,CAAC,CAAC,CAAC;QAEH,iCAAiC;QACjC,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACnC,MAAM,CAAC,OAAO,EAAE,CAAC;QAClB,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACD;AArQD,gCAqQC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,0BAA0B,CAAC,IAG1C;IACA,MAAM,GAAG,GAAqB;QAC7B,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QAC5B,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QAC5B,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG;KAC1B,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,12 @@
1
+ import { type ZodType } from "zod";
2
+ import type { LithiaMiddleware } from "../request-processor";
3
+ export interface ValidationSchemas {
4
+ body?: ZodType;
5
+ query?: ZodType;
6
+ params?: ZodType;
7
+ }
8
+ /**
9
+ * Creates a middleware that validates request data against Zod schemas.
10
+ * Validated data is assigned back to the request object.
11
+ */
12
+ export declare function validate(schemas: ValidationSchemas): LithiaMiddleware;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validate = validate;
4
+ const zod_1 = require("zod");
5
+ const errors_1 = require("../../errors");
6
+ /**
7
+ * Creates a middleware that validates request data against Zod schemas.
8
+ * Validated data is assigned back to the request object.
9
+ */
10
+ function validate(schemas) {
11
+ return async (req, _res, next) => {
12
+ try {
13
+ if (schemas.params) {
14
+ req.params = (await schemas.params.parseAsync(req.params));
15
+ }
16
+ if (schemas.query) {
17
+ req.query = (await schemas.query.parseAsync(req.query));
18
+ }
19
+ if (schemas.body) {
20
+ const body = await req.body();
21
+ const validatedBody = await schemas.body.parseAsync(body);
22
+ req.setBody(validatedBody);
23
+ }
24
+ next();
25
+ }
26
+ catch (err) {
27
+ if (err instanceof zod_1.ZodError) {
28
+ throw new errors_1.ValidationError("Validation failed", err.issues);
29
+ }
30
+ throw err;
31
+ }
32
+ };
33
+ }
34
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../src/server/middlewares/validation.ts"],"names":[],"mappings":";;AAcA,4BA+BC;AA7CD,6BAA6C;AAC7C,yCAA+C;AAS/C;;;GAGG;AACH,SAAgB,QAAQ,CAAC,OAA0B;IAClD,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QAChC,IAAI,CAAC;YACJ,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;gBACpB,GAAG,CAAC,MAAM,GAAG,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAGxD,CAAC;YACH,CAAC;YAED,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;gBACnB,GAAG,CAAC,KAAK,GAAG,CAAC,MAAM,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAGrD,CAAC;YACH,CAAC;YAED,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;gBAClB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;gBAC9B,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;gBAC1D,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;YAC5B,CAAC;YAED,IAAI,EAAE,CAAC;QACR,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,GAAG,YAAY,cAAQ,EAAE,CAAC;gBAC7B,MAAM,IAAI,wBAAe,CAAC,mBAAmB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;YAC5D,CAAC;YACD,MAAM,GAAG,CAAC;QACX,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}