TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
8.28k stars 664 forks source link

Start: createServerFn cannot return raw response objects #2779

Open nekochan0122 opened 1 week ago

nekochan0122 commented 1 week ago

Which project does this relate to?

Start

Describe the bug

Server functions should able to return Raw Response objects.

Your Example Website or App

https://github.com/nekochan0122/tanstack-start-return-response-object

Steps to Reproduce the Bug or Issue

  1. Open the dev server
  2. Click the "Trigger serverFn" button
  3. See the console output and cookies

Expected behavior

const serverFn = createServerFn({ method: "POST" }).handler(async () => {
  return new Response(JSON.stringify({ hello: "world" }), {
    status: 200,
    headers: {
      "Content-Type": "application/json",
      "Set-Cookie": "test=test",
    },
  });
});
  1. The console output is expected to contain { hello: "world" }
  2. The cookie should be set to test=test

Screenshots or Videos

No response

Platform

Additional context

No response

nekochan0122 commented 1 week ago

a temporary solution:

async function handleResponse<ResponseBody = unknown>(response: Response): Promise<ResponseBody> {
  const event = getEvent()

  setHeaders(event, Object.fromEntries(response.headers))

  switch (response.headers.get('Content-Type')) {
    case 'application/json':
      return response.json() as ResponseBody

    default:
      return response.body as ResponseBody
  }
}