Neon
Neon6mo ago

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 🙂
GitHub
trpc/examples/.experimental/next-formdata/src/server/routers/room.t...
🧙‍♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
4 Replies
DxD
DxD6mo ago
so u want to test your trpc with unit testing ? @Neon
Neon
Neon6mo ago
yep exactly
DxD
DxD6mo ago
Find a way to do it ? @Neon
Neon
Neon5mo ago
Hello @DxD , no i stoped searching for the moment, i will probably try to find a way later, its not very critical for now.
More Posts
Ability to mutate/extend `input` from middlewaresHi I have a global middleware that should run for every route. This middleware simply coerces `''` (Hot to redirect in middleware when using createCallerFactoryHello. I do have this tiny middleware: ```typescript const authMiddleware = middleware(async (opts) Remix: How can I return headers from a query or mutation back to the action/loader?I'm using Supabase Auth and a requirement is to return the headers back from its client creation funUsing output validator breaks query return type inference on react queryHi! I am not sure whether this is expected and whether there is some way to fix that but here it goeHow to pass headers from serverActions in Next.js App router.I'm using tRPC with Clerk auth provider in Next.js App directory. I created a `serverClient` to use Need Guidance Optimizing Multi-Select Typeahead### What your environment is: Node 18? Bun? pnpm / yarn / npm? - Bun v1.0.23 - Next.js v14.1.0 (app Private API, public clientsHey everyone, we're about to OSS some good stuff at my company and I'm wondering what's the best appHow to get the type of context after middleware context extension?I have a middleware where I validate and set a userId. My router knows that userId is no longer undewhat am i doing wrong?My turbo repo containing expo-clerk-trpc does NOT work. and I've tried everything I have no idea whaReusable Component to take router as props / dependency injection. How to type?for example: ```ts export const Content = async ({ api }) => { const items = await api.getAll.quer