Huge News!Announcing our $40M Series B led by Abstract Ventures.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.2.2.0 to 0.0.1-alpha.3.0

lib/analyzer/Analyzer.d.ts

20

lib/graph/builder/finder.d.ts

@@ -0,3 +1,6 @@

import { EdgesRefDict, Nodes, OpenAPIGraphsBuilderInterface, SchemaNodeInterface } from 'openapi-graph-types';
import { OpenAPIV3 } from 'openapi-types';
import { EdgesRefDict, Nodes, OpenAPIGraphInterface } from 'openapi-graph-types';
export declare function getDefinedSchemasNodes(api: OpenAPIV3.Document): {
[key: string]: SchemaNodeInterface;
};
/**

@@ -9,3 +12,8 @@ * It will find all the schemas defined in the specification

*/
export declare function getSchemaNodes(apiContent: OpenAPIV3.Document): Nodes['schemas'];
export declare function getInlineSchemasNodes(json: any, currentIndex?: number, nodes?: {
[key: string]: SchemaNodeInterface;
}): {
[key: string]: SchemaNodeInterface;
};
export declare function getSchemaNodes(api: OpenAPIV3.Document): Nodes['schemas'];
/**

@@ -18,2 +26,8 @@ * It will find all the references defined in the specification

export declare function getRefEdges(json: any, absolutePath: string, edges?: EdgesRefDict): EdgesRefDict;
export declare function resolveReference(graphs: OpenAPIGraphInterface[], refs: EdgesRefDict): EdgesRefDict;
/**
* Sets the edges' child value
* @param graphs
* @param refs
* @returns
*/
export declare function resolveReference(graphs: OpenAPIGraphsBuilderInterface['graphs'], refs: EdgesRefDict): EdgesRefDict;

85

lib/graph/builder/finder.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveReference = exports.getRefEdges = exports.getSchemaNodes = void 0;
exports.resolveReference = exports.getRefEdges = exports.getSchemaNodes = exports.getInlineSchemasNodes = exports.getDefinedSchemasNodes = void 0;
const SchemaNode_1 = require("../../graph/nodes/SchemaNode");
const edges_1 = require("../edges");
/**
* Creates a new Schema depends on its type. Swagger schemas can be Array or NonArray
*
* @param schema source
* @param name The given name for the schema
* @returns the new schema instance
*/
function createSchema(schemaNodes, schema, schemaName, isInline) {
if (schema && !('$ref' in schema)) {
if ('type' in schema && schema?.type === 'array') {
schemaNodes[schemaName] = new SchemaNode_1.SchemaNode(schemaName, schema, isInline);
}
else {
schemaNodes[schemaName] = new SchemaNode_1.SchemaNode(schemaName, schema, isInline);
}
}
}
function getDefinedSchemasNodes(api) {
const nodes = {};
const schemas = api.components?.schemas;
if (schemas) {
Object.keys(schemas).forEach((schemaName) => createSchema(nodes, schemas[schemaName], schemaName, false));
}
return nodes;
}
exports.getDefinedSchemasNodes = getDefinedSchemasNodes;
/**
* It will find all the schemas defined in the specification

@@ -12,14 +38,29 @@ *

*/
function getSchemaNodes(apiContent) {
const nodes = {};
const schemas = apiContent?.components?.schemas;
if (schemas) {
Object.keys(schemas).forEach((schema) => {
if ('$ref' !== schema) {
nodes[schema] = new SchemaNode_1.SchemaNode(schema, schemas[schema]);
}
});
function getInlineSchemasNodes(json, currentIndex = 1, nodes = {}) {
const schema = json?.schema;
if (schema) {
createSchema(nodes, schema, `inline-schema-${currentIndex++}`, true);
if (schema?.type === 'array') {
getInlineSchemasNodes(json.items, currentIndex, nodes);
}
}
else if (json) {
function handleJson() {
Object.keys(json).forEach((key) => {
nodes = getInlineSchemasNodes(json[key], currentIndex, nodes);
});
}
if ({}.constructor === json.constructor) {
handleJson();
}
else if ([].constructor === json.constructor) {
json.forEach(handleJson);
}
}
return nodes;
}
exports.getInlineSchemasNodes = getInlineSchemasNodes;
function getSchemaNodes(api) {
return { ...getDefinedSchemasNodes(api), ...getInlineSchemasNodes(api) };
}
exports.getSchemaNodes = getSchemaNodes;

@@ -33,4 +74,3 @@ /**

function getRefEdges(json, absolutePath, edges = { schemaRef: {} }) {
/* tslint:disable:no-string-literal */
const ref = json['$ref'];
const ref = json?.$ref;
if (ref) {

@@ -45,3 +85,3 @@ // TODO Should test any type of component

Object.keys(json).forEach((key) => {
edges = getRefEdges(json[key], absolutePath, edges);
getRefEdges(json[key], absolutePath, edges);
});

@@ -59,2 +99,8 @@ }

exports.getRefEdges = getRefEdges;
/**
* Sets the edges' child value
* @param graphs
* @param refs
* @returns
*/
function resolveReference(graphs, refs) {

@@ -65,7 +111,10 @@ const filteredRefs = {

Object.values(refs.schemaRef)
.map((r) => ({ r, g: graphs.find((g) => g.path === r.absolutePath) }))
.filter((o) => o.g?.nodes.schemas[o.r.tokenName])
.forEach((o) => {
o.r.child = o.g?.nodes.schemas[o.r.tokenName];
filteredRefs.schemaRef[o.r.getFullPath()] = o.r;
.filter((r) => graphs?.[r.refToFilePath].nodes.schemas[r.tokenName])
.forEach((r) => {
const schema = graphs?.[r.refToFilePath].nodes.schemas[r.tokenName];
if (schema) {
schema.referencedBy[r.path] = r;
r.child = schema;
filteredRefs.schemaRef[r.path] = r;
}
});

@@ -72,0 +121,0 @@ return filteredRefs;

@@ -11,10 +11,10 @@ "use strict";

initializeGraph(apis) {
const graphs = [];
const graphs = {};
apis.forEach((api) => {
const graph = new OpenAPIGraph_1.OpenAPIGraph(api.path);
graph.setSchemaNodes(this.getSchemaNodes(api));
graphs.push(graph);
graphs[api.path] = graph;
});
graphs.map((graph, i) => {
graph.setRefEdges(this.getRefEdges(graphs, apis[i]));
Object.keys(graphs).map((graphKey, i) => {
graphs[graphKey].setRefEdges(this.getRefEdges(graphs, apis[i]));
});

@@ -21,0 +21,0 @@ return graphs;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Edge = void 0;
const openapi_graph_types_1 = require("openapi-graph-types");
const path_1 = require("path");
const Edge = class EdgeImpl {
constructor(filePath, specificationPath) {
// TODO: Check that inputs are valid
this.filePath = filePath;
this.rawPath = specificationPath;
const cleanSpecificationPathParts = specificationPath?.split('#');
if (cleanSpecificationPathParts.length < 2) {
return;
}
this.specificationPath = cleanSpecificationPathParts[1];
switch (this.type) {
case openapi_graph_types_1.RefType.Local:
case openapi_graph_types_1.RefType.URL:
this.refToFilePath = path_1.resolve(`${filePath}/${cleanSpecificationPathParts[0]}`);
break;
case openapi_graph_types_1.RefType.Remote:
this.refToFilePath = path_1.resolve(`${filePath}/../${cleanSpecificationPathParts[0]}`);
break;
}
const specificationPathParts = specificationPath.split('/');
this.tokenName = specificationPathParts[specificationPathParts.length - 1];
}
get path() {
return `${this.filePath}#${this.specificationPath}`;
}
// TODO Tests
get type() {
// Test if extension file is in the string
if (/\.(yml|yaml|json)\/?#\//.test(this.rawPath)) {
if (/^https?:\/\//.test(this.rawPath)) {
return openapi_graph_types_1.RefType.URL;
}
else {
return openapi_graph_types_1.RefType.Remote;
}
}
else if (!/\.(yml|yaml|json)\/?#\//.test(this.rawPath)) {
return openapi_graph_types_1.RefType.Local;
}
return undefined;
}
};
exports.Edge = Edge;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefEdge = void 0;
const path_1 = require("path");
const openapi_graph_types_1 = require("openapi-graph-types");
const _1 = require(".");
const RefEdge = class RefEdgeImpl extends _1.Edge {
constructor(absolutePath, ref) {
super();
// Check that is valid ref and get its type
const type = this.getType(ref);
if (type === undefined) {
return;
}
this.type = type;
this.ref = ref;
this.tokenName = this.getTokenName(ref);
this.absolutePath = this.resolveAbsolutePath(absolutePath, ref);
constructor(filePath, ref) {
super(filePath, ref);
}
// TODO Tests
getType(ref) {
// Test if extension file is in the string
if (/\.(yml|yaml|json)\/?#\//.test(ref)) {
if (/^https?:\/\//.test(ref)) {
return openapi_graph_types_1.RefType.URL;
}
else {
return openapi_graph_types_1.RefType.Remote;
}
}
else if (!/\.(yml|yaml|json)\/?#\//.test(ref)) {
return openapi_graph_types_1.RefType.Local;
}
return undefined;
}
// TODO Tests
getTokenName(ref) {
const parts = ref.split('/');
return parts[parts.length - 1];
}
// TODO Tests
resolveAbsolutePath(absolutePath, ref) {
const filePath = ref.split('#')[0];
return path_1.resolve(this.type === openapi_graph_types_1.RefType.Remote ? `${absolutePath}/../${filePath}` : `${absolutePath}/${filePath}`);
}
// TODO Tests
getFullPath() {
return `${this.absolutePath}#${this.ref.split('#')[1]}`;
}
};
exports.RefEdge = RefEdge;

@@ -1,2 +0,2 @@

import { NodeConstructor } from "openapi-graph-types";
import { NodeConstructor } from 'openapi-graph-types';
export declare const Node: NodeConstructor;

@@ -6,7 +6,9 @@ "use strict";

const SchemaNode = class SchemaNodeImpl extends Node_1.Node {
constructor(name, content) {
constructor(name, content, isInline) {
super(name);
this.content = content;
this.referencedBy = {};
this.isInline = isInline;
}
};
exports.SchemaNode = SchemaNode;
export { OpenAPIGraphs } from './graph';
export { Analyzer } from './analyzer';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAPIGraphs = void 0;
exports.Analyzer = exports.OpenAPIGraphs = void 0;
var graph_1 = require("./graph");
Object.defineProperty(exports, "OpenAPIGraphs", { enumerable: true, get: function () { return graph_1.OpenAPIGraphs; } });
var analyzer_1 = require("./analyzer");
Object.defineProperty(exports, "Analyzer", { enumerable: true, get: function () { return analyzer_1.Analyzer; } });
{
"name": "openapi-graph-core",
"version": "0.0.1-alpha.2.2.0",
"version": "0.0.1-alpha.3.0",
"description": "A TS library to manage large API projects defined by OpenAPIv3 specification.",

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

"@apidevtools/swagger-parser": "^10.0.2",
"openapi-graph-types": "0.0.1-alpha.2.1.0"
"openapi-graph-types": "0.0.1-alpha.3.1"
}
}
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