mikeal / bent

Functional JS HTTP client (Node.js & Fetch) w/ async await
2.2k stars 106 forks source link

allow statusCodes to be passed as an array or a set #115

Closed redxtech closed 4 years ago

redxtech commented 4 years ago

Before this PR, you were only able to specify a single acceptable status code, as the type check for args was only for number.

If you passed an array of status codes it would attempt to set them as the headers, which doesn't work.

With this PR, you can now pass either an array or a set of the statusCodes that will be accepted.

I have included tests in the like of the status 201 test, so that should cover it.

redxtech commented 4 years ago

I have just realized that you can of course just pass the status' as multiple args, so this isn't really necessary, but I do feel that this is a very minimal addition to the codebase that adds some flexibility, which seems to fit with this project.

mikeal commented 4 years ago

At first I thought “we might want to give some meaning to Array as an input type later on that this will preclude.” But having thought about it a bit we could always do type checks inside of the array for other features since I doubt any of those features would be specific to “an array of numbers” so I’m actually fine with adding this.

This allows for some nicer expressions because you don’t have to use the spread operator to use a programmatically generated list of status codes.

redxtech commented 4 years ago

Yeah, that's pretty much exactly what I was thinking.

Passing an array will be much nicer than what I have currently in my project: ...(typeof status === 'number' ? [status] : status)