This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
scoresaber-reloadedv3/src/common/data-fetcher/data-fetcher.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-09-11 15:38:04 +01:00
import ky from "ky";
2024-09-11 23:10:16 +01:00
export default class DataFetcher {
2024-09-11 15:38:04 +01:00
/**
* The name of the leaderboard.
*/
private name: string;
constructor(name: string) {
this.name = name;
}
/**
* Logs a message to the console.
*
* @param data the data to log
*/
public log(data: unknown) {
console.debug(`[${this.name}]: ${data}`);
2024-09-11 15:38:04 +01:00
}
/**
* Builds a request url.
*
* @param useProxy whether to use proxy or not
* @param url the url to fetch
* @returns the request url
*/
private buildRequestUrl(useProxy: boolean, url: string): string {
return (useProxy ? "https://proxy.fascinated.cc/" : "") + url;
// return (useProxy ? config.siteUrl + "/api/proxy?url=" : "") + url;
2024-09-11 15:38:04 +01:00
}
/**
* Fetches data from the given url.
*
* @param useProxy whether to use proxy or not
* @param url the url to fetch
* @returns the fetched data
*/
2024-09-13 13:45:04 +01:00
public async fetch<T>(
useProxy: boolean,
url: string,
): Promise<T | undefined> {
2024-09-11 20:10:27 +01:00
try {
return await ky
.get<T>(this.buildRequestUrl(useProxy, url), {
next: {
revalidate: 60, // 1 minute
},
})
.json();
} catch (error) {
2024-09-12 00:20:50 +01:00
console.error(`Error fetching data from ${url}:`, error);
return undefined;
2024-09-11 20:10:27 +01:00
}
2024-09-11 15:38:04 +01:00
}
}