openapi-graph-core
Advanced tools
Comparing version 0.0.1-alpha.3.0 to 1.0.0
"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" | ||
} | ||
} |
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
33834
45
770
0
5
11
1
4
+ Addedpino@^6.11.2
+ Addedpino-pretty@^4.7.1
+ Addedyaml@^1.10.2
+ Added@hapi/bourne@2.1.0(transitive)
+ Addedansi-styles@3.2.14.3.0(transitive)
+ Addedargs@5.0.3(transitive)
+ Addedatomic-sleep@1.0.0(transitive)
+ Addedcamelcase@5.0.0(transitive)
+ Addedchalk@2.4.24.1.2(transitive)
+ Addedcolor-convert@1.9.32.0.1(transitive)
+ Addedcolor-name@1.1.31.1.4(transitive)
+ Addeddateformat@4.6.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfast-redact@3.5.0(transitive)
+ Addedfast-safe-stringify@2.1.1(transitive)
+ Addedflatstr@1.0.12(transitive)
+ Addedhas-flag@3.0.04.0.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjmespath@0.15.0(transitive)
+ Addedjoycon@2.2.5(transitive)
+ Addedleven@2.1.0(transitive)
+ Addedmri@1.1.4(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpino@6.14.0(transitive)
+ Addedpino-pretty@4.8.0(transitive)
+ Addedpino-std-serializers@3.2.0(transitive)
+ Addedprocess-warning@1.0.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedquick-format-unescaped@4.0.4(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrfdc@1.4.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsonic-boom@1.4.1(transitive)
+ Addedsplit2@3.2.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-json-comments@3.1.1(transitive)
+ Addedsupports-color@5.5.07.2.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyaml@1.10.2(transitive)
- Removedopenapi-graph-types@0.0.1-alpha.3.1(transitive)