Accessing req & res in Trpc + Next.js

Total beginner question. Would love to move my next.js project over to trpc, but am having problems getting started. How would I access req & res objects in NextJS with trpc? My current code / function starts like this:
export default async function createPaymentIntent(req, res) {
const { product, email, name, coupon } = req.body;
//...
}
export default async function createPaymentIntent(req, res) {
const { product, email, name, coupon } = req.body;
//...
}
Now, I'm trying to do the same thing, but with trpc:
export const paymentRouter = createRouter().query(
"stripe.create_payment_intent",
{
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ ctx }) {
console.log(ctx.req); ??

return {
greeting: `Creating Intent`,
};
},
}
);
export const paymentRouter = createRouter().query(
"stripe.create_payment_intent",
{
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ ctx }) {
console.log(ctx.req); ??

return {
greeting: `Creating Intent`,
};
},
}
);
7 Replies
cje
cje3y ago
you get data from input and return data in the return statement if you need req or res for something else, you can go into the function where context is created and pass them there but i'd recommend to try without first
resolve: ({ ctx, input }) => {
return `hello, ${input.text}`;
}
resolve: ({ ctx, input }) => {
return `hello, ${input.text}`;
}
scatter
scatter3y ago
yeah, tRPC is a different paradigm from REST, if you think you need req/res you're probably doing something wrong
plyglt
plygltOP3y ago
Oh damn, I didn't know that it's totally different in this regard
cje
cje3y ago
you might need it occasionally if you're doing some weird stuff with headers or something have you read the docs at all?
cje
cje3y ago
Quickstart | tRPC
We highly encourage you to check out the Example Apps to get a feel of tRPC and getting up & running as seamless as possible.
cje
cje3y ago
read through this
plyglt
plygltOP3y ago
thanks
Want results from more Discord servers?
Add your server