import { handleApiError } from ".";

export interface CodeResponse {
  code: string;
}

export const codeApi = async (accessToken: string, nonce: string) => {
  const response = await fetch("/api/v1/oauth/code", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({
      nonce,
    }),
  });

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

  const data: CodeResponse = await response.json();

  return data;
};
