@protocolnebula/ts-openapi-generator
Version:
Build API and models from Swagger/OpenAPI to use in any project type
128 lines • 5.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileReaderService = void 0;
const config_model_1 = require("../../models/config.model");
const files_util_1 = require("../../utils/files.util");
const string_util_1 = require("../../utils/string.util");
const versionRegx = /^([0-9]+)/;
/**
* Main calss which parse the JSON/YAML file
* If the file is a URL, it will download
*/
class FileReaderService {
constructor(path, configuration = config_model_1.config) {
this.path = path;
this.configuration = configuration;
/**
* Readed document
*/
this._document = null;
}
get document() {
return this._document;
}
readFile() {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepareFile();
const document = yield this.parseFile();
this._document = yield this.upgradeFile(document);
return this.document;
});
}
showDocumentInfo() {
const info = this.document.info;
console.group('API details');
console.info(info.title);
console.info(info.description);
console.info('Terms of service:', info.termsOfService);
if (info.contact) {
console.group('Contact details');
console.info('Name:', info.contact.name);
console.info('URL:', info.contact.url);
console.info('Email:', info.contact.email);
console.groupEnd();
}
console.groupEnd();
console.info('');
}
upgradeFile(document) {
return __awaiter(this, void 0, void 0, function* () {
let version;
if (document.openapi) {
version = versionRegx.exec(document.openapi)[1];
}
else if (document.swagger) {
version = versionRegx.exec(document.swagger)[1];
}
if (!version) {
throw 'This is not a valid OpenApi/Swagger document';
}
console.info('Swagger/API version detected:', version);
switch (version) {
case '3':
return document;
case '2':
console.info('Convertin to OpenAPI V3');
return this.convertFile('swagger_2', document);
case '1':
return this.convertFile('swagger_1', document);
default:
throw 'This OpenAPi/Swagger version is not compatible with this tool';
}
});
}
/**
* Convert from file definition to OpenAPI V3
* @param from
* @param document
* @url https://github.com/LucyBot-Inc/api-spec-converter
*/
convertFile(from, document) {
return __awaiter(this, void 0, void 0, function* () {
const Converter = require('api-spec-converter');
return Converter.convert({
from,
to: 'openapi_3',
source: 'https://api.gettyimages.com/swagger/api-docs',
});
});
}
prepareFile() {
return __awaiter(this, void 0, void 0, function* () {
let extension = (0, files_util_1.fileExtension)(this.path);
if (!(0, files_util_1.fileIsJSON)(this.path) && !(0, files_util_1.fileIsYAML)(this.path)) {
console.warn('WARNING: URL not match with JSON nor YAML, JSON will be used by default to read the file');
extension = 'json';
}
if ((0, string_util_1.isURL)(this.path)) {
this.localFilePath = `${this.configuration.tempFilePath}.${extension}`;
console.log(`${this.path} will be saved as ${this.localFilePath}...`);
yield (0, files_util_1.downloadFile)(this.path, this.localFilePath);
console.log(`${this.localFilePath} saved!`);
}
else {
this.localFilePath = this.path;
}
});
}
parseFile() {
const filePath = this.localFilePath;
switch ((0, files_util_1.fileExtension)(filePath)) {
case 'yaml':
return (0, files_util_1.parseYAML)(filePath);
default:
return (0, files_util_1.parseJSON)(filePath);
}
}
}
exports.FileReaderService = FileReaderService;
//# sourceMappingURL=file-reader.service.js.map