refactor(api): convert API functions to async/await with typed returns
Replace raw axios promise returns with async functions that unwrap response data, providing stronger type guarantees at call sites. - Unwrap `res.data.data` in all API functions instead of exposing AxiosResponse to consumers - Add missing return types (LoginVo, BorrowInfoVo, Book, etc.) - Convert all exported functions to async syntax
This commit is contained in:
+12
-8
@@ -1,18 +1,22 @@
|
|||||||
import client from "../client"
|
import client from "../client"
|
||||||
import type { ApiResultString, BookAddDto, BookUpdateDto } from "@/types/api"
|
import type { ApiResultString, BookAddDto, BookUpdateDto } from "@/types/api"
|
||||||
|
|
||||||
export function addBook(data: BookAddDto) {
|
export async function addBook(data: BookAddDto): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/books/add", data)
|
const res = await client.post<ApiResultString>("/api/admin/books/add", data)
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteBook(id: number) {
|
export async function deleteBook(id: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/books/delete", null, { params: { id } })
|
const res = await client.post<ApiResultString>("/api/admin/books/delete", null, { params: { id } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateBook(id: number, data: BookUpdateDto) {
|
export async function updateBook(id: number, data: BookUpdateDto): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/books/update", data, { params: { id } })
|
const res = await client.post<ApiResultString>("/api/admin/books/update", data, { params: { id } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateStock(id: number, stock: number) {
|
export async function updateStock(id: number, stock: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/books/update-stock", null, { params: { id, stock } })
|
const res = await client.post<ApiResultString>("/api/admin/books/update-stock", null, { params: { id, stock } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-11
@@ -1,22 +1,27 @@
|
|||||||
import client from "../client"
|
import client from "../client"
|
||||||
import type { ApiResultBorrowInfoVo, ApiResultListBorrowInfoVo, ApiResultString } from "@/types/api"
|
import type { ApiResultBorrowInfoVo, ApiResultListBorrowInfoVo, ApiResultString, BorrowInfoVo } from "@/types/api"
|
||||||
|
|
||||||
export function getAllBorrows() {
|
export async function getAllBorrows(): Promise<BorrowInfoVo[]> {
|
||||||
return client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/getall")
|
const res = await client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/getall")
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getOneBorrow(id: number) {
|
export async function getOneBorrow(id: number): Promise<BorrowInfoVo> {
|
||||||
return client.get<ApiResultBorrowInfoVo>("/api/admin/borrows/getone", { params: { id } })
|
const res = await client.get<ApiResultBorrowInfoVo>("/api/admin/borrows/getone", { params: { id } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function searchBorrows(query: string) {
|
export async function searchBorrows(query: string): Promise<BorrowInfoVo[]> {
|
||||||
return client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/search", { params: { query } })
|
const res = await client.get<ApiResultListBorrowInfoVo>("/api/admin/borrows/search", { params: { query } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function borrowBook(bookId: number, userId: number) {
|
export async function borrowBook(bookId: number, userId: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/borrows/borrowbook", null, { params: { bookId, userId } })
|
const res = await client.post<ApiResultString>("/api/admin/borrows/borrowbook", null, { params: { bookId, userId } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function returnBook(recordId: number) {
|
export async function returnBook(recordId: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/admin/borrows/returnbook", null, { params: { recordId } })
|
const res = await client.post<ApiResultString>("/api/admin/borrows/returnbook", null, { params: { recordId } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-5
@@ -1,10 +1,12 @@
|
|||||||
import client from "./client"
|
import client from "./client"
|
||||||
import type { ApiResultLoginVo, UserLoginDto } from "@/types/api"
|
import type { ApiResultLoginVo, ApiResultString, LoginVo, UserLoginDto } from "@/types/api"
|
||||||
|
|
||||||
export function login(data: UserLoginDto) {
|
export async function login(data: UserLoginDto): Promise<LoginVo> {
|
||||||
return client.post<ApiResultLoginVo>("/api/auth/login", data)
|
const res = await client.post<ApiResultLoginVo>("/api/auth/login", data)
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logout() {
|
export async function logout(): Promise<string | null> {
|
||||||
return client.post("/api/auth/logout")
|
const res = await client.post<ApiResultString>("/api/auth/logout")
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-6
@@ -1,14 +1,18 @@
|
|||||||
import client from "./client"
|
import client from "./client"
|
||||||
import type { ApiResultBook, ApiResultListBook } from "@/types/api"
|
import type { ApiResultBook, ApiResultListBook } from "@/types/api"
|
||||||
|
import type { Book } from "@/types/api"
|
||||||
|
|
||||||
export function getAllBooks() {
|
export async function getAllBooks(): Promise<Book[]> {
|
||||||
return client.get<ApiResultListBook>("/api/books/getall")
|
const res = await client.get<ApiResultListBook>("/api/books/getall")
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getOneBook(id: number) {
|
export async function getOneBook(id: number): Promise<Book> {
|
||||||
return client.get<ApiResultBook>("/api/books/getone", { params: { id } })
|
const res = await client.get<ApiResultBook>("/api/books/getone", { params: { id } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function searchBook(query: string) {
|
export async function searchBook(query: string): Promise<Book[]> {
|
||||||
return client.get<ApiResultListBook>("/api/books/search", { params: { query } })
|
const res = await client.get<ApiResultListBook>("/api/books/search", { params: { query } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-11
@@ -1,22 +1,27 @@
|
|||||||
import client from "./client"
|
import client from "./client"
|
||||||
import type { ApiResultListMyBorrowVo, ApiResultMyBorrowVo, ApiResultString } from "@/types/api"
|
import type { ApiResultListMyBorrowVo, ApiResultMyBorrowVo, ApiResultString, MyBorrowVo } from "@/types/api"
|
||||||
|
|
||||||
export function getAllMyBorrows() {
|
export async function getAllMyBorrows(): Promise<MyBorrowVo[]> {
|
||||||
return client.get<ApiResultListMyBorrowVo>("/api/borrows/getall")
|
const res = await client.get<ApiResultListMyBorrowVo>("/api/borrows/getall")
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getOneMyBorrow(borrowId: number) {
|
export async function getOneMyBorrow(borrowId: number): Promise<MyBorrowVo> {
|
||||||
return client.get<ApiResultMyBorrowVo>("/api/borrows/getone", { params: { borrowId } })
|
const res = await client.get<ApiResultMyBorrowVo>("/api/borrows/getone", { params: { borrowId } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function searchMyBorrows(query: string) {
|
export async function searchMyBorrows(query: string): Promise<MyBorrowVo[]> {
|
||||||
return client.get<ApiResultListMyBorrowVo>("/api/borrows/search", { params: { query } })
|
const res = await client.get<ApiResultListMyBorrowVo>("/api/borrows/search", { params: { query } })
|
||||||
|
return res.data.data!
|
||||||
}
|
}
|
||||||
|
|
||||||
export function borrowBookForMe(bookId: number) {
|
export async function borrowBookForMe(bookId: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/borrows/borrowbook", null, { params: { bookId } })
|
const res = await client.post<ApiResultString>("/api/borrows/borrowbook", null, { params: { bookId } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export function returnBookForMe(borrowId: number) {
|
export async function returnBookForMe(borrowId: number): Promise<string | null> {
|
||||||
return client.post<ApiResultString>("/api/borrows/returnbook", null, { params: { borrowId } })
|
const res = await client.post<ApiResultString>("/api/borrows/returnbook", null, { params: { borrowId } })
|
||||||
|
return res.data.data
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-1
@@ -16,7 +16,17 @@ client.interceptors.request.use((config) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
client.interceptors.response.use(
|
client.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => {
|
||||||
|
const body = response.data as ApiResult<unknown>
|
||||||
|
if (body.code !== 200) {
|
||||||
|
if (body.code === 401) {
|
||||||
|
useAuthStore.getState().clearAuth()
|
||||||
|
window.location.href = "/login"
|
||||||
|
}
|
||||||
|
throw new Error(body.message || "Request failed")
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
},
|
||||||
(error: AxiosError<ApiResult<unknown>>) => {
|
(error: AxiosError<ApiResult<unknown>>) => {
|
||||||
const data = error.response?.data
|
const data = error.response?.data
|
||||||
if (data) {
|
if (data) {
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export { login, logout } from "./auth"
|
||||||
|
export { getAllBooks, getOneBook, searchBook } from "./books"
|
||||||
|
export { getAllMyBorrows, getOneMyBorrow, searchMyBorrows, borrowBookForMe, returnBookForMe } from "./borrows"
|
||||||
|
export { addBook, deleteBook, updateBook, updateStock } from "./admin/books"
|
||||||
|
export { getAllBorrows, getOneBorrow, searchBorrows, borrowBook, returnBook } from "./admin/borrows"
|
||||||
Reference in New Issue
Block a user