Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

1.7 KB
Raw Blame
import type { AnyRouter } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import { TRPCClientError, type TRPCLink } from "@trpc/client";

type MemoryLinkOptions<TemplateRouter extends AnyRouter> = {
  appRouter: TemplateRouter;
  createContext?: () => TemplateRouter["_def"]["_config"]["$types"]["ctx"];
};

/**
 * https://github.com/trpc/trpc/issues/3335
 *
 * createCaller and createTRPCProxyClient provides different interfaces,
 * here we provide callerLink which can be used as a [trpc client link](https://trpc.io/docs/links)
 * Allowing us to call router api without http but through createTRPCProxyClient interface
 * See trpc-caller-link.test.ts for details
 **/
export const callerLink = <TemplateRouter extends AnyRouter>(
  opts: MemoryLinkOptions<TemplateRouter>
): TRPCLink<TemplateRouter> => {
  const { appRouter, createContext } = opts;

  return (_runtime) =>
    ({ op }) =>
      observable((observer) => {
        const caller = appRouter.createCaller(createContext?.() ?? {});
        const { path, input } = op;

        const paths = path.split(".");

        let localCaller = caller as unknown as (
          arg: unknown
        ) => Promise<unknown>;

        for (const functionName of paths) {
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          localCaller = localCaller[functionName];
        }

        const promise = localCaller(input);

        promise
          .then((data) => {
            observer.next({
              result: { data },
            });
            observer.complete();
          })
          .catch((error) => observer.error(TRPCClientError.from(error)));

        return () => {
          // nothing to cancel
        };
      });
};