github unnoq/orpc v0.52.0

latest releases: v1.8.7, v1.8.6, v1.8.5...
5 months ago

🚨 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

   🚀 Features

   🐞 Bug Fixes

    View changes on GitHub

Don't miss a new orpc release

NewReleases is sending notifications on new releases.