diff --git a/src/backend/helpers/request.mjs b/src/backend/helpers/request.mjs index 56180c5c..8bcdc3c0 100644 --- a/src/backend/helpers/request.mjs +++ b/src/backend/helpers/request.mjs @@ -6,6 +6,7 @@ import uriTool from './uri.mjs'; import gitlab from './gitlab.mjs'; import bitbucket from './bitbucket.mjs'; import logger from '../utils/logger.mjs'; +import xml from '../../global/helpers/xmlparser.mjs'; const REQUEST_TAG = 'request'; @@ -40,14 +41,20 @@ function isAvailablePath(path) { return path.startsWith(`${$paths.file_storage}/`); } +const CONTENT_TYPE_YAML = 'application/x-yaml'; +const CONTENT_TYPE_JSON = 'application/json'; +const CONTENT_TYPE_XML = 'application/xhtml+xml'; + // Определяет тип контента function getContentType(url) { let contentType = null; const uri = url.split('?')[0]; if (uri.endsWith('.yaml') || uri.endsWith('.yml') || (uri.indexOf('.yaml/raw') >= 0) || (uri.indexOf('.yml/raw') >= 0)) { - contentType = 'application/x-yaml'; + contentType = CONTENT_TYPE_YAML; } else if (uri.endsWith('.json') || (uri.indexOf('.json/raw') >= 0)) { - contentType = 'application/json'; + contentType = CONTENT_TYPE_JSON; + } else if (uri.endsWith('.xml') || (uri.indexOf('.xml/raw') >= 0)) { + contentType = CONTENT_TYPE_XML; } return contentType; } @@ -79,11 +86,13 @@ async function request(url, baseURI, response) { const result = { data: fs.readFileSync(fileName, { encoding: 'utf8', flag: 'r' }) }; - if (contentType === 'application/x-yaml') { + if (contentType === CONTENT_TYPE_YAML) { result.data = yaml.parse(result.data); - } else if (contentType === 'application/json') { + } else if (contentType === CONTENT_TYPE_JSON) { result.data = JSON.parse(result.data); - } + } else if (contentType === CONTENT_TYPE_XML) { + result.data = xml.parse(result.data); + } return result; } } // Если запрос по http / https diff --git a/src/frontend/helpers/requests.js b/src/frontend/helpers/requests.js index 87839b10..15e5d292 100644 --- a/src/frontend/helpers/requests.js +++ b/src/frontend/helpers/requests.js @@ -4,17 +4,7 @@ import crc16 from '@global/helpers/crc16'; import gitlab from '@front/helpers/gitlab'; import uriTool from '@front/helpers/uri'; import { Buffer } from 'buffer'; - -const XML = { - parse(xml) { - let result = null; - require('xml2js').parseString(xml, (err, json) => { - if (err) throw err; - result = json; - }); - return result; - } -}; +import xml from '@global/helpers/xmlparser'; import env, { Plugins } from './env'; import { responseCacheInterceptor, requestCacheInterceptor } from './cache'; @@ -69,7 +59,7 @@ axios.interceptors.response.use(async(response) => { (url.indexOf('.xml/raw') >= 0) || (url.endsWith('.xml')) || (response?.headers || {})['content-type'] === 'application/xml') - response.data = XML.parse(response.data); + response.data = xml.parse(response.data); } } @@ -93,7 +83,7 @@ function injectPAPIMiddleware() { switch (type) { case 'yaml': response.data = YAML.parse(response.data); break; case 'json': response.data = JSON.parse(response.data); break; - case 'xml': !request.raw && (response.data = XML.parse(response.data)); break; + case 'xml': !request.raw && (response.data = xml.parse(response.data)); break; case 'jpg': type = 'jpeg'; // eslint-disable-next-line no-fallthrough diff --git a/src/global/helpers/xmlparser.mjs b/src/global/helpers/xmlparser.mjs new file mode 100644 index 00000000..a9ee95a1 --- /dev/null +++ b/src/global/helpers/xmlparser.mjs @@ -0,0 +1,12 @@ +import parser from 'xml2js'; + +export default { + parse(xml) { + let result = null; + parser.parseString(xml, (err, json) => { + if (err) throw err; + result = json; + }); + return result; + } +};