Răsfoiți Sursa

move express.response.apiv3

mizozobu 6 ani în urmă
părinte
comite
2eee8ba43f

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

@@ -22,8 +22,6 @@ 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');
-  const ErrorV3 = require('../util/ErrorV3');
 
   // Old type config API
   const config = crowi.getConfig();
@@ -152,34 +150,4 @@ 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({ data: obj });
-  };
-
-  express.response.apiv3Err = function(_err, status = 400) { // not arrow function
-    if (!Number.isInteger(status)) {
-      throw new Error('invalid status supplied to res.apiv3Err');
-    }
-
-    let errors = toArrayIfNot(_err);
-    errors = errors.map((e) => {
-      if (e instanceof ErrorV3) {
-        return e;
-      }
-      if (typeof e === 'string') {
-        return { message: e };
-      }
-
-      throw new Error('invalid error supplied to res.apiv3Err');
-    });
-
-    this.status(status).json({ errors });
-  };
 };

+ 3 - 0
src/server/routes/apiv3/index.js

@@ -6,6 +6,9 @@ const express = require('express');
 
 const router = express.Router();
 
+const addCustomFunctionToResponse = require('./response');
+
+addCustomFunctionToResponse(express);
 
 module.exports = (crowi) => {
 

+ 36 - 0
src/server/routes/apiv3/response.js

@@ -0,0 +1,36 @@
+const toArrayIfNot = require('../../../lib/util/toArrayIfNot');
+const ErrorV3 = require('../../util/ErrorV3');
+
+// add custom functions to express res
+const addCustomFunctionToResponse = (express) => {
+  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({ data: obj });
+  };
+
+  express.response.apiv3Err = function(_err, status = 400) { // not arrow function
+    if (!Number.isInteger(status)) {
+      throw new Error('invalid status supplied to res.apiv3Err');
+    }
+
+    let errors = toArrayIfNot(_err);
+    errors = errors.map((e) => {
+      if (e instanceof ErrorV3) {
+        return e;
+      }
+      if (typeof e === 'string') {
+        return { message: e };
+      }
+
+      throw new Error('invalid error supplied to res.apiv3Err');
+    });
+
+    this.status(status).json({ errors });
+  };
+};
+
+module.exports = addCustomFunctionToResponse;