add bsr, map and yt buttons to scores
Some checks failed
Deploy SSR / deploy (push) Has been cancelled
Some checks failed
Deploy SSR / deploy (push) Has been cancelled
This commit is contained in:
54
src/common/data-fetcher/data-fetcher.ts
Normal file
54
src/common/data-fetcher/data-fetcher.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import ky from "ky";
|
||||
|
||||
export default class DataFetcher {
|
||||
/**
|
||||
* 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.log(`[${this.name}]: ${data}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches data from the given url.
|
||||
*
|
||||
* @param useProxy whether to use proxy or not
|
||||
* @param url the url to fetch
|
||||
* @returns the fetched data
|
||||
*/
|
||||
public async fetch<T>(useProxy: boolean, url: string): Promise<T> {
|
||||
try {
|
||||
return await ky
|
||||
.get<T>(this.buildRequestUrl(useProxy, url), {
|
||||
next: {
|
||||
revalidate: 60, // 1 minute
|
||||
},
|
||||
})
|
||||
.json();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user