Skip to content

Commit

Permalink
update-trpc
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Jan 3, 2024
1 parent 5070e00 commit b53e6f1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 99 deletions.
6 changes: 3 additions & 3 deletions apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@expo/metro-config": "^0.10.7",
"@shopify/flash-list": "1.4.3",
"@tanstack/react-query": "^5.8.7",
"@trpc/client": "next",
"@trpc/react-query": "next",
"@trpc/server": "next",
"@trpc/client": "12-28-caller-accept-fn",
"@trpc/react-query": "12-28-caller-accept-fn",
"@trpc/server": "12-28-caller-accept-fn",
"expo": "^49.0.21",
"expo-constants": "~14.4.2",
"expo-linking": "~5.0.2",
Expand Down
8 changes: 4 additions & 4 deletions apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"@tanstack/react-query": "^5.8.7",
"@tanstack/react-query-devtools": "^5.8.7",
"@tanstack/react-query-next-experimental": "5.8.7",
"@trpc/client": "next",
"@trpc/next": "next",
"@trpc/react-query": "next",
"@trpc/server": "next",
"@trpc/client": "12-28-caller-accept-fn",
"@trpc/next": "12-28-caller-accept-fn",
"@trpc/react-query": "12-28-caller-accept-fn",
"@trpc/server": "12-28-caller-accept-fn",
"geist": "^1.2.0",
"next": "^14.0.4",
"react": "18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function HomePage() {
// You don't need to fetch these here, just showing different usages
// If you don't want the Suspense loading state, you could pass these
// posts as props as use as initialData in the query.
const posts = await api.post.all.query();
const posts = await api.post.all();
console.log("RSC Posts:", posts);

return (
Expand Down
6 changes: 3 additions & 3 deletions apps/nextjs/src/trpc/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export function TRPCReactProvider(props: {
unstable_httpBatchStreamLink({
url: getBaseUrl() + "/api/trpc",
async headers() {
const headers = new Map(await props.headersPromise);
headers.set("x-trpc-source", "nextjs-react");
return Object.fromEntries(headers);
const heads = new Headers(await props.headersPromise);
heads.set("x-trpc-source", "nextjs-react");
return Object.fromEntries(heads);
},
}),
],
Expand Down
44 changes: 2 additions & 42 deletions apps/nextjs/src/trpc/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { TRPCErrorResponse } from "@trpc/server/rpc";
import { cache } from "react";
import { headers } from "next/headers";
import { createTRPCClient, loggerLink, TRPCClientError } from "@trpc/client";
import { callProcedure } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import SuperJSON from "superjson";

import { appRouter, createTRPCContext } from "@acme/api";
import { createCaller, createTRPCContext } from "@acme/api";
import { auth } from "@acme/auth";

/**
Expand All @@ -23,39 +18,4 @@ const createContext = cache(async () => {
});
});

export const api = createTRPCClient<typeof appRouter>({
transformer: SuperJSON,
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" ||
(op.direction === "down" && op.result instanceof Error),
}),
/**
* Custom RSC link that invokes procedures directly in the server component Don't be too afraid
* about the complexity here, it's just wrapping `callProcedure` with an observable to make it a
* valid ending link for tRPC.
*/
() =>
({ 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));
});
}),
],
});
export const api = createCaller(createContext);
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@acme/auth": "workspace:^0.1.0",
"@acme/db": "workspace:^0.1.0",
"@acme/validators": "workspace:^0.1.0",
"@trpc/client": "next",
"@trpc/server": "next",
"@trpc/client": "12-28-caller-accept-fn",
"@trpc/server": "12-28-caller-accept-fn",
"superjson": "2.2.1",
"zod": "^3.22.4"
},
Expand Down
27 changes: 21 additions & 6 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";

import type { AppRouter } from "./root";
import { appRouter } from "./root";
import { createCallerFactory, createTRPCContext } from "./trpc";

export { appRouter, type AppRouter } from "./root";
export { createTRPCContext } from "./trpc";
/**
* Create a server-side caller for the tRPC API
* @example
* const trpc = createCaller(createContext);
* const res = await trpc.post.all();
* ^? Post[]
*/
const createCaller = createCallerFactory(appRouter);

/**
* Inference helpers for input types
* @example type HelloInput = RouterInputs['example']['hello']
* @example
* type PostByIdInput = RouterInputs['post']['byId']
* ^? { id: number }
**/
export type RouterInputs = inferRouterInputs<AppRouter>;
type RouterInputs = inferRouterInputs<AppRouter>;

/**
* Inference helpers for output types
* @example type HelloOutput = RouterOutputs['example']['hello']
* @example
* type AllPostsOutput = RouterOutputs['post']['all']
* ^? Post[]
**/
export type RouterOutputs = inferRouterOutputs<AppRouter>;
type RouterOutputs = inferRouterOutputs<AppRouter>;

export { createTRPCContext, appRouter, createCaller };
export type { AppRouter, RouterInputs, RouterOutputs };
6 changes: 6 additions & 0 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const t = initTRPC.context<typeof createTRPCContext>().create({
}),
});

/**
* Create a server-side caller
* @see https://trpc.io/docs/server/server-side-calls
*/
export const createCallerFactory = t.createCallerFactory;

/**
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
*
Expand Down
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b53e6f1

Please sign in to comment.