Neon
Neon
TtRPC
Created by Neon on 2/23/2024 in #❓-help
Infinite query get direction info
Hello everyone, I'm implementing a bidirectional infinite query and i would need to access the direction param into my trpc endpoint: https://tanstack.com/query/v5/docs/framework/react/guides/query-functions#queryfunctioncontext. Is it possible to get this param passed in trpc procedure input with useInfiniteQuery? (for info i'm using trpc v11)
3 replies
TtRPC
Created by Neon on 2/9/2024 in #❓-help
Mock form data middleware (
Hello everyone, I'm using experimental_parseMultipartFormData on some of my procedures and i would like to know if some of you have found a nice way to mock the formData return from the middleware. I think this can be achieved either by mocking the middleware function with a mocked FormData resolved value (but i dont think thats possible) or by transform formData into a request body that could be correctly parsed by the middleware which use '@web3-storage/multipart-parser'. Here is my middleware code which is taken from the trpc example (https://github.com/trpc/trpc/blob/66d7db60e59b7c758709175a53765c9db0563dc0/examples/.experimental/next-formdata/src/server/routers/room.ts#L10C1-L23C1):
export const withFormdata = t.middleware(async (opts) => {
if (!opts.ctx.req || !opts.ctx.res) {
throw new Error("You are missing `req` or `res` in your call. 1");
}
if (!experimental_isMultipartFormDataRequest(opts.ctx.req)) {
return opts.next();
}

const formData = await experimental_parseMultipartFormData(
opts.ctx.req,
experimental_createMemoryUploadHandler(),
);

return opts.next({
getRawInput: async () => formData,
});
});
export const withFormdata = t.middleware(async (opts) => {
if (!opts.ctx.req || !opts.ctx.res) {
throw new Error("You are missing `req` or `res` in your call. 1");
}
if (!experimental_isMultipartFormDataRequest(opts.ctx.req)) {
return opts.next();
}

const formData = await experimental_parseMultipartFormData(
opts.ctx.req,
experimental_createMemoryUploadHandler(),
);

return opts.next({
getRawInput: async () => formData,
});
});
If someone have managed to do it, it would really help me if you explain how did you do it. Thanks you 🙂
7 replies
TtRPC
Created by Neon on 11/26/2022 in #❓-help
Throw custom TRPCError with specific cause
Hello everyone ! I'm trying to throw a custom TRPCError with a specific cause that i can differentiate from other trpc errors. My use case is to be able to catch error on client and display an error message if the error code is 'BAD_REQUEST' and a specific cause. After investigating i find that something like this should work in my mutation procedure:

throw new TRPCError({
message: 'Password changed too recently',
code: 'BAD_REQUEST',
cause: { name: 'VALIDATION_ERROR', message: "You can only modify your password once a day" },
});

throw new TRPCError({
message: 'Password changed too recently',
code: 'BAD_REQUEST',
cause: { name: 'VALIDATION_ERROR', message: "You can only modify your password once a day" },
});
But in my errorFormatter i only receive this object
{"code":"BAD_REQUEST","name":"TRPCError"}
{"code":"BAD_REQUEST","name":"TRPCError"}
cause field is not present. Can someone enlighten me on how to solve this problem ? Thanks 🙂
4 replies