api-crafter
v1.0.10
Published
An Advanced Api Crafting Package On Beta Stage
Downloads
18
Readme
API Crafter

API Crafter is a versatile Node.js package designed by adithyadev_blk for crafting robust and scalable APIs effortlessly. With Express.js integration, customizable middleware, API key management, error handling, analytics, and extensibility, API Crafter provides a comprehensive solution for building APIs.
Features
Express.js Integration
API Crafter seamlessly integrates with Express.js, facilitating the handling of HTTP requests and responses.
Middleware Configuration
- Rate Limiting: Limit the number of requests from a client within a specified time frame.
- Logging: Log incoming requests and responses for better debugging and monitoring.
- CORS Support: Handle Cross-Origin Resource Sharing to allow or restrict access to the API from different origins.
- Maintenance Mode: Toggle maintenance mode to return a service unavailable message to clients when enabled.
API Key Management
- Generation: Generate API keys dynamically.
- Verification: Verify API keys for authentication.
- Deletion: Delete existing API keys as needed.
Error Handling
- Customizable Error Messages: Customize error messages for unauthorized access and other errors.
Analytics
- Request Logging: Log details of incoming requests, including timestamp, HTTP method, URL, and client IP address.
- Analytics Endpoint: Access request analytics data through a dedicated endpoint (/analytics).
Customizable Options
- Project Name: Customize the project name displayed in console logs.
- Port: Define the port number on which the server listens.
- Rate Limiting Configuration: Customize window duration, maximum requests per window, and rate limit exceeded message.
- Maintenance Mode Configuration: Customize the maintenance mode message.
- CORS Configuration: Customize allowed origins and HTTP methods for CORS.
- Error Message Configuration: Customize error messages for unauthorized access and other errors.
Dynamic Functionality
- Maintenance Mode Toggle: Dynamically toggle maintenance mode on/off for testing purposes.
- Simple API Definition: Easily define API routes and their corresponding handlers using simple methods (get, post, delete, etc.).
Server Start
- Start Command: Start the Express server and listen on the specified port, displaying console logs indicating server status.
Package Extensibility
API Crafter is designed to be easily extensible, allowing users to add additional features and customize functionality as needed.
Usage Examples
// Example usage of the extended API Crafter package
const API = require('./api-cre');
// Define options for API initialization
const options = {
projectName: 'My API',
port: 3000,
rateLimit: {
windowMs: 60 * 1000, // 1 minute
max: 10, // 10 requests per minute
message: 'Custom rate limit exceeded message: Too many requests',
},
logging: true,
cors: {
origin: '*', // Allow requests from any origin
methods: 'GET,POST',
},
error: 'Custom unauthorized access message: You are not authorized to access this resource',
maintenanceMessage: 'Custom maintenance message: Service is currently under maintenance. Please try again later.',
};
// Initialize API with options
const myAPI = new API(options);
// Generate API key
const apiKey = myAPI.generateAPIKey();
console.log('Generated API Key:', apiKey);
// Verify API key (Example with a valid API key)
console.log('Verification with a valid API Key:', myAPI.verifyAPIKey(apiKey));
// Verify API key (Example with an invalid API key)
const invalidKey = 'invalid-api-key';
console.log('Verification with an invalid API Key:', myAPI.verifyAPIKey(invalidKey));
// Delete API key
console.log('API Key before deletion:', myAPI.verifyAPIKey(apiKey));
myAPI.deleteAPIKey(apiKey);
console.log('API Key after deletion:', myAPI.verifyAPIKey(apiKey));
// Define routes
myAPI.get('/', (req, res) => {
res.send('Hello World!');
});
// Route requiring authentication
myAPI.get('/protected', myAPI.auth, (req, res) => {
res.send('This is a protected route.');
});
// Maintenance mode route
myAPI.get('/maintenance', (req, res) => {
if (myAPI.maintenanceMode) {
res.send('Maintenance mode is currently enabled.');
} else {
res.send('Maintenance mode is currently disabled.');
}
});
// Toggle maintenance mode route (for testing purposes)
myAPI.post('/maintenance/toggle', (req, res) => {
myAPI.maintenanceMode = !myAPI.maintenanceMode;
res.send(`Maintenance mode toggled ${myAPI.maintenanceMode ? 'ON' : 'OFF'}.`);
});
// Error handling middleware
myAPI.app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({ error: err.message || 'Internal Server Error' });
});
// Start the server
myAPI.start();Author and License
API Crafter is created by adithyadev_blk and is available for everyone to use.
