@titanpl/route 1.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.
Files changed (3) hide show
  1. package/index.d.ts +16 -0
  2. package/index.js +52 -0
  3. package/package.json +14 -0
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export interface RouteBuilder {
2
+ reply(value: any): void;
3
+ action(name: string): void;
4
+ }
5
+
6
+ export interface TitanRoute {
7
+ get(route: string): RouteBuilder;
8
+ post(route: string): RouteBuilder;
9
+ put(route: string): RouteBuilder;
10
+ delete(route: string): RouteBuilder;
11
+ log(module: string, msg: string): void;
12
+ start(port?: number, msg?: string, threads?: number, stack_mb?: number): void;
13
+ }
14
+
15
+ declare const t: TitanRoute;
16
+ export default t;
package/index.js ADDED
@@ -0,0 +1,52 @@
1
+ const routes = {};
2
+ const dynamicRoutes = {};
3
+ const actionMap = {};
4
+
5
+ function addRoute(method, route) {
6
+ const key = `${method.toUpperCase()}:${route}`;
7
+
8
+ return {
9
+ reply(value) {
10
+ routes[key] = {
11
+ type: typeof value === "object" ? "json" : "text",
12
+ value
13
+ };
14
+ },
15
+
16
+ action(name) {
17
+ if (route.includes(":")) {
18
+ if (!dynamicRoutes[method]) dynamicRoutes[method] = [];
19
+ dynamicRoutes[method].push({
20
+ method: method.toUpperCase(),
21
+ pattern: route,
22
+ action: name
23
+ });
24
+ actionMap[key] = name;
25
+ } else {
26
+ routes[key] = {
27
+ type: "action",
28
+ value: name
29
+ };
30
+ actionMap[key] = name;
31
+ }
32
+ }
33
+ };
34
+ }
35
+
36
+ const t = {
37
+ get(route) { return addRoute("GET", route); },
38
+ post(route) { return addRoute("POST", route); },
39
+ put(route) { return addRoute("PUT", route); },
40
+ delete(route) { return addRoute("DELETE", route); },
41
+ log(module, msg) { console.log(`[${module}] ${msg}`); },
42
+
43
+ start(port = 3000, msg = "", threads, stack_mb = 8) {
44
+ globalThis.__TITAN_CONFIG__ = { port, msg, threads, stack_mb };
45
+ }
46
+ };
47
+
48
+ globalThis.__TITAN_ROUTES_MAP__ = routes;
49
+ globalThis.__TITAN_DYNAMIC_ROUTES__ = dynamicRoutes;
50
+ globalThis.__TITAN_ACTION_MAP__ = actionMap;
51
+
52
+ export default t;
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@titanpl/route",
3
+ "version": "1.0.0",
4
+ "description": "Titan routing DSL",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "types": "./index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ }
13
+ }
14
+ }