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.js | |
| */ | |
| 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 import from @/config exists | |
| if (content.includes('import') && content.includes('@/config')) { | |
| 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 | |
| else if (content.includes('import')) { | |
| content = content.replace( | |
| /import (.*?);(\r?\n)/, | |
| "import $1;$2import apiClient from '@/lib/api/axios-instance';$2" | |
| ); | |
| } | |
| // Handle case where createHeaders was added but apiClient wasn't | |
| else 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 | |
| else { | |
| content = `import apiClient from '@/lib/api/axios-instance';\n\n${content}`; | |
| } | |
| // Check if already using createHeaders | |
| if (content.includes('import { createHeaders }') || content.includes("import {createHeaders}")) { | |
| console.log(`✓ Already using createHeaders in ${file}`); | |
| return; | |
| } | |
| // Add import for createHeaders if import from @/config exists | |
| if (content.includes('import') && content.includes('@/config')) { | |
| content = content.replace( | |
| /import (.*?) from ['"]@\/config['"];/, | |
| "import $1 from '@/config';\nimport { createHeaders } from '@/lib/api';" | |
| ); | |
| } | |
| // If no @/config import but there are other imports | |
| else if (content.includes('import')) { | |
| content = content.replace( | |
| /import (.*?);(\r?\n)/, | |
| "import $1;$2import { createHeaders } from '@/lib/api';$2" | |
| ); | |
| } | |
| // If no imports at all | |
| else { | |
| content = `import { createHeaders } from '@/lib/api';\n\n${content}`; | |
| } | |
| // Replace direct axios calls with authenticated calls | |
| // GET requests | |
| content = content.replace( | |
| /await axios\.get\(`(.*?)`\)/g, | |
| "await apiClient.get(`$1`, {\n headers: createHeaders(true),\n withCredentials: true\n })" | |
| ); | |
| // POST requests | |
| content = content.replace( | |
| /await axios\.post\(`(.*?)`, (.*?)\)/g, | |
| "await apiClient.post(`$1`, $2, {\n headers: createHeaders(true),\n withCredentials: true\n })" | |
| ); | |
| // PUT requests | |
| content = content.replace( | |
| /await axios\.put\(`(.*?)`, (.*?)\)/g, | |
| "await apiClient.put(`$1`, $2, {\n headers: createHeaders(true),\n withCredentials: true\n })" | |
| ); | |
| // DELETE requests | |
| content = content.replace( | |
| /await axios\.delete\(`(.*?)`\)/g, | |
| "await apiClient.delete(`$1`, {\n headers: createHeaders(true),\n withCredentials: true\n })" | |
| ); | |
| // Replace custom headers with createHeaders | |
| content = content.replace( | |
| /headers: {[\s\S]*?},/g, | |
| "headers: createHeaders(true)," | |
| ); | |
| // Save updated content | |
| fs.writeFileSync(filePath, content, 'utf8'); | |
| console.log(`✓ Updated ${file}`); | |
| } catch (error) { | |
| console.error(`Error updating ${file}:`, error.message); | |
| } | |
| }); | |
| console.log('\nUpdate complete!'); | |