|
|
|
|
@ -9,6 +9,11 @@ const PostValidator = z.object({
|
|
|
|
|
updated_at: z.string(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const newPostValidator = z.object({
|
|
|
|
|
title: z.string(),
|
|
|
|
|
description: z.string(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const parsePost = (data: unknown) => {
|
|
|
|
|
return PostValidator.parse(data)
|
|
|
|
|
}
|
|
|
|
|
@ -19,4 +24,18 @@ const parsePosts = (data: unknown[]) => {
|
|
|
|
|
return PostsValidator.parse(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { parsePost, parsePosts, PostsValidator, PostValidator }
|
|
|
|
|
type Posts = z.infer<typeof PostsValidator>
|
|
|
|
|
|
|
|
|
|
const usePosts = () => useState<Posts>(() => [])
|
|
|
|
|
|
|
|
|
|
const createNewPost = async (data: unknown) => {
|
|
|
|
|
const parsedNewPost = newPostValidator.safeParse(data)
|
|
|
|
|
|
|
|
|
|
if (!parsedNewPost.success) return
|
|
|
|
|
|
|
|
|
|
const res = await request.post('/api/posts', parsedNewPost.data)
|
|
|
|
|
|
|
|
|
|
console.log(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { parsePost, parsePosts, PostValidator, createNewPost, usePosts }
|
|
|
|
|
|