Spaces:
Sleeping
Sleeping
| /** | |
| * Script to update all service API files to use the centralized authentication approach | |
| * with the apiClient instance | |
| * | |
| * To run: node update-api-auth.cjs | |
| */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Path to services directory | |
| const servicesDir = path.join(__dirname, 'src', 'services'); | |
| // Read all files in the services directory | |
| const files = fs.readdirSync(servicesDir); | |
| // Filter for TypeScript files | |
| const tsFiles = files.filter(file => file.endsWith('.ts') && !file.endsWith('.d.ts') && !file.includes('.bak')); | |
| console.log(`Found ${tsFiles.length} service files to update:`); | |
| console.log(tsFiles); | |
| // Process each file | |
| tsFiles.forEach(file => { | |
| const filePath = path.join(servicesDir, file); | |
| console.log(`\nProcessing ${file}...`); | |
| try { | |
| // Read file content | |
| let content = fs.readFileSync(filePath, 'utf8'); | |
| // Check if already using apiClient | |
| if (content.includes('import apiClient from') || content.includes('apiClient.get(')) { | |
| console.log(`✓ Already using apiClient in ${file}`); | |
| return; | |
| } | |
| // Add import for apiClient | |
| if (content.includes('import') && !content.includes('import apiClient')) { | |
| content = content.replace( | |
| /import (.*?) from ['"]@\/config['"];/, | |
| "import $1 from '@/config';\nimport apiClient from '@/lib/api/axios-instance';" | |
| ); | |
| // If no @/config import but there are other imports | |
| if (!content.includes('import apiClient')) { | |
| content = content.replace( | |
| /import (.*?);(\r?\n|\n)/, | |
| "import $1;$2import apiClient from '@/lib/api/axios-instance';$2" | |
| ); | |
| } | |
| } | |
| // Handle case where createHeaders was added but apiClient wasn't | |
| if (content.includes('import { createHeaders }') && !content.includes('import apiClient')) { | |
| content = content.replace( | |
| /import { createHeaders } from ['"]@\/lib\/api['"];/, | |
| "import { createHeaders } from '@/lib/api';\nimport apiClient from '@/lib/api/axios-instance';" | |
| ); | |
| } | |
| // If no imports at all, add both | |
| if (!content.includes('import')) { | |
| content = `import apiClient from '@/lib/api/axios-instance';\n\n${content}`; | |
| } | |
| // Replace direct axios calls with apiClient | |
| // Find and fix API URLs | |
| // This regex looks for a pattern like ${SOME_URL}/api/something and replaces with /api/something | |
| content = content.replace( | |
| /\$\{[^}]+\}\/api\/([^`]+)/g, | |
| '/api/$1' | |
| ); | |
| // GET requests with headers | |
| content = content.replace( | |
| /await axios\.get\(`(.*?)`, {[\s\S]*?}\)/g, | |
| "await apiClient.get(`$1`)" | |
| ); | |
| // GET requests without headers | |
| content = content.replace( | |
| /await axios\.get\(`(.*?)`\)/g, | |
| "await apiClient.get(`$1`)" | |
| ); | |
| // POST requests with headers | |
| content = content.replace( | |
| /await axios\.post\(`(.*?)`, (.*?), {[\s\S]*?}\)/g, | |
| "await apiClient.post(`$1`, $2)" | |
| ); | |
| // POST requests without headers | |
| content = content.replace( | |
| /await axios\.post\(`(.*?)`, (.*?)\)/g, | |
| "await apiClient.post(`$1`, $2)" | |
| ); | |
| // PUT requests with headers | |
| content = content.replace( | |
| /await axios\.put\(`(.*?)`, (.*?), {[\s\S]*?}\)/g, | |
| "await apiClient.put(`$1`, $2)" | |
| ); | |
| // PUT requests without headers | |
| content = content.replace( | |
| /await axios\.put\(`(.*?)`, (.*?)\)/g, | |
| "await apiClient.put(`$1`, $2)" | |
| ); | |
| // DELETE requests with headers | |
| content = content.replace( | |
| /await axios\.delete\(`(.*?)`, {[\s\S]*?}\)/g, | |
| "await apiClient.delete(`$1`)" | |
| ); | |
| // DELETE requests without headers | |
| content = content.replace( | |
| /await axios\.delete\(`(.*?)`\)/g, | |
| "await apiClient.delete(`$1`)" | |
| ); | |
| // Save updated content | |
| fs.writeFileSync(filePath, content, 'utf8'); | |
| console.log(`✓ Updated ${file} to use apiClient`); | |
| } catch (error) { | |
| console.error(`Error updating ${file}:`, error.message); | |
| } | |
| }); | |
| console.log('\nUpdate complete!'); |