refactor: reorganize API imports and simplify parameter handling in various components

This commit is contained in:
Ardeman
2025-03-09 10:23:11 +08:00
parent 687e3c8d01
commit d38bf3a705
10 changed files with 26 additions and 16 deletions
-42
View File
@@ -1,42 +0,0 @@
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),
})
export type TNewsResponse = z.infer<typeof newsResponseSchema>
export type TAuthor = z.infer<typeof authorSchema>
export const getNews = async (parameters: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/news`)
return dataResponseSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)
}
}
+3 -3
View File
@@ -15,10 +15,10 @@ type TParameters = {
}
export const updateTagRequest = async (parameters: TParameters) => {
const { accessToken, payload } = parameters
const { payload, ...restParameters } = parameters
const { id, ...restPayload } = payload
try {
const { id, ...restPayload } = payload
const { data } = await HttpServer({ accessToken }).put(
const { data } = await HttpServer(restParameters).put(
`/api/tag/${id}/update`,
restPayload,
)