oas-normalize
Advanced tools
Comparing version 8.4.1 to 9.0.0
@@ -1,11 +0,12 @@ | ||
import type { OpenAPI } from 'openapi-types'; | ||
import { OpenAPI } from 'openapi-types'; | ||
import openapiParser from '@readme/openapi-parser'; | ||
import * as utils from './lib/utils'; | ||
export interface Options { | ||
import { isAPIDefinition as isAPIDefinition$1, getAPIDefinitionType as getAPIDefinitionType$1, getType } from './lib/utils.js'; | ||
interface Options { | ||
colorizeErrors?: boolean; | ||
enablePaths?: boolean; | ||
} | ||
export declare const isAPIDefinition: typeof utils.isAPIDefinition; | ||
export declare const getAPIDefinitionType: typeof utils.getAPIDefinitionType; | ||
export default class OASNormalize { | ||
declare const isAPIDefinition: typeof isAPIDefinition$1; | ||
declare const getAPIDefinitionType: typeof getAPIDefinitionType$1; | ||
declare class OASNormalize { | ||
cache: { | ||
@@ -18,3 +19,3 @@ bundle?: false | OpenAPI.Document; | ||
opts: Options; | ||
type: ReturnType<typeof utils.getType>; | ||
type: ReturnType<typeof getType>; | ||
constructor(file: any, opts?: Options); | ||
@@ -59,1 +60,3 @@ /** | ||
} | ||
export { Options, OASNormalize as default, getAPIDefinitionType, isAPIDefinition }; |
@@ -1,300 +0,187 @@ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var chunkF3S2V53P_js = require('./chunk-F3S2V53P.js'); | ||
var fs = require('fs'); | ||
var openapiParser = require('@readme/openapi-parser'); | ||
var postmanToOpenAPI = require('@readme/postman-to-openapi'); | ||
var fetch = require('node-fetch'); | ||
var converter = require('swagger2openapi'); | ||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var fs__default = /*#__PURE__*/_interopDefault(fs); | ||
var openapiParser__default = /*#__PURE__*/_interopDefault(openapiParser); | ||
var postmanToOpenAPI__default = /*#__PURE__*/_interopDefault(postmanToOpenAPI); | ||
var fetch__default = /*#__PURE__*/_interopDefault(fetch); | ||
var converter__default = /*#__PURE__*/_interopDefault(converter); | ||
var isAPIDefinition2 = chunkF3S2V53P_js.isAPIDefinition; | ||
var getAPIDefinitionType2 = chunkF3S2V53P_js.getAPIDefinitionType; | ||
var OASNormalize = class _OASNormalize { | ||
constructor(file, opts) { | ||
this.file = file; | ||
this.opts = { | ||
colorizeErrors: false, | ||
enablePaths: false, | ||
...opts | ||
}; | ||
this.type = chunkF3S2V53P_js.getType(this.file); | ||
this.cache = { | ||
load: false, | ||
bundle: false, | ||
deref: false | ||
}; | ||
} | ||
/** | ||
* @private | ||
*/ | ||
async load() { | ||
if (this.cache.load) | ||
return Promise.resolve(this.cache.load); | ||
const resolve = (obj) => { | ||
const ret = chunkF3S2V53P_js.stringToJSON(obj); | ||
this.cache.load = ret; | ||
return Promise.resolve(ret); | ||
}; | ||
switch (this.type) { | ||
case "json": | ||
case "string-json": | ||
case "string-yaml": | ||
return resolve(this.file); | ||
case "buffer": | ||
return resolve(this.file.toString()); | ||
case "url": | ||
const resp = await fetch__default.default(chunkF3S2V53P_js.normalizeURL(this.file)).then((res) => res.text()); | ||
return resolve(resp); | ||
case "path": | ||
if (!this.opts.enablePaths) { | ||
return Promise.reject(new Error("Use `opts.enablePaths` to enable accessing local files.")); | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
const contents = fs__default.default.readFileSync(this.file).toString(); | ||
if (!contents.trim()) { | ||
return Promise.reject(new Error("No file contents found.")); | ||
} | ||
return resolve(contents); | ||
default: | ||
return Promise.reject(new Error("Could not load this file.")); | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
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()); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
static convertPostmanToOpenAPI(schema) { | ||
return postmanToOpenAPI__default.default(JSON.stringify(schema), null, { outputFormat: "json", replaceVars: true }).then(JSON.parse); | ||
} | ||
/** | ||
* Bundle up the given API definition, resolving any external `$ref` pointers in the process. | ||
* | ||
*/ | ||
async bundle() { | ||
if (this.cache.bundle) | ||
return Promise.resolve(this.cache.bundle); | ||
return this.load().then((schema) => { | ||
if (chunkF3S2V53P_js.isPostman(schema)) { | ||
return _OASNormalize.convertPostmanToOpenAPI(schema); | ||
} | ||
return schema; | ||
}).then((schema) => openapiParser__default.default.bundle(schema)).then((bundle) => { | ||
this.cache.bundle = bundle; | ||
return bundle; | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
/** | ||
* Dereference the given API definition. | ||
* | ||
*/ | ||
async deref() { | ||
if (this.cache.deref) | ||
return Promise.resolve(this.cache.deref); | ||
return this.load().then((schema) => { | ||
if (chunkF3S2V53P_js.isPostman(schema)) { | ||
return _OASNormalize.convertPostmanToOpenAPI(schema); | ||
} | ||
return schema; | ||
}).then((schema) => openapiParser__default.default.dereference(schema)).then((dereferenced) => { | ||
this.cache.deref = dereferenced; | ||
return dereferenced; | ||
}); | ||
} | ||
/** | ||
* Validate, and potentially convert to OpenAPI, a given API definition. | ||
* | ||
*/ | ||
async validate(opts = { convertToLatest: false }) { | ||
const convertToLatest = opts.convertToLatest; | ||
const parserOptions = opts.parser || {}; | ||
if (!parserOptions.validate) { | ||
parserOptions.validate = {}; | ||
} | ||
parserOptions.validate.colorizeErrors = this.opts.colorizeErrors; | ||
return this.load().then(async (schema) => { | ||
if (!chunkF3S2V53P_js.isPostman(schema)) { | ||
return schema; | ||
} | ||
return _OASNormalize.convertPostmanToOpenAPI(schema); | ||
}).then(async (schema) => { | ||
if (!chunkF3S2V53P_js.isSwagger(schema) && !chunkF3S2V53P_js.isOpenAPI(schema)) { | ||
return Promise.reject(new Error("The supplied API definition is unsupported.")); | ||
} else if (chunkF3S2V53P_js.isSwagger(schema)) { | ||
const baseVersion = parseInt(schema.swagger, 10); | ||
if (baseVersion === 1) { | ||
return Promise.reject(new Error("Swagger v1.2 is unsupported.")); | ||
} | ||
} | ||
const clonedSchema = JSON.parse(JSON.stringify(schema)); | ||
return openapiParser__default.default.validate(clonedSchema, parserOptions).then(() => { | ||
if (!convertToLatest) { | ||
return schema; | ||
} | ||
return converter__default.default.convertObj(schema, { anchors: true }).then((options) => options.openapi); | ||
}).catch((err) => Promise.reject(err)); | ||
}); | ||
} | ||
/** | ||
* Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition. | ||
* | ||
*/ | ||
version() { | ||
return this.load().then((schema) => { | ||
switch (getAPIDefinitionType2(schema)) { | ||
case "openapi": | ||
return { | ||
specification: "openapi", | ||
version: schema.openapi | ||
}; | ||
case "postman": | ||
let version = "unknown"; | ||
if (schema?.info?.schema) { | ||
const match = schema.info.schema.match( | ||
/http(s?):\/\/schema.getpostman.com\/json\/collection\/v([0-9.]+)\// | ||
); | ||
if (match) { | ||
version = match[2]; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
} | ||
return { | ||
specification: "postman", | ||
version | ||
}; | ||
case "swagger": | ||
return { | ||
specification: "swagger", | ||
version: schema.swagger | ||
}; | ||
default: | ||
throw new Error("Unknown file detected."); | ||
} | ||
}); | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getAPIDefinitionType = exports.isAPIDefinition = void 0; | ||
var fs_1 = __importDefault(require("fs")); | ||
var openapi_parser_1 = __importDefault(require("@readme/openapi-parser")); | ||
var postman_to_openapi_1 = __importDefault(require("@readme/postman-to-openapi")); | ||
var node_fetch_1 = __importDefault(require("node-fetch")); | ||
var swagger2openapi_1 = __importDefault(require("swagger2openapi")); | ||
var utils = __importStar(require("./lib/utils")); | ||
exports.isAPIDefinition = utils.isAPIDefinition; | ||
exports.getAPIDefinitionType = utils.getAPIDefinitionType; | ||
var OASNormalize = /** @class */ (function () { | ||
function OASNormalize(file, opts) { | ||
this.file = file; | ||
this.opts = __assign({ colorizeErrors: false, enablePaths: false }, opts); | ||
this.type = utils.getType(this.file); | ||
this.cache = { | ||
load: false, | ||
bundle: false, | ||
deref: false, | ||
}; | ||
} | ||
/** | ||
* @private | ||
*/ | ||
OASNormalize.prototype.load = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var resolve, _a, resp, contents; | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (this.cache.load) | ||
return [2 /*return*/, Promise.resolve(this.cache.load)]; | ||
resolve = function (obj) { | ||
var ret = utils.stringToJSON(obj); | ||
_this.cache.load = ret; | ||
return Promise.resolve(ret); | ||
}; | ||
_a = this.type; | ||
switch (_a) { | ||
case 'json': return [3 /*break*/, 1]; | ||
case 'string-json': return [3 /*break*/, 1]; | ||
case 'string-yaml': return [3 /*break*/, 1]; | ||
case 'buffer': return [3 /*break*/, 2]; | ||
case 'url': return [3 /*break*/, 3]; | ||
case 'path': return [3 /*break*/, 5]; | ||
} | ||
return [3 /*break*/, 6]; | ||
case 1: return [2 /*return*/, resolve(this.file)]; | ||
case 2: return [2 /*return*/, resolve(this.file.toString())]; | ||
case 3: return [4 /*yield*/, (0, node_fetch_1.default)(utils.normalizeURL(this.file)).then(function (res) { return res.text(); })]; | ||
case 4: | ||
resp = _b.sent(); | ||
return [2 /*return*/, resolve(resp)]; | ||
case 5: | ||
// Load a local file | ||
if (!this.opts.enablePaths) { | ||
return [2 /*return*/, Promise.reject(new Error('Use `opts.enablePaths` to enable accessing local files.'))]; | ||
} | ||
contents = fs_1.default.readFileSync(this.file).toString(); | ||
if (!contents.trim()) { | ||
return [2 /*return*/, Promise.reject(new Error('No file contents found.'))]; | ||
} | ||
return [2 /*return*/, resolve(contents)]; | ||
case 6: return [2 /*return*/, Promise.reject(new Error('Could not load this file.'))]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
OASNormalize.convertPostmanToOpenAPI = function (schema) { | ||
return (0, postman_to_openapi_1.default)(JSON.stringify(schema), null, { outputFormat: 'json', replaceVars: true }).then(JSON.parse); | ||
}; | ||
/** | ||
* Bundle up the given API definition, resolving any external `$ref` pointers in the process. | ||
* | ||
*/ | ||
OASNormalize.prototype.bundle = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (this.cache.bundle) | ||
return [2 /*return*/, Promise.resolve(this.cache.bundle)]; | ||
return [2 /*return*/, this.load() | ||
.then(function (schema) { | ||
// Though Postman collections don't support `$ref` pointers for us to bundle we'll still | ||
// upconvert it to an OpenAPI definition file so our returned dataset is always one of | ||
// those for a Postman dataset. | ||
if (utils.isPostman(schema)) { | ||
return OASNormalize.convertPostmanToOpenAPI(schema); | ||
} | ||
return schema; | ||
}) | ||
.then(function (schema) { return openapi_parser_1.default.bundle(schema); }) | ||
.then(function (bundle) { | ||
_this.cache.bundle = bundle; | ||
return bundle; | ||
})]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Dereference the given API definition. | ||
* | ||
*/ | ||
OASNormalize.prototype.deref = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (this.cache.deref) | ||
return [2 /*return*/, Promise.resolve(this.cache.deref)]; | ||
return [2 /*return*/, this.load() | ||
.then(function (schema) { | ||
// Though Postman collections don't support `$ref` pointers for us to dereference we'll | ||
// still upconvert it to an OpenAPI definition file so our returned dataset is always one | ||
// of those for a Postman dataset. | ||
if (utils.isPostman(schema)) { | ||
return OASNormalize.convertPostmanToOpenAPI(schema); | ||
} | ||
return schema; | ||
}) | ||
.then(function (schema) { return openapi_parser_1.default.dereference(schema); }) | ||
.then(function (dereferenced) { | ||
_this.cache.deref = dereferenced; | ||
return dereferenced; | ||
})]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Validate, and potentially convert to OpenAPI, a given API definition. | ||
* | ||
*/ | ||
OASNormalize.prototype.validate = function (opts) { | ||
if (opts === void 0) { opts = { convertToLatest: false }; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var convertToLatest, parserOptions; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
convertToLatest = opts.convertToLatest; | ||
parserOptions = opts.parser || {}; | ||
if (!parserOptions.validate) { | ||
parserOptions.validate = {}; | ||
} | ||
parserOptions.validate.colorizeErrors = this.opts.colorizeErrors; | ||
return [2 /*return*/, this.load() | ||
.then(function (schema) { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
if (!utils.isPostman(schema)) { | ||
return [2 /*return*/, schema]; | ||
} | ||
return [2 /*return*/, OASNormalize.convertPostmanToOpenAPI(schema)]; | ||
}); | ||
}); }) | ||
.then(function (schema) { return __awaiter(_this, void 0, void 0, function () { | ||
var baseVersion, clonedSchema; | ||
return __generator(this, function (_a) { | ||
if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) { | ||
return [2 /*return*/, Promise.reject(new Error('The supplied API definition is unsupported.'))]; | ||
} | ||
else if (utils.isSwagger(schema)) { | ||
baseVersion = parseInt(schema.swagger, 10); | ||
if (baseVersion === 1) { | ||
return [2 /*return*/, Promise.reject(new Error('Swagger v1.2 is unsupported.'))]; | ||
} | ||
} | ||
clonedSchema = JSON.parse(JSON.stringify(schema)); | ||
return [2 /*return*/, openapi_parser_1.default | ||
.validate(clonedSchema, parserOptions) | ||
.then(function () { | ||
if (!convertToLatest) { | ||
return schema; | ||
} | ||
return swagger2openapi_1.default | ||
.convertObj(schema, { anchors: true }) | ||
.then(function (options) { return options.openapi; }); | ||
}) | ||
.catch(function (err) { return Promise.reject(err); })]; | ||
}); | ||
}); })]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition. | ||
* | ||
*/ | ||
OASNormalize.prototype.version = function () { | ||
return this.load().then(function (schema) { | ||
var _a; | ||
switch ((0, exports.getAPIDefinitionType)(schema)) { | ||
case 'openapi': | ||
return { | ||
specification: 'openapi', | ||
version: schema.openapi, | ||
}; | ||
case 'postman': | ||
var version = 'unknown'; | ||
if ((_a = schema === null || schema === void 0 ? void 0 : schema.info) === null || _a === void 0 ? void 0 : _a.schema) { | ||
// Though `info.schema` is required by the Postman spec there's no strictness to its | ||
// contents so we'll do our best to extract a version out of this schema URL that they | ||
// seem to usually match. If not we'll fallback to treating it as an `unknown` version. | ||
var match = schema.info.schema.match(/http(s?):\/\/schema.getpostman.com\/json\/collection\/v([0-9.]+)\//); | ||
if (match) { | ||
version = match[2]; | ||
} | ||
} | ||
return { | ||
specification: 'postman', | ||
version: version, | ||
}; | ||
case 'swagger': | ||
return { | ||
specification: 'swagger', | ||
version: schema.swagger, | ||
}; | ||
default: | ||
throw new Error('Unknown file detected.'); | ||
} | ||
}); | ||
}; | ||
return OASNormalize; | ||
}()); | ||
exports.default = OASNormalize; | ||
exports.getAPIDefinitionType = getAPIDefinitionType2; | ||
exports.isAPIDefinition = isAPIDefinition2; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
@@ -5,7 +5,7 @@ /** | ||
*/ | ||
export declare function isBuffer(obj: any): any; | ||
declare function isBuffer(obj: any): any; | ||
/** | ||
* Converts GitHub blob URLs to raw URLs | ||
*/ | ||
export declare function normalizeURL(url: string): string; | ||
declare function normalizeURL(url: string): string; | ||
/** | ||
@@ -15,3 +15,3 @@ * Determine the type of a given variable. Returns `false` if unrecognized. | ||
*/ | ||
export declare function getType(obj: any): false | "buffer" | "json" | "string-json" | "string-yaml" | "url" | "path"; | ||
declare function getType(obj: any): false | "buffer" | "json" | "string-json" | "string-yaml" | "url" | "path"; | ||
/** | ||
@@ -21,3 +21,3 @@ * Determine if a given schema if an OpenAPI definition. | ||
*/ | ||
export declare function isOpenAPI(schema: Record<string, unknown>): boolean; | ||
declare function isOpenAPI(schema: Record<string, unknown>): boolean; | ||
/** | ||
@@ -34,3 +34,3 @@ * Determine if a given schema is a Postman collection. | ||
*/ | ||
export declare function isPostman(schema: Record<string, unknown>): boolean; | ||
declare function isPostman(schema: Record<string, unknown>): boolean; | ||
/** | ||
@@ -40,3 +40,3 @@ * Determine if a given schema if an Swagger definition. | ||
*/ | ||
export declare function isSwagger(schema: Record<string, unknown>): boolean; | ||
declare function isSwagger(schema: Record<string, unknown>): boolean; | ||
/** | ||
@@ -46,3 +46,3 @@ * Convert a YAML blob or stringified JSON object into a JSON object. | ||
*/ | ||
export declare function stringToJSON(string: string | Record<string, unknown>): any; | ||
declare function stringToJSON(string: string | Record<string, unknown>): any; | ||
/** | ||
@@ -52,3 +52,3 @@ * Determine if a given schema is an API definition that we can support. | ||
*/ | ||
export declare function isAPIDefinition(schema: Record<string, unknown>): boolean; | ||
declare function isAPIDefinition(schema: Record<string, unknown>): boolean; | ||
/** | ||
@@ -58,2 +58,4 @@ * Retrieve the type of API definition that a given schema is. | ||
*/ | ||
export declare function getAPIDefinitionType(schema: Record<string, unknown>): "openapi" | "postman" | "swagger" | "unknown"; | ||
declare function getAPIDefinitionType(schema: Record<string, unknown>): "openapi" | "postman" | "swagger" | "unknown"; | ||
export { getAPIDefinitionType, getType, isAPIDefinition, isBuffer, isOpenAPI, isPostman, isSwagger, normalizeURL, stringToJSON }; |
@@ -1,145 +0,44 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
'use strict'; | ||
var chunkF3S2V53P_js = require('../chunk-F3S2V53P.js'); | ||
Object.defineProperty(exports, 'getAPIDefinitionType', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.getAPIDefinitionType; } | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getAPIDefinitionType = exports.isAPIDefinition = exports.stringToJSON = exports.isSwagger = exports.isPostman = exports.isOpenAPI = exports.getType = exports.normalizeURL = exports.isBuffer = void 0; | ||
var js_yaml_1 = __importStar(require("js-yaml")); | ||
/** | ||
* Determine if a given variable is a `Buffer`. | ||
* | ||
*/ | ||
function isBuffer(obj) { | ||
return (obj != null && | ||
obj.constructor != null && | ||
typeof obj.constructor.isBuffer === 'function' && | ||
obj.constructor.isBuffer(obj)); | ||
} | ||
exports.isBuffer = isBuffer; | ||
/** | ||
* Converts GitHub blob URLs to raw URLs | ||
*/ | ||
function normalizeURL(url) { | ||
if (url.startsWith('https://github.com/') && url.includes('/blob/')) { | ||
return url.replace('github.com', 'raw.githubusercontent.com').replace('/blob/', '/'); | ||
} | ||
return url; | ||
} | ||
exports.normalizeURL = normalizeURL; | ||
/** | ||
* Determine the type of a given variable. Returns `false` if unrecognized. | ||
* | ||
*/ | ||
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/)) { | ||
// Not sure about this... | ||
return 'string-yaml'; | ||
} | ||
else if (obj.substring(0, 4) === 'http') { | ||
return 'url'; | ||
} | ||
return 'path'; | ||
} | ||
return false; | ||
} | ||
exports.getType = getType; | ||
/** | ||
* Determine if a given schema if an OpenAPI definition. | ||
* | ||
*/ | ||
function isOpenAPI(schema) { | ||
return !!schema.openapi; | ||
} | ||
exports.isOpenAPI = isOpenAPI; | ||
/** | ||
* Determine if a given schema is a Postman collection. | ||
* | ||
* Unfortunately the Postman schema spec doesn't have anything like `openapi` or `swagger` for us | ||
* to look at but it does require that `info` and `item` be present and as `item` doesn't exist in | ||
* OpenAPI or Swagger we can use the combination of those two properties to determine if what we | ||
* have is a Postman collection. | ||
* | ||
* @see {@link https://schema.postman.com/json/collection/v2.0.0/collection.json} | ||
* @see {@link https://schema.postman.com/json/collection/v2.1.0/collection.json} | ||
*/ | ||
function isPostman(schema) { | ||
return !!schema.info && !!schema.item; | ||
} | ||
exports.isPostman = isPostman; | ||
/** | ||
* Determine if a given schema if an Swagger definition. | ||
* | ||
*/ | ||
function isSwagger(schema) { | ||
return !!schema.swagger; | ||
} | ||
exports.isSwagger = isSwagger; | ||
/** | ||
* Convert a YAML blob or stringified JSON object into a JSON object. | ||
* | ||
*/ | ||
function stringToJSON(string) { | ||
if (typeof string === 'object') { | ||
return string; | ||
} | ||
else if (string.match(/^\s*{/)) { | ||
return JSON.parse(string); | ||
} | ||
return js_yaml_1.default.load(string, { schema: js_yaml_1.JSON_SCHEMA }); | ||
} | ||
exports.stringToJSON = stringToJSON; | ||
/** | ||
* Determine if a given schema is an API definition that we can support. | ||
* | ||
*/ | ||
function isAPIDefinition(schema) { | ||
return isOpenAPI(schema) || isPostman(schema) || isSwagger(schema); | ||
} | ||
exports.isAPIDefinition = isAPIDefinition; | ||
/** | ||
* Retrieve the type of API definition that a given schema is. | ||
* | ||
*/ | ||
function getAPIDefinitionType(schema) { | ||
if (isOpenAPI(schema)) { | ||
return 'openapi'; | ||
} | ||
else if (isPostman(schema)) { | ||
return 'postman'; | ||
} | ||
else if (isSwagger(schema)) { | ||
return 'swagger'; | ||
} | ||
return 'unknown'; | ||
} | ||
exports.getAPIDefinitionType = getAPIDefinitionType; | ||
Object.defineProperty(exports, 'getType', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.getType; } | ||
}); | ||
Object.defineProperty(exports, 'isAPIDefinition', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.isAPIDefinition; } | ||
}); | ||
Object.defineProperty(exports, 'isBuffer', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.isBuffer; } | ||
}); | ||
Object.defineProperty(exports, 'isOpenAPI', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.isOpenAPI; } | ||
}); | ||
Object.defineProperty(exports, 'isPostman', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.isPostman; } | ||
}); | ||
Object.defineProperty(exports, 'isSwagger', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.isSwagger; } | ||
}); | ||
Object.defineProperty(exports, 'normalizeURL', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.normalizeURL; } | ||
}); | ||
Object.defineProperty(exports, 'stringToJSON', { | ||
enumerable: true, | ||
get: function () { return chunkF3S2V53P_js.stringToJSON; } | ||
}); | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "oas-normalize", | ||
"version": "8.4.1", | ||
"description": "Tooling for converting, valiating, and parsing OpenAPI, Swagger, and Postman API definitions", | ||
"version": "9.0.0", | ||
"description": "Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
}, | ||
"./lib/utils": { | ||
"types": "./dist/lib/utils.d.ts", | ||
"require": "./dist/lib/utils.js", | ||
"import": "./dist/lib/utils.mjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
@@ -34,3 +48,3 @@ "engines": { | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "tsup", | ||
"lint": "eslint . --ext .js,.ts && npm run prettier", | ||
@@ -42,3 +56,3 @@ "prebuild": "rm -rf dist/", | ||
"prettier:write": "prettier --list-different --write \"./**/**.{md,js,ts}\"", | ||
"test": "jest --coverage" | ||
"test": "vitest --coverage" | ||
}, | ||
@@ -50,20 +64,20 @@ "license": "MIT", | ||
"js-yaml": "^4.1.0", | ||
"node-fetch": "^2.6.1", | ||
"openapi-types": "^12.1.0", | ||
"node-fetch": "^2.7.0", | ||
"openapi-types": "^12.1.3", | ||
"swagger2openapi": "^7.0.8" | ||
}, | ||
"devDependencies": { | ||
"@readme/eslint-config": "^10.5.2", | ||
"@readme/oas-examples": "^5.10.0", | ||
"@types/jest": "^29.5.1", | ||
"@readme/eslint-config": "^12.0.2", | ||
"@readme/oas-examples": "^5.12.0", | ||
"@types/js-yaml": "^4.0.5", | ||
"@types/node-fetch": "^2.6.3", | ||
"eslint": "^8.39.0", | ||
"jest": "^29.5.0", | ||
"nock": "^13.3.1", | ||
"prettier": "^2.8.8", | ||
"ts-jest": "^29.1.0", | ||
"typescript": "^5.0.4" | ||
"@types/node-fetch": "^2.6.4", | ||
"@vitest/coverage-v8": "^0.34.1", | ||
"eslint": "^8.46.0", | ||
"nock": "^13.3.2", | ||
"prettier": "^3.0.1", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.1.6", | ||
"vitest": "^0.34.1" | ||
}, | ||
"prettier": "@readme/eslint-config/prettier" | ||
} |
@@ -8,3 +8,3 @@ <p align="center"> | ||
<p align="center"> | ||
Tooling for converting, valiating, and parsing OpenAPI, Swagger, and Postman API definitions | ||
Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions | ||
</p> | ||
@@ -36,3 +36,3 @@ | ||
const oas = new OASNormalize( | ||
'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml' | ||
'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml', | ||
// ...or a string, path, JSON blob, whatever you've got. | ||
@@ -39,0 +39,0 @@ ); |
@@ -161,3 +161,3 @@ import type { OpenAPI } from 'openapi-types'; | ||
parser?: openapiParser.Options; | ||
} = { convertToLatest: false } | ||
} = { convertToLatest: false }, | ||
) { | ||
@@ -234,3 +234,3 @@ const convertToLatest = opts.convertToLatest; | ||
const match = schema.info.schema.match( | ||
/http(s?):\/\/schema.getpostman.com\/json\/collection\/v([0-9.]+)\// | ||
/http(s?):\/\/schema.getpostman.com\/json\/collection\/v([0-9.]+)\//, | ||
); | ||
@@ -237,0 +237,0 @@ |
@@ -8,2 +8,3 @@ { | ||
"lib": ["dom", "es2020"], | ||
"moduleResolution": "node", | ||
"outDir": "dist/", | ||
@@ -13,5 +14,6 @@ "paths": { | ||
}, | ||
"skipLibCheck": true | ||
"skipLibCheck": true, | ||
"target": "ES2020" | ||
}, | ||
"include": ["./src/**/*"] | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
2694722
28
994
2
2
Updatednode-fetch@^2.7.0
Updatedopenapi-types@^12.1.3