Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
use new common for selective expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Feb 5, 2021
1 parent 2ded71f commit 0b627db
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 40 deletions.
7 changes: 1 addition & 6 deletions lib/Adaptor.js
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/Utils.js
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions 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": {
Expand All @@ -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",
Expand Down
15 changes: 7 additions & 8 deletions src/Adaptor.js
Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions src/Utils.js
Expand Up @@ -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 ??
Expand All @@ -128,6 +126,5 @@ export function mapToAxiosConfig(requestConfig) {
// cancelToken,
// decompress,
};

return finalConfig;
}
24 changes: 13 additions & 11 deletions test/index.js
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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 });
});
});

Expand Down Expand Up @@ -692,19 +692,21 @@ describe('The `agentOptions` param', () => {
prublicKey: 'something@mamadou.org',
privateKey: 'abc123',
},
data: { a: 1 },
data: { a: 2 },
};

const finalState = await execute(
alterState(state => {
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');
});
});
Expand Down

0 comments on commit 0b627db

Please sign in to comment.