♻️ Move trpc client code to app (#906)

This commit is contained in:
Luke Vella 2023-10-20 15:46:02 +01:00 committed by GitHub
parent 502f2a7a43
commit 5be17fd249
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 133 additions and 124 deletions

View 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>;