/** * @file RateLimitsService.ts * @description Service class for handling rate limit operations * @author Matthew Raymer * @version 1.0.0 */ import { logger } from "../utils/logger"; import { getHeaders } from "../libs/endorserServer"; import type { EndorserRateLimits, ImageRateLimits } from "../interfaces/limits"; export class RateLimitsService { /** * Fetches rate limits for a given DID * @param apiServer - The API server URL * @param activeDid - The user's active DID * @returns Promise */ static async fetchRateLimits( apiServer: string, activeDid: string, ): Promise { try { const headers = await getHeaders(activeDid); const response = await fetch( `${apiServer}/api/endorser/rateLimits/${activeDid}`, { headers }, ); if (!response.ok) { throw new Error(`Failed to fetch rate limits: ${response.statusText}`); } return await response.json(); } catch (error) { logger.error("Error fetching rate limits:", error); throw error; } } /** * Fetches image rate limits for a given DID * @param apiServer - The API server URL * @param activeDid - The user's active DID * @returns Promise */ static async fetchImageRateLimits( apiServer: string, activeDid: string, ): Promise { try { const headers = await getHeaders(activeDid); const response = await fetch( `${apiServer}/api/endorser/imageRateLimits/${activeDid}`, { headers }, ); if (!response.ok) { throw new Error( `Failed to fetch image rate limits: ${response.statusText}`, ); } return await response.json(); } catch (error) { logger.error("Error fetching image rate limits:", error); throw error; } } /** * Formats rate limit error messages * @param error - The error object * @returns string */ static formatRateLimitError(error: unknown): string { if (error instanceof Error) { return error.message; } if (typeof error === "object" && error !== null) { const err = error as { response?: { data?: { error?: { message?: string } } }; }; return err.response?.data?.error?.message || "An unknown error occurred"; } return "An unknown error occurred"; } }