feat: refactor API response schemas for improve consistency

This commit is contained in:
Ardeman
2025-03-07 09:04:11 +08:00
parent 7163266100
commit d765d92df7
11 changed files with 40 additions and 50 deletions
+6 -6
View File
@@ -2,22 +2,22 @@ import { z } from 'zod'
import { HttpServer, type THttpServer } from '~/libs/http-server'
export const categorySchema = z.object({
export const categoryResponseSchema = z.object({
id: z.string(),
name: z.string(),
code: z.string(),
})
const categoriesSchema = z.object({
data: z.array(categorySchema),
const categoriesResponseSchema = z.object({
data: z.array(categoryResponseSchema),
})
export type TCategorySchema = z.infer<typeof categorySchema>
export type TCategoriesSchema = z.infer<typeof categoriesSchema>
export type TCategoryResponse = z.infer<typeof categoryResponseSchema>
export type TCategoriesResponse = z.infer<typeof categoriesResponseSchema>
export const getCategories = async (parameters?: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/category`)
return categoriesSchema.parse(data)
return categoriesResponseSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)