You are viewing the docs for the newest version. You can view the docs for 0.5 here

Arbitrary parser

This is an example on how to make an arbitrary parser, e.g., to deserialize data from an API:

Example with valid data
import { z, makeParser } from "@sidebase/nuxt-parse"// Define the expected response schemaconst responseSchema = z.object({    uuid: z.string().uuid(),})// Perform the request, use `makeParse` to pass a transformer for the dataconst { data, error } = await useFetch('https://httpbin.org/uuid', {    transform: makeParser(responseSchema),})console.log(`data is ${data.value}`)// -> `data is {"uuid":"f8df921c-d7f3-43c1-ac9b-3cf5d4da2f7b"}`console.log(`error is ${error.value}`)// -> `error is false`
Example with invalid data
import { z, makeParser } from "@sidebase/nuxt-parse"// Define the expected response schemaconst responseSchema = z.object({    uuid: z.string().uuid(),})// Perform the request, use `makeParse` to pass a transformer for the dataconst { data, error } = await useFetch('https://httpbin.org/ip', {    transform: makeParser(responseSchema),})console.log(`data is ${data.value}`)// -> `data is null`console.log(`error is ${error.value}`)// -> `error is true`