hermes-web/app/api/settings/groups/twitchchatters/route.ts

55 lines
1.9 KiB
TypeScript
Raw Normal View History

import { NextResponse } from "next/server";
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
import axios from "axios";
import { env } from "process";
import { TwitchUpdateAuthorization } from "@/lib/twitch";
export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user)
return new NextResponse("Unauthorized", { status: 401 });
const { searchParams } = new URL(req.url)
const logins = (searchParams.get('logins') as string)?.split(',').reduce((a,b) => a + ',&login=' + b)
const ids = (searchParams.get('ids') as string)?.split(',').reduce((a,b) => a + ',&id=' + b)
if (!logins && !ids) {
return new NextResponse("Bad Request", { status: 400 });
}
let suffix = ""
if (!!logins)
suffix += "&login=" + logins
if (!!ids)
suffix += "&id=" + ids
if (!!suffix)
suffix = "?" + suffix.substring(1)
const auth = await TwitchUpdateAuthorization(user.id)
if (!auth) {
return new NextResponse("", { status: 403 })
}
console.log('TWITCH URL:', 'https://api.twitch.tv/helix/users' + suffix)
console.log("AUTH", auth)
const users = await axios.get("https://api.twitch.tv/helix/users" + suffix, {
headers: {
"Authorization": "Bearer " + auth.access_token,
"Client-Id": env.TWITCH_BOT_CLIENT_ID
}
})
if (!users || !users.data) {
return new NextResponse("", { status: 400 })
}
if (users.data.data.length == 0) {
return NextResponse.json([])
}
return NextResponse.json(users.data.data.map((u: any) => ({ id: u.id, username: u.login })));
} catch (error) {
console.log("[GROUPS/USERS]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}