github unnoq/orpc v1.3.0

latest releases: v1.8.6, v1.8.5, v1.8.4...
3 months ago

Message Port Adapter

oRPC now supports common message port implementations for communication between windows, tabs, processes, and more. We also provide integration guides for Electron, Browser Extensions, and Worker Threads.

import { MessageChannel, Worker } from 'node:worker_threads'
import { experimental_RPCLink as RPCLink } from '@orpc/client/message-port'

const { port1: clientPort, port2: serverPort } = new MessageChannel()

const worker = new Worker('some-worker.js')

worker.postMessage(serverPort, [serverPort])

const link = new RPCLink({
  port: clientPort
})

Implementing oRPC Contracts in NestJS

You can now implement oRPC contracts directly within a NestJS controller:

import { Implement, implement, ORPCError } from '@orpc/nest'

@Controller()
export class PlanetController {
  /**
   * Implements a standalone procedure
   */
  @Implement(contract.planet.list)
  list() {
    return implement(contract.planet.list).handler(({ input }) => {
      // Add your logic here
      return []
    })
  }
}

Support for Multiple Success Responses

You can now define multiple success responses using the "detailed" structure:

const multipleStatus = os
  .route({ outputStructure: 'detailed' })
  .output(z.union([
    z.object({
      status: z.literal(201).describe('Record created'),
      body: z.string(),
    }),
    z.object({
      status: z.literal(200).describe('Record updated'),
      body: z.string(),
    }),
  ]))
  .handler(async ({ input }) => {
    if (something) {
      return {
        status: 201,
        body: 'created',
      }
    }

    return {
      status: 200,
      body: 'updated',
    }
  })

TanStack Streamed Query Support

You can now consume oRPC event iterators in TanStack Query using the new .streamedOptions:

const query = useQuery(orpc.streamed.experimental_streamedOptions({
  input: { id: 123 }
}))

parseFormData and getIssueMessage Utilities

Interact with form data more easily using the new utility functions:

import { getIssueMessage, parseFormData } from '@orpc/react'

export function MyComponent() {
  const { execute, data, error, status } = useServerAction(someAction)

  return (
    <form action={(form) => { execute(parseFormData(form)) }}>
      <label>
        Name:
        <input name="user[name]" type="text" />
        <span>{getIssueMessage(error, 'user[name]')}</span>
      </label>

      <label>
        Age:
        <input name="user[age]" type="number" />
        <span>{getIssueMessage(error, 'user[age]')}</span>
      </label>

      <label>
        Images:
        <input name="images[]" type="file" multiple />
        <span>{getIssueMessage(error, 'images[]')}</span>
      </label>

      <button disabled={status === 'pending'}>
        Submit
      </button>
    </form>
  )
}

   🚀 Features

   🐞 Bug Fixes

  • deps: Update dependency type-fest to v4.41.0  -  in #475 (4d7bc)
  • query: Improve compatibility with useQueries and useSuspenseQueries  -  by @unnoq in #521 (ba44c)
  • standard-server: Unify empty body parsing behavior across envs  -  by @unnoq in #533 (5a89d)
    View changes on GitHub

Don't miss a new orpc release

NewReleases is sending notifications on new releases.