diff --git a/src/openapi-extractor.js b/src/openapi-extractor.js index 9a060e6..fe5dca7 100644 --- a/src/openapi-extractor.js +++ b/src/openapi-extractor.js @@ -1,9 +1,11 @@ export class OpenAPIMethod { - constructor(method, path, body) { + constructor(method, path, body, operationId, tags) { this.method = method; this.path = path; - this.body = body; // defiition of the body + this.body = body; // definition of the body + this.operationId=operationId; + this.tags=tags; } } @@ -21,7 +23,9 @@ export class OpenAPIExtractor { methods.push(new OpenAPIMethod( method, path, - this.openapi.paths[path][method]?.requestBody?.content?.['application/json']?.schema?.['$ref'] + this.openapi.paths[path][method]?.requestBody?.content?.['application/json']?.schema?.['$ref'], + this.openapi.paths[path][method].operationId, + this.openapi.paths[path][method].tags )); } } diff --git a/src/request-docs.js b/src/request-docs.js new file mode 100644 index 0000000..e6952ea --- /dev/null +++ b/src/request-docs.js @@ -0,0 +1,30 @@ +import { tokenizeHeader } from "./parse-request-header.js"; +import { OpenAPIExtractor } from "./openapi-extractor.js"; +import { AutocompleteTrie } from "./trie-completion.js"; + +export class OpenapiDocs{ + constructor(openapi){ + this.openapi = openapi; + this.extractor = new OpenAPIExtractor(openapi); + + this.methods = this.extractor.getAllMethods(); + + this.trieCompletion = new AutocompleteTrie(); + + + for (let method of this.methods) { + let requestString = `${method.method.toUpperCase()} ${method.path}`; + let tokens = tokenizeHeader(requestString); + this.trieCompletion.addPath(tokens, {}, { + operationId: method.operationId, + tags: method.tags + }); + } + } + + getRequestDocs(requestString){ + let tokens = tokenizeHeader(requestString); + let terminal = this.trieCompletion.match(tokens); + return terminal; + } +} diff --git a/tests/openapi-extractor.tests.js b/tests/openapi-extractor.tests.js index 13b5f1f..f6d1b5a 100644 --- a/tests/openapi-extractor.tests.js +++ b/tests/openapi-extractor.tests.js @@ -17,13 +17,21 @@ describe("OpenAPI extractor", () => { } }); + it("should extract all method definitions", () => { + let allMethods = new OpenAPIExtractor(openapi).getAllMethods(); + for (const method of allMethods){ + if (method.path == '/telemetry'){ + assert.equal(method.tags[0], 'service') + } + } + }) + it("should extract model object by ref", () => { let objects = new OpenAPIExtractor(openapi).objectByRef("#/components/schemas/CreateCollection"); // console.log(object); }); - it("should extract model object by path", () => { let objects = []; let openapiExtractor = new OpenAPIExtractor(openapi); diff --git a/tests/request-docs.tests.js b/tests/request-docs.tests.js new file mode 100644 index 0000000..6064c15 --- /dev/null +++ b/tests/request-docs.tests.js @@ -0,0 +1,47 @@ +import assert from 'assert'; + +import openapi from "./data/openapi.json" assert { type: "json" }; + +import { OpenapiDocs } from "../src/request-docs.js"; + +describe("OpenAPI Docs", () => { + + const apiDocs = new OpenapiDocs(openapi) + + it("should return null for incorrect HTTP Method Name", () => { + assert.equal(apiDocs.getRequestDocs('gat'), null) + }) + + it("should return null for comments", () => { + assert.equal( + apiDocs.getRequestDocs('// this is a comment'), + null + ) + }) + + it("should return null for incomplete header", () => { + assert.equal(apiDocs.getRequestDocs('PATCH '), null) + }) + + it("should return url for both absolute & relative valid paths", () => { + assert.equal( + apiDocs.getRequestDocs('GET telemetry').operationId, + 'telemetry' + ); + assert.equal( + apiDocs.getRequestDocs('GET /telemetry').operationId, + 'telemetry' + ); + }) + + it("should return url for valid request with path variables", () => { + assert.equal( + apiDocs.getRequestDocs('DELETE collections/my_collection/index/my_field').operationId, + 'delete_field_index' + ) + assert.equal( + apiDocs.getRequestDocs('PATCH collections/test').operationId, + 'update_collection' + ) + }) +}) \ No newline at end of file