fnexpose
v1.0.0
Published
A powerful Node.js package to effortlessly transform your JavaScript functions into accessible CLI commands and HTTP API endpoints.
Downloads
3
Readme
FnExpose
FnExpose is a powerful and flexible Node.js package designed to effortlessly transform your existing JavaScript functions into accessible CLI commands and HTTP API endpoints. This tool is perfect for developers looking to quickly expose utility functions, business logic, or any JavaScript module as a service, enabling rapid prototyping, microservice development, and seamless integration into existing workflows.
Features
- Function to CLI: Automatically generates CLI commands for your JavaScript functions.
- Function to Endpoint: Exposes your functions as HTTP API endpoints, ready for consumption.
- File-based Scanning: Easily expose all functions from a single JavaScript file.
- Folder-based Scanning: Turn an entire folder of JavaScript files into a collection of CLI commands and API endpoints.
- Minimal Configuration: Get started quickly with sensible defaults.
- Extensible: Designed to be integrated into new or existing Node.js projects.
Quick Start
1. Installation
To add function-to-cli-endpoint to your project, run:
npm install fnexpose
# or
yarn add function-to-cli-endpoint2. Basic Usage (Function to Endpoint)
Let's say you have a file myFunctions.js:
// myFunctions.js
module.exports = {
greet: (name) => `Hello, ${name}!`,
add: (a, b) => a + b
};You can expose these functions as API endpoints with a few lines of code:
// server.js
const express = require('express');
const { fileToServer } = require('fnexpose');
const path = require('path');
const app = express();
const functionsFilePath = path.resolve(__dirname, './myFunctions.js');
// Create a router from your functions file
const apiRouter = fileToServer.makeRouter(fileToServer.loadFunctionsFromFile(functionsFilePath));
// Mount the router at a specific path
app.use('/api', apiRouter);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}/api`);
console.log(`Try: POST http://localhost:${PORT}/api/greet with body { "args": ["World"] }`);
console.log(`Try: POST http://localhost:${PORT}/api/add with body { "args": [5, 3] }`);
});Run your server:
node server.js3. Basic Usage (Function to CLI)
To use your functions as CLI commands, you can create a simple CLI entry point:
// cli.js
const { fileToCli } = require('fnexpose');
const path = require('path');
const functionsFilePath = path.resolve(__dirname, './myFunctions.js');
fileToCli.run(fileToCli.loadFunctionsFromFile(functionsFilePath));Run your CLI:
node cli.js greet --name World
node cli.js add --a 5 --b 3(Note: fileToCli is a conceptual example. The actual package structure might require importing from specific sub-modules like fnexpose/function-to-cli-and-endpoint/cli or fnexpose/file-to-function-to-cli-and-endpoint/cli depending on how you structure your CLI integration.)
Why use this tool?
- Rapid Prototyping: Quickly turn ideas into testable API endpoints or CLI tools.
- Microservices: Easily expose small, focused functions as independent services.
- Automation: Automate tasks by calling your JavaScript functions directly from the command line or via HTTP requests.
- Existing Codebase Integration: Integrate with existing utility functions without extensive refactoring.
About Leumas Tech
This tool is brought to you by Leumas Tech, a company dedicated to building innovative software solutions.
Contributing
We welcome contributions! Please see our CONTRIBUTING.md for details.
License
This project is licensed under the ISC License.
