UNPKG

daaah

Version:

_Currently UnderDevelopment_

117 lines (95 loc) 3 kB
// SDK for Dah const axios = require('axios'); const baseURL = "https://daaah.cyclic.app" /* --- POST, GET and Call Functions --- */ // GET All Functions async function getFunctions() { try { const list = await axios.get(`${baseURL}/functions/`); return list.data; } catch (e) { console.error('Error fetching functions:', error); return []; } } // POST a function async function postFunction(func, dependencies) { try { const functionData = parseFunction(func); if(dependencies) { const undefinedDependency = dependencies.find(dependency => !dependency.name); if(undefinedDependency) { console.error('Dependency Name is not defined'); return false; } functionData.dependencies = dependencies; } else { functionData.dependencies = getDependencies(); } const response = await axios.post(`${baseURL}/function/`, functionData); return response.data.status ? true : false; } catch (e) { console.error('Error posting function:', error); return false; } } // Call a function async function callFunction(funcName, params) { try { const response = await axios.get(`${baseURL}/${funcName}/`, { data: {params} }); return response.data.return; } catch (e) { console.error('Error calling function:', error); return undefined; } } /* --- Support Functions --- */ function parseFunction(func) { // Extract function name const functionName = func.name; // Extract function arguments const argsRegex = /\(([^)]+)\)/; const argsString = argsRegex.exec(func.toString())[1]; const functionArgs = argsString.split(',').map(arg => arg.trim()); // Extract function body let functionBody = null; const arrowIndex = func.toString().indexOf('=>'); if (arrowIndex !== -1) { functionBody = func.toString().substring(arrowIndex + 2).trim(); } else { const bodyRegex = /{([^]+)}/; const bodyMatch = bodyRegex.exec(func.toString()); if (bodyMatch) { functionBody = bodyMatch[1].trim(); } } // Return the parsed function details as an object return { functionName: functionName, functionBody: functionBody, functionArgs: functionArgs }; } function getDependencies() { const fs = require('fs'); fs.readFileSync('package.json', 'utf8', (err, data) => { if (err) { console.error('Error reading package.json:', err); return; } try { const packageJson = JSON.parse(data); const dependencies = packageJson.dependencies; const dependencyArray = Object.entries(dependencies).map(([name, version]) => { return { name, version }; }); return dependencyArray; } catch (error) { console.error('Error parsing package.json:', error); return []; } }); } module.exports = {getFunctions, postFunction, callFunction};