Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start: createServerFn cannot return raw response objects #2779

Open
nekochan0122 opened this issue Nov 16, 2024 · 1 comment
Open

Start: createServerFn cannot return raw response objects #2779

nekochan0122 opened this issue Nov 16, 2024 · 1 comment
Labels
start Everything about TanStack Start

Comments

@nekochan0122
Copy link
Contributor

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

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

@SeanCassiere SeanCassiere added types Changes to the typescript types start Everything about TanStack Start and removed types Changes to the typescript types labels Nov 16, 2024
@nekochan0122
Copy link
Contributor Author

nekochan0122 commented Nov 16, 2024

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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
start Everything about TanStack Start
Projects
Status: Backlog
Development

No branches or pull requests

2 participants