mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-29 08:46:22 +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
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>;
|
Loading…
Add table
Add a link
Reference in a new issue