2025-03-02 12:06:30 +08:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
|
|
|
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
|
|
|
|
|
2025-03-07 09:04:11 +08:00
|
|
|
export const categoryResponseSchema = z.object({
|
2025-03-07 08:50:04 +08:00
|
|
|
id: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
code: z.string(),
|
|
|
|
|
})
|
2025-03-07 09:04:11 +08:00
|
|
|
const categoriesResponseSchema = z.object({
|
|
|
|
|
data: z.array(categoryResponseSchema),
|
2025-03-02 12:06:30 +08:00
|
|
|
})
|
|
|
|
|
|
2025-03-07 09:04:11 +08:00
|
|
|
export type TCategoryResponse = z.infer<typeof categoryResponseSchema>
|
|
|
|
|
export type TCategoriesResponse = z.infer<typeof categoriesResponseSchema>
|
2025-03-02 12:06:30 +08:00
|
|
|
|
|
|
|
|
export const getCategories = async (parameters?: THttpServer) => {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await HttpServer(parameters).get(`/api/category`)
|
2025-03-07 09:04:11 +08:00
|
|
|
return categoriesResponseSchema.parse(data)
|
2025-03-02 12:06:30 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
}
|