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

49 lines
2.0 KiB
TypeScript
Raw Permalink 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)
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { 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) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Either logins or ids must not be null.', error: null, value: null }, { 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) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'You do not have permissions for this.', error: null, value: null }, { 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
}
})
2024-08-25 17:35:46 -04:00
if (!users?.data?.data)
return NextResponse.json([], { status: 200 });
2024-08-25 17:35:46 -04:00
return NextResponse.json(users.data.data.map((u: any) => ({ id: u.id, username: u.login })), { status: 200 });
} catch (error) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}