You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
/**
|
|
* @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";
|
|
import axios from "axios";
|
|
|
|
export class RateLimitsService {
|
|
/**
|
|
* Fetches rate limits for a given DID
|
|
* @param apiServer - The API server URL
|
|
* @param did - The user's DID
|
|
* @returns Promise<EndorserRateLimits>
|
|
*/
|
|
static async fetchRateLimits(apiServer: string, did: string): Promise<EndorserRateLimits> {
|
|
logger.log('Fetching rate limits for DID:', did);
|
|
logger.log('Using API server:', apiServer);
|
|
|
|
try {
|
|
const headers = await getHeaders(did);
|
|
const response = await axios.get(`${apiServer}/api/v2/rate-limits/${did}`, { headers });
|
|
logger.log('Rate limits response:', response.data);
|
|
return response.data;
|
|
} catch (error) {
|
|
if (axios.isAxiosError(error) && (error.response?.status === 400 || error.response?.status === 404)) {
|
|
const errorData = error.response.data as { error?: { message?: string, code?: string } };
|
|
if (errorData.error?.code === 'UNREGISTERED_USER' || error.response?.status === 404) {
|
|
logger.log('User is not registered, returning default limits');
|
|
return {
|
|
doneClaimsThisWeek: "0",
|
|
maxClaimsPerWeek: "0",
|
|
nextWeekBeginDateTime: new Date().toISOString(),
|
|
doneRegistrationsThisMonth: "0",
|
|
maxRegistrationsPerMonth: "0",
|
|
nextMonthBeginDateTime: new Date().toISOString()
|
|
};
|
|
}
|
|
}
|
|
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<ImageRateLimits>
|
|
*/
|
|
static async fetchImageRateLimits(
|
|
apiServer: string,
|
|
activeDid: string,
|
|
): Promise<ImageRateLimits> {
|
|
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";
|
|
}
|
|
}
|
|
|