-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to Request URL to OpenAPI Docs for a Request Header (#3)
* 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
1 parent
0d7287f
commit 2409d4d
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
}) | ||
}) |