File size: 4,555 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
/**

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