@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,154 @@
1
+ /**
2
+ * HTTP route hooks for Lithia.
3
+ *
4
+ * Provides convenient hooks to access request-specific data in route handlers.
5
+ * All hooks must be called within an HTTP request handler or middleware.
6
+ *
7
+ * @module hooks/route-hooks
8
+ */
9
+ import type { IncomingHttpHeaders } from "node:http";
10
+ import type { Route } from "@lithia-js/native";
11
+ import type { Server } from "socket.io";
12
+ import type { LithiaRequest, Params, Query } from "../server/request";
13
+ import type { LithiaResponse } from "../server/response";
14
+ /**
15
+ * Accesses the current HTTP request object.
16
+ *
17
+ * Provides access to all request properties including params, query,
18
+ * headers, body, and other request metadata.
19
+ *
20
+ * @returns The current LithiaRequest instance
21
+ * @throws {Error} If called outside of a request handler
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * export default async function handler() {
26
+ * const req = useRequest();
27
+ * console.log(req.method, req.url);
28
+ * }
29
+ * ```
30
+ */
31
+ export declare function useRequest(): LithiaRequest;
32
+ /**
33
+ * Accesses the current HTTP response object.
34
+ *
35
+ * Used to send responses back to the client, set headers, status codes, etc.
36
+ *
37
+ * @returns The current LithiaResponse instance
38
+ * @throws {Error} If called outside of a request handler
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * export default async function handler() {
43
+ * const res = useResponse();
44
+ * res.status(200).json({ message: 'Success' });
45
+ * }
46
+ * ```
47
+ */
48
+ export declare function useResponse(): LithiaResponse;
49
+ /**
50
+ * Accesses the current matched route definition.
51
+ *
52
+ * Contains metadata about the current route including path pattern,
53
+ * HTTP method, and handler information.
54
+ *
55
+ * @returns The matched Route, or undefined if no route matched or called before route resolution
56
+ * @throws {Error} If called outside of a request handler
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * export default async function handler() {
61
+ * const route = useRoute();
62
+ * console.log('Handling route:', route?.path);
63
+ * }
64
+ * ```
65
+ */
66
+ export declare function useRoute(): Route | undefined;
67
+ /**
68
+ * Accesses route parameters (dynamic segments).
69
+ *
70
+ * Extracts parameters from dynamic route segments like `/users/:id`.
71
+ *
72
+ * @returns Object containing route parameters
73
+ * @throws {Error} If called outside of a request handler
74
+ * @template T - Type of the params object
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * // Route: /users/:id
79
+ * export default async function handler() {
80
+ * const { id } = useParams<{ id: string }>();
81
+ * console.log('User ID:', id);
82
+ * }
83
+ * ```
84
+ */
85
+ export declare function useParams<T extends Params = Params>(): T;
86
+ /**
87
+ * Accesses URL query parameters.
88
+ *
89
+ * Extracts query string parameters from the request URL.
90
+ *
91
+ * @returns Object containing query parameters
92
+ * @throws {Error} If called outside of a request handler
93
+ * @template T - Type of the query object
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * // URL: /search?q=lithia&page=1
98
+ * export default async function handler() {
99
+ * const { q, page } = useQuery<{ q: string; page: string }>();
100
+ * console.log('Search:', q, 'Page:', page);
101
+ * }
102
+ * ```
103
+ */
104
+ export declare function useQuery<T extends Query = Query>(): T;
105
+ /**
106
+ * Accesses HTTP request headers.
107
+ *
108
+ * Returns all headers sent with the request.
109
+ *
110
+ * @returns Object containing HTTP headers
111
+ * @throws {Error} If called outside of a request handler
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * export default async function handler() {
116
+ * const headers = useHeaders();
117
+ * const auth = headers.authorization;
118
+ * console.log('Auth header:', auth);
119
+ * }
120
+ * ```
121
+ */
122
+ export declare function useHeaders(): IncomingHttpHeaders;
123
+ /**
124
+ * Accesses the Socket.IO server instance from within an HTTP route handler.
125
+ *
126
+ * This hook allows HTTP routes to interact with Socket.IO, enabling you to
127
+ * emit events to connected clients, broadcast messages, or manage rooms in
128
+ * response to HTTP requests.
129
+ *
130
+ * @returns The Socket.IO Server instance
131
+ * @throws {Error} If called outside of a request handler
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * // HTTP route that triggers real-time updates
136
+ * export default async function handler() {
137
+ * const io = useSocketServer();
138
+ * const { message } = useQuery<{ message: string }>();
139
+ *
140
+ * // Broadcast to all connected clients
141
+ * io.emit('notification', { message, timestamp: Date.now() });
142
+ *
143
+ * // Emit to a specific room
144
+ * io.to('admin-room').emit('alert', { message });
145
+ *
146
+ * // Get all connected sockets
147
+ * const sockets = await io.fetchSockets();
148
+ * console.log(`Broadcasting to ${sockets.length} clients`);
149
+ *
150
+ * return { success: true, clients: sockets.length };
151
+ * }
152
+ * ```
153
+ */
154
+ export declare function useSocketServer(): Server;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ /**
3
+ * HTTP route hooks for Lithia.
4
+ *
5
+ * Provides convenient hooks to access request-specific data in route handlers.
6
+ * All hooks must be called within an HTTP request handler or middleware.
7
+ *
8
+ * @module hooks/route-hooks
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.useRequest = useRequest;
12
+ exports.useResponse = useResponse;
13
+ exports.useRoute = useRoute;
14
+ exports.useParams = useParams;
15
+ exports.useQuery = useQuery;
16
+ exports.useHeaders = useHeaders;
17
+ exports.useSocketServer = useSocketServer;
18
+ const route_context_1 = require("../context/route-context");
19
+ /**
20
+ * Accesses the current HTTP request object.
21
+ *
22
+ * Provides access to all request properties including params, query,
23
+ * headers, body, and other request metadata.
24
+ *
25
+ * @returns The current LithiaRequest instance
26
+ * @throws {Error} If called outside of a request handler
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * export default async function handler() {
31
+ * const req = useRequest();
32
+ * console.log(req.method, req.url);
33
+ * }
34
+ * ```
35
+ */
36
+ function useRequest() {
37
+ return (0, route_context_1.getRouteContext)().req;
38
+ }
39
+ /**
40
+ * Accesses the current HTTP response object.
41
+ *
42
+ * Used to send responses back to the client, set headers, status codes, etc.
43
+ *
44
+ * @returns The current LithiaResponse instance
45
+ * @throws {Error} If called outside of a request handler
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * export default async function handler() {
50
+ * const res = useResponse();
51
+ * res.status(200).json({ message: 'Success' });
52
+ * }
53
+ * ```
54
+ */
55
+ function useResponse() {
56
+ return (0, route_context_1.getRouteContext)().res;
57
+ }
58
+ /**
59
+ * Accesses the current matched route definition.
60
+ *
61
+ * Contains metadata about the current route including path pattern,
62
+ * HTTP method, and handler information.
63
+ *
64
+ * @returns The matched Route, or undefined if no route matched or called before route resolution
65
+ * @throws {Error} If called outside of a request handler
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * export default async function handler() {
70
+ * const route = useRoute();
71
+ * console.log('Handling route:', route?.path);
72
+ * }
73
+ * ```
74
+ */
75
+ function useRoute() {
76
+ return (0, route_context_1.getRouteContext)().route;
77
+ }
78
+ /**
79
+ * Accesses route parameters (dynamic segments).
80
+ *
81
+ * Extracts parameters from dynamic route segments like `/users/:id`.
82
+ *
83
+ * @returns Object containing route parameters
84
+ * @throws {Error} If called outside of a request handler
85
+ * @template T - Type of the params object
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * // Route: /users/:id
90
+ * export default async function handler() {
91
+ * const { id } = useParams<{ id: string }>();
92
+ * console.log('User ID:', id);
93
+ * }
94
+ * ```
95
+ */
96
+ function useParams() {
97
+ return (0, route_context_1.getRouteContext)().req.params;
98
+ }
99
+ /**
100
+ * Accesses URL query parameters.
101
+ *
102
+ * Extracts query string parameters from the request URL.
103
+ *
104
+ * @returns Object containing query parameters
105
+ * @throws {Error} If called outside of a request handler
106
+ * @template T - Type of the query object
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * // URL: /search?q=lithia&page=1
111
+ * export default async function handler() {
112
+ * const { q, page } = useQuery<{ q: string; page: string }>();
113
+ * console.log('Search:', q, 'Page:', page);
114
+ * }
115
+ * ```
116
+ */
117
+ function useQuery() {
118
+ return (0, route_context_1.getRouteContext)().req.query;
119
+ }
120
+ /**
121
+ * Accesses HTTP request headers.
122
+ *
123
+ * Returns all headers sent with the request.
124
+ *
125
+ * @returns Object containing HTTP headers
126
+ * @throws {Error} If called outside of a request handler
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * export default async function handler() {
131
+ * const headers = useHeaders();
132
+ * const auth = headers.authorization;
133
+ * console.log('Auth header:', auth);
134
+ * }
135
+ * ```
136
+ */
137
+ function useHeaders() {
138
+ return (0, route_context_1.getRouteContext)().req.headers;
139
+ }
140
+ /**
141
+ * Accesses the Socket.IO server instance from within an HTTP route handler.
142
+ *
143
+ * This hook allows HTTP routes to interact with Socket.IO, enabling you to
144
+ * emit events to connected clients, broadcast messages, or manage rooms in
145
+ * response to HTTP requests.
146
+ *
147
+ * @returns The Socket.IO Server instance
148
+ * @throws {Error} If called outside of a request handler
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * // HTTP route that triggers real-time updates
153
+ * export default async function handler() {
154
+ * const io = useSocketServer();
155
+ * const { message } = useQuery<{ message: string }>();
156
+ *
157
+ * // Broadcast to all connected clients
158
+ * io.emit('notification', { message, timestamp: Date.now() });
159
+ *
160
+ * // Emit to a specific room
161
+ * io.to('admin-room').emit('alert', { message });
162
+ *
163
+ * // Get all connected sockets
164
+ * const sockets = await io.fetchSockets();
165
+ * console.log(`Broadcasting to ${sockets.length} clients`);
166
+ *
167
+ * return { success: true, clients: sockets.length };
168
+ * }
169
+ * ```
170
+ */
171
+ function useSocketServer() {
172
+ return (0, route_context_1.getRouteContext)().socketServer;
173
+ }
174
+ //# sourceMappingURL=route-hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route-hooks.js","sourceRoot":"","sources":["../../src/hooks/route-hooks.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AA0BH,gCAEC;AAkBD,kCAEC;AAmBD,4BAEC;AAoBD,8BAEC;AAoBD,4BAEC;AAmBD,gCAEC;AAiCD,0CAEC;AApKD,4DAA2D;AAI3D;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,UAAU;IACzB,OAAO,IAAA,+BAAe,GAAE,CAAC,GAAG,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,WAAW;IAC1B,OAAO,IAAA,+BAAe,GAAE,CAAC,GAAG,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,QAAQ;IACvB,OAAO,IAAA,+BAAe,GAAE,CAAC,KAAK,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,SAAS;IACxB,OAAO,IAAA,+BAAe,GAAE,CAAC,GAAG,CAAC,MAAW,CAAC;AAC1C,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,QAAQ;IACvB,OAAO,IAAA,+BAAe,GAAE,CAAC,GAAG,CAAC,KAAU,CAAC;AACzC,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,UAAU;IACzB,OAAO,IAAA,+BAAe,GAAE,CAAC,GAAG,CAAC,OAAO,CAAC;AACtC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,SAAgB,eAAe;IAC9B,OAAO,IAAA,+BAAe,GAAE,CAAC,YAAY,CAAC;AACvC,CAAC"}
package/dist/lib.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { defineConfig, LithiaConfig } from "./config";
2
+ export { loadEnv } from "./env";
3
+ export { InjectionKey, inject, injectOptional, provide, useData, useHeaders, useParams, useQuery, useRequest, useResponse, useRoute, useSocketServer, } from "./hooks";
4
+ export { Lithia } from "./lithia";
5
+ export { logger } from "./logger";
6
+ export { EventErrorInfo, EventHandler } from "./server/event-processor";
7
+ export { validate } from "./server/middlewares/validation";
8
+ export { LithiaRequest, Params, Query } from "./server/request";
9
+ export { LithiaHandler, LithiaMiddleware, RequestErrorInfo, } from "./server/request-processor";
10
+ export { LithiaResponse } from "./server/response";
package/dist/lib.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LithiaResponse = exports.LithiaRequest = exports.validate = exports.logger = exports.Lithia = exports.useSocketServer = exports.useRoute = exports.useResponse = exports.useRequest = exports.useQuery = exports.useParams = exports.useHeaders = exports.useData = exports.provide = exports.injectOptional = exports.inject = exports.loadEnv = exports.defineConfig = void 0;
4
+ var config_1 = require("./config");
5
+ Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return config_1.defineConfig; } });
6
+ var env_1 = require("./env");
7
+ Object.defineProperty(exports, "loadEnv", { enumerable: true, get: function () { return env_1.loadEnv; } });
8
+ var hooks_1 = require("./hooks");
9
+ Object.defineProperty(exports, "inject", { enumerable: true, get: function () { return hooks_1.inject; } });
10
+ Object.defineProperty(exports, "injectOptional", { enumerable: true, get: function () { return hooks_1.injectOptional; } });
11
+ Object.defineProperty(exports, "provide", { enumerable: true, get: function () { return hooks_1.provide; } });
12
+ Object.defineProperty(exports, "useData", { enumerable: true, get: function () { return hooks_1.useData; } });
13
+ Object.defineProperty(exports, "useHeaders", { enumerable: true, get: function () { return hooks_1.useHeaders; } });
14
+ Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return hooks_1.useParams; } });
15
+ Object.defineProperty(exports, "useQuery", { enumerable: true, get: function () { return hooks_1.useQuery; } });
16
+ Object.defineProperty(exports, "useRequest", { enumerable: true, get: function () { return hooks_1.useRequest; } });
17
+ Object.defineProperty(exports, "useResponse", { enumerable: true, get: function () { return hooks_1.useResponse; } });
18
+ Object.defineProperty(exports, "useRoute", { enumerable: true, get: function () { return hooks_1.useRoute; } });
19
+ Object.defineProperty(exports, "useSocketServer", { enumerable: true, get: function () { return hooks_1.useSocketServer; } });
20
+ var lithia_1 = require("./lithia");
21
+ Object.defineProperty(exports, "Lithia", { enumerable: true, get: function () { return lithia_1.Lithia; } });
22
+ var logger_1 = require("./logger");
23
+ Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.logger; } });
24
+ var validation_1 = require("./server/middlewares/validation");
25
+ Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return validation_1.validate; } });
26
+ var request_1 = require("./server/request");
27
+ Object.defineProperty(exports, "LithiaRequest", { enumerable: true, get: function () { return request_1.LithiaRequest; } });
28
+ var response_1 = require("./server/response");
29
+ Object.defineProperty(exports, "LithiaResponse", { enumerable: true, get: function () { return response_1.LithiaResponse; } });
30
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":";;;AAAA,mCAAsD;AAA7C,sGAAA,YAAY,OAAA;AACrB,6BAAgC;AAAvB,8FAAA,OAAO,OAAA;AAChB,iCAaiB;AAXhB,+FAAA,MAAM,OAAA;AACN,uGAAA,cAAc,OAAA;AACd,gGAAA,OAAO,OAAA;AACP,gGAAA,OAAO,OAAA;AACP,mGAAA,UAAU,OAAA;AACV,kGAAA,SAAS,OAAA;AACT,iGAAA,QAAQ,OAAA;AACR,mGAAA,UAAU,OAAA;AACV,oGAAA,WAAW,OAAA;AACX,iGAAA,QAAQ,OAAA;AACR,wGAAA,eAAe,OAAA;AAEhB,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AAEf,8DAA2D;AAAlD,sGAAA,QAAQ,OAAA;AACjB,4CAAgE;AAAvD,wGAAA,aAAa,OAAA;AAMtB,8CAAmD;AAA1C,0GAAA,cAAc,OAAA"}