-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatically parse the api response based on content-type (#240)
* refactor: switch to using memfs instead of mock-fs 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) * feat: automatically parse the api response based on content-type 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. * chore: relax commitlint rules on body and footer length Taken from main codebase * feat: remove res.json() line from the httpsnippet client * fix: always output `.then(res => console.log(res))` in code sample 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)
- Loading branch information
1 parent
47792bc
commit ae50813
Showing
30 changed files
with
168 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Nabbed these test cases from here: | ||
// https://github.com/readmeio/api-explorer/blob/77b90ebed4673f168354cdcd730e34b7ee016360/packages/api-explorer/__tests__/lib/parse-response.test.js#L182-L210 | ||
const { Response } = require('node-fetch'); | ||
const parseResponse = require('../../src/lib/parseResponse'); | ||
|
||
const responseBody = JSON.stringify({ | ||
id: 9205436248879918000, | ||
category: { id: 0 }, | ||
name: '1', | ||
photoUrls: ['1'], | ||
tags: [], | ||
}); | ||
|
||
let response; | ||
|
||
beforeEach(() => { | ||
response = new Response(responseBody, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-custom-header': 'application/json', | ||
}, | ||
}); | ||
}); | ||
|
||
describe('#parseResponse', () => { | ||
it('should parse application/json response as json', async () => { | ||
expect(await parseResponse(response)).toStrictEqual(JSON.parse(responseBody)); | ||
}); | ||
|
||
it('should parse application/vnd.api+json as json', async () => { | ||
response.headers['Content-Type'] = 'application/vnd.api+json'; | ||
expect(await parseResponse(response)).toStrictEqual(JSON.parse(responseBody)); | ||
}); | ||
|
||
it('should parse non-json response as text', async () => { | ||
const nonJsonResponseBody = '<xml-string />'; | ||
const nonJsonResponse = new Response('<xml-string />', { | ||
headers: { | ||
'Content-Type': 'application/xml', | ||
}, | ||
}); | ||
|
||
expect(await parseResponse(nonJsonResponse)).toStrictEqual(nonJsonResponseBody); | ||
}); | ||
|
||
it('should not error if invalid json is returned', async () => { | ||
const invalidJsonResponse = new Response('plain text', { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
expect(await parseResponse(invalidJsonResponse)).toBe('plain text'); | ||
}); | ||
|
||
it('should default to JSON with wildcard content-type', async () => { | ||
const wildcardResponse = new Response(responseBody, { | ||
headers: { | ||
'Content-Type': '*/*', | ||
}, | ||
}); | ||
|
||
expect(await parseResponse(wildcardResponse)).toStrictEqual(JSON.parse(responseBody)); | ||
}); | ||
|
||
it('should return with empty string if there is no response', async () => { | ||
const emptyResponse = new Response(null, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
expect(await parseResponse(emptyResponse)).toStrictEqual(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const prepareAuth = require('./prepareAuth'); | ||
const prepareParams = require('./prepareParams'); | ||
const parseResponse = require('./parseResponse'); | ||
|
||
module.exports = { | ||
prepareAuth, | ||
prepareParams, | ||
parseResponse, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Nabbed from here: | ||
// https://github.com/readmeio/api-explorer/blob/77b90ebed4673f168354cdcd730e34b7ee016360/packages/api-explorer/src/lib/parse-response.js#L13-L30 | ||
const { matchesMimeType } = require('oas/tooling/utils'); | ||
|
||
module.exports = async function getResponseBody(response) { | ||
const contentType = response.headers.get('Content-Type'); | ||
const isJson = contentType && (matchesMimeType.json(contentType) || matchesMimeType.wildcard(contentType)); | ||
|
||
// We have to clone it before reading, just incase | ||
// we cannot parse it as JSON later, then we can | ||
// re-read again as plain text | ||
const clonedResponse = response.clone(); | ||
let responseBody; | ||
|
||
try { | ||
responseBody = await response[isJson ? 'json' : 'text'](); | ||
} catch (e) { | ||
responseBody = await clonedResponse.text(); | ||
} | ||
|
||
return responseBody; | ||
}; |
3 changes: 1 addition & 2 deletions
3
packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-form-encoded.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
const sdk = require('api')('https://example.com/openapi.json'); | ||
|
||
sdk.post('/har', {foo: 'bar', hello: 'world'}) | ||
.then(res => res.json()) | ||
.then(json => console.log(json)) | ||
.then(res => console.log(res)) | ||
.catch(err => console.error(err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.