UNPKG

oas-normalize

Version:

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

86 lines (85 loc) 2.1 kB
// src/lib/utils.ts import YAML, { JSON_SCHEMA } from "js-yaml"; import { compileErrors } from "@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 YAML.load(string, { schema: 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"; } export { isBuffer, prepareURL, getType, isOpenAPI, isPostman, isSwagger, stringToJSON, isAPIDefinition, getAPIDefinitionType, compileErrors }; //# sourceMappingURL=chunk-EHNNEPCG.js.map