Skip to content

Instantly share code, notes, and snippets.

@ikenna
Created June 5, 2023 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikenna/d7834f652f14b93e9e3df7698a486b52 to your computer and use it in GitHub Desktop.
Save ikenna/d7834f652f14b93e9e3df7698a486b52 to your computer and use it in GitHub Desktop.
Extract endpoints from OpenAPI YAML file
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);
}
@ikenna
Copy link
Author

ikenna commented Jun 5, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment