🚨 Breaking Change Server Action
🚨
The Server Action has been completely remake and you need upgrade your code to reflect new changes
Tuple return
'use client'
// before
const data = await ping({ name })
// now
const [error, data] = await ping({ name })
Support Typesafe error handling
'use client'
const [error, data] = await someAction({ name: 'John' })
if (error) {
if (error.defined) {
console.log(error.data)
// ^ Typed error data
}
// Handle unknown errors
}
else {
// Handle success
console.log(data)
}
useServerAction
Hook
The useServerAction
hook simplifies invoking server actions in React.
const { execute, data, error, status } = useServerAction(someAction, {
interceptors: [
onError((error) => {
if (isDefinedError(error)) {
console.error(error.data)
// ^ Typed error data
}
}),
],
})
createFormAction
Utility
The createFormAction
utility accepts a procedure and returns a function to handle form submissions. It uses Bracket Notation to deserialize form data.
import { createFormAction } from '@orpc/react'
const dosomething = os
.input(
z.object({
user: z.object({
name: z.string(),
age: z.coerce.number(),
}),
})
)
.handler(({ input }) => {
console.log('Form action called!')
console.log(input)
})
export const redirectSomeWhereForm = createFormAction(dosomething, {
interceptors: [
onSuccess(async () => {
redirect('/some-where')
}),
],
})
export function MyComponent() {
return (
<form action={redirectSomeWhereForm}>
<input type="text" name="user[name]" required />
<input type="number" name="user[age]" required />
<button type="submit">Submit</button>
</form>
)
}
🚀 Features
OpenAPILink
OpenAPILink enables communication with an OpenAPIHandler on your server using HTTP/Fetch.
const link = new OpenAPILink(contract, {
url: 'http://localhost:3000/api',
headers: () => ({
'x-api-key': 'my-api-key',
}),
// fetch: <-- polyfill fetch if needed
})
Support 3xx response
By combining the successStatus
and outputStructure
options, you can return a standard HTTP redirect response.
const redirect = os
.route({
method: 'GET',
path: '/redirect',
successStatus: 307,
outputStructure: 'detailed'
})
.handler(async () => {
return {
headers: {
location: 'https://orpc.unnoq.com',
},
}
})
And many other improvements
🚨 Breaking Changes
- client:
- server:
- server, react:
- standard-server:
🚀 Features
- client, contract, openapi: Support 3xx response - by @unnoq in #304 (30c0e)
- client, server: Allow numbers and symbols as context keys - by @unnoq in #312 (10ace)
- openapi: OpenAPILink - by @unnoq in #303 (a2467)
- server: Support config
ThrowableError
- by @unnoq in #311 (d1f8e)