This Node.js npm package scans a directory for API endpoints implemented using Express framework, extracts endpoint details including headers, query parameters, and request body, and generates a Postman API collection file.
npm install nodejs-api-discovery-
Configuration
Modify the
config.jsonfile to specify the directory to scan, the server framework (expressfor now), the object instance where endpoints are defined (appfor Express), and other parameters.{ "directoryToScan": "./", "framework": "express", "objectInstance": "app", "postmanCollectionFile": "./postman_collection.json", "baseUrl": "http://localhost:3000" }import { generatePostmanCollection, discoverEndpoints, } from "nodejs-api-discovery"; import fs from 'fs'; // Load configuration const configPath = './config.json'; const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); async function runTool(writeCollection = false) { try { const endpoints = await discoverEndpoints( config.directoryToScan, config.framework, config.objectInstance ); await generatePostmanCollection(endpoints, config.baseUrl, writeCollection); console.log("Postman collection generated successfully!"); } catch (error) { console.error("Error generating Postman collection:", error); } } runTool(true); -
Generated Postman Collection
The generated Postman collection (postman_collection.json) contains API endpoints with methods, URLs prefixed by baseUrl, headers, query parameters, and request bodies (if available).
-
Support for More Frameworks: Extend the tool to support other Node.js frameworks like Hapi, Koa, etc.
-
Enhanced Body Parsing: Improve body parsing logic to handle more complex body types (e.g., JSON, form-data, multipart).
-
Query Parameters: Currently supports basic query parameter extraction. Enhance to handle nested parameters and arrays.
-
Error Handling and Edge Cases: Add robust error handling for parsing failures and handle edge cases in route definition parsing.
Contributions are welcome! Fork the repository, create a feature branch, and submit a pull request with your enhancements.
This project is licensed under the MIT License. See the LICENSE file for details.
- This markdown section provides a clear and structured guide on how to install, configure, and use your npm package for discovering API endpoints and generating a Postman collection.
- Adjust paths (
index.mjs,config.json, etc.) and specific configurations (baseUrl,framework, etc.) according to your actual project setup and requirements. - Ensure to include this section in your
README.mdfile in the root directory of your npm package to effectively communicate usage instructions to users and contributors.