From fd53a36379f09261e297c54d8dc8e6a044226945 Mon Sep 17 00:00:00 2001 From: Taylor Downs Date: Sat, 6 Feb 2021 06:57:53 +0000 Subject: [PATCH] array response is not modified unless keepCookie is used --- lib/Adaptor.js | 7 +++++-- lib/Utils.js | 6 +----- package.json | 2 +- src/Adaptor.js | 10 ++++++---- src/Utils.js | 4 +--- test/index.js | 36 ++++++++++++------------------------ 6 files changed, 26 insertions(+), 39 deletions(-) diff --git a/lib/Adaptor.js b/lib/Adaptor.js index e438da3..ea49031 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -159,8 +159,11 @@ function handleCookies(response) { }); } + const extendableData = Array.isArray(data) ? { + body: data + } : data; return { ...response, - data: { ...data, + data: { ...extendableData, __cookie: (keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies, __headers: response.headers } @@ -171,7 +174,7 @@ function handleCookies(response) { } function handleResponse(state, response) { - console.log('✓', response.config.method.toUpperCase(), 'request succeeded with', response.status); + console.log(response.config.method.toUpperCase(), 'request succeeded with', response.status, '✓'); const compatibleResp = { ...response, httpStatus: response.status, message: response.statusText, diff --git a/lib/Utils.js b/lib/Utils.js index cbc8914..7a12d78 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -54,11 +54,7 @@ function assembleError({ } function tryJson(data) { - if (Array.isArray(data)) { - return { - body: data - }; - } else if (typeof data === 'string') { + if (typeof data === 'string') { try { return JSON.parse(data); } catch (e) { diff --git a/package.json b/package.json index ec62d47..fac4458 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/language-http", - "version": "3.1.4", + "version": "3.1.5", "description": "An HTTP request language package for use with Open Function", "homepage": "https://docs.openfn.org", "repository": { diff --git a/src/Adaptor.js b/src/Adaptor.js index 55cb4f6..94791ad 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -75,10 +75,12 @@ function handleCookies(response) { }); } + const extendableData = Array.isArray(data) ? { body: data } : data; + return { ...response, data: { - ...data, + ...extendableData, __cookie: keepCookies?.length === 1 ? keepCookies[0] : keepCookies, __headers: response.headers, }, @@ -90,12 +92,12 @@ function handleCookies(response) { function handleResponse(state, response) { console.log( - '✓', response.config.method.toUpperCase(), 'request succeeded with', - response.status + response.status, + '✓' ); - + const compatibleResp = { ...response, httpStatus: response.status, diff --git a/src/Utils.js b/src/Utils.js index 4aec8d5..4c95bfa 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -39,9 +39,7 @@ export function assembleError({ response, error, params }) { } export function tryJson(data) { - if (Array.isArray(data)) { - return { body: data }; - } else if (typeof data === 'string') { + if (typeof data === 'string') { try { return JSON.parse(data); } catch (e) { diff --git a/test/index.js b/test/index.js index 0e9f7d6..6b15ce2 100644 --- a/test/index.js +++ b/test/index.js @@ -259,19 +259,16 @@ describe('get()', () => { }) )(state); - expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders'); + expect(finalState.data[0]).to.eql('/api/showMeMyHeaders'); - expect(finalState.data.body[1]).to.haveOwnProperty('x-openfn', 'testing'); + expect(finalState.data[1]).to.haveOwnProperty('x-openfn', 'testing'); - expect(finalState.data.body[1]).to.haveOwnProperty( + expect(finalState.data[1]).to.haveOwnProperty( 'authorization', 'Basic aGVsbG86dGhlcmU=' ); - expect(finalState.data.body[1]).to.haveOwnProperty( - 'host', - 'www.example.com' - ); + expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com'); expect(finalState.references).to.eql([{ triggering: 'event' }]); }); @@ -288,15 +285,12 @@ describe('get()', () => { const finalState = await execute( get('https://www.example.com/api/showMeMyHeaders') )(state); - expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders'); - expect(finalState.data.body[1]).to.haveOwnProperty( + expect(finalState.data[0]).to.eql('/api/showMeMyHeaders'); + expect(finalState.data[1]).to.haveOwnProperty( 'authorization', 'Basic aGVsbG86dGhlcmU=' ); - expect(finalState.data.body[1]).to.haveOwnProperty( - 'host', - 'www.example.com' - ); + expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com'); }); it('can enable gzip', async () => { @@ -309,17 +303,14 @@ describe('get()', () => { get('https://www.example.com/api/showMeMyHeaders', { gzip: true }) )(state); - expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders'); + expect(finalState.data[0]).to.eql('/api/showMeMyHeaders'); - expect(finalState.data.body[1]).to.haveOwnProperty( + expect(finalState.data[1]).to.haveOwnProperty( 'accept-encoding', 'gzip, deflate' ); - expect(finalState.data.body[1]).to.haveOwnProperty( - 'host', - 'www.example.com' - ); + expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com'); }); it('allows query strings to be set', async () => { @@ -332,12 +323,9 @@ describe('get()', () => { get('https://www.example.com/api/showMeMyHeaders', { query: { id: 1 } }) )(state); - expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders?id=1'); + expect(finalState.data[0]).to.eql('/api/showMeMyHeaders?id=1'); - expect(finalState.data.body[1]).to.haveOwnProperty( - 'host', - 'www.example.com' - ); + expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com'); }); it('can follow redirects', async () => {