diff --git a/README.md b/README.md index be54293..05acf9c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Swagger-graph +# OpenAPI-graph A TS library to manage large API projects defined by OpenAPIv3 specification. @@ -8,33 +8,19 @@ A TS library to manage large API projects defined by OpenAPIv3 specification. Just run -> npm install openapi-graph +> npm install openapi-graph-core and you are good to go -## Functions +## Creating an OpenAPI graph -For now, just one function is being exposed - -### `getUnusedSchemas(path: string)` - -It will create a OpenAPIGraph, and get the schemas that are not being used as a list. - -#### Arguments - -`path: string` - Abosolute, relative or url where the API has being defined - -#### Returns - -A list of `Schemas` that are not being used in the API. - -#### Example +The constructor needs to be a path of the root of the proyect where all your openAPI specifications are stored which will be fetched automatically. ```javascript -const openApiGraph = require('openapi-graph'); +const OpenAPIGraphs = require('openapi-graph-core'); (async () => { - const a = await openApiGraph.getUnusedSchemas('./') - console.log(a) + const graphs = await new OpenAPIGraphs('./').build() + console.log(graphs) })(); ``` \ No newline at end of file diff --git a/package.json b/package.json index c739522..f42bd5a 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,12 @@ "files": [ "lib" ], + "publishConfig": { + "registry": "https://npm.pkg.github.com" + }, + "repository": { + "url": "git://github.com/onmax/open-api-graph.git" + }, "devDependencies": { "@types/jest": "^26.0.20", "@types/js-yaml": "^4.0.0", @@ -37,4 +43,4 @@ "dependencies": { "@apidevtools/swagger-parser": "^10.0.2" } -} +} \ No newline at end of file diff --git a/src/graph/OpenAPIGraphsManager.ts b/src/graph/OpenAPIGraphs.ts similarity index 76% rename from src/graph/OpenAPIGraphsManager.ts rename to src/graph/OpenAPIGraphs.ts index 04806ac..9af8f06 100644 --- a/src/graph/OpenAPIGraphsManager.ts +++ b/src/graph/OpenAPIGraphs.ts @@ -1,12 +1,18 @@ -import { OpenAPIContent } from '../../model'; +import { fetcher } from '../openapi/fetcher'; import { OpenAPIGraphsBuilder } from './builder/OpenAPIGraphsBuilder'; import { RefEdge } from './edges'; import { SchemaNode } from './nodes/SchemaNode'; -export class OpenAPIGraphsManager { +export class OpenAPIGraphs { builder!: OpenAPIGraphsBuilder; + rootPath!: string; - constructor(apis: OpenAPIContent[]) { + constructor(rootPath: string) { + this.rootPath = rootPath; + } + + async build() { + const apis = await fetcher(this.rootPath) this.builder = new OpenAPIGraphsBuilder(apis); } diff --git a/src/graph/index.ts b/src/graph/index.ts index 38d4eea..70aefbf 100644 --- a/src/graph/index.ts +++ b/src/graph/index.ts @@ -1,3 +1,3 @@ export { OpenAPIGraph } from './OpenAPIGraph'; export { OpenAPIGraphsBuilder } from './builder/OpenAPIGraphsBuilder'; -export { OpenAPIGraphsManager } from './OpenAPIGraphsManager'; +export { OpenAPIGraphs } from './OpenAPIGraphs'; diff --git a/src/index.ts b/src/index.ts index 026cc34..1d8738e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export { getUnusedSchemas } from './swagger-graph'; +export { OpenAPIGraphs } from './graph'; diff --git a/src/fetcher.ts b/src/openapi/fetcher.ts similarity index 95% rename from src/fetcher.ts rename to src/openapi/fetcher.ts index 56a67df..b10444d 100644 --- a/src/fetcher.ts +++ b/src/openapi/fetcher.ts @@ -7,8 +7,8 @@ import { existsSync, readdirSync } from 'fs'; import { resolve } from 'path'; -import { OpenAPIContent } from '../model'; -import { getOpenApisContent } from './openapi'; +import { OpenAPIContent } from '../../model'; +import { getOpenApisContent } from '.'; export async function fetcher(path: string): Promise { // Converts path to absolute diff --git a/src/swagger-graph.ts b/src/swagger-graph.ts deleted file mode 100644 index 249206e..0000000 --- a/src/swagger-graph.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { fetcher } from './fetcher'; -import { OpenAPIGraphsManager } from './graph'; -import { SchemaNode } from './graph/nodes/SchemaNode'; - -export async function getUnusedSchemas(path: string): Promise { - const apis = await fetcher(path); - const graph = new OpenAPIGraphsManager(apis); - return graph.checkForUnusedSchemas(); -} diff --git a/tests/graph/GraphsManager.test.ts b/tests/graph/GraphsManager.test.ts index d000cf3..4b695ef 100644 --- a/tests/graph/GraphsManager.test.ts +++ b/tests/graph/GraphsManager.test.ts @@ -1,16 +1,9 @@ -import { fetcher } from '../../src/fetcher'; -import { OpenAPIGraphsManager } from '../../src/graph'; +import { fetcher } from '../../src/openapi/fetcher'; +import { OpenAPIGraphs } from '../../src/graph'; +// TODO test('Creates a graph from the petstore specification', async () => { - const petstoreApis = await fetcher("tests/resources/petstore"); - const petstoreGraphs = new OpenAPIGraphsManager(petstoreApis); - const unusedSchemas = petstoreGraphs.checkForUnusedSchemas(); - expect(unusedSchemas).toHaveLength(1); - expect(unusedSchemas[0]).toMatchSnapshot({ - content: expect.any(Object), - name: 'SchemaNotBeingUsed' - }) }); // TODO diff --git a/tests/graph/__snapshots__/GraphsManager.test.ts.snap b/tests/graph/__snapshots__/GraphsManager.test.ts.snap deleted file mode 100644 index d18acea..0000000 --- a/tests/graph/__snapshots__/GraphsManager.test.ts.snap +++ /dev/null @@ -1,8 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Creates a graph from the petstore specification 1`] = ` -Object { - "content": Any, - "name": "SchemaNotBeingUsed", -} -`; diff --git a/tests/graph/builder/GraphsBuilder.test.ts b/tests/graph/builder/GraphsBuilder.test.ts index 2bcc28a..204b274 100644 --- a/tests/graph/builder/GraphsBuilder.test.ts +++ b/tests/graph/builder/GraphsBuilder.test.ts @@ -1,5 +1,5 @@ import { RefType } from '../../../model'; -import { fetcher } from '../../../src/fetcher'; +import { fetcher } from '../../../src/openapi/fetcher'; import { OpenAPIGraph, OpenAPIGraphsBuilder } from '../../../src/graph'; import { RefEdge } from '../../../src/graph/edges'; import { SchemaNode } from '../../../src/graph/nodes/SchemaNode'; diff --git a/tests/graph/builder/__snapshots__/GraphsBuilder.test.ts.snap b/tests/graph/builder/__snapshots__/GraphsBuilder.test.ts.snap index b03a43c..dd2ef0a 100644 --- a/tests/graph/builder/__snapshots__/GraphsBuilder.test.ts.snap +++ b/tests/graph/builder/__snapshots__/GraphsBuilder.test.ts.snap @@ -13,8 +13,8 @@ Object { "edges": Object { "ref": Object { "schemaRef": Object { - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/Email": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/Email": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "format": "email", @@ -26,8 +26,8 @@ Object { "tokenName": "Email", "type": "local", }, - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/Name": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/Name": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "type": "string", @@ -38,8 +38,8 @@ Object { "tokenName": "Name", "type": "local", }, - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/User": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/User": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "properties": Object { @@ -66,8 +66,8 @@ Object { "tokenName": "User", "type": "local", }, - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/Username": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/Username": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "type": "string", @@ -157,8 +157,8 @@ Object { "edges": Object { "ref": Object { "schemaRef": Object { - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/User": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/User": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "properties": Object { @@ -185,8 +185,8 @@ Object { "tokenName": "User", "type": "remote", }, - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/Username": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/Username": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "type": "string", @@ -212,8 +212,8 @@ Object { "edges": Object { "ref": Object { "schemaRef": Object { - "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml#/components/schemas/Post": RefEdge { - "absolutePath": "/home/max/openapi-graph/tests/resources/social-network/social-network.yaml", + "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml#/components/schemas/Post": RefEdge { + "absolutePath": "/home/max/openapi-graph-core/tests/resources/social-network/social-network.yaml", "child": SchemaNode { "content": Object { "properties": Object { diff --git a/tests/__snapshots__/fetcher.test.ts.snap b/tests/openapi/__snapshots__/fetcher.test.ts.snap similarity index 100% rename from tests/__snapshots__/fetcher.test.ts.snap rename to tests/openapi/__snapshots__/fetcher.test.ts.snap diff --git a/tests/fetcher.test.ts b/tests/openapi/fetcher.test.ts similarity index 97% rename from tests/fetcher.test.ts rename to tests/openapi/fetcher.test.ts index 5e4a4e4..5f414c1 100644 --- a/tests/fetcher.test.ts +++ b/tests/openapi/fetcher.test.ts @@ -1,6 +1,6 @@ import { resolve } from 'path'; -import { fetcher, testables } from '../src/fetcher'; +import { fetcher, testables } from '../../src/openapi/fetcher'; const { loadsSwaggerFiles, getFiles } = testables; const resourcesDir = `${resolve('.')}/tests/resources`;