@increase21/simplenodejs 1.0.16 → 1.0.18

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
@@ -33,17 +33,7 @@ npm install @increase21/simplenodejs
33
33
 
34
34
  ```ts
35
35
  import { CreateSimpleJsHttpServer } from "@increase21/simplenodejs";
36
-
37
- const app = CreateSimpleJsHttpServer({
38
- controllersDir: process.cwd()+ "/controllers",
39
- trustProxy: true
40
- });
41
-
42
- app.listen(3000, () => {
43
- console.log("Server running on http://localhost:3000");
44
- });
45
36
  ```
46
-
47
37
  ---
48
38
 
49
39
  ## ⚙️ CreateSimpleJsHttpServer(options)
@@ -63,10 +53,12 @@ Creates and returns the HTTP app instance.
63
53
  ```ts
64
54
  const app = CreateSimpleJsHttpServer({
65
55
  controllersDir: process.cwd()+ "/controllers",
56
+ trustProxy: true
66
57
  });
67
58
 
68
- app.listen(4000,callback)
69
-
59
+ app.listen(3000, () => {
60
+ console.log("Server running on http://localhost:3000");
61
+ });
70
62
  ```
71
63
 
72
64
  ---
@@ -74,7 +66,7 @@ app.listen(4000,callback)
74
66
  ## 📁 Controllers
75
67
 
76
68
  Controllers are auto-loaded from `controllersDir`.
77
- #### Running Multiple Methods
69
+ #### Running an endpoint using RunRequest
78
70
  ##### ./controllers/{servicefolder}/auth.ts
79
71
  #### or
80
72
  #### ./controllers/{servicefolder}/auth.js
@@ -93,9 +85,11 @@ export default AuthControllers extends SimpleNodeJsController {
93
85
  }
94
86
  };
95
87
  ```
96
- Available on POST|GET|PUT|DELETE at http://baseURl/{servicefolder}/auth/account
97
88
 
98
- #### Running Single Method
89
+ The above endpoint is accessible on http://baseURl/{servicefolder}/auth/account.
90
+ The endpoint receives optional parameter (id:string) which can be passed in the url e.g http://baseURl/{servicefolder}/auth/account/{id}
91
+
92
+ #### Running an endpoint without RunRequest
99
93
  ```ts
100
94
  export default AuthControllers extends SimpleNodeJsController {
101
95
 
@@ -106,6 +100,26 @@ export default AuthControllers extends SimpleNodeJsController {
106
100
  }
107
101
  };
108
102
  ```
103
+ ### Endpoint Naming
104
+ Endpoints are defined using camelCase method names in controller files and are exposed as kebab-case in the URL path.
105
+ ```ts
106
+ async vehicleList(id:string) {}
107
+ ```
108
+
109
+ ```code
110
+ /vehicle-list
111
+ /vehicle-list/{id}
112
+ ```
113
+
114
+ ```ts
115
+ async vehicle() {}
116
+ ```
117
+
118
+ ```code
119
+ /vehicle
120
+ ```
121
+
122
+ ---
109
123
 
110
124
  ### Controller Object Params
111
125
  Each method defined in a controller file is exposed as an endpoint by SimpleNodeJsController.
@@ -130,7 +144,7 @@ Methods can receive parameters, which are passed through the URL pathname. When
130
144
  patch?: 'required' | 'optional',
131
145
  }
132
146
  }
133
- ```
147
+ ```
134
148
 
135
149
  ## 🧾 RequestObject (req)
136
150
 
@@ -215,7 +229,7 @@ Registers a plugin.
215
229
  ### Plugin Shape
216
230
 
217
231
  ```ts
218
- type Plugin = (app: SimpleJsServer, opts?: any) => void | Promise<void>;
232
+ type Plugin = (app: SimpleJsServer, opts?: any) => Promise<any>|void;
219
233
  ```
220
234
 
221
235
  ### Built-In Plugins
@@ -240,10 +254,10 @@ All the standard http headers
240
254
 
241
255
  ### Usage
242
256
  ```ts
243
- app.use(app=>SetRequestCORS(app, [{
257
+ app.use(SetRequestCORS({
244
258
  "Access-Control-Allow-Origin": "*",
245
259
  "X-Frame-Options": "DENY",
246
- }]));
260
+ }));
247
261
  ```
248
262
 
249
263
  ---
package/dist/router.d.ts CHANGED
@@ -2,4 +2,4 @@ import { RequestObject, ResponseObject } from "./typings/general";
2
2
  import { SimpleJsControllerMeta } from "./typings/simpletypes";
3
3
  export declare function loadControllers(root?: string): Map<string, SimpleJsControllerMeta>;
4
4
  export declare function setControllersDir(dir: string): void;
5
- export declare function route(req: RequestObject, res: ResponseObject, controllersDir?: string): Promise<void>;
5
+ export declare function route(req: RequestObject, res: ResponseObject): Promise<void>;
package/dist/router.js CHANGED
@@ -9,6 +9,7 @@ exports.route = route;
9
9
  // router.ts
10
10
  const node_fs_1 = __importDefault(require("node:fs"));
11
11
  const node_path_1 = __importDefault(require("node:path"));
12
+ const helpers_1 = require("./utils/helpers");
12
13
  let controllers = new Map();
13
14
  function loadControllers(root = "controllers") {
14
15
  const base = node_path_1.default.resolve(process.cwd(), root);
@@ -42,7 +43,7 @@ function loadControllers(root = "controllers") {
42
43
  function setControllersDir(dir) {
43
44
  controllers = loadControllers(dir);
44
45
  }
45
- async function route(req, res, controllersDir) {
46
+ async function route(req, res) {
46
47
  let parts = req._end_point_path || [];
47
48
  let controllerPath = (parts.length > 2 ? "/" + parts.slice(0, 2).join("/") : `/${parts.join("/")}`).toLocaleLowerCase();
48
49
  let methodName = parts.length > 2 ? parts[2] : "index";
@@ -50,7 +51,7 @@ async function route(req, res, controllersDir) {
50
51
  const meta = controllers.get(controllerPath);
51
52
  //if the controller is not available or not found
52
53
  if (!meta || !meta.name || !meta.Controller)
53
- return res.status(404).json({ error: "The requested resource does not exist" });
54
+ return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
54
55
  const ControllerClass = meta.Controller;
55
56
  const controller = new ControllerClass();
56
57
  //Update the method name to the framework pathen
@@ -63,12 +64,12 @@ async function route(req, res, controllersDir) {
63
64
  id = parts.slice(2);
64
65
  }
65
66
  else {
66
- return res.status(404).json({ error: "The requested resource does not exist" });
67
+ return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
67
68
  }
68
69
  }
69
70
  //if the data require params but there's no matching params
70
71
  if (id && id.length && (!controller[methodName].length || controller[methodName].length < id.length)) {
71
- return res.status(404).json({ error: "The requested resource does not exist. Kindly check your url" });
72
+ return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
72
73
  }
73
74
  //bind the controller to use the global properties
74
75
  controller.__bindContext({ req, res });
@@ -28,7 +28,6 @@ function composeWithError(middlewares) {
28
28
  }
29
29
  function throwHttpError(code, message) {
30
30
  const error = new Error(message);
31
- error.statusCode = code;
32
31
  error.toJSON = () => ({ code, error: message });
33
32
  throw error;
34
33
  }
@@ -14,6 +14,6 @@ export declare class SimpleNodeJsController {
14
14
  }): void;
15
15
  /** framework-internal method */
16
16
  __checkContext(): void;
17
- protected RunRequest(handlers: SubRequestHandler, params?: SimpleJsPrivateMethodProps): any;
17
+ protected RunRequest(handlers: SubRequestHandler, params?: Partial<SimpleJsPrivateMethodProps>): any;
18
18
  }
19
19
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@increase21/simplenodejs",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Lightweight Node.js HTTP framework with middleware and plugins",
5
5
  "dev": "dist/index.js",
6
6
  "bugs": "https://github.com/increase21/simplenodejs/issues",