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 e0aeec5d5a
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m11s
log rate limit left
2024-10-19 12:48:57 +01:00

52 lines
1.2 KiB
TypeScript

import ky from "ky";
import { isServer } from "../utils/utils";
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 {
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();
} catch (error) {
return undefined;
}
}
}