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

Numeric status codes #380

Closed
jankuca opened this issue Nov 23, 2020 · 3 comments · Fixed by #381
Closed

Numeric status codes #380

jankuca opened this issue Nov 23, 2020 · 3 comments · Fixed by #381

Comments

@jankuca
Copy link
Contributor

jankuca commented Nov 23, 2020

Hello, I really like this library but I had to fork it to be able to actually use it.

My issue is that the responses object lists the response status codes as strings. I need to be able to use numeric values in my codebase and I am not able to check them against the generated TS types.


My solution was to replace the { [statusCode]: responseBodies } object with a discriminated union:

responses:
  | { statusCode: 200, bodies:  }
  | { statusCode: 202, bodies:  }
  | { statusCode: 400, bodies:  }

I can get a numeric enum from that simply by using responses['statusCode'].


I've also noticed that TS allows actual numeric keys such as { 200: … } (instead of { "200": … }) so that could be enough.

Would such a change be acceptable? What is the actual reason the keys are strings anyway?

@gr2m
Copy link
Contributor

gr2m commented Nov 23, 2020

I've run into a similar problem. I worked around it by using a type map

type StatusCodes = {
  "200": 200,
  "201": 201,
  // etc
}

I've also noticed that TS allows actual numeric keys such as { 200: … } (instead of { "200": … }) so that could be enough.

Would such a change be acceptable? What is the actual reason the keys are strings anyway?

I'd like that, too. I don't see a reason why the status code type should be a string

@jankuca
Copy link
Contributor Author

jankuca commented Nov 23, 2020

I went ahead and prepared a PR for this change.

@drwpow
Copy link
Contributor

drwpow commented Nov 23, 2020

I agree with this reasoning, and the PR at first glance looks great too. Will test it a bit more later tonight.

Thank you!

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

Successfully merging a pull request may close this issue.

3 participants