UNPKG

avantation

Version:

Build OpenAPI3.0 specification from HAR

115 lines (114 loc) 4.28 kB
"use strict"; const tslib_1 = require("tslib"); const command_1 = tslib_1.__importStar(require("@oclif/command")); const avantation_1 = require("./apis/avantation"); const avantation_2 = tslib_1.__importDefault(require("./templates/avantation")); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); const util_1 = require("./apis/util"); let pipe = !process.stdout.isTTY; let stdin = !process.stdin.isTTY; let stdinput = ''; if (stdin) { process.stdin.setEncoding('utf8'); process.stdin.on('readable', () => { let chunk; // Use a loop to make sure we read all available data. while ((chunk = process.stdin.read()) !== null) { stdinput = stdinput + chunk; } }); process.stdin.on('end', () => { }); } class Avantation extends command_1.default { async run() { const { flags } = this.parse(Avantation); const { args } = this.parse(Avantation); if (!stdin && !fs.existsSync(path.resolve(args.har))) { this.error('Invalid file location ' + path.resolve(args.har)); } let har = stdin ? JSON.parse(stdinput) : JSON.parse(fs.readFileSync(path.resolve(args.har), { encoding: 'utf-8' })); let template; if (flags.template) { let _template = JSON.parse(fs.readFileSync(path.resolve(flags.template), { encoding: 'utf-8' })); for (let prop in _template) { avantation_2.default[prop] = _template[prop]; } } template = avantation_2.default; let host = flags.host ? flags.host : har.log.entries.length > 0 ? util_1.Util.inferHost(har.log.entries) : ""; var input = { har: har, title: "Avantation REST Template", host: host, basePath: flags['base-path'] || '', template: template, out: flags.out || './openapi.yaml', pathParamRegex: flags['path-param-regex'] || '^([0-9]|[-$@!~%^*()_+])+$', pipe: pipe, json: flags.json, disableTag: flags['disable-tag'], securityHeaders: JSON.parse(flags['security-headers'] || '{}'), 'http-snippet': flags['http-snippet'] // maxDepth: flags['array-max-depth'] }; new avantation_1.AvantationAPI(input, this); } } Avantation.description = 'Build OpenAPI specification from HAR.'; Avantation.args = [ { name: 'har', description: 'http archive(har) path', required: !stdin } ]; Avantation.flags = { host: command_1.flags.string({ char: 'h', description: 'Filter the http request from HAR and use it as server url in output.', required: false }), 'base-path': command_1.flags.string({ char: 'b', description: "Separate the common path as base path from HTTP requests. Example:['api/v1']", required: false }), template: command_1.flags.string({ char: 't', description: 'To override the default template pass the your template file location.' }), out: command_1.flags.string({ char: 'o', description: 'Write output result at this DEST location.', default: './openapi.yaml' }), 'path-param-regex': command_1.flags.string({ char: 'r', description: 'Convert Regex matching params into dynamic path ', default: '^([0-9]|[-$@!~%^*()_+])+$' }), json: command_1.flags.boolean({ char: 'j', description: 'Write output result in JSON format.' }), 'disable-tag': command_1.flags.boolean({ description: 'Disable end points grouping based on route path in HAR' }), 'security-headers': command_1.flags.string({ char: 's', description: 'Map matching HTTP headers into security headers on request.', default: '{}' }), 'http-snippet': command_1.flags.boolean({ description: "Generate HTTP sample code snippet for request and append it as 'x-code-sample' to OpenAPI path object.", default: false }) }; module.exports = { Avantation, AvantationAPI: avantation_1.AvantationAPI };