Skip to content

Commit

Permalink
Add API to Request URL to OpenAPI Docs for a Request Header (#3)
Browse files Browse the repository at this point in the history
* Add Method to get URL to docs out of a Request Header

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Add OpenAPI JSON as a Parameter to the Method

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Add New Method in OpenAPI Extractor for Method Definitions

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Refactor Method into a Generic Class

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Add Tests for OpenAPI Docs API

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Refactor for readability

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Apply Patch from @kartik-gupta-ij

Co-authored-by: Kartik Gupta <kartikgupta7267@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

* Return terminal instead of the entire URL

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>

---------

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Co-authored-by: Kartik Gupta <kartikgupta7267@gmail.com>
  • Loading branch information
Snehil-Shah and kartik-gupta-ij authored Mar 10, 2024
1 parent 0d7287f commit 2409d4d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/openapi-extractor.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand All @@ -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
));
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/request-docs.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 9 additions & 1 deletion tests/openapi-extractor.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions tests/request-docs.tests.js
Original file line number Diff line number Diff line change
@@ -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'
)
})
})

0 comments on commit 2409d4d

Please sign in to comment.