import { Vercel } from '@vercel/sdk';
import { getSession } from './auth';

export function getVercelClient(token: string) {
  if (!token) {
    throw new Error('Authentication token is missing');
  }

  const vercel = new Vercel({
    bearerToken: token,
  });

  return vercel;
}

export async function getServerVercelClient() {
  const session = await getSession();

  if (!session?.user?.vercelAccessToken) {
    throw new Error('Vercel authentication token is missing', { cause: 401 });
  }

  return getVercelClient(session.user.vercelAccessToken);
}
