Browse Source

refine TransformResInterceptor and ErrorInterceptor

czhen 3 years ago
parent
commit
2341016dcb
1 changed files with 17 additions and 6 deletions
  1. 17 6
      express/src/interceptors/index.ts

+ 17 - 6
express/src/interceptors/index.ts

@@ -6,12 +6,17 @@ export const TransformResInterceptor = (
   res: Response,
   res: Response,
   next: NextFunction
   next: NextFunction
 ) => {
 ) => {
-  const oldSend = res.send;
-  res.send = (data) => {
+  const oldSend = res.json;
+  res.json = (data) => {
     // console.log(data); // do something with the data
     // console.log(data); // do something with the data
-    res.send = oldSend; // set function back to avoid the 'double-send'
-    return res.send({ data });
-    // return res.send({ data, statusCode: 200 }); // just call as normal with data
+    const statusCode = data?.statusCode;
+    const message = data?.message;
+    const error = data?.error;
+    res.json = oldSend; // set function back to avoid the 'double-send'
+    if (statusCode || message || error) {
+      return res.json({ statusCode, message, error });
+    }
+    return res.json({ data, statusCode: 200 }); // just call as normal with data
   };
   };
   next();
   next();
 };
 };
@@ -49,6 +54,7 @@ export const LoggingInterceptor = (
   res.on("close", () => {
   res.on("close", () => {
     const durationInMilliseconds = getDurationInMilliseconds(start);
     const durationInMilliseconds = getDurationInMilliseconds(start);
     const { statusCode = "" } = res;
     const { statusCode = "" } = res;
+    // TODO: Need some special log instead of console.log
     console.log(
     console.log(
       `${req.method} ${
       `${req.method} ${
         req.originalUrl
         req.originalUrl
@@ -73,5 +79,10 @@ export const ErrorInterceptor = (
   if (res.headersSent) {
   if (res.headersSent) {
     return next(err);
     return next(err);
   }
   }
-  res.status(500).json({ error: err });
+  if (err) {
+    res
+      .status(500)
+      .json({ message: `${err}`, error: "Bad Request", statusCode: 500 });
+  }
+  next();
 };
 };