2025-03-06 09:30:53 +08:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
2025-03-07 09:04:11 +08:00
|
|
|
import { categoryResponseSchema } from '~/apis/common/get-categories'
|
|
|
|
|
import { tagResponseSchema } from '~/apis/common/get-tags'
|
2025-03-06 09:30:53 +08:00
|
|
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
|
|
|
|
|
2025-03-06 22:49:56 +07:00
|
|
|
const authorSchema = z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
profile_picture: z.string(),
|
|
|
|
|
})
|
2025-03-07 13:50:16 +08:00
|
|
|
export const newsResponseSchema = z.object({
|
2025-03-07 08:50:04 +08:00
|
|
|
id: z.string(),
|
|
|
|
|
title: z.string(),
|
|
|
|
|
content: z.string(),
|
2025-03-07 09:04:11 +08:00
|
|
|
categories: z.array(categoryResponseSchema),
|
|
|
|
|
tags: z.array(tagResponseSchema),
|
2025-03-07 08:50:04 +08:00
|
|
|
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,
|
|
|
|
|
})
|
2025-03-07 09:04:11 +08:00
|
|
|
const dataResponseSchema = z.object({
|
|
|
|
|
data: z.array(newsResponseSchema),
|
2025-03-06 09:30:53 +08:00
|
|
|
})
|
|
|
|
|
|
2025-03-07 09:04:11 +08:00
|
|
|
export type TNewsResponse = z.infer<typeof newsResponseSchema>
|
2025-03-08 21:29:39 +07:00
|
|
|
export type TAuthor = z.infer<typeof authorSchema>
|
2025-03-09 10:23:11 +08:00
|
|
|
type TParameters = {
|
|
|
|
|
categories?: string[]
|
|
|
|
|
tags?: string[]
|
|
|
|
|
} & THttpServer
|
2025-03-06 22:49:56 +07:00
|
|
|
|
2025-03-09 11:36:47 +08:00
|
|
|
export const getNews = async (parameters?: TParameters) => {
|
|
|
|
|
const { categories, tags, ...restParameters } = parameters || {}
|
2025-03-06 09:30:53 +08:00
|
|
|
try {
|
2025-03-09 10:23:11 +08:00
|
|
|
const { data } = await HttpServer(restParameters).get(`/api/news`, {
|
|
|
|
|
params: {
|
|
|
|
|
...(categories && { categories: categories.join('+') }),
|
|
|
|
|
...(tags && { tags: tags.join('+') }),
|
|
|
|
|
},
|
|
|
|
|
})
|
2025-03-07 09:04:11 +08:00
|
|
|
return dataResponseSchema.parse(data)
|
2025-03-06 09:30:53 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
}
|