Created
June 5, 2023 13:02
-
-
Save ikenna/d7834f652f14b93e9e3df7698a486b52 to your computer and use it in GitHub Desktop.
Extract endpoints from OpenAPI YAML file
This file contains hidden or 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
| const fs = require('fs'); | |
| const yaml = require('js-yaml'); | |
| function listEndpoints(openapiFile) { | |
| try { | |
| const openapiData = yaml.safeLoad(fs.readFileSync(openapiFile, 'utf8')); | |
| const paths = openapiData.paths || {}; | |
| const endpoints = []; | |
| for (const path in paths) { | |
| const methods = paths[path]; | |
| for (const method in methods) { | |
| const endpoint = `${method.toUpperCase()} ${path}`; | |
| endpoints.push(endpoint); | |
| } | |
| } | |
| return endpoints; | |
| } catch (error) { | |
| console.error(`Error loading OpenAPI file: ${error}`); | |
| return []; | |
| } | |
| } | |
| // Usage example | |
| const openapiFile = 'your-openapi.yaml'; | |
| const endpoints = listEndpoints(openapiFile); | |
| for (const endpoint of endpoints) { | |
| console.log(endpoint); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This might be of interest for anyone landing here too.
yq+qsv+csview- Extract to CSV + markdown table outputUsing the same Docker OpenAPI document (direct YAML link) as before, here is a way to extract the desired data with
yqand do some extra processing withqsvto format it:This will output a table like this:
Markdown output (rendered)
Markdown output (raw)
With
qsvyou can rearrange the columns, omit columns, etc quite easily.Advanced processing with QSV
This example that transforms that same CSV output from
yqto a filtered subset rearranged and sorted to different requirements:That output is more aligned with the original JS example (extra processing aside).
Swapping
qsv table | tail -n +2forcsview --style markdownwe get the same data formatted for rendering:JS to
yqportThe actual minimal equivalent for the JS example output would be:
NOTE: Result outputs were filtered with
grep secretsfor brevity, unlike the prior advanced example which actually filtered results by specific column viaqsv search.