2025-03-06 05:46:25 +08:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
|
|
|
|
import { HttpServer } from '~/libs/http-server'
|
2025-03-08 00:47:33 +08:00
|
|
|
import type { TContentSchema } from '~/pages/form-contents'
|
2025-03-06 05:46:25 +08:00
|
|
|
|
|
|
|
|
const newsResponseSchema = z.object({
|
|
|
|
|
data: z.object({
|
|
|
|
|
Message: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
type TParameter = {
|
|
|
|
|
accessToken: string
|
|
|
|
|
payload: TContentSchema
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createNewsRequest = async (parameters: TParameter) => {
|
|
|
|
|
const { accessToken, payload } = parameters
|
|
|
|
|
try {
|
|
|
|
|
const { categories, tags, ...restPayload } = payload
|
|
|
|
|
const transformedPayload = {
|
|
|
|
|
...restPayload,
|
2025-03-06 05:49:49 +08:00
|
|
|
categories: categories.map((category) => category?.id),
|
2025-03-07 14:40:04 +08:00
|
|
|
tags: tags?.map((tag) => tag?.id) || [],
|
2025-03-06 05:46:25 +08:00
|
|
|
live_at: new Date(payload?.live_at).toISOString(),
|
|
|
|
|
}
|
|
|
|
|
const { data } = await HttpServer({ accessToken }).post(
|
|
|
|
|
'/api/news/create',
|
|
|
|
|
transformedPayload,
|
|
|
|
|
)
|
|
|
|
|
return newsResponseSchema.parse(data)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
}
|