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

Allow to pass status code into response methods #583

Closed
ebebbington opened this issue Nov 28, 2021 · 4 comments · Fixed by #598
Closed

Allow to pass status code into response methods #583

ebebbington opened this issue Nov 28, 2021 · 4 comments · Fixed by #598

Comments

@ebebbington
Copy link
Member

eg response.json({ name: 'drash' }, 200) or response.text("Invalid request", 200)

declare function <name>(param1: unknown, status = 200): void
@ebebbington
Copy link
Member Author

found whilst working on realworld, would be great to not have to add an extra line to specify status code

@crookse
Copy link
Member

crookse commented Nov 29, 2021

should we just overloaded methods? i ask because what if we end up wanting to add headers easily

@ebebbington
Copy link
Member Author

@crookse it's a good idea actually, but im generally against overloads cause the main function gets messy, like what if we then wanna add something else?

text(text: string, status = 200): void

text(text: string, headers = []): void

text(text: string, status = 200, headers = [])

text(text: string, param?: number | string[]) {
  if
  if
  if
  if

duplicate that for each response method, what ive actually been thinking about instead after reading your comment, is something like

text(text: string, status = 200, headers = []) {
  this.setResponse(text, status, headers)
}
json(json: object, status = 200, headers = []) {
  this.setResponse(JSON.stringify(json), status, headers)
}
...
#setResponse(body: BodyInit | null, status = 200, headers = []) {
  this.status = status
  if (headers.length) {
    headers.forEach(h => this.headers.set(h.name, h.value))
  }
  tis.body = body
}

what are your thoughts? if you feel storngly about overloads then we can try it out

@crookse
Copy link
Member

crookse commented Dec 16, 2021

nah i don't have a preference. just thought overloads would make it easier.

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

Successfully merging a pull request may close this issue.

2 participants