You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
916 B
TypeScript

import { z } from 'zod'
const noteSchema = z.object({
title: z.string(),
description: z.string(),
})
const submitNote = async (note: unknown, noteId: string | string[]) => {
const parsedNote = noteSchema.safeParse(note)
if (!parsedNote.success) return
let response = {
status: 200,
message: 'Update',
error: null,
}
await useFetch('/api/note', {
method: 'POST',
query: { id: noteId },
body: parsedNote.data,
onResponse({ response }) {
console.log('POST:', response._data.message)
response = response._data
},
})
return response
}
type Note = z.infer<typeof noteSchema>
const useNote = () =>
useState<Note>('note', () => Object({ title: '', description: '' }))
const useDescription = () => useState<string>('description', () => '')
export { submitNote, useNote, useDescription }