import { fetch } from "https://esm.town/v/std/fetch";
import { API_BASE_URL } from "https://esm.town/v/mangadex/API_BASE_URL";
type LoginSuccess = {
result: "ok";
token: {
session: string;
refresh: string;
};
};
type LoginFailure = {
result: "error";
errors: Array<{
id: string;
status: number;
title: string;
detail: string;
}>;
};
type LoginResponse = LoginSuccess | LoginFailure;
export const login = async (
username: string,
password: string,
): Promise<LoginResponse> => {
const response = await fetch(
`${API_BASE_URL}/auth/login`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
},
);
return await response.json();
};