UNPKG

swagger-theme

Version:

Convert any API Specification into an awesome HTML documentation website

64 lines (55 loc) 1.25 kB
const https = require("https"); const fs = require('fs'); const throwError = (msg = 'Something went wrong!!!') => { throw msg; process.exit(0); }; const getFile = (url) => new Promise((resolve, reject) => { try { const content = fs.readFileSync(url, 'utf8'); return resolve(JSON.parse(content)); } catch { resolve({}); }; }); const getApiHtml = (body) => new Promise((resolve, reject) => { try { const req = https.request({ hostname: 'swagger-theme.herokuapp.com', port: 443, path: '/return-html', method: 'POST', headers: { 'Content-Type': 'application/json', } }, (res) => { var result = ''; res.on('data', chunk => result += chunk); res.on('end', () => { try { const res = JSON.parse(result); const {error, html} = res; if (error) { return throwError(); } resolve(html); } catch (e) { return throwError(); } }); }); req.on('error', (e) => { return throwError(); }); req.write(body); req.end(); } catch (e) { return throwError(); } }); module.exports = { getApiHtml, getFile, throwError, };