refactor: Add type annotations for response objects in API request handling
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 7s
Vercel Production Deployment / Deploy-Production (push) Successful in 1m32s

This commit is contained in:
2025-10-16 13:21:01 +02:00
parent bbf3725acf
commit 968a843120
5 changed files with 22 additions and 10 deletions

View File

@@ -50,7 +50,7 @@ export async function POST(request: Request) {
for (const part of parts[0] as FilePart[]) {
if (!part.discord_message_id) continue;
try {
const res = await rateLimiter.request(
const res: Response = await rateLimiter.request(
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages/${part.discord_message_id}`,
{
method: 'DELETE',
@@ -89,7 +89,7 @@ export async function POST(request: Request) {
for (const part of partsRows[0] as FilePart[]) {
if (!part.discord_message_id) continue;
try {
const res = await rateLimiter.request(
const res: Response = await rateLimiter.request(
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages/${part.discord_message_id}`,
{
method: 'DELETE',

View File

@@ -54,7 +54,7 @@ export async function POST(request: Request) {
if (!part.discord_message_id) continue;
try {
const res = await rateLimiter.request(
const res: Response = await rateLimiter.request(
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages/${part.discord_message_id}`,
{
method: 'DELETE',

View File

@@ -16,6 +16,16 @@ interface RouteContext {
}>;
}
interface DiscordAttachment {
url: string;
filename: string;
size: number;
}
interface DiscordMessageData {
attachments: DiscordAttachment[];
}
if (!process.env.DISCORD_BOT_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
throw new Error('Discord bot token or channel ID is not configured');
}
@@ -48,7 +58,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
}
// 1. Get fresh message data from Discord
const messageData = await rateLimiter.request(
const messageData: DiscordMessageData = await rateLimiter.request<DiscordMessageData>(
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages/${part.discord_message_id}`
);

View File

@@ -8,8 +8,6 @@ if (!process.env.DISCORD_BOT_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
throw new Error('Please define DISCORD_BOT_TOKEN and DISCORD_CHANNEL_ID in .env.local');
}
const DISCORD_API_URL = `https://discord.com/api/v10/channels/${process.env.DISCORD_CHANNEL_ID}/messages`;
const rateLimiter = new DiscordRateLimiter(DISCORD_API_BASE_URL, process.env.DISCORD_BOT_TOKEN);
export async function POST(request: Request) {
@@ -27,7 +25,7 @@ export async function POST(request: Request) {
const discordFormData = new FormData();
discordFormData.append('file', chunk, `chunk-${partIndex}.bin`);
const discordRes = await rateLimiter.request(
const discordRes: Response = await rateLimiter.request(
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages`,
{
method: 'POST',

View File

@@ -19,12 +19,16 @@ export class DiscordRateLimiter {
public async request<T>(path: string, options: RequestInit = {}): Promise<T> {
const url = `${this.baseUrl}${path}`;
const headers = {
const headers: Record<string, string> = {
'Authorization': `Bot ${this.botToken}`,
'Content-Type': 'application/json',
...options.headers,
...(options.headers as Record<string, string>),
};
// Only set Content-Type to application/json if body is a string and not explicitly set
if (typeof options.body === 'string' && !('Content-Type' in headers)) {
headers['Content-Type'] = 'application/json';
}
const response = await fetch(url, { ...options, headers });
if (response.status === 429) {