UNPKG

oas-normalize

Version:

Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions

86 lines (74 loc) 2.54 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/lib/utils.ts var _jsyaml = require('js-yaml'); var _jsyaml2 = _interopRequireDefault(_jsyaml); var _openapiparser = require('@readme/openapi-parser'); function isBuffer(obj) { return obj != null && obj.constructor != null && typeof obj.constructor.isBuffer === "function" && !!obj.constructor.isBuffer(obj); } function prepareURL(url) { const options = {}; const u = new URL(url); if (u.username || u.password) { options.headers = { Authorization: `Basic ${btoa(`${u.username}:${u.password}`)}` }; u.username = ""; u.password = ""; } if (u.host === "github.com" && u.pathname.includes("/blob/")) { u.host = "raw.githubusercontent.com"; u.pathname = u.pathname.replace("/blob/", "/"); } return { url: u.toString(), options }; } function getType(obj) { if (isBuffer(obj)) { return "buffer"; } else if (typeof obj === "object") { return "json"; } else if (typeof obj === "string") { if (obj.match(/\s*{/)) { return "string-json"; } else if (obj.match(/\n/)) { return "string-yaml"; } else if (obj.substring(0, 4) === "http") { return "url"; } return "path"; } return false; } function isOpenAPI(schema) { return !!schema.openapi; } function isPostman(schema) { return !!schema.info && !!schema.item; } function isSwagger(schema) { return !!schema.swagger; } function stringToJSON(string) { if (typeof string === "object") { return string; } else if (string.match(/^\s*{/)) { return JSON.parse(string); } return _jsyaml2.default.load(string, { schema: _jsyaml.JSON_SCHEMA }); } function isAPIDefinition(schema) { return isOpenAPI(schema) || isPostman(schema) || isSwagger(schema); } function getAPIDefinitionType(schema) { if (isOpenAPI(schema)) { return "openapi"; } else if (isPostman(schema)) { return "postman"; } else if (isSwagger(schema)) { return "swagger"; } return "unknown"; } exports.isBuffer = isBuffer; exports.prepareURL = prepareURL; exports.getType = getType; exports.isOpenAPI = isOpenAPI; exports.isPostman = isPostman; exports.isSwagger = isSwagger; exports.stringToJSON = stringToJSON; exports.isAPIDefinition = isAPIDefinition; exports.getAPIDefinitionType = getAPIDefinitionType; exports.compileErrors = _openapiparser.compileErrors; //# sourceMappingURL=chunk-ZTTIELJH.cjs.map