oRPC is currently pre-stable, please report any issues on our Discord or GitHub 🚧
oRPC
background

Vanilla

Fully typed client for Vanilla JS

Installations

npm i @orpc/client

Usage

To create a fully typed client, you need either the type of the router you intend to use or the contract.

import { ,  } from '@orpc/client'
import {  } from '@orpc/client/fetch'
import type {  } from 'examples/server'
 
const  = new ({
  : 'http://localhost:3000/api',
  // fetch: optional override for the default fetch function
  // headers: provide additional headers
})
 
const  = <typeof  /* or contract router */>()
 
//  File upload out of the box
const  = await ..({
  : 'My Amazing Title',
  : 'This is a detailed description of my content',
  : (.('thumb') as HTMLInputElement).[0]!,
})
 
..
  • createPost
  • getPost

Client Context

The Client Context feature allows you to pass additional contextual information (like caching policies) to your client calls.

import type {  } from 'examples/server'
import { ,  } from '@orpc/client'
import {  } from '@orpc/client/fetch'
 
type  = { ?:  } | undefined
// if context is not undefinable, it will require you pass context in every call
 
const  = new <>({
  : 'http://localhost:3000/api',
  // headers: provide additional headers
  : (, , ) => .(, {
    ...,
    : ?.,
  }),
  : (, , ) => {
    // if input contain file, and you return GET, oRPC will change it to POST automatically
 
    if (?.) {
      return 'GET'
    }
 
    // or base on the path
    if (['get', 'find', 'list', 'search'].(.(-1)!)) {
      return 'GET'
    }
 
    return 'POST'
  },
})
 
const  = <typeof , >()
 
.({ : '123' }, { : { : 'force-cache' } })

Note: This works seamlessly with Vue Query and React Query.

With the Dynamic Link mechanism, you can define custom logic to dynamically choose between different links based on the request's context, path, or input.

import type {  } from 'examples/server'
import { , ,  } from '@orpc/client'
import {  } from '@orpc/client/fetch'
 
const  = new ({
  : 'http://localhost:3000/api',
  // headers: provide additional headers
})
 
const  = new ({
  : 'http://localhost:8000/api',
  // headers: provide additional headers
})
 
const  = new ((, , ) => { // can be async
  if (.('post')) {
    return 
  }
 
  return 
})
 
const  = <typeof >()

On this page