feat(create-turbo): create https://github.com/juliusmarminge/acme-corp
This commit is contained in:
44
apps/nextjs/src/trpc/shared.ts
Normal file
44
apps/nextjs/src/trpc/shared.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { HTTPBatchLinkOptions, HTTPHeaders, TRPCLink } from "@trpc/client";
|
||||
import { httpBatchLink } from "@trpc/client";
|
||||
|
||||
import type { AppRouter } from "@acme/api";
|
||||
|
||||
export { transformer } from "@acme/api/transformer";
|
||||
|
||||
const getBaseUrl = () => {
|
||||
if (typeof window !== "undefined") return "";
|
||||
const vc = process.env.VERCEL_URL;
|
||||
if (vc) return `https://${vc}`;
|
||||
return `http://localhost:3000`;
|
||||
};
|
||||
|
||||
const lambdas = ["ingestion"];
|
||||
|
||||
export const endingLink = (opts?: {
|
||||
headers?: HTTPHeaders | (() => HTTPHeaders);
|
||||
}) =>
|
||||
((runtime) => {
|
||||
const sharedOpts = {
|
||||
headers: opts?.headers,
|
||||
} satisfies Partial<HTTPBatchLinkOptions>;
|
||||
|
||||
const edgeLink = httpBatchLink({
|
||||
...sharedOpts,
|
||||
url: `${getBaseUrl()}/api/trpc/edge`,
|
||||
})(runtime);
|
||||
const lambdaLink = httpBatchLink({
|
||||
...sharedOpts,
|
||||
url: `${getBaseUrl()}/api/trpc/lambda`,
|
||||
})(runtime);
|
||||
|
||||
return (ctx) => {
|
||||
const path = ctx.op.path.split(".") as [string, ...string[]];
|
||||
const endpoint = lambdas.includes(path[0]) ? "lambda" : "edge";
|
||||
|
||||
const newCtx = {
|
||||
...ctx,
|
||||
op: { ...ctx.op, path: path.join(".") },
|
||||
};
|
||||
return endpoint === "edge" ? edgeLink(newCtx) : lambdaLink(newCtx);
|
||||
};
|
||||
}) satisfies TRPCLink<AppRouter>;
|
||||
Reference in New Issue
Block a user