New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

openapi-graph-core

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-graph-core - npm Package Compare versions

Comparing version 0.0.1-alpha.3.0 to 1.0.0

lib/utils/fetcher.d.ts

11

lib/graph/builder/finder.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveReference = exports.getRefEdges = exports.getSchemaNodes = exports.getInlineSchemasNodes = exports.getDefinedSchemasNodes = void 0;
const openapi_graph_types_1 = require("openapi-graph-types");
const SchemaNode_1 = require("../../graph/nodes/SchemaNode");
const utils_1 = require("../../utils");
const edges_1 = require("../edges");

@@ -63,3 +65,6 @@ /**

function getSchemaNodes(api) {
return { ...getDefinedSchemasNodes(api), ...getInlineSchemasNodes(api) };
const schemas = getDefinedSchemasNodes(api);
const inlineSchemas = getInlineSchemasNodes(api);
utils_1.log(`Found ${Object.values(schemas).length} schemas and ${Object.values(inlineSchemas).length} inline schemas in ${api.info.title}`, openapi_graph_types_1.LogLevel.DEBUG);
return { ...schemas, ...inlineSchemas };
}

@@ -81,3 +86,3 @@ exports.getSchemaNodes = getSchemaNodes;

}
else {
else if (json !== null) {
function handleJson() {

@@ -109,3 +114,3 @@ Object.keys(json).forEach((key) => {

Object.values(refs.schemaRef)
.filter((r) => graphs?.[r.refToFilePath].nodes.schemas[r.tokenName])
.filter((r) => graphs?.[r.refToFilePath] && graphs?.[r.refToFilePath].nodes.schemas[r.tokenName])
.forEach((r) => {

@@ -112,0 +117,0 @@ const schema = graphs?.[r.refToFilePath].nodes.schemas[r.tokenName];

@@ -5,3 +5,5 @@ "use strict";

const _1 = require(".");
const openapi_graph_types_1 = require("openapi-graph-types");
const OpenAPIGraph_1 = require("../OpenAPIGraph");
const utils_1 = require("../../utils");
const OpenAPIGraphsBuilder = class OpenAPIGraphsBuilderImpl {

@@ -28,2 +30,3 @@ constructor(apis) {

const edges = _1.getRefEdges(api.content, api.path);
utils_1.log(`Found ${Object.values(edges.schemaRef).length} schema references in ${api.content.info.title}`, openapi_graph_types_1.LogLevel.DEBUG);
return _1.resolveReference(graphs, edges);

@@ -30,0 +33,0 @@ }

@@ -5,9 +5,12 @@ "use strict";

const _1 = require(".");
const fetcher_1 = require("../openapi/fetcher");
const utils_1 = require("../utils");
const utils_2 = require("../utils");
const OpenAPIGraphs = class OpenAPIGraphsImpl {
constructor(rootPath) {
constructor(rootPath, options = utils_2.defaultOpenAPIGraphLibConfig) {
this.rootPath = rootPath;
utils_2.setOpenAPIGraphLibConfig({ ...utils_2.openAPIGraphLibConfig, ...options });
}
async build() {
const apis = await fetcher_1.fetcher(this.rootPath);
const apis = await utils_1.fetcher(this.rootPath);
utils_2.log(`Found ${apis.length} openAPI definitions`);
this.builder = new _1.OpenAPIGraphsBuilder(apis);

@@ -14,0 +17,0 @@ }

@@ -8,2 +8,3 @@ /**

import { OpenAPIContent } from 'openapi-graph-types';
export declare function fetcher(paths: string[]): Promise<OpenAPIContent[]>;
export declare function fetcher(path: string): Promise<OpenAPIContent[]>;

@@ -10,0 +11,0 @@ /**

@@ -13,11 +13,33 @@ "use strict";

const _1 = require(".");
async function fetcher(path) {
// Converts path to absolute
path = path_1.resolve(path);
const pathExists = fs_1.existsSync(path);
if (!pathExists) {
throw new Error('The given path does not exist in your system');
const logger = require('pino')({
prettyPrint: {
ignore: 'time,pid,hostname',
singleLine: true
}
});
const COMMON_FOLDER_NAMES = ["node_modules", "target"];
const COMMON_FILES_NAMES = ["docker-compose"].flatMap(e => [`${e}.yml`, `${e}.yaml`]);
async function fetcher(input) {
if (typeof input === 'string') {
// If we are working with a path, then it means that we have to find all openAPI specifications
// Converts path to absolute
const resolvedPath = path_1.resolve(input);
const pathExists = fs_1.existsSync(resolvedPath);
if (!pathExists) {
throw new Error('The given path does not exist in your system');
}
else {
return await loadsSwaggerFiles(resolvedPath);
}
}
else if (([]).constructor === input.constructor) {
// If we are working with a path, then it means that we have to find all openAPI specifications
const resolvedPaths = input
.map(i => path_1.resolve(input))
.filter(resolvedPath => fs_1.existsSync(resolvedPath) || logger.console.warn(`Ignoring ${resolvedPath} because the path is not reachable`));
return _1.getOpenApisContent(resolvedPaths);
}
else {
return await loadsSwaggerFiles(path);
logger.console.warn(`Invalid given input. It was expected a string or array of strings. Received ${input}.`);
return [];
}

@@ -36,2 +58,3 @@ }

const projectContent = await getFiles(path_1.resolve(projectPath));
logger.info(`Found (${projectContent.length}) ${projectContent}`);
if (projectContent === undefined || projectContent.length === 0) {

@@ -45,8 +68,17 @@ return [];

for (const entry of entries) {
if (entry.isDirectory()) {
paths.push(...(await getFiles(`${fromPath}/${entry.name}/`)));
// We don't check hidden files or folders
if (!/(^|\/)\.[^/.]/g.test(entry.name)) {
if (entry.isDirectory() &&
// Don't check common folder names
!COMMON_FOLDER_NAMES.includes(entry.name)) {
paths.push(...(await getFiles(`${fromPath}/${entry.name}/`)));
}
else if (entry.isFile() &&
// JSON are also valid, but they are more generic, so we don't look for them because it takes a lot of time
entry.name.match(/.*\.(yml|yaml)/gi) &&
// Don't check common file names
!COMMON_FILES_NAMES.includes(entry.name)) {
paths.push(path_1.resolve(`${fromPath}/${entry.name}`));
}
}
else if (entry.name.match(/.*\.(yml|yaml|json)/gi)) {
paths.push(path_1.resolve(`${fromPath}/${entry.name}`));
}
}

@@ -53,0 +85,0 @@ return paths;

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOpenApisContent = void 0;
const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
const fs_1 = require("fs");
const js_yaml_1 = require("js-yaml");
const logger = require('pino')({
prettyPrint: {
ignore: 'time,pid,hostname',
singleLine: true
}
});
/**

@@ -17,14 +19,6 @@ * reads the contents of all given paths. The files will be validated.

async function getOpenApisContent(paths) {
// TODO Check that only v3 are valid
// Validates it contents, but they are
const validationPromises = paths.map((p) => swagger_parser_1.default.validate(p));
// Filter only promises which return some content
const validationResponse = await Promise.allSettled(validationPromises);
return (validationResponse
.map((v, i) => ({ promise: v, path: paths[i] }))
.filter((r) => r.promise.status === 'fulfilled')
// as you can see, the file is read twice. One in the SwaggerParser.validate
// and another one here. It cool be cool to get SwaggerParser.validate value dereferenced
.map((r) => ({ path: r.path, content: js_yaml_1.load(fs_1.readFileSync(r.path, 'utf8')) })));
return (paths
.map(p => ({ path: p, content: js_yaml_1.load(fs_1.readFileSync(p, 'utf8')) || logger.warn(`Couldn't load ${p}`) }))
.filter(p => p.content?.openapi?.includes("3.0")));
}
exports.getOpenApisContent = getOpenApisContent;
{
"name": "openapi-graph-core",
"version": "0.0.1-alpha.3.0",
"version": "1.0.0",
"description": "A TS library to manage large API projects defined by OpenAPIv3 specification.",

@@ -18,3 +18,4 @@ "main": "./lib/index.js",

"lint": "tslint -p tsconfig.json",
"debug": "node --debug-brk --inspect ./node_modules/jest/bin/jest -i"
"debug": "node --debug-brk --inspect ./node_modules/jest/bin/jest -i",
"release": "release-it"
},

@@ -36,2 +37,3 @@ "author": "onmax",

"prettier": "^2.2.1",
"release-it": "^14.6.1",
"ts-jest": "^26.5.3",

@@ -44,4 +46,7 @@ "tslint": "^6.1.3",

"@apidevtools/swagger-parser": "^10.0.2",
"openapi-graph-types": "0.0.1-alpha.3.1"
"openapi-graph-types": "../openapi-graph-types",
"pino": "^6.11.2",
"pino-pretty": "^4.7.1",
"yaml": "^1.10.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc