Search code examples
typescriptbunelysiajs

How do I set the POST response body in Bun and Elysia?


I am trying to change the response body to a POST route in Elysia, but it appears that CTX only has the request and I can't find anything on modifying the response. My use case is I want to give the user a task ID after they have uploaded a video file for processing ( which may take a long time ) so I start the processing in the background and give the user a task-ID.

app.post("/upload", async (ctx: any) => {
    const taskId = uuidv4(); // generate taskId first
    console.log(ctx)
    try {
        const f = ctx.body.video;
        if (!f) {
            ctx.status = 400;
            ctx.body = { error: 'No video file provided' };
            return;
        }

        const fileName = `${taskId}.mp4`;
        const fileNameOut = `${taskId}.webm`;
        const filePath = `${uploadDir}/${fileName}`;
        const outputPath = `${outputDir}/${fileNameOut}`;

        await Bun.write(Bun.file(filePath), f);

        // Start the asynchronous video processing
        // processVideo(taskId, filePath, outputPath); 

    } catch (error) {
        console.error('Error in /upload route:', error);
        ctx.status = 500;
        ctx.body = { error: 'Internal Server Error' };
    }
    
    // Always respond with the taskId even when an error occurs
    if (ctx) {
        ctx.status = 200;
        ctx.body = { taskId: taskId }; // give the user their video task id
    } else {
        console.log('Context (ctx) is not defined correctly');
    }
});

changing ctx.body works and changes the body of the req to the right taskID and the response gives a 200 code but the body is empty.

ctx.response is undefined and I get a 'Error: Error: Network response was not ok.' from my test script.

Am I missing something, I have really racked my brain and the Elysia docs on what im missing but I can't find it.

I tried going through all of the docs, I tried using express and multer but there is currently a bug with bun 1.2 and multer that causes it to hang. I just need to get the response of the post request to return a task ID if its started succesfully.


Solution

  • Ok I figured it out - you actually have to return the a value... duh anyways here is the adjusted code

    app.post("/upload", async (ctx:any) => {
        console.log(ctx)
        try {
          const f = ctx.body.video;
          if (!f) {
              return { error: 'No video file provided' };
          }
      
          const taskId = uuidv4();
          const fileName = `${taskId}.mp4`;
          const fileNameOut = `${taskId}.webm`;
          const filePath = `${uploadDir}/${fileName}`;
          const outputPath = `${outputDir}/${fileNameOut}`;
      
          await Bun.write(Bun.file(filePath), f);
      
          // Start the asynchronous video processing
          processVideo(taskId, filePath, outputPath);
      
          // Immediately respond with the taskId
          return { taskId };
        } catch (error) {
            console.error('Error in /upload route:', error);
            return { error: 'Internal Server Error' };
        }
      });