-
-
Notifications
You must be signed in to change notification settings - Fork 745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add proxy for CosmosAsync calls #2846
Conversation
If it's spotify api - add necessary headers It it's external uri - redirect the request via cors proxy
It doesn't work, but I don't want the proxy to be spammed with these
Would you consider something more like: const corsProxyURL = "https://cors-proxy.ririxi.workers.dev/";
const allowedMethodsMap = {
get: "get",
post: "post",
del: "delete",
put: "put",
patch: "patch"
};
const allowedMethodsSet = new Set(allowedMethodsMap.keys());
const internalEndpoints = new Set(["sp", "wg"]);
const handler = {
// biome-ignore lint/complexity/useArrowFunction: <explanation>
get: function (target, prop, receiver) {
const internalFetch = Reflect.get(target, prop, receiver);
if (typeof internalFetch !== "function" || !allowedMethodsSet.has(prop) || Spicetify.Platform.version >= "1.2.31") {
return internalFetch;
}
return function (url, paramsOrBody) {
const urlObj = new URL(url);
const isWebAPI = urlObj.hostname === "api.spotify.com";
const isSpClientAPI = urlObj.hostname.includes(".spotify.com") && urlObj.hostname.includes("spclient");
const isInternalURL = internalEndpoints.has(urlObj.protocol);
const shouldUseCORSProxy = !isWebAPI && !isSpClientAPI && !isInternalURL;
const method = allowedMethodsMap.get(prop.toLowerCase());
const headers = {
"Content-Type": "application/json"
};
const options = {
method,
headers,
timeout: 1000 * 15
};
let finalURL = urlObj.toString();
if (paramsOrBody) {
if (method === "get") {
const params = new URLSearchParams(paramsOrBody)
finalURL += `?${params.toString()}`;
} else {
options.body = JSON.stringify(paramsOrBody);
}
}
if (shouldUseCORSProxy) {
finalURL = `${corsProxyURL}${finalURL}`;
}
const Authorization = `Bearer ${Spicetify.Platform.Session.accessToken}`;
let injectedHeaders = {};
if (isWebAPI) {
injectedHeaders = { Authorization };
};
if (isSpClientAPI) {
injectedHeaders = {
Authorization,
"Spotify-App-Version": Spicetify.Platform.version,
"App-Platform": Spicetify.Platform.PlatformData.app_platform
};
}
Object.assign(options.headers, injectedHeaders);
try {
if (isInternalURL) {
return internalFetch.apply(this, args);
}
return fetch(finalURL, options).then(res => res.json());
} catch (e) {
console.error(e);
}
};
}
}; Which is pretty much the same with minor variable name & control flow changes. |
Co-authored-by: Delusoire <deluso7re@outlook.com>
it's added automatically via biome, I cannot remove the |
Best of luck @rxri, let's hope this all goes well! 🙏 |
|
What about localhost? It should never be proxied, but it is. The cors proxy obviously denies that request. |
@kuba2k2 please use fetch in this instance. CosmosAsync shouldn't be used unless you need to bypass cors or want to easily get data from spotify api without managing headers manually |
If it's spotify api - add necessary headers
It it's external uri - redirect the request via cors proxy
fixes #2844