diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 4aeb83b..e438da3 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -171,12 +171,7 @@ function handleCookies(response) { } function handleResponse(state, response) { - const error = (0, _Utils.assembleError)({ - error: response.error, - response, - params: response.config - }); - if (error) throw error; + 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 44361bc..cbc8914 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -141,10 +141,11 @@ function mapToAxiosConfig(requestConfig) { // maxContentLength, // maxBodyLength, validateStatus: status => { - var _requestConfig20, _requestConfig20$opti, _requestConfig20$opti2; + var _requestConfig20, _requestConfig20$opti; - console.log('Status received by axios', status); - return status >= 200 && status < 300 || ((_requestConfig20 = requestConfig) === null || _requestConfig20 === void 0 ? void 0 : (_requestConfig20$opti = _requestConfig20.options) === null || _requestConfig20$opti === void 0 ? void 0 : (_requestConfig20$opti2 = _requestConfig20$opti.successCodes) === null || _requestConfig20$opti2 === void 0 ? void 0 : _requestConfig20$opti2.includes(status)); + const customCodes = (_requestConfig20 = requestConfig) === null || _requestConfig20 === void 0 ? void 0 : (_requestConfig20$opti = _requestConfig20.options) === null || _requestConfig20$opti === void 0 ? void 0 : _requestConfig20$opti.successCodes; + if (customCodes) return customCodes.includes(status); + return status >= 200 && status < 300; }, maxRedirects: (_requestConfig$maxRed = (_requestConfig21 = requestConfig) === null || _requestConfig21 === void 0 ? void 0 : _requestConfig21.maxRedirects) !== null && _requestConfig$maxRed !== void 0 ? _requestConfig$maxRed : ((_requestConfig22 = requestConfig) === null || _requestConfig22 === void 0 ? void 0 : _requestConfig22.followAllRedirects) === false ? 0 : 5 // socketPath, // httpAgent: requestConfig?.httpAgent ?? requestConfig?.agent, diff --git a/package-lock.json b/package-lock.json index c75c083..95c748a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@openfn/language-http", - "version": "3.1.3", + "version": "3.1.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1154,9 +1154,9 @@ } }, "@openfn/language-common": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@openfn/language-common/-/language-common-1.2.4.tgz", - "integrity": "sha512-VEYfutS8UFN7lHUSSpME0OlLv9ZSbTLZ2lP569RSLwXOGvmJ4f6mn/zShh3ZGpmLuD+XgkCepFkdw+DVfQA2qA==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/@openfn/language-common/-/language-common-1.2.5.tgz", + "integrity": "sha512-NaVagIkr46s2TULUddUKPRmfwBuqDmBgImjiiDVqb+WyY2X7+nG+oPE7YoGj1c3PJppP6kciOtJ/oolePyHC9Q==", "requires": { "axios": "^0.21.1", "jsonpath-plus": "^4.0.0", diff --git a/package.json b/package.json index e95bc5d..ec62d47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/language-http", - "version": "3.1.3", + "version": "3.1.4", "description": "An HTTP request language package for use with Open Function", "homepage": "https://docs.openfn.org", "repository": { @@ -19,7 +19,7 @@ "lib/" ], "dependencies": { - "@openfn/language-common": "1.2.4", + "@openfn/language-common": "1.2.5", "cheerio": "^1.0.0-rc.3", "cheerio-tableparser": "^1.0.1", "csv-parse": "^4.10.1", diff --git a/src/Adaptor.js b/src/Adaptor.js index 6a99d39..55cb4f6 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -89,14 +89,13 @@ function handleCookies(response) { } function handleResponse(state, response) { - const error = assembleError({ - error: response.error, - response, - params: response.config, - }); - - if (error) throw error; - + console.log( + '✓', + response.config.method.toUpperCase(), + 'request succeeded with', + response.status + ); + const compatibleResp = { ...response, httpStatus: response.status, diff --git a/src/Utils.js b/src/Utils.js index 499c50b..4aec8d5 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -112,11 +112,9 @@ export function mapToAxiosConfig(requestConfig) { // maxContentLength, // maxBodyLength, validateStatus: status => { - console.log('Status received by axios', status); - return ( - (status >= 200 && status < 300) || - requestConfig?.options?.successCodes?.includes(status) - ); + const customCodes = requestConfig?.options?.successCodes; + if (customCodes) return customCodes.includes(status); + return status >= 200 && status < 300; }, maxRedirects: requestConfig?.maxRedirects ?? @@ -128,6 +126,5 @@ export function mapToAxiosConfig(requestConfig) { // cancelToken, // decompress, }; - return finalConfig; } diff --git a/test/index.js b/test/index.js index 754868f..0e9f7d6 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,7 @@ import Adaptor from '../src'; import { expect } from 'chai'; import nock from 'nock'; -const { execute, get, post, put, patch, del, alterState } = Adaptor; +const { execute, get, post, put, patch, del, alterState, request } = Adaptor; function stdGet(state) { return execute(get('https://www.example.com/api/fake', {}))(state).then( @@ -419,11 +419,9 @@ describe('get()', () => { const error = await execute(get('https://www.example.com/api/crashDummy'))( state - ).catch(error => { - return JSON.parse(error.message); - }); + ).catch(error => error); - expect(error.status).to.eql(500); + expect(error.response.status).to.eql(500); }); }); @@ -623,13 +621,15 @@ describe('the old request operation', () => { data: { a: 1 }, }; const finalState = await execute( - post('https://www.example.com/api/oldEndpoint', { + request({ + method: 'post', + url: 'https://www.example.com/api/oldEndpoint', json: { a: 1 }, qs: { hi: 'there' }, }) )(state); - expect(finalState.data).to.eql({ a: 1 }); + expect(finalState).to.eql({ a: 1 }); }); }); @@ -692,7 +692,7 @@ describe('The `agentOptions` param', () => { prublicKey: 'something@mamadou.org', privateKey: 'abc123', }, - data: { a: 1 }, + data: { a: 2 }, }; const finalState = await execute( @@ -700,11 +700,13 @@ describe('The `agentOptions` param', () => { state.httpsOptions = { ca: state.configuration.privateKey }; return state; }), - post('https://www.example.com/api/sslCertCheck', state => { - return { body: state.data, agentOptions: state.httpsOptions }; + post('https://www.example.com/api/sslCertCheck', { + body: state => state.data, + agentOptions: state => state.httpsOptions, }) )(state); - expect(finalState.data).to.eql({ a: 1 }); + + expect(finalState.data).to.eql({ a: 2 }); expect(finalState.response.config.httpsAgent.options.ca).to.eql('abc123'); }); });