Added support for providing custom redoc JS source.

This commit is contained in:
konrad.bartecki 2019-06-07 16:33:50 +02:00
parent 753b013eee
commit ddcd0fbdbd

View File

@ -29,6 +29,7 @@ interface Options {
templateFileName?: string;
templateOptions?: any;
redocOptions?: any;
customScript?: string;
}
const BUNDLES_DIR = dirname(require.resolve('redoc'));
@ -59,6 +60,12 @@ YargsParser.command(
type: 'boolean',
});
yargs.option('customScript', {
describe: 'Include custom ReDoc JavaScript source file',
type: 'string',
default: null
});
yargs.demandOption('spec');
return yargs;
},
@ -66,6 +73,7 @@ YargsParser.command(
const config: Options = {
ssr: argv.ssr as boolean,
watch: argv.watch as boolean,
customScript: argv.customScript as string,
templateFileName: argv.template as string,
templateOptions: argv.templateOptions || {},
redocOptions: argv.options || {},
@ -105,6 +113,12 @@ YargsParser.command(
default: false,
});
yargs.option('customScript', {
describe: 'Include custom ReDoc JavaScript source file',
type: 'string',
default: null
});
yargs.demandOption('spec');
return yargs;
},
@ -117,6 +131,7 @@ YargsParser.command(
templateFileName: argv.template as string,
templateOptions: argv.templateOptions || {},
redocOptions: argv.options || {},
customScript: argv.customScript as string
};
try {
@ -147,8 +162,9 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
const server = createServer((request, response) => {
console.time('GET ' + request.url);
if (request.url === '/redoc.standalone.js') {
let actualRedocPath = options.customScript ? options.customScript : join(BUNDLES_DIR, 'redoc.standalone.js');
respondWithGzip(
createReadStream(join(BUNDLES_DIR, 'redoc.standalone.js'), 'utf8'),
createReadStream(actualRedocPath, 'utf8'),
request,
response,
{
@ -216,7 +232,7 @@ async function bundle(pathToSpec, options: Options = {}) {
async function getPageHTML(
spec: any,
pathToSpec: string,
{ ssr, cdn, title, templateFileName, templateOptions, redocOptions = {} }: Options,
{ ssr, cdn, customScript, title, templateFileName, templateOptions, redocOptions = {} }: Options,
) {
let html;
let css;
@ -233,9 +249,13 @@ async function getPageHTML(
state = await store.toJS();
if (!cdn) {
if (customScript) {
redocStandaloneSrc = readFileSync(customScript);
} else {
redocStandaloneSrc = readFileSync(join(BUNDLES_DIR, 'redoc.standalone.js'));
}
}
}
templateFileName = templateFileName ? templateFileName : join(__dirname, './template.hbs');
const template = compile(readFileSync(templateFileName).toString());