Skip to content

Conversation

domharrington
Copy link
Member

🧰 What's being changed?

This allows us to remove the .then(res => res.json()) line from our
code samples.

const sdk = require('api')('@readme/5f7ceacf43621b0311080a58');

sdk.getAPISpecification({perPage: '10', page: '1'})
  .then(res => console.log(res))
  .catch(err => console.error(err));

We only perform the parsing if it's a successful response. If there's an error
case then we return with the original response object so you can access
the status as well as the actual body.

The code and tests for this have been stolen verbatim from the explorer:
https://github.com/readmeio/api-explorer/blob/77b90ebed4673f168354cdcd730e34b7ee016360/packages/api-explorer/src/lib/parse-response.js#L13-L30

BREAKING CHANGE: this is a breaking change.

mock-fs has limitations which means you can use console.log during test runs
which is not ideal! Ended up doing something like this to get it to work:

tschaub/mock-fs#234 (comment)
This allows us to remove the `.then(res => res.json())` line from our
code samples.

```
const sdk = require('api')('@readme/5f7ceacf43621b0311080a58');

sdk.getAPISpecification({perPage: '10', page: '1'})
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

We only perform the parsing if it's a successful response. If there's an error
case then we return with the original response object so you can access
the status as well as the actual body.

The code and tests for this have been stolen verbatim from the explorer:
https://github.com/readmeio/api-explorer/blob/77b90ebed4673f168354cdcd730e34b7ee016360/packages/api-explorer/src/lib/parse-response.js#L13-L30

BREAKING CHANGE: this is a breaking change.
@erunion erunion self-requested a review February 3, 2021 23:38
Since we dont know if the response is json or not, we can't make
assumptions. In an ideal world we'd conditionally do this based
on the accept header in the response, but Operation.getHeaders() only
returns with an array of headers and not their actual values. I think
this is good enough for now!

#240 (comment)
@erunion erunion added the enhancement New feature or request label Mar 24, 2021
@erunion erunion merged commit ae50813 into master Mar 24, 2021
@erunion erunion deleted the feature/auto-parse-response branch March 24, 2021 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants