Get started
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 'decoders'
importfrom '@sugardarius/anzen'
export const createThread = createSafeServerAction
'create-thread-action',
asyncid=>
return
Validate the input
Validate your input.
importfrom 'decoders'
importfrom '@sugardarius/anzen'
export const createThread = createSafeServerAction
'create-thread-action',
object
asyncid, input=>
// ^^^^^ 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 createThread = createSafeServerAction
'create-thread-action',
object
authorize: async=>
const session = await auth
if!session.user) {
throw new Error'user is not authenticated'
return
asyncid, input, auth=>
// ^^^^ 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={asyncformData) =>
const result = await createThread
log
input id='title' name='title'
button type='submit'>Create Thread</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 POST = createSafeRouteHandler
'thread-route',
asyncid=>
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 POST = createSafeRouteHandler
'thread-route',
object
asyncid, body=>
// ^^^^ 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 POST = createSafeRouteHandler
'thread-route',
object
authorize: async=>
const session = await auth
if!session.user) {
return new Responsenull, { status: 401
return
asyncid, body, auth=>
// ^^^^ 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',
asyncid=>
returndiv>Page {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
asyncid, segments, searchParams=>
// ^^^^^^^^ ^^^^^^^^^^^^^ Inferred from validation
return
div>
?? '—'}
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 session = await auth
if!session.user) {
unauthorized
return
asyncid, segments, searchParams, auth=>
// ^^^^ Inferred from authorize
return
div>
?? '—'})
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',
asyncchildren=>
returndiv 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',
asyncid, segments, children=>
// ^^^^^^^^ Inferred from segments validation
return
div>
header>Thread {segments.threadId}</header>
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 session = await auth
if!session.user) {
unauthorized
return
asyncid, segments, auth, children=>
// ^^^^ Inferred from authorize
return
div>
header>
header>
div>
Last updated on