Split-länk
Denna sida har översatts av PageTurner AI (beta). Inte officiellt godkänd av projektet. Hittade du ett fel? Rapportera problem →
splitLink är en länk som låter dig förgrena länkkedjans exekvering baserat på ett givet villkor. Både true- och false-grenarna krävs. Du kan ange en enda länk eller flera länkar per gren via en array.
Det är viktigt att notera att när du anger länkar för splitLink att exekvera, kommer splitLink skapa en helt ny länkkedja baserad på de länkar du skickat. Därför behöver du använda en avslutande länk om du bara anger en länk, eller lägga till den avslutande länken i slutet av arrayen om du anger flera länkar som ska exekveras på en gren. Här är en visuell representation av hur splitLink fungerar:
Användningsexempel
Inaktivera batchhantering för specifika förfrågningar
Anta att du använder httpBatchLink som avslutande länk i din tRPC-klientkonfiguration. Det innebär att request batching är aktiverat för alla förfrågningar. Men om du behöver inaktivera batchhantering endast för vissa förfrågningar, måste du dynamiskt ändra den avslutande länken i din tRPC-klientkonfiguration mellan httpLink och httpBatchLink. Detta är ett perfekt tillfälle att använda splitLink:
1. Konfigurera klienten / utils/trpc.ts
client/index.tstsimport {createTRPCClient,httpBatchLink,httpLink,splitLink,} from '@trpc/client';import type { AppRouter } from '../server';const url = `http://localhost:3000`;const client = createTRPCClient<AppRouter>({links: [splitLink({condition(op) {// check for context property `skipBatch`return Boolean(op.context.skipBatch);},// when condition is true, use normal requesttrue: httpLink({url,}),// when condition is false, use batchingfalse: httpBatchLink({url,}),}),],});
client/index.tstsimport {createTRPCClient,httpBatchLink,httpLink,splitLink,} from '@trpc/client';import type { AppRouter } from '../server';const url = `http://localhost:3000`;const client = createTRPCClient<AppRouter>({links: [splitLink({condition(op) {// check for context property `skipBatch`return Boolean(op.context.skipBatch);},// when condition is true, use normal requesttrue: httpLink({url,}),// when condition is false, use batchingfalse: httpBatchLink({url,}),}),],});
2. Utför förfrågan utan batchhantering
client.tstsconst postResult = proxy.posts.query(null, {context: {skipBatch: true,},});
client.tstsconst postResult = proxy.posts.query(null, {context: {skipBatch: true,},});
eller:
MyComponent.tsxtsxexport function MyComponent() {const postsQuery = proxy.posts.useQuery(undefined, {trpc: {context: {skipBatch: true,},}});return (<pre>{JSON.stringify(postsQuery.data ?? null, null, 4)}</pre>)})
MyComponent.tsxtsxexport function MyComponent() {const postsQuery = proxy.posts.useQuery(undefined, {trpc: {context: {skipBatch: true,},}});return (<pre>{JSON.stringify(postsQuery.data ?? null, null, 4)}</pre>)})
Alternativ för splitLink
Funktionen splitLink tar ett alternativsobjekt med tre fält: condition, true och false.
tsfunction splitLink<TRouter extends AnyRouter = AnyRouter>(opts: {condition: (op: Operation) => boolean;/*** The link to execute next if the test function returns `true`.*/true: TRPCLink<TRouter> | TRPCLink<TRouter>[];/*** The link to execute next if the test function returns `false`.*/false: TRPCLink<TRouter> | TRPCLink<TRouter>[];}) => TRPCLink<TRouter>
tsfunction splitLink<TRouter extends AnyRouter = AnyRouter>(opts: {condition: (op: Operation) => boolean;/*** The link to execute next if the test function returns `true`.*/true: TRPCLink<TRouter> | TRPCLink<TRouter>[];/*** The link to execute next if the test function returns `false`.*/false: TRPCLink<TRouter> | TRPCLink<TRouter>[];}) => TRPCLink<TRouter>
Referens
Du kan titta på källkoden för denna länk på GitHub.