curveball / core

The Curveball framework is a TypeScript framework for node.js with support for modern HTTP features.
https://curveballjs.org/
MIT License
525 stars 7 forks source link

Unintuitive shortcut placement #134

Closed avin-kavish closed 4 years ago

avin-kavish commented 4 years ago

Why am I setting the response status code on the ctx object but the response body at ctx.response? Feels a bit awkward.

  ctx.status = 200;
  ctx.response.body = 'Hello world!'
evert commented 4 years ago

The status on ctx is a shortcut for ctx.response.status (which also works).

The reason that some some of these properties are available on the Context object, but some aren't is because only the unambiguous appear.

That basically excludes body, header, type and a few others. All of those exist on both the request and the response.

avin-kavish commented 4 years ago

That's not very intuitive...

Would be better if that kind of shortcut cases are not accommodated at all. It can just be written as

app.use(({ req, res }: Context) => {
  res.status = 200
  res.body = 'Hello world!'
})

which is much cleaner. On that topic, I think req, res are as verbose as request and response need to be.

evert commented 4 years ago

Makes sense, but this is unlikely to change at this point. I added the shortcuts to give some familiarity to Koa users, but even then felt it was a bit unneeded. Renaming request/response to req/res to save a few characters is not really a high enough priority.