import { observable } from '@trpc/server/observable'; import type { AnyRouter } from '@trpc/server/unstable-core-do-not-import'; import { createChain } from './internals/createChain'; import type { Operation, TRPCLink } from './types'; function asArray(value: TType | TType[]) { return Array.isArray(value) ? value : [value]; } export function splitLink(opts: { condition: (op: Operation) => boolean; /** * The link to execute next if the test function returns `true`. */ true: TRPCLink | TRPCLink[]; /** * The link to execute next if the test function returns `false`. */ false: TRPCLink | TRPCLink[]; }): TRPCLink { return (runtime) => { const yes = asArray(opts.true).map((link) => link(runtime)); const no = asArray(opts.false).map((link) => link(runtime)); return (props) => { return observable((observer) => { const links = opts.condition(props.op) ? yes : no; return createChain({ op: props.op, links }).subscribe(observer); }); }; }; }