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
+9 -14
View File
@@ -1,6 +1,7 @@
import { z } from 'zod'
import { categorySchema } from '~/apis/common/get-categories'
import { categoryResponseSchema } from '~/apis/common/get-categories'
import { tagResponseSchema } from '~/apis/common/get-tags'
import { HttpServer, type THttpServer } from '~/libs/http-server'
const authorSchema = z.object({
@@ -8,18 +9,12 @@ const authorSchema = z.object({
name: z.string(),
profile_picture: z.string(),
})
const newsSchema = z.object({
const newsResponseSchema = z.object({
id: z.string(),
title: z.string(),
content: z.string(),
categories: z.array(categorySchema),
tags: z.array(
z.object({
id: z.string(),
name: z.string(),
code: z.string(),
}),
),
categories: z.array(categoryResponseSchema),
tags: z.array(tagResponseSchema),
is_premium: z.boolean(),
slug: z.string(),
featured_image: z.string(),
@@ -29,16 +24,16 @@ const newsSchema = z.object({
updated_at: z.string(),
author: authorSchema,
})
const dataSchema = z.object({
data: z.array(newsSchema),
const dataResponseSchema = z.object({
data: z.array(newsResponseSchema),
})
export type TNewsSchema = z.infer<typeof newsSchema>
export type TNewsResponse = z.infer<typeof newsResponseSchema>
export const getNews = async (parameters: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/news`)
return dataSchema.parse(data)
return dataResponseSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)
+2 -2
View File
@@ -2,7 +2,7 @@ import { z } from 'zod'
import { HttpServer, type THttpServer } from '~/libs/http-server'
const staffSchema = z.object({
const staffResponseSchema = z.object({
data: z.object({
id: z.string(),
email: z.string(),
@@ -14,7 +14,7 @@ const staffSchema = z.object({
export const getStaff = async (parameters: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/staff/profile`)
return staffSchema.parse(data)
return staffResponseSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)