/** * @fileoverview Fix duplicate parameters and structure issues in endpoints.js * Removes duplicate parameter definitions and ensures proper structure */ import fs from "fs"; import path from "path"; /** * Fix duplicate parameters in endpoints.js */ function fixDuplicateParameters() { try { console.log("=== FIXING DUPLICATE PARAMETERS IN ENDPOINTS.JS ==="); console.log(""); const endpointsPath = path.join(process.cwd(), "src/config/endpoints.js"); const originalContent = fs.readFileSync(endpointsPath, "utf8"); console.log("๐Ÿ“ Reading endpoints.js..."); console.log(`๐Ÿ“Š Original file size: ${originalContent.length} characters`); // Create backup const backupPath = path.join( process.cwd(), `endpoints_backup_${Date.now()}.js` ); fs.writeFileSync(backupPath, originalContent); console.log(`๐Ÿ’พ Backup created: ${backupPath}`); // Fix the content const fixedContent = removeDuplicateParameters(originalContent); // Write the fixed content fs.writeFileSync(endpointsPath, fixedContent); console.log(`๐Ÿ“Š Fixed file size: ${fixedContent.length} characters`); console.log( `๐Ÿ“‰ Size reduction: ${ originalContent.length - fixedContent.length } characters` ); console.log(""); console.log("โœ… Duplicate parameters removed successfully!"); console.log("โœ… Endpoints.js structure corrected!"); return { originalSize: originalContent.length, fixedSize: fixedContent.length, reduction: originalContent.length - fixedContent.length, backupPath: backupPath, }; } catch (error) { console.error("โŒ Error fixing duplicate parameters:", error); throw error; } } /** * Remove duplicate parameters from endpoints content */ function removeDuplicateParameters(content) { console.log("๐Ÿ”ง Processing endpoint sections..."); const sections = [ "PUBLIC_ENDPOINTS", "PROVIDER_ENDPOINTS", "PATIENT_ENDPOINTS", "PARTNER_ENDPOINTS", "AFFILIATE_ENDPOINTS", "NETWORK_ENDPOINTS", ]; let fixedContent = content; let totalDuplicatesRemoved = 0; sections.forEach((sectionName) => { console.log(` Processing ${sectionName}...`); const sectionRegex = new RegExp( `(export const ${sectionName}\\s*=\\s*\\[)([\\s\\S]*?)(\\];)`, "g" ); fixedContent = fixedContent.replace( sectionRegex, (match, start, sectionContent, end) => { const { cleanedContent, duplicatesRemoved } = cleanEndpointSection(sectionContent); totalDuplicatesRemoved += duplicatesRemoved; if (duplicatesRemoved > 0) { console.log( ` โœ… Removed ${duplicatesRemoved} duplicate parameters` ); } return start + cleanedContent + end; } ); }); console.log( `๐ŸŽฏ Total duplicate parameters removed: ${totalDuplicatesRemoved}` ); // Additional cleanup fixedContent = performAdditionalCleanup(fixedContent); return fixedContent; } /** * Clean an individual endpoint section */ function cleanEndpointSection(sectionContent) { let duplicatesRemoved = 0; // Find all endpoint objects const endpointRegex = /\{[\s\S]*?\}/g; let cleanedContent = sectionContent; const endpoints = []; let match; while ((match = endpointRegex.exec(sectionContent)) !== null) { endpoints.push({ original: match[0], start: match.index, end: match.index + match[0].length, }); } // Process each endpoint endpoints.forEach((endpoint, index) => { const cleanedEndpoint = cleanSingleEndpoint(endpoint.original); if (cleanedEndpoint.duplicatesRemoved > 0) { duplicatesRemoved += cleanedEndpoint.duplicatesRemoved; } // Replace in the content cleanedContent = cleanedContent.replace( endpoint.original, cleanedEndpoint.content ); }); return { cleanedContent, duplicatesRemoved, }; } /** * Clean a single endpoint object */ function cleanSingleEndpoint(endpointStr) { let duplicatesRemoved = 0; let cleanedEndpoint = endpointStr; // Extract parameters section const paramMatch = endpointStr.match( /parameters:\s*\{([\s\S]*?)\}(?=\s*[,}])/ ); if (paramMatch) { const paramContent = paramMatch[1]; const cleanedParams = removeDuplicateParameterDefinitions(paramContent); if (cleanedParams.duplicatesRemoved > 0) { duplicatesRemoved = cleanedParams.duplicatesRemoved; // Replace the parameters section cleanedEndpoint = endpointStr.replace( /parameters:\s*\{[\s\S]*?\}(?=\s*[,}])/, `parameters: {${cleanedParams.content}}` ); } } return { content: cleanedEndpoint, duplicatesRemoved, }; } /** * Remove duplicate parameter definitions */ function removeDuplicateParameterDefinitions(paramContent) { const parameterNames = new Set(); const cleanParameters = []; let duplicatesRemoved = 0; // Find all parameter definitions const paramRegex = /(\w+):\s*\{([^}]+)\}/g; let match; while ((match = paramRegex.exec(paramContent)) !== null) { const paramName = match[1]; const paramDef = match[2]; if (!parameterNames.has(paramName)) { parameterNames.add(paramName); cleanParameters.push(`${paramName}: {${paramDef}}`); } else { duplicatesRemoved++; } } // Preserve any non-parameter content (comments, etc.) let cleanContent = paramContent; if (duplicatesRemoved > 0) { // Rebuild the parameters section cleanContent = "\n " + cleanParameters.join(",\n ") + "\n "; } return { content: cleanContent, duplicatesRemoved, }; } /** * Perform additional cleanup */ function performAdditionalCleanup(content) { console.log("๐Ÿงน Performing additional cleanup..."); // Remove excessive whitespace content = content.replace(/\n\s*\n\s*\n/g, "\n\n"); // Fix comma issues content = content.replace(/,\s*,/g, ","); // Fix bracket spacing content = content.replace(/\{\s*\n\s*\}/g, "{}"); // Ensure proper indentation for parameters content = content.replace( /parameters:\s*\{\s*\n\s*([^}]+)\s*\n\s*\}/g, (match, params) => { const lines = params .split("\n") .map((line) => line.trim()) .filter((line) => line); const indentedLines = lines.map((line) => " " + line); return `parameters: {\n${indentedLines.join("\n")}\n }`; } ); console.log("โœ… Additional cleanup completed"); return content; } /** * Validate the fixed file */ async function validateFixedFile() { try { console.log("๐Ÿ” Validating fixed endpoints.js..."); const endpointsPath = path.join(process.cwd(), "src/config/endpoints.js"); const content = fs.readFileSync(endpointsPath, "utf8"); // Basic syntax validation - check for common issues const syntaxIssues = []; // Check for unmatched brackets const openBrackets = (content.match(/\{/g) || []).length; const closeBrackets = (content.match(/\}/g) || []).length; if (openBrackets !== closeBrackets) { syntaxIssues.push( `Unmatched brackets: ${openBrackets} open, ${closeBrackets} close` ); } // Check for unmatched parentheses const openParens = (content.match(/\(/g) || []).length; const closeParens = (content.match(/\)/g) || []).length; if (openParens !== closeParens) { syntaxIssues.push( `Unmatched parentheses: ${openParens} open, ${closeParens} close` ); } // Check for trailing commas before closing brackets if (content.includes(",}") || content.includes(",]")) { syntaxIssues.push("Trailing commas found before closing brackets"); } if (syntaxIssues.length > 0) { console.error("โŒ Syntax issues found:"); syntaxIssues.forEach((issue) => console.error(` - ${issue}`)); return false; } console.log("โœ… Basic syntax validation passed"); return true; } catch (error) { console.error("โŒ Error validating file:", error); return false; } } // Run the fix if (import.meta.url === `file://${process.argv[1]}`) { (async () => { try { const result = fixDuplicateParameters(); console.log(""); console.log("=== SUMMARY ==="); console.log(`Original size: ${result.originalSize} characters`); console.log(`Fixed size: ${result.fixedSize} characters`); console.log(`Reduction: ${result.reduction} characters`); console.log(`Backup: ${result.backupPath}`); const isValid = await validateFixedFile(); if (isValid) { console.log("๐ŸŽ‰ Endpoints.js successfully fixed and validated!"); } else { console.log( "โš ๏ธ File fixed but validation failed. Check syntax manually." ); } } catch (error) { console.error("โŒ Failed to fix endpoints.js:", error); } })(); } export { fixDuplicateParameters };