axios.ts
1.24 KB
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
import axios, { AxiosRequestConfig } from 'axios';
import { getDebugConfig } from './config';
import { writeFile } from './file';
import { workspaceManager } from './workspace';
/**
* Save debug log
*/
function saveDebugLog(
requestInfo: { url: string; config: AxiosRequestConfig },
responseInfo: { status: number; statusText: string; data: unknown }
): void {
const debugConfig = getDebugConfig();
if (!debugConfig.enabled) {
return;
}
const debugContent = [
'------------request------------',
JSON.stringify(requestInfo, null, 2),
'------------response------------',
JSON.stringify(responseInfo, null, 2),
].join('\n');
writeFile(workspaceManager.path?.debug ?? '', `fetch_${new Date().toISOString().replace(/:/g, '-')}.md`, debugContent);
}
/**
* Axios get request with debug logging
*/
export async function get<T>(url: string, config?: AxiosRequestConfig<T>): Promise<T> {
const response = await axios.get<T>(url, config);
saveDebugLog(
{
url,
config: config ?? {},
},
{
status: response.status,
statusText: response.statusText,
data: response.data,
}
);
return response.data;
}