feat: refactor news and category schemas, update API response handling, and improve date formatting
This commit is contained in:
+21
-30
@@ -1,5 +1,6 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
import { categorySchema } from '~/apis/common/get-categories'
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
|
||||
const authorSchema = z.object({
|
||||
@@ -7,47 +8,37 @@ const authorSchema = z.object({
|
||||
name: z.string(),
|
||||
profile_picture: z.string(),
|
||||
})
|
||||
|
||||
const categoriesCodeSchema = z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
code: z.string(),
|
||||
}),
|
||||
)
|
||||
const newsSchema = z.object({
|
||||
data: z.array(
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
content: z.string(),
|
||||
categories: z.array(categorySchema),
|
||||
tags: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
content: z.string(),
|
||||
categories: categoriesCodeSchema,
|
||||
tags: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
code: z.string(),
|
||||
}),
|
||||
),
|
||||
is_premium: z.boolean(),
|
||||
slug: z.string(),
|
||||
featured_image: z.string(),
|
||||
author_id: z.string(),
|
||||
live_at: z.string(),
|
||||
created_at: z.string(),
|
||||
updated_at: z.string(),
|
||||
author: authorSchema,
|
||||
name: z.string(),
|
||||
code: z.string(),
|
||||
}),
|
||||
),
|
||||
is_premium: z.boolean(),
|
||||
slug: z.string(),
|
||||
featured_image: z.string(),
|
||||
author_id: z.string(),
|
||||
live_at: z.string(),
|
||||
created_at: z.string(),
|
||||
updated_at: z.string(),
|
||||
author: authorSchema,
|
||||
})
|
||||
const dataSchema = z.object({
|
||||
data: z.array(newsSchema),
|
||||
})
|
||||
|
||||
export type TAuthor = z.infer<typeof authorSchema>
|
||||
export type TCategories = z.infer<typeof categoriesCodeSchema>
|
||||
export type TNewsSchema = z.infer<typeof newsSchema>
|
||||
|
||||
export const getNews = async (parameters: THttpServer) => {
|
||||
try {
|
||||
const { data } = await HttpServer(parameters).get(`/api/news`)
|
||||
return newsSchema.parse(data)
|
||||
return dataSchema.parse(data)
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
return Promise.reject(error)
|
||||
|
||||
Reference in New Issue
Block a user