refactor: Simplify Discord API response handling and define response interface
This commit is contained in:
@@ -10,6 +10,15 @@ if (!process.env.DISCORD_BOT_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
|
|||||||
|
|
||||||
const rateLimiter = new DiscordRateLimiter(DISCORD_API_BASE_URL, process.env.DISCORD_BOT_TOKEN);
|
const rateLimiter = new DiscordRateLimiter(DISCORD_API_BASE_URL, process.env.DISCORD_BOT_TOKEN);
|
||||||
|
|
||||||
|
interface DiscordMessageResponse {
|
||||||
|
id: string;
|
||||||
|
attachments: Array<{
|
||||||
|
id: string;
|
||||||
|
url: string;
|
||||||
|
size: number;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
@@ -25,26 +34,13 @@ export async function POST(request: Request) {
|
|||||||
const discordFormData = new FormData();
|
const discordFormData = new FormData();
|
||||||
discordFormData.append('file', chunk, `chunk-${partIndex}.bin`);
|
discordFormData.append('file', chunk, `chunk-${partIndex}.bin`);
|
||||||
|
|
||||||
const discordRes: Response = await rateLimiter.request(
|
const discordData = await rateLimiter.request<DiscordMessageResponse>(
|
||||||
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages`,
|
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages`,
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: discordFormData,
|
body: discordFormData,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let discordData;
|
|
||||||
try {
|
|
||||||
discordData = await discordRes.json();
|
|
||||||
} catch (jsonError) {
|
|
||||||
console.error('Failed to parse Discord response as JSON:', jsonError);
|
|
||||||
return NextResponse.json({ error: 'Failed to parse Discord response' }, { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!discordRes.ok) {
|
|
||||||
console.error('Discord API Error:', discordData);
|
|
||||||
return NextResponse.json({ error: 'Failed to upload to Discord' }, { status: 500 });
|
|
||||||
}
|
|
||||||
const attachment = discordData.attachments[0];
|
const attachment = discordData.attachments[0];
|
||||||
|
|
||||||
if (!attachment) {
|
if (!attachment) {
|
||||||
|
|||||||
Reference in New Issue
Block a user