UNPKG

openapi-diff

Version:

A CLI tool to identify differences between Swagger/OpenAPI specs.

51 lines (50 loc) 2.16 kB
"use strict"; 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.ContentLoader = void 0; const url = require("url"); const open_api_diff_error_impl_1 = require("../common/open-api-diff-error-impl"); class ContentLoader { constructor(httpClient, fileSystem) { this.httpClient = httpClient; this.fileSystem = fileSystem; } static isUrl(location) { const urlObject = url.parse(location); return urlObject.protocol !== null; } load(location) { return ContentLoader.isUrl(location) ? this.getContentFromUrl(location) : this.getContentFromFile(location); } getContentFromFile(filePath) { return __awaiter(this, void 0, void 0, function* () { try { return yield this.fileSystem.readFile(filePath); } catch (error) { throw new open_api_diff_error_impl_1.OpenApiDiffErrorImpl('OPENAPI_DIFF_READ_ERROR', `Unable to read "${filePath}"`, error); } }); } getContentFromUrl(fileUrl) { return __awaiter(this, void 0, void 0, function* () { try { return yield this.httpClient.get(fileUrl); } catch (error) { throw new open_api_diff_error_impl_1.OpenApiDiffErrorImpl('OPENAPI_DIFF_READ_ERROR', `Unable to load "${fileUrl}"`, error); } }); } } exports.ContentLoader = ContentLoader;