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

Commit

Permalink
allow options with 'successCodes' to be passed to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Dec 11, 2019
1 parent ee2cef7 commit 264db49
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 39 deletions.
26 changes: 21 additions & 5 deletions lib/Adaptor.js
Expand Up @@ -185,7 +185,8 @@ function post(path, params, callback) {
authentication = _expandReferences2.authentication,
body = _expandReferences2.body,
formData = _expandReferences2.formData,
rest = _objectWithoutProperties(_expandReferences2, ['query', 'headers', 'authentication', 'body', 'formData']);
options = _expandReferences2.options,
rest = _objectWithoutProperties(_expandReferences2, ['query', 'headers', 'authentication', 'body', 'formData', 'options']);

var auth = (0, _Utils.setAuth)(state.configuration, authentication);

Expand All @@ -196,6 +197,7 @@ function post(path, params, callback) {
auth: auth,
headers: headers,
formData: formData,
options: options,
rest: rest
}).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
Expand Down Expand Up @@ -234,11 +236,21 @@ function put(path, params, callback) {
authentication = _expandReferences3.authentication,
body = _expandReferences3.body,
formData = _expandReferences3.formData,
rest = _objectWithoutProperties(_expandReferences3, ['query', 'headers', 'authentication', 'body', 'formData']);
options = _expandReferences3.options,
rest = _objectWithoutProperties(_expandReferences3, ['query', 'headers', 'authentication', 'body', 'formData', 'options']);

var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)('PUT', { url: url, query: query, body: body, formData: formData, auth: auth, headers: headers, rest: rest }).then(function (response) {
return (0, _Client.req)('PUT', {
url: url,
query: query,
body: body,
formData: formData,
auth: auth,
headers: headers,
options: options,
rest: rest
}).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
if (callback) return callback(nextState);
return nextState;
Expand Down Expand Up @@ -275,7 +287,8 @@ function patch(path, params, callback) {
authentication = _expandReferences4.authentication,
body = _expandReferences4.body,
formData = _expandReferences4.formData,
rest = _objectWithoutProperties(_expandReferences4, ['query', 'headers', 'authentication', 'body', 'formData']);
options = _expandReferences4.options,
rest = _objectWithoutProperties(_expandReferences4, ['query', 'headers', 'authentication', 'body', 'formData', 'options']);

var auth = (0, _Utils.setAuth)(state.configuration, authentication);

Expand All @@ -284,6 +297,7 @@ function patch(path, params, callback) {
query: query,
body: body,
formData: formData,
options: options,
auth: auth,
headers: headers,
rest: rest
Expand Down Expand Up @@ -324,7 +338,8 @@ function del(path, params, callback) {
authentication = _expandReferences5.authentication,
body = _expandReferences5.body,
formData = _expandReferences5.formData,
rest = _objectWithoutProperties(_expandReferences5, ['query', 'headers', 'authentication', 'body', 'formData']);
options = _expandReferences5.options,
rest = _objectWithoutProperties(_expandReferences5, ['query', 'headers', 'authentication', 'body', 'formData', 'options']);

var auth = (0, _Utils.setAuth)(state.configuration, authentication);

Expand All @@ -333,6 +348,7 @@ function del(path, params, callback) {
query: query,
body: body,
formData: formData,
options: options,
auth: auth,
headers: headers,
rest: rest
Expand Down
9 changes: 6 additions & 3 deletions lib/Client.js
Expand Up @@ -20,7 +20,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
function rawRequest(params) {
return new Promise(function (resolve, reject) {
(0, _request2.default)(params, function (error, response, body) {
error = (0, _Utils.assembleError)({ error: error, response: response });
error = (0, _Utils.assembleError)({ error: error, response: response, params: params });
error && reject(error);

console.log('\u2713 Request succeeded.');
Expand All @@ -38,6 +38,7 @@ function req(method, params) {
formData = params.formData,
auth = params.auth,
query = params.query,
options = params.options,
rest = params.rest;

return new Promise(function (resolve, reject) {
Expand All @@ -50,9 +51,11 @@ function req(method, params) {
method: method,
json: body,
formData: formData,
jar: j
jar: j,
options: options
}, rest), function (error, response, body) {
error = (0, _Utils.assembleError)({ error: error, response: response });
console.log(params);
error = (0, _Utils.assembleError)({ error: error, response: response, params: params });
if (error) {
reject(error);
} else {
Expand Down
12 changes: 7 additions & 5 deletions lib/Utils.js
Expand Up @@ -13,18 +13,20 @@ function setUrl(configuration, path) {

function setAuth(configuration, manualAuth) {
if (manualAuth) return manualAuth;else if (configuration && configuration.username) return {
'username': configuration.username,
'password': configuration.password,
'sendImmediately': configuration.authType != 'digest'
username: configuration.username,
password: configuration.password,
sendImmediately: configuration.authType != 'digest'
};else return null;
}

function assembleError(_ref) {
var response = _ref.response,
error = _ref.error;
error = _ref.error,
params = _ref.params;

if (response) {
if ([200, 201, 202].indexOf(response.statusCode) > -1) return false;
var customCodes = params.options && params.options.successCodes;
if ((customCodes || [200, 201, 202]).indexOf(response.statusCode) > -1) return false;
}
if (error) return error;
return new Error('Server responded with: \n' + JSON.stringify(response, null, 2));
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "language-http",
"version": "2.2.2",
"version": "2.3.0",
"description": "An HTTP request language package for use with Open Function",
"main": "lib/index.js",
"scripts": {
Expand Down
28 changes: 21 additions & 7 deletions src/Adaptor.js
Expand Up @@ -98,6 +98,7 @@ export function post(path, params, callback) {
authentication,
body,
formData,
options,
...rest
} = expandReferences(params)(state);

Expand All @@ -110,6 +111,7 @@ export function post(path, params, callback) {
auth,
headers,
formData,
options,
rest,
}).then(response => {
const nextState = composeNextState(state, response);
Expand Down Expand Up @@ -148,18 +150,26 @@ export function put(path, params, callback) {
authentication,
body,
formData,
options,
...rest
} = expandReferences(params)(state);

const auth = setAuth(state.configuration, authentication);

return req('PUT', { url, query, body, formData, auth, headers, rest }).then(
response => {
const nextState = composeNextState(state, response);
if (callback) return callback(nextState);
return nextState;
}
);
return req('PUT', {
url,
query,
body,
formData,
auth,
headers,
options,
rest,
}).then(response => {
const nextState = composeNextState(state, response);
if (callback) return callback(nextState);
return nextState;
});
};
}

Expand Down Expand Up @@ -192,6 +202,7 @@ export function patch(path, params, callback) {
authentication,
body,
formData,
options,
...rest
} = expandReferences(params)(state);

Expand All @@ -202,6 +213,7 @@ export function patch(path, params, callback) {
query,
body,
formData,
options,
auth,
headers,
rest,
Expand Down Expand Up @@ -242,6 +254,7 @@ export function del(path, params, callback) {
authentication,
body,
formData,
options,
...rest
} = expandReferences(params)(state);

Expand All @@ -252,6 +265,7 @@ export function del(path, params, callback) {
query,
body,
formData,
options,
auth,
headers,
rest,
Expand Down
8 changes: 5 additions & 3 deletions src/Client.js
Expand Up @@ -4,7 +4,7 @@ import { assembleError, tryJson } from './Utils';
export function rawRequest(params) {
return new Promise((resolve, reject) => {
request(params, (error, response, body) => {
error = assembleError({ error, response });
error = assembleError({ error, response, params });
error && reject(error);

console.log(`✓ Request succeeded.`);
Expand All @@ -18,7 +18,7 @@ export function rawRequest(params) {
}

export function req(method, params) {
const { url, headers, body, formData, auth, query, rest } = params;
const { url, headers, body, formData, auth, query, options, rest } = params;
return new Promise((resolve, reject) => {
const j = request.jar();
request(
Expand All @@ -31,10 +31,12 @@ export function req(method, params) {
json: body,
formData,
jar: j,
options,
...rest,
},
function(error, response, body) {
error = assembleError({ error, response });
console.log(params);
error = assembleError({ error, response, params });
if (error) {
reject(error);
} else {
Expand Down
36 changes: 21 additions & 15 deletions src/Utils.js
@@ -1,30 +1,36 @@
export function setUrl(configuration, path) {
if ( configuration && configuration.baseUrl ) return configuration.baseUrl + path
else return path
if (configuration && configuration.baseUrl)
return configuration.baseUrl + path;
else return path;
}

export function setAuth(configuration, manualAuth) {
if ( manualAuth ) return manualAuth
else if ( configuration && configuration.username ) return {
'username': configuration.username,
'password': configuration.password,
'sendImmediately': ( configuration.authType != 'digest' )
}
else return null
if (manualAuth) return manualAuth;
else if (configuration && configuration.username)
return {
username: configuration.username,
password: configuration.password,
sendImmediately: configuration.authType != 'digest',
};
else return null;
}

export function assembleError({ response, error }) {
export function assembleError({ response, error, params }) {
if (response) {
if ([200,201,202].indexOf(response.statusCode) > -1) return false;
const customCodes = params.options && params.options.successCodes;
if ((customCodes || [200, 201, 202]).indexOf(response.statusCode) > -1)
return false;
}
if (error) return error;
return new Error(`Server responded with: \n${JSON.stringify(response, null, 2)}`)
return new Error(
`Server responded with: \n${JSON.stringify(response, null, 2)}`
);
}

export function tryJson(data) {
try {
return JSON.parse(data)
} catch(e) {
return {body: data}
return JSON.parse(data);
} catch (e) {
return { body: data };
}
}

0 comments on commit 264db49

Please sign in to comment.