67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
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({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
profile_picture: z.string(),
|
|
})
|
|
export const newsResponseSchema = z.object({
|
|
id: z.string(),
|
|
title: z.string(),
|
|
content: z.string(),
|
|
categories: z.array(categoryResponseSchema),
|
|
tags: z.array(tagResponseSchema),
|
|
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 dataResponseSchema = z.object({
|
|
data: z.array(
|
|
newsResponseSchema.extend({
|
|
views: z.number(),
|
|
}),
|
|
),
|
|
})
|
|
|
|
export type TNewsResponse = z.infer<typeof newsResponseSchema>
|
|
export type TNewsResponseData = z.infer<typeof dataResponseSchema>
|
|
export type TAuthorResponse = z.infer<typeof authorSchema>
|
|
type TParameters = {
|
|
categories?: string[]
|
|
tags?: string[]
|
|
active?: boolean
|
|
limit?: number
|
|
page?: number
|
|
query?: string
|
|
} & THttpServer
|
|
|
|
export const getNews = async (parameters?: TParameters) => {
|
|
const { categories, tags, active, limit, page, query, ...restParameters } =
|
|
parameters || {}
|
|
try {
|
|
const { data } = await HttpServer(restParameters).get(`/api/news`, {
|
|
params: {
|
|
...(categories && { categories: categories.join('+') }),
|
|
...(tags && { tags: tags.join('+') }),
|
|
...(active && { active }),
|
|
...(limit && { limit }),
|
|
...(page && { page }),
|
|
...(query && { q: query }),
|
|
},
|
|
})
|
|
return dataResponseSchema.parse(data)
|
|
} catch (error) {
|
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
|
return Promise.reject(error)
|
|
}
|
|
}
|