@titanpl/route 2.0.3 → 6.0.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.
package/README.md CHANGED
@@ -14,6 +14,9 @@ import t from "@titanpl/route";
14
14
  // Define an action-based route
15
15
  t.post("/hello").action("hello"); // pass a json payload { "name": "titan" }
16
16
 
17
+ // Define a WebSocket route
18
+ t.ws("/chat").action("chat");
19
+
17
20
  // Define a direct reply route
18
21
  t.get("/").reply("Ready to land on Titan Planet 🚀");
19
22
 
@@ -21,4 +24,22 @@ t.get("/").reply("Ready to land on Titan Planet 🚀");
21
24
  t.start(5100, "Titan Running!");
22
25
  ```
23
26
 
27
+ ### WebSocket Action Handling
28
+ When using `t.ws()`, your action receives the `event`, `socketId`, and `body`:
29
+
30
+ ```javascript
31
+ export const chat = (req) => {
32
+ const { event, socketId, body } = req;
33
+
34
+ if (event === "open") {
35
+ t.ws.send(socketId, "Connected to Titan Orbital Hub!");
36
+ t.ws.broadcast(`User ${socketId} has docked.`);
37
+ }
38
+
39
+ if (event === "message") {
40
+ t.ws.broadcast(`${socketId}: ${body}`);
41
+ }
42
+ };
43
+ ```
44
+
24
45
  **Important Note:** Currently, Titan Planet and its entire package ecosystem are only for Windows. The Linux version is in development (dev only) for the new architecture and will be launched later.
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface TitanRoute {
8
8
  post(route: string): RouteBuilder;
9
9
  put(route: string): RouteBuilder;
10
10
  delete(route: string): RouteBuilder;
11
+ ws(route: string): { action(name: string): void };
11
12
  log(module: string, msg: string): void;
12
13
  start(port?: number, msg?: string, threads?: number, stack_mb?: number): void;
13
14
  }
package/index.js CHANGED
@@ -42,6 +42,28 @@ const t = {
42
42
 
43
43
  start(port = 3000, msg = "", threads, stack_mb = 8) {
44
44
  globalThis.__TITAN_CONFIG__ = { port, msg, threads, stack_mb };
45
+ },
46
+
47
+ ws(route) {
48
+ return {
49
+ action(name) {
50
+ if (route.includes(":")) {
51
+ if (!dynamicRoutes["WS"]) dynamicRoutes["WS"] = [];
52
+ dynamicRoutes["WS"].push({
53
+ method: "WS",
54
+ pattern: route,
55
+ action: name
56
+ });
57
+ actionMap[`WS:${route}`] = name;
58
+ } else {
59
+ routes[`WS:${route}`] = {
60
+ type: "websocket",
61
+ value: name
62
+ };
63
+ actionMap[`WS:${route}`] = name;
64
+ }
65
+ }
66
+ };
45
67
  }
46
68
  };
47
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@titanpl/route",
3
- "version": "2.0.3",
3
+ "version": "6.0.0",
4
4
  "description": "Titan routing DSL",
5
5
  "type": "module",
6
6
  "main": "index.js",