feat: refactor API response schemas for improve consistency
This commit is contained in:
parent
7163266100
commit
d765d92df7
@ -1,6 +1,7 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
import { categorySchema } from '~/apis/common/get-categories'
|
import { categoryResponseSchema } from '~/apis/common/get-categories'
|
||||||
|
import { tagResponseSchema } from '~/apis/common/get-tags'
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const authorSchema = z.object({
|
const authorSchema = z.object({
|
||||||
@ -8,18 +9,12 @@ const authorSchema = z.object({
|
|||||||
name: z.string(),
|
name: z.string(),
|
||||||
profile_picture: z.string(),
|
profile_picture: z.string(),
|
||||||
})
|
})
|
||||||
const newsSchema = z.object({
|
const newsResponseSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
content: z.string(),
|
content: z.string(),
|
||||||
categories: z.array(categorySchema),
|
categories: z.array(categoryResponseSchema),
|
||||||
tags: z.array(
|
tags: z.array(tagResponseSchema),
|
||||||
z.object({
|
|
||||||
id: z.string(),
|
|
||||||
name: z.string(),
|
|
||||||
code: z.string(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
is_premium: z.boolean(),
|
is_premium: z.boolean(),
|
||||||
slug: z.string(),
|
slug: z.string(),
|
||||||
featured_image: z.string(),
|
featured_image: z.string(),
|
||||||
@ -29,16 +24,16 @@ const newsSchema = z.object({
|
|||||||
updated_at: z.string(),
|
updated_at: z.string(),
|
||||||
author: authorSchema,
|
author: authorSchema,
|
||||||
})
|
})
|
||||||
const dataSchema = z.object({
|
const dataResponseSchema = z.object({
|
||||||
data: z.array(newsSchema),
|
data: z.array(newsResponseSchema),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type TNewsSchema = z.infer<typeof newsSchema>
|
export type TNewsResponse = z.infer<typeof newsResponseSchema>
|
||||||
|
|
||||||
export const getNews = async (parameters: THttpServer) => {
|
export const getNews = async (parameters: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/news`)
|
const { data } = await HttpServer(parameters).get(`/api/news`)
|
||||||
return dataSchema.parse(data)
|
return dataResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const staffSchema = z.object({
|
const staffResponseSchema = z.object({
|
||||||
data: z.object({
|
data: z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
email: z.string(),
|
email: z.string(),
|
||||||
@ -14,7 +14,7 @@ const staffSchema = z.object({
|
|||||||
export const getStaff = async (parameters: THttpServer) => {
|
export const getStaff = async (parameters: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/staff/profile`)
|
const { data } = await HttpServer(parameters).get(`/api/staff/profile`)
|
||||||
return staffSchema.parse(data)
|
return staffResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -2,22 +2,22 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
export const categorySchema = z.object({
|
export const categoryResponseSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
code: z.string(),
|
code: z.string(),
|
||||||
})
|
})
|
||||||
const categoriesSchema = z.object({
|
const categoriesResponseSchema = z.object({
|
||||||
data: z.array(categorySchema),
|
data: z.array(categoryResponseSchema),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type TCategorySchema = z.infer<typeof categorySchema>
|
export type TCategoryResponse = z.infer<typeof categoryResponseSchema>
|
||||||
export type TCategoriesSchema = z.infer<typeof categoriesSchema>
|
export type TCategoriesResponse = z.infer<typeof categoriesResponseSchema>
|
||||||
|
|
||||||
export const getCategories = async (parameters?: THttpServer) => {
|
export const getCategories = async (parameters?: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/category`)
|
const { data } = await HttpServer(parameters).get(`/api/category`)
|
||||||
return categoriesSchema.parse(data)
|
return categoriesResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const subscriptionSchema = z.object({
|
const subscriptionResponseSchema = z.object({
|
||||||
data: z.array(
|
data: z.array(
|
||||||
z.object({
|
z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@ -15,7 +15,7 @@ const subscriptionSchema = z.object({
|
|||||||
export const getSubscriptions = async (parameters?: THttpServer) => {
|
export const getSubscriptions = async (parameters?: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/subscribe-plan`)
|
const { data } = await HttpServer(parameters).get(`/api/subscribe-plan`)
|
||||||
return subscriptionSchema.parse(data)
|
return subscriptionResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -2,21 +2,21 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const tagSchema = z.object({
|
export const tagResponseSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
code: z.string(),
|
code: z.string(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
})
|
})
|
||||||
const tagsSchema = z.object({
|
const tagsResponseSchema = z.object({
|
||||||
data: z.array(tagSchema),
|
data: z.array(tagResponseSchema),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type TTagSchema = z.infer<typeof tagSchema>
|
export type TTagResponse = z.infer<typeof tagResponseSchema>
|
||||||
|
|
||||||
export const getTags = async (parameters?: THttpServer) => {
|
export const getTags = async (parameters?: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/tag`)
|
const { data } = await HttpServer(parameters).get(`/api/tag`)
|
||||||
return tagsSchema.parse(data)
|
return tagsResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const userSchema = z.object({
|
const userResponseSchema = z.object({
|
||||||
data: z.object({
|
data: z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
email: z.string(),
|
email: z.string(),
|
||||||
@ -23,12 +23,12 @@ const userSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type TUser = z.infer<typeof userSchema>
|
export type TUserResponse = z.infer<typeof userResponseSchema>
|
||||||
|
|
||||||
export const getUser = async (parameters: THttpServer) => {
|
export const getUser = async (parameters: THttpServer) => {
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(parameters).get(`/api/user/profile`)
|
const { data } = await HttpServer(parameters).get(`/api/user/profile`)
|
||||||
return userSchema.parse(data)
|
return userResponseSchema.parse(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { z } from 'zod'
|
|||||||
import { type TLoginSchema } from '~/layouts/news/form-login'
|
import { type TLoginSchema } from '~/layouts/news/form-login'
|
||||||
import { HttpServer } from '~/libs/http-server'
|
import { HttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const loginResponseSchema = z.object({
|
export const loginResponseSchema = z.object({
|
||||||
data: z.object({
|
data: z.object({
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@ -1,13 +1,7 @@
|
|||||||
import { z } from 'zod'
|
|
||||||
|
|
||||||
import type { TRegisterSchema } from '~/layouts/news/form-register'
|
import type { TRegisterSchema } from '~/layouts/news/form-register'
|
||||||
import { HttpServer } from '~/libs/http-server'
|
import { HttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
const loginResponseSchema = z.object({
|
import { loginResponseSchema } from './login-user'
|
||||||
data: z.object({
|
|
||||||
token: z.string(),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
export const userRegisterRequest = async (payload: TRegisterSchema) => {
|
export const userRegisterRequest = async (payload: TRegisterSchema) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { Link, useFetcher, useRouteLoaderData } from 'react-router'
|
import { Link, useFetcher, useRouteLoaderData } from 'react-router'
|
||||||
|
|
||||||
import type { TCategoriesSchema } from '~/apis/common/get-categories'
|
import type { TCategoriesResponse } from '~/apis/common/get-categories'
|
||||||
import { CloseIcon } from '~/components/icons/close'
|
import { CloseIcon } from '~/components/icons/close'
|
||||||
import { MenuIcon } from '~/components/icons/menu'
|
import { MenuIcon } from '~/components/icons/menu'
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
@ -10,7 +10,7 @@ import { HeaderSearch } from '~/layouts/news/header-search'
|
|||||||
import type { loader } from '~/routes/_layout'
|
import type { loader } from '~/routes/_layout'
|
||||||
|
|
||||||
type THeaderMenuMobile = {
|
type THeaderMenuMobile = {
|
||||||
menu?: TCategoriesSchema['data']
|
menu?: TCategoriesResponse['data']
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function HeaderMenuMobile(properties: THeaderMenuMobile) {
|
export default function HeaderMenuMobile(properties: THeaderMenuMobile) {
|
||||||
|
|||||||
@ -2,9 +2,9 @@ import DT from 'datatables.net-dt'
|
|||||||
import DataTable from 'datatables.net-react'
|
import DataTable from 'datatables.net-react'
|
||||||
import { Link, useRouteLoaderData } from 'react-router'
|
import { Link, useRouteLoaderData } from 'react-router'
|
||||||
|
|
||||||
import type { TNewsSchema } from '~/apis/admin/get-news'
|
import type { TNewsResponse } from '~/apis/admin/get-news'
|
||||||
import type { TCategorySchema } from '~/apis/common/get-categories'
|
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
||||||
import type { TTagSchema } from '~/apis/common/get-tags'
|
import type { TTagResponse } from '~/apis/common/get-tags'
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
import { UiTable } from '~/components/ui/table'
|
import { UiTable } from '~/components/ui/table'
|
||||||
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
||||||
@ -55,14 +55,15 @@ export const ContentsPage = () => {
|
|||||||
]
|
]
|
||||||
const dataSlot = {
|
const dataSlot = {
|
||||||
1: (value: string) => formatDate(value),
|
1: (value: string) => formatDate(value),
|
||||||
2: (_value: unknown, _type: unknown, data: TNewsSchema) => (
|
2: (_value: unknown, _type: unknown, data: TNewsResponse) => (
|
||||||
<div>
|
<div>
|
||||||
<div>{data.author.name}</div>
|
<div>{data.author.name}</div>
|
||||||
<div className="text-sm text-[#7C7C7C]">ID: {data.id.slice(0, 8)}</div>
|
<div className="text-sm text-[#7C7C7C]">ID: {data.id.slice(0, 8)}</div>
|
||||||
</div>
|
</div>
|
||||||
),
|
),
|
||||||
4: (value: TCategorySchema[]) => value.map((item) => item.name).join(', '),
|
4: (value: TCategoryResponse[]) =>
|
||||||
5: (value: TTagSchema[]) => value.map((item) => item.name).join(', '),
|
value.map((item) => item.name).join(', '),
|
||||||
|
5: (value: TTagResponse[]) => value.map((item) => item.name).join(', '),
|
||||||
6: (value: string) =>
|
6: (value: string) =>
|
||||||
value ? (
|
value ? (
|
||||||
<div className="rounded-full bg-[#FFFCAF] px-2 text-center text-[#DBCA6E]">
|
<div className="rounded-full bg-[#FFFCAF] px-2 text-center text-[#DBCA6E]">
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
import type { MouseEventHandler } from 'react'
|
import type { MouseEventHandler } from 'react'
|
||||||
import { Link } from 'react-router'
|
import { Link } from 'react-router'
|
||||||
|
|
||||||
import type { TUser } from '~/apis/news/get-user'
|
import type { TUserResponse } from '~/apis/news/get-user'
|
||||||
|
|
||||||
type TGetPremiumAttribute = {
|
type TGetPremiumAttribute = {
|
||||||
isPremium?: boolean
|
isPremium?: boolean
|
||||||
slug: string
|
slug: string
|
||||||
onClick: MouseEventHandler<HTMLElement>
|
onClick: MouseEventHandler<HTMLElement>
|
||||||
userData?: TUser['data']
|
userData?: TUserResponse['data']
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getPremiumAttribute = (parameters: TGetPremiumAttribute) => {
|
export const getPremiumAttribute = (parameters: TGetPremiumAttribute) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user