Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/utils/httpUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import axios, { AxiosRequestConfig, AxiosPromise } from "axios";
import { omit } from "lodash";
import { globalState } from "../globalState";
import { getUrl } from "../shared";
import { DialogType, promptForOpenOutputChannel } from "./uiUtils";

const referer = "vscode-lc-extension";
function extractCsrfToken(cookie: string): string {
const match = cookie.match(/csrftoken=([^;]+)/);
return match ? match[1] : "";
}

export function LcAxios<T = any>(path: string, settings?: AxiosRequestConfig): AxiosPromise<T> {
const cookie = globalState.getCookie();
Expand All @@ -14,15 +18,21 @@ export function LcAxios<T = any>(path: string, settings?: AxiosRequestConfig): A
);
return Promise.reject("Failed to obtain the cookie.");
}

const baseUrl = getUrl("base");
const csrfToken = extractCsrfToken(cookie);

return axios(path, {
headers: {
referer,
"Origin": baseUrl,
"Referer": baseUrl,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"content-type": "application/json",
cookie,
"cookie": cookie,
"X-CSRFToken": csrfToken,
"X-Requested-With": "XMLHttpRequest",
...(settings && settings.headers),
},
xsrfCookieName: "csrftoken",
xsrfHeaderName: "X-CSRFToken",
...(settings && omit(settings, "headers")),
});
}