From 03d8964a7a0be8f31f3a160b38e682a69491605f Mon Sep 17 00:00:00 2001 From: Viljami Kuosmanen Date: Sun, 29 Mar 2020 17:51:56 +0200 Subject: [PATCH] openapi: upgrade to openapi-backend 2.4.0 --- src/node/hooks/express/apicalls.js | 40 ------------------------------ src/node/hooks/express/openapi.js | 30 +++++++++++----------- src/package.json | 2 +- 3 files changed, 15 insertions(+), 57 deletions(-) diff --git a/src/node/hooks/express/apicalls.js b/src/node/hooks/express/apicalls.js index cf4507480..c0967c35f 100644 --- a/src/node/hooks/express/apicalls.js +++ b/src/node/hooks/express/apicalls.js @@ -1,49 +1,9 @@ var log4js = require('log4js'); -var apiLogger = log4js.getLogger("API"); var clientLogger = log4js.getLogger("client"); var formidable = require('formidable'); var apiHandler = require('../../handler/APIHandler'); -var isValidJSONPName = require('./isValidJSONPName'); - -//This is for making an api call, collecting all post information and passing it to the apiHandler -var apiCaller = function(req, res, fields) { - res.header("Content-Type", "application/json; charset=utf-8"); - - apiLogger.info("REQUEST, v"+ req.params.version + ":" + req.params.func + ", " + JSON.stringify(fields)); - - //wrap the send function so we can log the response - //note: res._send seems to be already in use, so better use a "unique" name - res._____send = res.send; - res.send = function (response) { - response = JSON.stringify(response); - apiLogger.info("RESPONSE, " + req.params.func + ", " + response); - - //is this a jsonp call, if yes, add the function call - if(req.query.jsonp && isValidJSONPName.check(req.query.jsonp)) - response = req.query.jsonp + "(" + response + ")"; - - res._____send(response); - } - - //call the api handler - apiHandler.handle(req.params.version, req.params.func, fields, req, res); -} - -exports.apiCaller = apiCaller; exports.expressCreateServer = function (hook_name, args, cb) { - //This is a api GET call, collect all post informations and pass it to the apiHandler - args.app.get('/api/:version/:func', function (req, res) { - apiCaller(req, res, req.query) - }); - - //This is a api POST call, collect all post informations and pass it to the apiHandler - args.app.post('/api/:version/:func', function(req, res) { - new formidable.IncomingForm().parse(req, function (err, fields, files) { - apiCaller(req, res, Object.assign(req.query, fields)) - }); - }); - //The Etherpad client side sends information about how a disconnect happened args.app.post('/ep/pad/connection-diagnostic-info', function(req, res) { new formidable.IncomingForm().parse(req, function(err, fields, files) { diff --git a/src/node/hooks/express/openapi.js b/src/node/hooks/express/openapi.js index bc8671f98..7471481c3 100644 --- a/src/node/hooks/express/openapi.js +++ b/src/node/hooks/express/openapi.js @@ -11,7 +11,19 @@ const apiLogger = log4js.getLogger('API'); // https://github.com/OAI/OpenAPI-Specification/tree/master/schemas/v3.0 const OPENAPI_VERSION = '3.0.2'; // Swagger/OAS version -// We'll add some more info to the API methods +// enum for two different styles of API paths used for etherpad +const APIPathStyle = { + FLAT: 'api', // flat paths e.g. /api/createGroup + REST: 'rest', // restful paths e.g. /rest/group/create +}; + +// helper to get api root +const getApiRootForVersion = (version, style = APIPathStyle.FLAT) => `/${style}/${version}`; + +// helper to generate an OpenAPI server object when serving definitions +const generateServerForApiVersion = (apiRoot, req) => ({ + url: `${settings.ssl ? 'https' : 'http'}://${req.headers.host}${apiRoot}`, +}); // operations const API = { @@ -214,20 +226,6 @@ const API = { }, }; -// enum for two different styles of API paths used for etherpad -const APIPathStyle = { - FLAT: 'api', // flat paths e.g. /api/createGroup - REST: 'rest', // restful paths e.g. /rest/group/create -}; - -// helper to get api root -const getApiRootForVersion = (version, style = APIPathStyle.FLAT) => `/${style}/${version}`; - -// helper to generate an OpenAPI server object when serving definitions -const generateServerForApiVersion = (apiRoot, req) => ({ - url: `${settings.ssl ? 'https' : 'http'}://${req.headers.host}${apiRoot}`, -}); - const defaultResponses = { 200: { description: 'ok', @@ -407,7 +405,7 @@ exports.expressCreateServer = (_, args) => { apiRoot, definition, validate: false, - strict: true, + quick: true, // recommended when running multiple instances in parallel }); // register default handlers diff --git a/src/package.json b/src/package.json index 026473407..e09831860 100644 --- a/src/package.json +++ b/src/package.json @@ -51,7 +51,7 @@ "nodeify": "^1.0.1", "npm": "6.14.3", "object.values": "^1.0.4", - "openapi-backend": "^2.3.8", + "openapi-backend": "^2.4.0", "request": "2.88.0", "resolve": "1.1.7", "security": "1.0.0",