diff --git a/package.json b/package.json index b7973195..7faee259 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,18 @@ "commitlint": { "extends": [ "@commitlint/config-conventional" - ] + ], + "rules": { + "body-max-line-length": [ + 0, + "always", + "Infinity" + ], + "footer-max-line-length": [ + 0, + "always", + "Infinity" + ] + } } } diff --git a/packages/api/__tests__/auth.test.js b/packages/api/__tests__/auth.test.js index 64b1528b..d0f38035 100644 --- a/packages/api/__tests__/auth.test.js +++ b/packages/api/__tests__/auth.test.js @@ -42,17 +42,11 @@ describe('#auth()', () => { return sdk .auth(apiKey) .getSomething() - .then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + .then(() => mock.done()); } sdk.auth(apiKey); - return sdk.getSomething().then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return sdk.getSomething().then(() => mock.done()); }); it('should throw if you supply multiple auth keys', () => { @@ -89,17 +83,11 @@ describe('#auth()', () => { return sdk .auth(apiKey) .getSomething() - .then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + .then(() => mock.done()); } sdk.auth(apiKey); - return sdk.getSomething().then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return sdk.getSomething().then(() => mock.done()); }); it('should throw if you supply multiple auth keys', () => { @@ -135,21 +123,21 @@ describe('#auth()', () => { reqheaders: { authorization: `Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}` }, }) .get('/') - .reply(200, {}); + .reply(200, { id: 1 }); if (chained) { return sdk .auth(user, pass) .getSomething() .then(res => { - expect(res.status).toBe(200); + expect(res.id).toBe(1); mock.done(); }); } sdk.auth(user, pass); return sdk.getSomething().then(res => { - expect(res.status).toBe(200); + expect(res.id).toBe(1); mock.done(); }); }); @@ -165,10 +153,7 @@ describe('#auth()', () => { return sdk .auth(user) .getSomething() - .then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + .then(() => mock.done()); }); }); @@ -199,17 +184,11 @@ describe('#auth()', () => { return sdk .auth(apiKey) .getSomething() - .then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + .then(() => mock.done()); } sdk.auth(apiKey); - return sdk.getSomething().then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return sdk.getSomething().then(() => mock.done()); }); it('should throw if you pass in multiple bearer tokens', () => { @@ -246,17 +225,11 @@ describe('#auth()', () => { return sdk .auth(apiKey) .getSomething() - .then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + .then(() => mock.done()); } sdk.auth(apiKey); - return sdk.getSomething().then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return sdk.getSomething().then(() => mock.done()); }); it('should throw if you pass in multiple bearer tokens', () => { diff --git a/packages/api/__tests__/index.test.js b/packages/api/__tests__/index.test.js index 59610f3c..70431012 100644 --- a/packages/api/__tests__/index.test.js +++ b/packages/api/__tests__/index.test.js @@ -173,7 +173,7 @@ describe('#fetch', () => { }); it('should contain a custom user agent for the library in requests', () => { - expect.assertions(2); + expect.assertions(1); const userAgent = `${pkg.name} (node)/${pkg.version}`; const mock = nock(petstoreServerUrl, { @@ -186,10 +186,7 @@ describe('#fetch', () => { expect(this.req.headers['user-agent']).toStrictEqual([userAgent]); }); - return petstoreSdk.deletePet({ id: petId }).then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return petstoreSdk.deletePet({ id: petId }).then(() => mock.done()); }); describe('operationId', () => { @@ -201,26 +198,17 @@ describe('#fetch', () => { const mock = nock(petstoreServerUrl).delete(`/pets/${petId}`).reply(200, response); - return petstoreSdk - .deletePet({ id: petId }) - .then(res => { - expect(res.status).toBe(200); - return res.json(); - }) - .then(res => { - expect(res).toStrictEqual(response); - mock.done(); - }); + return petstoreSdk.deletePet({ id: petId }).then(res => { + expect(res).toStrictEqual(response); + mock.done(); + }); }); it('should pass through body for operationId', () => { const body = { name: 'Buster' }; const mock = nock(petstoreServerUrl).post('/pets', body).reply(200); - return petstoreSdk.addPet(body).then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return petstoreSdk.addPet(body).then(() => mock.done()); }); it('should pass through parameters and body for operationId', () => { @@ -232,10 +220,7 @@ describe('#fetch', () => { const mock = nock('https://dash.readme.io/api/v1').put(`/changelogs/${slug}`, body).reply(200); - return readmeSdk.updateChangelog(body, { slug }).then(res => { - expect(res.status).toBe(200); - mock.done(); - }); + return readmeSdk.updateChangelog(body, { slug }).then(() => mock.done()); }); }); @@ -252,26 +237,16 @@ describe('#fetch', () => { }; }); - return petstoreSdk - .post('/pets', body) - .then(res => { - expect(res.status).toBe(200); - return res.json(); - }) - .then(res => { - expect(res).toStrictEqual({ id: 100, name: body.name }); - mock.done(); - }); + return petstoreSdk.post('/pets', body).then(res => { + expect(res).toStrictEqual({ id: 100, name: body.name }); + mock.done(); + }); }); it('should pass through parameters for method + path', () => { const slug = 'new-release'; const mock = nock('https://dash.readme.io/api/v1').put(`/changelogs/${slug}`).reply(200); - return readmeSdk.put('/changelogs/{slug}', { slug }).then(res => { - expect(res.status).toBe(200); - expect(res.url).toBe(`https://dash.readme.io/api/v1/changelogs/${slug}`); - mock.done(); - }); + return readmeSdk.put('/changelogs/{slug}', { slug }).then(() => mock.done()); }); it('should pass through parameters and body for method + path', () => { @@ -290,17 +265,10 @@ describe('#fetch', () => { }; }); - return readmeSdk - .put('/changelogs/{slug}', body, { slug }) - .then(res => { - expect(res.status).toBe(200); - expect(res.url).toBe(`https://dash.readme.io/api/v1/changelogs/${slug}`); - return res.json(); - }) - .then(res => { - expect(res).toStrictEqual({ ...body, slug }); - mock.done(); - }); + return readmeSdk.put('/changelogs/{slug}', body, { slug }).then(res => { + expect(res).toStrictEqual({ ...body, slug }); + mock.done(); + }); }); }); }); diff --git a/packages/api/__tests__/lib/parseResponse.test.js b/packages/api/__tests__/lib/parseResponse.test.js new file mode 100644 index 00000000..cfb0d315 --- /dev/null +++ b/packages/api/__tests__/lib/parseResponse.test.js @@ -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 = ''; + const nonJsonResponse = new Response('', { + 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(''); + }); +}); diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 64d08b78..a59b5312 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -5,7 +5,7 @@ const oasToHar = require('@readme/oas-to-har'); const pkg = require('../package.json'); const Cache = require('./cache'); -const { prepareAuth, prepareParams } = require('./lib/index'); +const { prepareAuth, prepareParams, parseResponse } = require('./lib/index'); global.fetch = fetch; global.Request = fetch.Request; @@ -48,7 +48,7 @@ class Sdk { throw res; } - return res; + return parseResponse(res); }); }); } diff --git a/packages/api/src/lib/index.js b/packages/api/src/lib/index.js index 706f5131..3ff1c716 100644 --- a/packages/api/src/lib/index.js +++ b/packages/api/src/lib/index.js @@ -1,7 +1,9 @@ const prepareAuth = require('./prepareAuth'); const prepareParams = require('./prepareParams'); +const parseResponse = require('./parseResponse'); module.exports = { prepareAuth, prepareParams, + parseResponse, }; diff --git a/packages/api/src/lib/parseResponse.js b/packages/api/src/lib/parseResponse.js new file mode 100644 index 00000000..1048e662 --- /dev/null +++ b/packages/api/src/lib/parseResponse.js @@ -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; +}; diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-form-encoded.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-form-encoded.js index cd7a4055..140052f1 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-form-encoded.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-form-encoded.js @@ -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)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-json.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-json.js index f58a6181..c28dac50 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-json.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/application-json.js @@ -8,6 +8,5 @@ sdk.post('/har', { arr_mix: [1, 'a', {arr_mix_nested: {}}], boolean: false }) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/cookies.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/cookies.js index 7bc93374..906a0f1c 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/cookies.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/cookies.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har') - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/full.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/full.js index 1681214c..a66f8161 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/full.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/full.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: 'bar'}, {foo: ['bar', 'baz'], baz: 'abc', key: 'value', accept: 'application/json'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/headers.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/headers.js index 392af4ce..2f173d1e 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/headers.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/headers.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.get('/har', {'x-foo': 'Bar'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/https.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/https.js index 03cdef35..30dea639 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/https.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/https.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.get('/har') - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-119.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-119.js index 492b6191..41aae53c 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-119.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-119.js @@ -2,6 +2,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.auth('123'); sdk['find/pets-by-status']({status: 'available'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-128.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-128.js index 44fa1a03..0a2d40e9 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-128.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-128.js @@ -2,6 +2,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.auth('authKey\'With\'Apostrophes'); sdk.getItem({Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-76.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-76.js index 891feaaf..4f7ccb20 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-76.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-76.js @@ -2,6 +2,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.auth('a5a220e'); sdk.get('/pet/findByStatus', {status: 'available', Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78-operationid.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78-operationid.js index d00b44b6..36b0b7a7 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78-operationid.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78-operationid.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.getOrder({orderId: '1234', Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78.js index 9e81c783..37db9e5d 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/issue-78.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.get('/store/order/1234/tracking/{trackingId}', {Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-multiline.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-multiline.js index b89829e0..5294fc44 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-multiline.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-multiline.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: 'bar'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-null-value.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-null-value.js index 0c7c90e5..6bef38ed 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-null-value.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/jsonObj-null-value.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: null}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-data.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-data.js index 51226497..27c44ce7 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-data.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-data.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: 'hello.txt'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-file.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-file.js index e3f1a3cf..41bbdeab 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-file.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-file.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: 'test/fixtures/files/hello.txt'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-form-data.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-form-data.js index b89829e0..5294fc44 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-form-data.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/multipart-form-data.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', {foo: 'bar'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/petstore.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/petstore.js index 3cce7895..2726d563 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/petstore.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/petstore.js @@ -2,6 +2,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.auth('123'); sdk.findPetsByStatus({status: 'available', Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query-auth.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query-auth.js index 25695764..56c92ead 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query-auth.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query-auth.js @@ -2,6 +2,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.auth('a5a220e'); sdk.findPetsByStatus({status: 'available', Accept: 'application/xml'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query.js index 757dd381..ac908fda 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/query.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.get('/har', {foo: ['bar', 'baz'], baz: 'abc', key: 'value'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/short.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/short.js index 03cdef35..30dea639 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/short.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/short.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.get('/har') - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/text-plain.js b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/text-plain.js index c092111b..816fb13a 100644 --- a/packages/httpsnippet-client-api/__tests__/__fixtures__/output/text-plain.js +++ b/packages/httpsnippet-client-api/__tests__/__fixtures__/output/text-plain.js @@ -1,6 +1,5 @@ const sdk = require('api')('https://example.com/openapi.json'); sdk.post('/har', 'Hello World') - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err)); diff --git a/packages/httpsnippet-client-api/__tests__/__snapshots__/index.test.js.snap b/packages/httpsnippet-client-api/__tests__/__snapshots__/index.test.js.snap index f3c7a5dd..18839cb5 100644 --- a/packages/httpsnippet-client-api/__tests__/__snapshots__/index.test.js.snap +++ b/packages/httpsnippet-client-api/__tests__/__snapshots__/index.test.js.snap @@ -5,8 +5,7 @@ exports[`auth handling basic should be able to handle basic auth that's just a p sdk.auth('', 'pug') sdk.getAPISpecification({perPage: '10', page: '1'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err));" `; @@ -15,8 +14,7 @@ exports[`auth handling basic should be able to handle basic auth that's just a u sdk.auth('buster') sdk.getAPISpecification({perPage: '10', page: '1'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err));" `; @@ -25,7 +23,6 @@ exports[`auth handling basic should not encode basic auth in the \`.auth()\` cal sdk.auth('buster', 'pug') sdk.getAPISpecification({perPage: '10', page: '1'}) - .then(res => res.json()) - .then(json => console.log(json)) + .then(res => console.log(res)) .catch(err => console.error(err));" `; diff --git a/packages/httpsnippet-client-api/src/index.js b/packages/httpsnippet-client-api/src/index.js index 14e79e6a..39038593 100644 --- a/packages/httpsnippet-client-api/src/index.js +++ b/packages/httpsnippet-client-api/src/index.js @@ -245,8 +245,7 @@ module.exports = function (source, options) { code .push(`sdk${accessor}(${args.join(', ')})`) - .push(1, '.then(res => res.json())') - .push(1, '.then(json => console.log(json))') + .push(1, '.then(res => console.log(res))') .push(1, '.catch(err => console.error(err));'); return code.join();