IceAge2OnDVD
IceAge2OnDVD
Explore posts from servers
TtRPC
Created by IceAge2OnDVD on 9/8/2023 in #❓-help
Next cookies() not being set on Mutation in App Dir (T3 Turbo)
Trying to set cookies inside TRPC using Nextjs App Router, using the T3 Turbo setup. Setting cookies works fine inside Queries, but cookies are not set inside Mutations. Maybe I'm doing something wrong, not very up to date with TRPC + App Dir, or it could be a bug in next, not sure.
export const postRouter = createTRPCRouter({
all: publicProcedure.query(({ ctx }) => {
cookies().set("Cookies work on QUERY", "yes")
return [{id:0,
title: "query test",
content: "a test for trpc query",
createdAt: Date.now(),
updatedAt: Date.now()}]
}),

create: publicProcedure
.input(
z.object({
title: z.string().min(1),
content: z.string().min(1),
}),
)
.mutation(({ ctx, input }) => {
cookies().set("Cookies work on MUTATION", "No!")
console.log(cookies().getAll())
return 200;
}),
});
export const postRouter = createTRPCRouter({
all: publicProcedure.query(({ ctx }) => {
cookies().set("Cookies work on QUERY", "yes")
return [{id:0,
title: "query test",
content: "a test for trpc query",
createdAt: Date.now(),
updatedAt: Date.now()}]
}),

create: publicProcedure
.input(
z.object({
title: z.string().min(1),
content: z.string().min(1),
}),
)
.mutation(({ ctx, input }) => {
cookies().set("Cookies work on MUTATION", "No!")
console.log(cookies().getAll())
return 200;
}),
});
After calling both the query and the mutation on frontend you only get the cookie from the Query. But this gets logged to console.
@acme/nextjs:dev: [
@acme/nextjs:dev: { name: 'Cookies work on QUERY', value: 'yes', path: '/' },
@acme/nextjs:dev: { name: 'Cookies work on MUTATION', value: 'No!', path: '/' }
@acme/nextjs:dev: ]
@acme/nextjs:dev: [
@acme/nextjs:dev: { name: 'Cookies work on QUERY', value: 'yes', path: '/' },
@acme/nextjs:dev: { name: 'Cookies work on MUTATION', value: 'No!', path: '/' }
@acme/nextjs:dev: ]
(this is after calling the query first then the mutation.)
19 replies
DTDrizzle Team
Created by IceAge2OnDVD on 7/5/2023 in #help
Transaction rollback error catching
I'm using drizzle with TRPC and want my api to give detailed reasons for a rollback. However tx.rollback() throws a TransactionRollbackError, which means the TRPC error is never thrown (i think). This means the client sees an error with message "Rollback", which isn't super descriptive.
await db.transaction(async (tx) => {
...some logic
if (logicFails) {
tx.rollback();
throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
}
});
await db.transaction(async (tx) => {
...some logic
if (logicFails) {
tx.rollback();
throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
}
});
Is it safe to catch the drizzle error inside of the transaction and throw a TRPC one instead? Im guessing so but just wanted to check.
await db.transaction(async (tx) => {
...some logic
if (logicFails) {
try {
tx.rollback();
} catch (e) { };
throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
}
});
await db.transaction(async (tx) => {
...some logic
if (logicFails) {
try {
tx.rollback();
} catch (e) { };
throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
}
});
4 replies
TTCTheo's Typesafe Cult
Created by IceAge2OnDVD on 6/12/2023 in #questions
Whats going on with these type errors? (Challenge, TSPlayground Included)
No mater how hard I try I can't seem to fix these Typescript errors. Tbh I'm not much of a typescript wizard and I've mostly been guessing at the types, any pointers in the right direction would be greatly appreciated. I seem to fix one error and three more pop up 🤦‍♂️ Playground (Test-cases & example included): https://tsplay.dev/Wzxn2N The aim of the function is to turn an array of objects with a sibling relationship to one with a parent-child relationship. The example input & output in the TSPlayground should make it clearer. Currently the function works, its just the types are strange.
// This
[
{
keyA: objA,
keyB: objB
},
{
keyA: objA,
keyB: objC
},
{
keyA: objA,
keyB: objD
},
]
// To
[
{
...objA,
allKeyBs: [objB, objC, objD]
}
]
// This
[
{
keyA: objA,
keyB: objB
},
{
keyA: objA,
keyB: objC
},
{
keyA: objA,
keyB: objD
},
]
// To
[
{
...objA,
allKeyBs: [objB, objC, objD]
}
]
13 replies