adds watcher support to urls

This commit is contained in:
Ryan Lee 2020-03-05 11:57:45 -08:00
parent af415e89e8
commit a115faab97

View File

@ -26,6 +26,8 @@ import * as mkdirp from 'mkdirp';
import * as YargsParser from 'yargs'; import * as YargsParser from 'yargs';
import { URL } from 'url';
interface Options { interface Options {
ssr?: boolean; ssr?: boolean;
watch?: boolean; watch?: boolean;
@ -159,8 +161,16 @@ YargsParser.command(
async function serve(port: number, pathToSpec: string, options: Options = {}) { async function serve(port: number, pathToSpec: string, options: Options = {}) {
let spec = await loadAndBundleSpec(pathToSpec); let spec = await loadAndBundleSpec(pathToSpec);
let pageHTML = await getPageHTML(spec, pathToSpec, options); let pageHTML = await getPageHTML(spec, pathToSpec, options);
let isUrl;
try {
// tslint:disable-next-line
new URL(pathToSpec);
isUrl = true;
} catch (e) {
isUrl = false;
}
const server = createServer((request, response) => { const server = createServer(async (request, response) => {
console.time('GET ' + request.url); console.time('GET ' + request.url);
if (request.url === '/redoc.standalone.js') { if (request.url === '/redoc.standalone.js') {
respondWithGzip( respondWithGzip(
@ -176,6 +186,12 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
'Content-Type': 'text/html', 'Content-Type': 'text/html',
}); });
} else if (request.url === '/spec.json') { } else if (request.url === '/spec.json') {
if (options.watch && isUrl) {
spec = await loadAndBundleSpec(pathToSpec).catch(e => {
console.log('Cannot fetch specs:', e);
return {};
});
}
const specStr = JSON.stringify(spec, null, 2); const specStr = JSON.stringify(spec, null, 2);
respondWithGzip(specStr, request, response, { respondWithGzip(specStr, request, response, {
'Content-Type': 'application/json', 'Content-Type': 'application/json',