Get started with Anzen quickly in your app.
Server Action
Learn how to get started with createSafeServerAction to create a safe server action with Anzen. It's a factory that helps you create a server action with input validation, authorization, and error handling.
Create the server action
Create a server action using the createSafeServerAction factory.
'use server'
importfrom '@sugardarius/anzen'
export const = createSafeServerAction
'create-thread-action',
async=>
return
Validate the input
Validate your input.
importfrom 'decoders'
importfrom '@sugardarius/anzen'
export const = createSafeServerAction
'create-thread-action',
object
async=>
// ^^^^^ Input is inferred from the input validation
return
Authorize the action
Authorize the action (if required).
importfrom 'decoders'
importfrom '@sugardarius/anzen'
importfrom '~/lib/auth'
export const = createSafeServerAction
'create-thread-action',
object
authorize: async=>
const = await auth
if!session.user) {
throw new Error'user is not authenticated'
return
async=>
// ^^^^ Auth context is inferred from the authorize function
return
Call the action
Call the action from a Client Component.
importfrom '~/actions/create-thread'
export default function Form() {
return
<form
action={async=>
const = await createThread
/*
* result: {
* success: true,
* output: {
* id: 'create-thread-action',
* title: 'What's new in Anzen?',
* username: 'John Doe',
* },
* }
*/
log
}
>
<input id='title' name='title' />
<button type='submit'></button>
</form>
}Route handler
Learn how to get started with createSafeRouteHandler to create a safe route handler with Anzen. It's a factory that helps you create a route handler with input validation, authorization, and error handling.
Create the route handler
Add a Route Handler using the createSafeRouteHandler factory.
importfrom '@sugardarius/anzen'
export const = createSafeRouteHandler
'thread-route',
async=>
returnjson
Validate the request body
Validate your different inputs like the request body. For POST, PUT, or PATCH, use body with a Standard Schema (JSON). You can also validate dynamic segments, searchParams, or formData depending on the request.
importfrom 'decoders'
importfrom '@sugardarius/anzen'
export const = createSafeRouteHandler
'thread-route',
object
async=>
// ^^^^ Body is inferred from the body validation
returnjson
Authorize the route
Authorize the request (if required).
importfrom 'decoders'
importfrom '@sugardarius/anzen'
importfrom '~/lib/auth'
export const = createSafeRouteHandler
'thread-route',
object
authorize: async=>
const = await auth
if!session.user) {
return new Response401
return
async=>
// ^^^^ Auth context is inferred from the authorize function
returnjson
Page Server Component
Learn how to get started with createSafePageServerComponent to wrap a Next.js App Router page.tsx with Anzen. It validates dynamic segments and searchParams, runs authorization, and handles errors consistently.
Create a page using the createSafePageServerComponent factory.
importfrom '@sugardarius/anzen/server-components'
export default createSafePageServerComponent
'thread-page',
async=>
return <div>{id}</div>
Validate the URL data
Validate URL data with segments and/or searchParams (Standard Schema).
importfrom 'decoders'
importfrom '@sugardarius/anzen/server-components'
export default createSafePageServerComponent
'thread-page',
optional
async=>
// ^^^^^^^^ ^^^^^^^^^^^^^ Inferred from validation
return
<div>
{segments.threadId}{searchParams.q ?? '—'}
</div>
Authorize the page
Authorize the page (if required). Use next/navigation helpers such as unauthorized or notFound when access should stop; they work with Anzen’s error handling.
importfrom 'next/navigation'
importfrom 'decoders'
importfrom '@sugardarius/anzen/server-components'
importfrom '~/lib/auth'
export default createSafePageServerComponent
'thread-page',
optional
authorize: async=>
const = await auth
if!session.user) {
unauthorized
return
async=>
// ^^^^ Inferred from authorize
return
<div>
{auth.user.name}{segments.threadId}{searchParams.q ?? '—'}
</div>
Layout Server Component
Learn how to get started with createSafeLayoutServerComponent to wrap a Next.js App Router layout.tsx with Anzen. It supports the same segments validation and authorization as pages, and receives children (and optional parallel-route slots via experimental_slots when you need them).
Create the layout
Create a layout using the createSafeLayoutServerComponent factory.
importfrom '@sugardarius/anzen/server-components'
export default createSafeLayoutServerComponent
'thread-layout',
async=>
return <div className='mx-auto max-w-lg py-8'>{children}</div>
Validate dynamic segments for that route.
importfrom 'decoders'
importfrom '@sugardarius/anzen/server-components'
export default createSafeLayoutServerComponent
'thread-layout',
async=>
// ^^^^^^^^ Inferred from segments validation
return
<div>
<header>{segments.threadId}</header>
{children}
</div>
Authorize the layout
Authorize the layout (if required).
importfrom 'next/navigation'
importfrom 'decoders'
importfrom '@sugardarius/anzen/server-components'
importfrom '~/lib/auth'
export default createSafeLayoutServerComponent
'thread-layout',
authorize: async=>
const = await auth
if!session.user) {
unauthorized
return
async=>
// ^^^^ Inferred from authorize
return
<div>
<header>
{auth.user.name}{segments.threadId}
</header>
{children}
</div>
Last updated on