Jelajahi Sumber

fix req body interception

Pablo Barrera Yaksic 1 tahun lalu
induk
melakukan
102bdeb25c
2 mengubah file dengan 23 tambahan dan 25 penghapusan
  1. 1 1
      package.json
  2. 22 24
      src/index.ts

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "@pablo/pablo-rest-api-standard",
-  "version": "0.0.5",
+  "version": "0.0.6",
   "description": "Pablo Rest API Standard Library",
   "main": "lib/index.js",
   "scripts": {

+ 22 - 24
src/index.ts

@@ -103,32 +103,30 @@ export class PRAS {
         },
       };
 
-      if (/application\/json/.test(res.get("Content-Type"))) {
-        const originalSend = res.send;
-        res.send = (body) => {
-          if (typeof body === "string") {
-            body = JSON.parse(body);
-          }
-          if (typeof body === "string") {
-            body = { data: body };
-          } else if (!body.hasOwnProperty("data")) {
-            body = { data: body };
-          }
+      const originalSend = res.send;
+      res.send = (body) => {
+        if (typeof body === "string") {
+          body = JSON.parse(body);
+        }
+        if (typeof body === "string") {
+          body = { data: body };
+        } else if (!body.hasOwnProperty("data")) {
+          body = { data: body };
+        }
 
-          const prasBody = this.getPRASResultObject(
-            body.statusCode,
-            body.status,
-            body.message,
-            body.data,
-            body.code,
-            body.extra
-          );
-          res.status(Number(prasBody) || null);
-          delete prasBody.statusCode;
+        const prasBody = this.getPRASResultObject(
+          body.statusCode,
+          body.status,
+          body.message,
+          body.data,
+          body.code,
+          body.extra
+        );
+        res.status(Number(prasBody.statusCode) || 200);
+        delete prasBody.statusCode;
 
-          return originalSend(JSON.stringify(prasBody));
-        };
-      }
+        return originalSend.call(res, JSON.stringify(prasBody));
+      };
 
       next();
     };