-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import "server-only"; | ||
|
||
import { cache } from "react"; | ||
import { cookies, headers } from "next/headers"; | ||
import { NextRequest } from "next/server"; | ||
import { getAuth } from "@clerk/nextjs/server"; | ||
import { | ||
createTRPCProxyClient, | ||
loggerLink, | ||
TRPCClientError, | ||
} from "@trpc/client"; | ||
import { callProcedure } from "@trpc/server"; | ||
import { observable } from "@trpc/server/observable"; | ||
import type { TRPCErrorResponse } from "@trpc/server/rpc"; | ||
import SuperJSON from "superjson"; | ||
|
||
import type { AppRouter } from "@vivat/api"; | ||
import { appRouter, createTRPCContext } from "@vivat/api"; | ||
|
||
/** | ||
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when | ||
* handling a tRPC call from a React Server Component. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
const createContext = cache(async () => { | ||
return createTRPCContext({ | ||
headers: new Headers({ | ||
cookie: cookies().toString(), | ||
"x-trpc-source": "rsc", | ||
}), | ||
auth: getAuth( | ||
new NextRequest("https://notused.com", { headers: headers() }), | ||
), | ||
}); | ||
}); | ||
|
||
export const api = createTRPCProxyClient<AppRouter>({ | ||
transformer: SuperJSON, | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => | ||
process.env.NODE_ENV === "development" || | ||
(op.direction === "down" && op.result instanceof Error), | ||
}), | ||
/** | ||
* Custom RSC link that lets us invoke procedures without using http requests. Since Server | ||
* Components always run on the server, we can just call the procedure as a function. | ||
*/ | ||
() => | ||
({ op }) => | ||
observable((observer) => { | ||
createContext() | ||
.then((ctx) => { | ||
return callProcedure({ | ||
procedures: appRouter._def.procedures, | ||
path: op.path, | ||
getRawInput: () => Promise.resolve(op.input), | ||
ctx, | ||
type: op.type, | ||
}); | ||
}) | ||
.then((data) => { | ||
observer.next({ result: { data } }); | ||
observer.complete(); | ||
}) | ||
.catch((cause: TRPCErrorResponse) => { | ||
observer.error(TRPCClientError.from(cause)); | ||
}); | ||
}), | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters