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 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
To use this script, replace 'your-openapi.yaml' with the path to your actual OpenAPI YAML file. The script uses the fs module to read the file, js-yaml to parse the YAML data, and then iterates over the paths section to extract the endpoints in the format HTTP_METHOD PATH. Finally, it prints each endpoint.
Make sure you have the js-yaml package installed before running the script. You can install it using npm install js-yaml.