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/projects/common/src/service/service.ts
Liam b3c124631a
Some checks failed
Deploy Backend / deploy (push) Successful in 2m26s
Deploy Website / deploy (push) Failing after 1m52s
move score page fetching to the backend
2024-10-17 15:30:14 +01:00

47 lines
967 B
TypeScript

import ky from "ky";
export default class Service {
/**
* The name of the service.
*/
private readonly 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 url the url to fetch
* @returns the fetched data
*/
public async fetch<T>(url: string): Promise<T | undefined> {
try {
return await ky.get<T>(this.buildRequestUrl(true, url)).json();
} catch (error) {
return undefined;
}
}
}