refactor: Add type annotations for response objects in API request handling
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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}`
|
||||
);
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user