Vilian
Vilian
TtRPC
Created by Vilian on 8/4/2024 in #❓-help
How to query with async/await
I'll try this
12 replies
TtRPC
Created by Vilian on 8/4/2024 in #❓-help
How to query with async/await
I haven't really thought about doing it like this
12 replies
TtRPC
Created by Vilian on 8/4/2024 in #❓-help
How to query with async/await
This looks interesting
12 replies
TtRPC
Created by Vilian on 8/4/2024 in #❓-help
How to query with async/await
So what would be the next prefered method ?
12 replies
TtRPC
Created by Vilian on 8/4/2024 in #❓-help
How to query with async/await
I take it that you aren't actually supposed to use the query client like that outside of component
12 replies
TtRPC
Created by nitestack on 8/26/2023 in #❓-help
Next.js app router + TRPC...how to set it up?
This doesn't showcase how to implement context
7 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
I'm trying to understand how the whole structure maps out and works under the hood.
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Well nothing so far. I'm very new to react space and i've been meaning to setup myself a nextjs app router project template to use
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Do you have an example/snippet you could link me to see?
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
But as we know, this means top level await... which is not supported afaik.
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Well there were some example with appRouter.createCaller(await createContext())
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
What if it's used with appRouter.createCaller?
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Should the createContext run on every single request made to trpc ?
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Last question: Are you sure what i sent above is a bug?
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
Hey @sachinraja sorry for the ping, however, do you guys have an example tRPC with react 13 app router ?
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
I'll try to
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
I'm pretty sure the createTRPCContext should be invoked on every request/batch of requests
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
When i invoke any trpc endpoint with an http request, the request goes through, but the context does not run.
24 replies
TtRPC
Created by Vilian on 8/24/2023 in #❓-help
nextjs app router `fetchRequestHandler`'s createContext doesn't run.
// /src/trpc/trpc.ts
import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson';
import { ZodError } from 'zod';
import { getServerAuthSession } from '~/auth/auth';

export const createTRPCContext = async () => {
console.log('Context');
return await getServerAuthSession();
};

const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter({ shape, error }) {
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
},
};
},
});

export const createTRPCRouter = t.router;

const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
console.log(ctx);
if (!ctx.session?.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
});

export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
// /src/trpc/trpc.ts
import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson';
import { ZodError } from 'zod';
import { getServerAuthSession } from '~/auth/auth';

export const createTRPCContext = async () => {
console.log('Context');
return await getServerAuthSession();
};

const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter({ shape, error }) {
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
},
};
},
});

export const createTRPCRouter = t.router;

const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
console.log(ctx);
if (!ctx.session?.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
});

export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
24 replies
TtRPC
Created by Vilian on 4/1/2023 in #❓-help
context question
import type { IncomingMessage, ServerResponse } from 'node:http';
import type { inferAsyncReturnType } from '@trpc/server';
import type { NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http';


export const createContext = async ({
req,
res
}: NodeHTTPCreateContextFnOptions<IncomingMessage, ServerResponse>) => {
// Stuff...

return {};
};

export type Context = inferAsyncReturnType<typeof createContext>;
import type { IncomingMessage, ServerResponse } from 'node:http';
import type { inferAsyncReturnType } from '@trpc/server';
import type { NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http';


export const createContext = async ({
req,
res
}: NodeHTTPCreateContextFnOptions<IncomingMessage, ServerResponse>) => {
// Stuff...

return {};
};

export type Context = inferAsyncReturnType<typeof createContext>;
51 replies