diff --git a/frontend/composables/post.ts b/frontend/composables/post.ts index 6d7572a..4516a2f 100644 --- a/frontend/composables/post.ts +++ b/frontend/composables/post.ts @@ -1,22 +1,41 @@ import { z } from 'zod' const PostValidator = z.object({ - sequence: z.number(), - id: z.string(), - title: z.string(), - description: z.string(), - created_at: z.string(), - updated_at: z.string(), + sequence: z.number(), + id: z.string(), + title: z.string(), + description: z.string(), + created_at: z.string(), + updated_at: z.string(), +}) + +const newPostValidator = z.object({ + title: z.string(), + description: z.string(), }) const parsePost = (data: unknown) => { - return PostValidator.parse(data) + return PostValidator.parse(data) } const PostsValidator = PostValidator.array() const parsePosts = (data: unknown[]) => { - return PostsValidator.parse(data) + return PostsValidator.parse(data) +} + +type Posts = z.infer + +const usePosts = () => useState(() => []) + +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, PostsValidator, PostValidator } +export { parsePost, parsePosts, PostValidator, createNewPost, usePosts } diff --git a/frontend/pages/index.vue b/frontend/pages/index.vue index 6051777..59f41fa 100644 --- a/frontend/pages/index.vue +++ b/frontend/pages/index.vue @@ -5,9 +5,7 @@ definePageMeta({ import { z } from 'zod' -type Posts = z.infer - -const posts = ref([]) +const posts = usePosts() const lastPost = ref(0) const responseValidator = z.object({