Skip to content

Commit

Permalink
client context + dynamic link docs
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Dec 29, 2024
1 parent e487428 commit 1ee6424
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions apps/content/content/docs/client/vanilla.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,57 @@ const output = await client.post.create({
client.post.
// ^|
```

## Client Context

```ts twoslash
import { createORPCClient, ORPCError } from '@orpc/client'
import { ORPCLink } from '@orpc/client/fetch'
import type { router } from 'examples/server'

type ClientContext = { cache?: string, tags?: string[] } | undefined
// if context is not undefinable, it will require you pass context in every call

const orpcLink = new ORPCLink<ClientContext>({
url: 'http://localhost:3000/api',
// headers: provide additional headers
fetch: (input, init, context) => globalThis.fetch(input, {
...init,
cache: context.cache,
tags: context.tags,
}),
})

const client = createORPCClient<typeof router, ClientContext>(orpcLink)

client.getting({ name: 'unnoq' }, { context: { cache: 'force-cache' } })
```

## Dynamic Link

```ts twoslash
import { createORPCClient, ORPCError } from '@orpc/client'
import { ORPCLink } from '@orpc/client/fetch'
import type { router } from 'examples/server'

const orpcLink1 = new ORPCLink({
url: 'http://localhost:3000/api',
// headers: provide additional headers
})

const orpcLink2 = new ORPCLink({
url: 'http://localhost:8000/api',
// headers: provide additional headers
})

const dynamicLink = new DynamicLink<ClientContext>((path, input, options) => { // can be async
// const clientContext = options.context
if (path.includes('post')) {
return orpcLink1
}

return orpcLink2
})

const client = createORPCClient<typeof router>(dynamicLink)
```

0 comments on commit 1ee6424

Please sign in to comment.