node-api-document v0.0.7
node-api-document
node-api-document
is a utility function that integrates API documentation into an Express.js application. It provides a simple way to inject headers and API documentation into your app, ensuring consistency and reusability.
Installation
Install node-api-document
with npm
npm install node-api-document
Environment Variables
To use the middleware effectively, add the following variables to your .env file:
Required Variables
These variables are essential for the middleware to operate properly.
APP_NAME
The name of your project.APP_NAME=Your Project Name
LOGO_URL
The URL for your project logo. This will be displayed in relevant sections.LOGO_URL=Your Project Logo URL
BASE_URL
The base URL for your project's API. Ensure this points to the correct API endpoint.BASE_URL=Your Project API Base URL
API_PASSWORD
Password to access the API documentation. This adds an additional layer of security.API_PASSWORD=123
Notes
- Ensure all URLs are accessible and correctly formatted.
- Keep the
API_PASSWORD
secure to prevent unauthorized access. - Optional variables enhance the UI but are not critical for core functionality.
Usage/Examples
Import the middleware and use it in your Express application:
const express = require('express');
const createDoc = require('node-api-document');
const app = express();
const BASE_URL_FOR_CMS = "https://example.com/cms/";
const BASE_URL_WITHOUT_API_DOC = "https://example.com/api/";
const API_DOC_BASE_URL_PATH = "/api-doc";
const apiDoc = [
{},
{
"new_tag": "0",
"color": "blue",
"icon": "http",
"title": "DIRECT LINK",
"name": "FAQ",
"meth": "Link",
"link": `http://localhost:3000/faq`,
"imp": "FAQ"
},
{},
{
"new_tag": "1",
"color": "black",
"title": "COMMON API",
"icon": "list"
},
{
"new_tag": "0",
"color": "orange",
"icon": "flag",
"title": `1 : COMMON`,
"name": "Get Country List",
"meth": "GET",
"link": `http://localhost:3000/country_list`,
"mandatory": "",
"optional": "",
"is_header": "YES",
"is_push": "NO",
"header": "API-KEY",
"notes": "This API is used to get the country list",
"example": "",
"status": "<br>1. HTTP_OK [200] - status : success <br>2. HTTP_OK [201] - status : error <br>3. HTTP_NOT_FOUND [204]",
"imp": ""
},
{}
];
createDoc(app, 'token, api-key', apiDoc);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Function Description
createDoc(app, acceptHeaders, apiDoc)
This function creates middleware for API documentation and integrates it into your Express app.
Parameters
- app (Required): The Express.js application instance.
- acceptHeaders (Optional): A string representing custom headers to be added to requests.
- apiDoc (Optional): An array of objects defining the API documentation structure.
Returns
A promise that resolves when the documentation is successfully integrated.
Example
createDoc(app, 'application/json', apiDoc);
Access the API documentation
When you access the API documentation, pass the endpoint as follows:
http://localhost:3000/api-doc/
API Documentation Format
Each object in the apiDoc array should follow this structure:
- new_tag: A flag to denote new items ("0" or "1").
- color: Display color for the item.
- icon: Icon representing the item.
- title: Title of the documentation section.
- name: Name of the API or link.
- meth: HTTP method (e.g., GET, POST, Link).
- link: The API endpoint or link.
- imp: Additional important notes.
- Other optional fields for customization.
Error Handling
If an error occurs during the setup of the documentation, the promise will reject, and the error will be logged to the console:
error in CreateDoc =----->> <Error Details>
Contributing
Feel free to submit issues or pull requests for new features, bug fixes, or general improvements.