import { error } from '@sveltejs/kit';
import type { User } from './types';

const globalPrefix = '/api';

const usersPrefix = `${globalPrefix}/users`;
export async function readUser(fetch: CallableFunction, userId: number): Promise<User> {
  const endpoint = `${usersPrefix}/${userId}`;
  const response = await fetch(endpoint);

  if (!response.ok) {
    throw error(response.status, response.statusText);
  }

  const responseJson = await response.json();
  return responseJson as User;
}
