Kay
Kay
TtRPC
Created by Kay on 12/23/2023 in #❓-help
ReferenceError: Cannot access 'appRouter' before initialization
import { httpBatchLink } from "@trpc/client";

import { appRouter } from "@/server";
import { ServerConfig } from "@/config/server";

export const TrpcClientServer = appRouter.createCaller({
links: [
httpBatchLink({
url: `${ServerConfig.NEXTAUTH_URL}/api/trpc`,
}),
],
});
import { httpBatchLink } from "@trpc/client";

import { appRouter } from "@/server";
import { ServerConfig } from "@/config/server";

export const TrpcClientServer = appRouter.createCaller({
links: [
httpBatchLink({
url: `${ServerConfig.NEXTAUTH_URL}/api/trpc`,
}),
],
});
6 replies
TtRPC
Created by Kay on 12/23/2023 in #❓-help
ReferenceError: Cannot access 'appRouter' before initialization
If i understand this correctly it should be setting a context for trpc. The context is the session object from the nextauth function getServerSession
6 replies
TtRPC
Created by Kay on 12/23/2023 in #❓-help
ReferenceError: Cannot access 'appRouter' before initialization
import { TRPCError, initTRPC } from "@trpc/server";
import superjson from "superjson";
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
import { getServerAuthSession } from "./auth";
import { Session } from "next-auth";

type CreateContextOptions = {
session: Session | null;
};

const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
//prisma,
};
};

export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({
session,
});
};

const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
});

const isAuthed = t.middleware(({ ctx, next }: any) => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
});

export const router = t.router;
export const protectedProcedure = t.procedure.use(isAuthed);
export const publicProcedure = t.procedure;
import { TRPCError, initTRPC } from "@trpc/server";
import superjson from "superjson";
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
import { getServerAuthSession } from "./auth";
import { Session } from "next-auth";

type CreateContextOptions = {
session: Session | null;
};

const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
//prisma,
};
};

export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({
session,
});
};

const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
});

const isAuthed = t.middleware(({ ctx, next }: any) => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
});

export const router = t.router;
export const protectedProcedure = t.procedure.use(isAuthed);
export const publicProcedure = t.procedure;
6 replies
TtRPC
Created by Kay on 12/14/2023 in #❓-help
Trpc calls being uncessarily made
im still experiecin a refetch whenever i swap tabs and tab back in
7 replies
TtRPC
Created by Kay on 12/14/2023 in #❓-help
Trpc calls being uncessarily made
i wonde what other quirks like this exists
7 replies
TtRPC
Created by Kay on 12/14/2023 in #❓-help
Trpc calls being uncessarily made
So i think this might havebeen because of "refetchOnWindowFocus"
7 replies