2024-09-11 15:38:04 +01:00
|
|
|
import ky from "ky";
|
2024-10-19 03:50:29 +01:00
|
|
|
import { isServer } from "../utils/utils";
|
2024-09-11 15:38:04 +01:00
|
|
|
|
2024-09-13 20:04:04 +01:00
|
|
|
export default class Service {
|
2024-09-11 15:38:04 +01:00
|
|
|
/**
|
2024-09-13 20:04:04 +01:00
|
|
|
* The name of the service.
|
2024-09-11 15:38:04 +01:00
|
|
|
*/
|
2024-10-08 15:32:02 +01:00
|
|
|
private readonly name: string;
|
2024-09-11 15:38:04 +01:00
|
|
|
|
|
|
|
constructor(name: string) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a message to the console.
|
|
|
|
*
|
|
|
|
* @param data the data to log
|
|
|
|
*/
|
|
|
|
public log(data: unknown) {
|
2024-10-08 15:32:02 +01:00
|
|
|
console.log(`[${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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches data from the given url.
|
|
|
|
*
|
|
|
|
* @param url the url to fetch
|
|
|
|
* @returns the fetched data
|
|
|
|
*/
|
2024-10-08 15:32:02 +01:00
|
|
|
public async fetch<T>(url: string): Promise<T | undefined> {
|
2024-09-11 20:10:27 +01:00
|
|
|
try {
|
2024-10-19 12:48:57 +01:00
|
|
|
const response = await ky.get<T>(this.buildRequestUrl(!isServer(), url));
|
|
|
|
if (response.headers.has("X-RateLimit-Remaining")) {
|
|
|
|
this.log(`Rate limit remaining: ${response.headers.get("X-RateLimit-Remaining")}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
2024-09-11 20:10:27 +01:00
|
|
|
} catch (error) {
|
2024-09-12 00:20:50 +01:00
|
|
|
return undefined;
|
2024-09-11 20:10:27 +01:00
|
|
|
}
|
2024-09-11 15:38:04 +01:00
|
|
|
}
|
|
|
|
}
|