HoshiAI-app/api/_client.ts

104 lines
2.0 KiB
TypeScript

import axiosInstance from "./_axiosInstance";
import {
AuthLoginResponse,
CategoryResponse,
Pagination,
QuestionResponse,
TestResponse,
UserResponse,
UserTestResponse,
} from "./types";
export const get_questions = async (
page: number,
test_id?: number,
category_id?: number
) => {
const response = await axiosInstance.get<Pagination<QuestionResponse>>(
"/api/questions/",
{
params: {
page,
test_id,
category_id,
},
}
);
return response.data;
};
export const get_question = async (id: number) => {
const response = await axiosInstance.get<{
data: QuestionResponse;
}>(`/api/questions/${id}`);
return response.data;
};
export const get_current_user = async () => {
const response = await axiosInstance.get<{
user: UserResponse;
}>("/api/auth/me/");
return response.data;
};
export const get_categories = async () => {
const response = await axiosInstance.get<{
data: CategoryResponse[];
}>("/api/categories/");
return response.data;
};
export const get_tests = async (page: number, category_id?: number) => {
const response = await axiosInstance.get<Pagination<TestResponse>>(
"/api/tests/",
{
params: {
page,
category_id,
},
}
);
return response.data;
};
export const get_test = async (id: number) => {
const response = await axiosInstance.get<{
data: TestResponse;
}>(`/api/tests/${id}`);
return response.data;
}
export const get_user_tests = async () => {
const response = await axiosInstance.get<{
data: UserTestResponse[];
}>("/api/user-tests/");
return response.data;
}
export const get_user_test = async (id: number) => {
const response = await axiosInstance.get<{
data: UserTestResponse;
}>(`/api/user-tests/${id}`);
return response.data;
}
export const post_login = async (email: string, password: string) => {
const response = await axiosInstance.post<AuthLoginResponse>(
"/api/auth/login/",
{
email,
password,
}
);
return response.data;
};