@increase21/simplenodejs 1.1.0 → 1.1.1

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 (2) hide show
  1. package/dist/router.js +14 -7
  2. package/package.json +2 -2
package/dist/router.js CHANGED
@@ -19,19 +19,23 @@ async function route(req, res) {
19
19
  const meta = controllers.get(controllerPath);
20
20
  if (!meta || !meta.name || !meta.Controller)
21
21
  return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
22
- const ControllerClass = meta.Controller;
23
- const controller = new ControllerClass();
24
- methodName = (methodName || "").replace(/\-{1}\w{1}/g, match => match.replace("-", "").toUpperCase());
25
- // Block Object.prototype methods and __private convention
26
- if (methodName.startsWith("__") || UNSAFE_METHODS.has(methodName)) {
27
- return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
28
- }
29
22
  const ctx = {
30
23
  req, res,
31
24
  body: req.body,
32
25
  query: req.query,
33
26
  customData: req._custom_data,
34
27
  };
28
+ const ControllerClass = meta.Controller;
29
+ const controller = new ControllerClass(ctx);
30
+ //if request has ended, do not proceed
31
+ if (res.writableEnded)
32
+ return;
33
+ //sanitize method name, convert kebab-case to camelCase
34
+ methodName = (methodName || "").replace(/\-{1}\w{1}/g, match => match.replace("-", "").toUpperCase());
35
+ // Block Object.prototype methods and __private convention
36
+ if (methodName.startsWith("__") || UNSAFE_METHODS.has(methodName)) {
37
+ return (0, helpers_1.throwHttpError)(404, "The requested resource does not exist");
38
+ }
35
39
  // Fallback to index if method not found (treat path segment as id)
36
40
  if (typeof controller[methodName] !== "function") {
37
41
  if (typeof controller["index"] === "function" && parts.length === 3) {
@@ -47,10 +51,13 @@ async function route(req, res) {
47
51
  return (0, helpers_1.throwHttpError)(404, "Resource not found");
48
52
  }
49
53
  const descriptors = await controller[methodName](ctx, ...id);
54
+ // If the controller method has already sent a response, do not proceed
50
55
  if (res.writableEnded)
51
56
  return;
57
+ //if the handler returns no descriptors or an invalid format, end the response
52
58
  if (!descriptors || !Array.isArray(descriptors))
53
59
  return res.end();
60
+ // Find the descriptor matching the HTTP method
54
61
  const descriptor = descriptors.find((d) => d.method === httpMethod);
55
62
  if (!descriptor)
56
63
  return (0, helpers_1.throwHttpError)(405, "Method Not Allowed");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@increase21/simplenodejs",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Lightweight Node.js HTTP framework with middlewares and plugins",
5
5
  "dev": "dist/index.js",
6
6
  "bugs": "https://github.com/increase21/simplenodejs/issues",
@@ -40,4 +40,4 @@
40
40
  "devDependencies": {
41
41
  "typescript": "^5.3.0"
42
42
  }
43
- }
43
+ }