agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
75 lines (67 loc) • 2.18 kB
JavaScript
/**
* @file Frontend API call extractor
* @description Extracts API calls from frontend code
*/
/**
* Extract frontend API calls from files
* @param {Array<string>} filePaths - Array of file paths to analyze
* @returns {Promise<Array>} API calls found
*/
async function extractFrontendCalls(filePaths) {
const { promises: fs } = require('fs');
const calls = [];
for (const filePath of filePaths) {
try {
const content = await fs.readFile(filePath, 'utf8');
const lines = content.split('\n');
lines.forEach((line, index) => {
// Look for fetch() calls (including template literals)
const fetchMatch = line.match(/fetch\s*\(\s*['"`]([^'"`]+)['"`]/);
if (fetchMatch) {
calls.push({
type: 'fetch',
route: fetchMatch[1],
url: fetchMatch[1],
method: extractMethodFromFetch(line),
line: index + 1,
file: filePath
});
}
// Look for axios calls (including template literals)
const axiosMatch = line.match(/axios\.(\w+)\s*\(\s*['"`]([^'"`]+)['"`]/);
if (axiosMatch) {
calls.push({
type: 'axios',
route: axiosMatch[2],
method: axiosMatch[1].toUpperCase(),
url: axiosMatch[2],
line: index + 1,
file: filePath
});
}
// Look for XMLHttpRequest calls
const xhrMatch = line.match(/\.open\s*\(\s*['"`](\w+)['"`]\s*,\s*['"`]([^'"`]+)['"`]/);
if (xhrMatch) {
calls.push({
type: 'xhr',
route: xhrMatch[2],
method: xhrMatch[1].toUpperCase(),
url: xhrMatch[2],
line: index + 1,
file: filePath
});
}
});
} catch (error) {
console.warn(`Warning: Could not read frontend file ${filePath}: ${error.message}`);
}
}
return calls;
}
function extractMethodFromFetch(line) {
const methodMatch = line.match(/method\s*:\s*['"`](\w+)['"`]/);
return methodMatch ? methodMatch[1].toUpperCase() : 'GET';
}
module.exports = {
extractFrontendCalls
};