nuxt-parse
A nuxt focused package to make data validation and parsing easy. This package follows the design philosophy of the article parse, don't validate. It uses zod
for parsing data from the user, APIs, your own functions, ...
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
Moved here from original mono-repo
Features
- ✔️ Validate Data using
zod
- ✔️ Deserialize and Serialize user, backend, api data
- ✔️ Helpers focused on Nuxt 3 usage and developer experience
Usage
npm i @sidebase/nuxt-parse
Then, e.g., in your code:
- Make an arbitrary parser, e.g., to deserialize data from an API:
- Example with valid data:
import { z, makeParser } from "@sidebase/nuxt-parse"
const responseSchema = z.object({
uuid: z.string().uuid(),
})
const { data, error } = await useFetch('https://httpbin.org/uuid', {
transform: makeParser(responseSchema),
})
console.log(`data is ${data.value}`)
console.log(`error is ${error.value}`)
- Example with invalid data:
import { z, makeParser } from "@sidebase/nuxt-parse"
const responseSchema = z.object({
uuid: z.string().uuid(),
})
const { data, error } = await useFetch('https://httpbin.org/ip', {
transform: makeParser(responseSchema),
})
console.log(`data is ${data.value}`)
console.log(`error is ${error.value}`)
- Handle user data in an endpoint:
import { defineEventHandler } from 'h3'
import type { CompatibilityEvent } from 'h3'
import { z, parseParamsAs, parseBodyAs } from "@sidebase/nuxt-parse"
const paramsSchema = z.object({
id: z.string().uuid(),
})
const bodySchema = z.object({
name: z.string(),
age: z.number()
})
type RequestBody = z.infer<typeof bodySchema>
export default defineEventHandler(async (event: CompatibilityEvent) => {
const params = parseParamsAs(event, paramsSchema)
let body: RequestBody;
try {
body = parseBodyAs(event, paramsSchema)
} catch(error) {
body = {
name: 'Bernd',
age: 88
}
}
return {
id: params.id,
...body
}
})
- Parse any data:
import { z, parseDataAs } from "@sidebase/nuxt-parse"
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.number() )}))
const parsedData = await parseDataAs({ test: 1 }, z.object({ test: z.number() )}))
console.log(parsedData)
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.string().transform(v => parseInt(v)) )}))
console.log(parsedData)
- Also works with async data, e.g., when fetching from another API or DB:
import { z, parseDataAs } from "@sidebase/nuxt-parse"
const fakeDatabaseQuery = async () => { id: 1 }
const parsedData = await parseDataAs(fakeDatabaseQuery, z.object({ id: z.number() )}))
console.log(parsedData)
Documentation
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
This module exports:
parseBodyAs
: Parse body of h3
eventparseParamsAs
: Parse params of h3
eventparseQueryAs
: Parse query of h3
eventparseCookieAs
: Parse cookies of h3
eventparseHeaderAs
: Parse header of h3
eventparseDataAs
: Parse sync or async datamakeParser
: Make your own parser (see example above)z
: zod
, the library used for parsing
Development
- Run
npm run test
to generate type stubs - Run
npm run lint
to run eslint - Run
npm run type
to run typescheck via tsc - Run
npm publish
to run build and publish the package