mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-01 15:39:03 +02:00
♻️ Move trpc client code to app (#906)
This commit is contained in:
parent
502f2a7a43
commit
5be17fd249
34 changed files with 133 additions and 124 deletions
|
@ -1 +1 @@
|
|||
export * from "./next/trpc/client";
|
||||
export * from "./trpc/types";
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
import { MutationCache } from "@tanstack/react-query";
|
||||
import { httpBatchLink } from "@trpc/client";
|
||||
import { createTRPCNext } from "@trpc/next";
|
||||
import toast from "react-hot-toast";
|
||||
import superjson from "superjson";
|
||||
|
||||
import { AppRouter } from "../../trpc/routers";
|
||||
|
||||
export * from "../../trpc/types";
|
||||
|
||||
export const trpc = createTRPCNext<AppRouter>({
|
||||
config() {
|
||||
return {
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `/api/trpc`,
|
||||
}),
|
||||
],
|
||||
transformer: superjson,
|
||||
queryClientConfig: {
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: false,
|
||||
networkMode: "always",
|
||||
cacheTime: Infinity,
|
||||
staleTime: 1000 * 60,
|
||||
},
|
||||
},
|
||||
mutationCache: new MutationCache({
|
||||
onError: () => {
|
||||
toast.error(
|
||||
"Uh oh! Something went wrong. The issue has been logged and we'll fix it as soon as possible. Please try again later.",
|
||||
);
|
||||
},
|
||||
}),
|
||||
},
|
||||
};
|
||||
},
|
||||
ssr: false,
|
||||
});
|
|
@ -1,27 +0,0 @@
|
|||
import { EmailClient } from "@rallly/emails";
|
||||
import * as trpcNext from "@trpc/server/adapters/next";
|
||||
|
||||
import { appRouter } from "../../trpc/routers";
|
||||
|
||||
export interface TRPCContext {
|
||||
user: { id: string; isGuest: boolean };
|
||||
emailClient: EmailClient;
|
||||
isSelfHosted: boolean;
|
||||
isEmailBlocked?: (email: string) => boolean;
|
||||
/**
|
||||
* Takes a relative path and returns an absolute URL to the app
|
||||
* @param path
|
||||
* @returns absolute URL
|
||||
*/
|
||||
absoluteUrl: (path?: string) => string;
|
||||
shortUrl: (path?: string) => string;
|
||||
}
|
||||
|
||||
export const trpcNextApiHandler = (context: TRPCContext) => {
|
||||
return trpcNext.createNextApiHandler({
|
||||
router: appRouter,
|
||||
createContext: async () => {
|
||||
return context;
|
||||
},
|
||||
});
|
||||
};
|
|
@ -12,15 +12,10 @@
|
|||
"@rallly/database": "*",
|
||||
"@rallly/emails": "*",
|
||||
"@rallly/utils": "*",
|
||||
"@tanstack/react-query": "^4.22.0",
|
||||
"@trpc/client": "^10.13.0",
|
||||
"@trpc/next": "^10.13.0",
|
||||
"@trpc/react-query": "^10.13.0",
|
||||
"@trpc/server": "^10.13.0",
|
||||
"iron-session": "^6.3.1",
|
||||
"spacetime": "^7.4.7",
|
||||
"stripe": "^13.2.0",
|
||||
"superjson": "^1.12.2",
|
||||
"timezone-soft": "^1.4.1"
|
||||
}
|
||||
}
|
||||
|
|
43
packages/backend/trpc/context.ts
Normal file
43
packages/backend/trpc/context.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import { EmailClient } from "@rallly/emails";
|
||||
import { inferAsyncReturnType, TRPCError } from "@trpc/server";
|
||||
import { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||
|
||||
export type GetUserFn = (opts: CreateNextContextOptions) => Promise<{
|
||||
id: string;
|
||||
isGuest: boolean;
|
||||
} | null>;
|
||||
|
||||
export interface TRPCContextParams {
|
||||
getUser: GetUserFn;
|
||||
emailClient: EmailClient;
|
||||
isSelfHosted: boolean;
|
||||
isEmailBlocked?: (email: string) => boolean;
|
||||
/**
|
||||
* Takes a relative path and returns an absolute URL to the app
|
||||
* @param path
|
||||
* @returns absolute URL
|
||||
*/
|
||||
absoluteUrl: (path?: string) => string;
|
||||
shortUrl: (path?: string) => string;
|
||||
}
|
||||
|
||||
export const createTRPCContext = async (
|
||||
opts: CreateNextContextOptions,
|
||||
{ getUser, ...params }: TRPCContextParams,
|
||||
) => {
|
||||
const user = await getUser(opts);
|
||||
|
||||
if (!user) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Request has no session",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
...params,
|
||||
};
|
||||
};
|
||||
|
||||
export type TRPCContext = inferAsyncReturnType<typeof createTRPCContext>;
|
|
@ -1,8 +1,8 @@
|
|||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
|
||||
import { TRPCContext } from "../next/trpc/server";
|
||||
import { getSubscriptionStatus } from "../utils/auth";
|
||||
import { TRPCContext } from "./context";
|
||||
|
||||
const t = initTRPC.context<TRPCContext>().create({
|
||||
transformer: superjson,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue