feat: refactor news and category schemas, update API response handling, and improve date formatting

This commit is contained in:
Ardeman
2025-03-07 08:50:04 +08:00
parent 661437a2d3
commit 1bb63bec8e
7 changed files with 92 additions and 192 deletions
+9 -9
View File
@@ -2,22 +2,22 @@ import { z } from 'zod'
import { HttpServer, type THttpServer } from '~/libs/http-server'
const categorySchema = z.object({
data: z.array(
z.object({
id: z.string(),
code: z.string(),
name: z.string(),
}),
),
export const categorySchema = z.object({
id: z.string(),
name: z.string(),
code: z.string(),
})
const categoriesSchema = z.object({
data: z.array(categorySchema),
})
export type TCategorySchema = z.infer<typeof categorySchema>
export type TCategoriesSchema = z.infer<typeof categoriesSchema>
export const getCategories = async (parameters?: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/category`)
return categorySchema.parse(data)
return categoriesSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)
+7 -8
View File
@@ -3,13 +3,12 @@ import { z } from 'zod'
import { HttpServer, type THttpServer } from '~/libs/http-server'
const tagSchema = z.object({
data: z.array(
z.object({
id: z.string(),
code: z.string(),
name: z.string(),
}),
),
id: z.string(),
code: z.string(),
name: z.string(),
})
const tagsSchema = z.object({
data: z.array(tagSchema),
})
export type TTagSchema = z.infer<typeof tagSchema>
@@ -17,7 +16,7 @@ export type TTagSchema = z.infer<typeof tagSchema>
export const getTags = async (parameters?: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/tag`)
return tagSchema.parse(data)
return tagsSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)