39 lines
1.6 KiB
TypeScript
39 lines
1.6 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
|
import axios from "axios";
|
|
import { updateTwitchToken } from "@/data/twitch-reauthorize";
|
|
|
|
export async function GET(req: Request) {
|
|
try {
|
|
if (!process.env.TWITCH_BOT_CLIENT_ID)
|
|
return NextResponse.json({ message: 'Something went wrong.', error: null, value: null }, { status: 500 })
|
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
|
}
|
|
|
|
const auth = await updateTwitchToken(user.id)
|
|
if (!auth)
|
|
return NextResponse.json({ message: 'Failed to authorize to Twitch.', error: null, value: null }, { status: 403 });
|
|
|
|
try {
|
|
const redemptions = await axios.get("https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=" + auth.broadcaster_id,
|
|
{
|
|
headers: {
|
|
"Client-Id": process.env.TWITCH_BOT_CLIENT_ID,
|
|
"Authorization": "Bearer " + auth.access_token
|
|
}
|
|
}
|
|
)
|
|
|
|
return NextResponse.json(redemptions.data);
|
|
} catch (error: any) {
|
|
console.error('Fetching Twitch channel redemptions:', error.response.data)
|
|
}
|
|
return NextResponse.json([]);
|
|
} catch (error) {
|
|
console.log("[REDEMPTIONS/ACTIONS]", error);
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
|
}
|
|
} |