From 1ee64245c9ada41def30671091bc69541cb51b95 Mon Sep 17 00:00:00 2001 From: unnoq Date: Sun, 29 Dec 2024 20:01:21 +0700 Subject: [PATCH] client context + dynamic link docs --- apps/content/content/docs/client/vanilla.mdx | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/apps/content/content/docs/client/vanilla.mdx b/apps/content/content/docs/client/vanilla.mdx index cf99c921..7c77cb26 100644 --- a/apps/content/content/docs/client/vanilla.mdx +++ b/apps/content/content/docs/client/vanilla.mdx @@ -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({ + 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(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((path, input, options) => { // can be async + // const clientContext = options.context + if (path.includes('post')) { + return orpcLink1 + } + + return orpcLink2 +}) + +const client = createORPCClient(dynamicLink) +``` \ No newline at end of file