import axios from "axios";
import { handleApiError } from ".";

export interface RefreshTokenResponse {
  access: string;
  refresh: string;
}

export const refreshTokenApi = async (refreshToken: string) => {
  const response = await axios.post(
    "/api/v1/auth/refresh",
    {},
    {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${refreshToken}`,
      },
    },
  );

  if (response.status !== 200 && response.status !== 201)
    throw await handleApiError(response);

  const data: RefreshTokenResponse = response.data;

  return data;
};
