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

new Response('message', { status: 404 }) should auto-translate to set.status #331

Open
asilvas opened this issue Nov 24, 2023 · 1 comment

Comments

@asilvas
Copy link

asilvas commented Nov 24, 2023

The docs aren't super clear on this subject, but I see no reason the formal Bun format could not auto translate.

Basically in addition to the current pattern, it would be very desirable to also support new Response:

// TODAY
app.get('/test', ({ set }) => {
  if (somethingBad) {
    set.status = 404;
    return "that doesn't exist";
  }

  return 'yay';
});

Would be great if this worked as well!

// NEXT
app.get('/test', () => {
  if (somethingBad) return new Response("that doesn't exist", { status: 404 });

  return 'yay';
});

Alternatively (or in addition to) throwing an error and attaching status should have the same affect. This can already be achieved manually today via custom onError handler to detect status in error. Ideally this would be out of the box behavior. Exposing ErrorStatus class would also make this type safe (ala throw new ErrorStatus('something wrong', 400)).

Awesome package otherwise!

@dodas
Copy link

dodas commented Nov 27, 2023

+1

my two cents:

  • sometimes it would be handy to be able to throw a response and have Elysia respond with it. For example, let's say in derive() we come across an error and want to respond instead of returning the derived context. Currently, throwing a response seems to respond with the correct status, but the response body is always empty.

  • if we allow returning Response objects, we could also expose a generic Response interface, so we will be able to infer response type from Response.json(), similar to fets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants