File size: 4,212 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**

 * 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!');