import { handleApiError, axios } from ".";

export interface CodeResponse {
  code: string;
}

export const codeApi = async (
  accessToken: string,
  nonce: string,
  clientId: string,
) => {
  const response = await axios.post(
    "/api/v1/oauth/code",
    { nonce, client_id: clientId },
    {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${accessToken}`,
      },
    },
  );

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

  const data: CodeResponse = response.data;

  return data;
};
