mizozobu 6 лет назад
Родитель
Сommit
539193b537
2 измененных файлов с 63 добавлено и 0 удалено
  1. 13 0
      src/lib/util/toArrayIfNot.js
  2. 50 0
      src/server/crowi/express-init.js

+ 13 - 0
src/lib/util/toArrayIfNot.js

@@ -0,0 +1,13 @@
+const toArrayIfNot = (item) => {
+  if (item == null) {
+    return [];
+  }
+
+  if (Array.isArray(item)) {
+    return item;
+  }
+
+  return [item];
+};
+
+module.exports = toArrayIfNot;

+ 50 - 0
src/server/crowi/express-init.js

@@ -22,6 +22,7 @@ module.exports = function(crowi, app) {
   const i18nUserSettingDetector = require('../util/i18nUserSettingDetector');
   const env = crowi.node_env;
   const middleware = require('../util/middlewares');
+  const toArrayIfNot = require('../../lib/util/toArrayIfNot');
 
   // Old type config API
   const config = crowi.getConfig();
@@ -150,4 +151,53 @@ module.exports = function(crowi, app) {
   }
 
   app.use(i18nMiddleware.handle(i18next));
+
+  // add custom functions to express res
+  express.response.apiv3 = function(obj) { // not arrow function
+    // obj must be object
+    if (typeof obj !== 'object' || obj instanceof Array) {
+      throw new Error('invalid value supplied to res.apiv3');
+    }
+
+    this.json({
+      oK: true,
+      data: obj,
+    });
+  };
+
+  express.response.apiv3Err = function(errs, status = 400) { // not arrow function
+    if (!Number.isInteger(status)) {
+      throw new Error('invalid status supplied to res.apiv3Err');
+    }
+
+    let errors = toArrayIfNot(errs);
+    errors = errors.map((_err) => {
+      const err = {};
+
+      if (_err instanceof Error) {
+        let message = _err.toString();
+
+        const prefixRegexp = /^Error: /;
+        if (prefixRegexp.test(message)) {
+          message = message.replace(prefixRegexp, '');
+        }
+
+        err.message = message;
+
+        return err;
+      }
+      if (typeof _err === 'string') {
+        err.message = _err;
+        return err;
+      }
+
+      throw new Error('invalid error supplied to res.apiv3Err');
+    });
+
+    this.status(status).json({
+      oK: false,
+      status,
+      errors: toArrayIfNot(errors),
+    });
+  };
 };