feat: refactor API response schemas for improve consistency
This commit is contained in:
@@ -2,22 +2,22 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
|
||||
export const categorySchema = z.object({
|
||||
export const categoryResponseSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
code: z.string(),
|
||||
})
|
||||
const categoriesSchema = z.object({
|
||||
data: z.array(categorySchema),
|
||||
const categoriesResponseSchema = z.object({
|
||||
data: z.array(categoryResponseSchema),
|
||||
})
|
||||
|
||||
export type TCategorySchema = z.infer<typeof categorySchema>
|
||||
export type TCategoriesSchema = z.infer<typeof categoriesSchema>
|
||||
export type TCategoryResponse = z.infer<typeof categoryResponseSchema>
|
||||
export type TCategoriesResponse = z.infer<typeof categoriesResponseSchema>
|
||||
|
||||
export const getCategories = async (parameters?: THttpServer) => {
|
||||
try {
|
||||
const { data } = await HttpServer(parameters).get(`/api/category`)
|
||||
return categoriesSchema.parse(data)
|
||||
return categoriesResponseSchema.parse(data)
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
return Promise.reject(error)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
|
||||
const subscriptionSchema = z.object({
|
||||
const subscriptionResponseSchema = z.object({
|
||||
data: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
@@ -15,7 +15,7 @@ const subscriptionSchema = z.object({
|
||||
export const getSubscriptions = async (parameters?: THttpServer) => {
|
||||
try {
|
||||
const { data } = await HttpServer(parameters).get(`/api/subscribe-plan`)
|
||||
return subscriptionSchema.parse(data)
|
||||
return subscriptionResponseSchema.parse(data)
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
return Promise.reject(error)
|
||||
|
||||
@@ -2,21 +2,21 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
|
||||
const tagSchema = z.object({
|
||||
export const tagResponseSchema = z.object({
|
||||
id: z.string(),
|
||||
code: z.string(),
|
||||
name: z.string(),
|
||||
})
|
||||
const tagsSchema = z.object({
|
||||
data: z.array(tagSchema),
|
||||
const tagsResponseSchema = z.object({
|
||||
data: z.array(tagResponseSchema),
|
||||
})
|
||||
|
||||
export type TTagSchema = z.infer<typeof tagSchema>
|
||||
export type TTagResponse = z.infer<typeof tagResponseSchema>
|
||||
|
||||
export const getTags = async (parameters?: THttpServer) => {
|
||||
try {
|
||||
const { data } = await HttpServer(parameters).get(`/api/tag`)
|
||||
return tagsSchema.parse(data)
|
||||
return tagsResponseSchema.parse(data)
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
return Promise.reject(error)
|
||||
|
||||
Reference in New Issue
Block a user