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

Commit

Permalink
Merge pull request #18 from OpenFn/replace_requestdependency_14
Browse files Browse the repository at this point in the history
Replace request dependency
  • Loading branch information
taylordowns2000 authored Jan 25, 2021
2 parents 6392340 + 6be6ce5 commit b93f199
Show file tree
Hide file tree
Showing 7 changed files with 637 additions and 368 deletions.
209 changes: 106 additions & 103 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _Client = require("./Client");

var _Utils = require("./Utils");

var _languageCommon = require("language-common");
var _languageCommon = require("@openfn/language-common");

var _cheerio = _interopRequireDefault(require("cheerio"));

Expand All @@ -81,10 +81,15 @@ var _fs = _interopRequireDefault(require("fs"));

var _csvParse = _interopRequireDefault(require("csv-parse"));

var _toughCookie = _interopRequireDefault(require("tough-cookie"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/** @module Adaptor */

const {
axios
} = _languageCommon.http;
exports.axios = axios;
/**
* Execute a sequence of operations.
* Wraps `language-common/execute`, and prepends initial state for http.
Expand All @@ -97,6 +102,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @param {Operations} operations - Operations to be performed.
* @returns {Operation}
*/

function execute(...operations) {
const initialState = {
references: [],
Expand All @@ -107,7 +113,48 @@ function execute(...operations) {
...state
});
};
}
} // axios interceptors


var Cookie = _toughCookie.default.Cookie;
var cookiejar = new _toughCookie.default.CookieJar();
axios.interceptors.request.use(function (config) {
cookiejar === null || cookiejar === void 0 ? void 0 : cookiejar.getCookies(config.url, function (err, cookies) {
config.headers.cookie = cookies === null || cookies === void 0 ? void 0 : cookies.join('; ');
});
return config;
});
axios.interceptors.response.use(function (response) {
let cookies;
let keepCookies = [];
response = { ...response,
httpStatus: response.status,
message: response.statusText
};

if (response.headers['set-cookie']) {
var _response$headers$set, _response$headers$set2;

if (response.headers['set-cookie'] instanceof Array) cookies = (_response$headers$set = response.headers['set-cookie']) === null || _response$headers$set === void 0 ? void 0 : _response$headers$set.map(Cookie.parse);else cookies = [Cookie.parse(response.headers['set-cookie'])];
(_response$headers$set2 = response.headers['set-cookie']) === null || _response$headers$set2 === void 0 ? void 0 : _response$headers$set2.forEach(function (c) {
cookiejar.setCookie(Cookie.parse(c), response.config.url, function (err, cookie) {
var _response$config;

if ((_response$config = response.config) === null || _response$config === void 0 ? void 0 : _response$config.keepCookie) {
keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.push(cookie === null || cookie === void 0 ? void 0 : cookie.cookieString());
}
});
});
}

const resData = (0, _Utils.tryJson)(response.data);
return { ...response,
data: { ...resData,
__cookie: (keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies,
__headers: response.headers
}
};
});
/**
* Make a GET request
* @public
Expand All @@ -128,29 +175,20 @@ function execute(...operations) {
* @returns {Operation}
*/


function get(path, params, callback) {
return state => {
var _params$authenticatio, _params, _params2;

path = (0, _languageCommon.expandReferences)(path)(state);
params = (0, _languageCommon.expandReferences)(params)(state);
const url = (0, _Utils.setUrl)(state.configuration, path);
const {
query,
headers,
authentication,
body,
formData,
options,
...rest
} = (0, _languageCommon.expandReferences)(params)(state);
const auth = (0, _Utils.setAuth)(state.configuration, authentication);
return (0, _Client.req)('GET', {
const auth = (0, _Utils.setAuth)(state.configuration, (_params$authenticatio = (_params = params) === null || _params === void 0 ? void 0 : _params.authentication) !== null && _params$authenticatio !== void 0 ? _params$authenticatio : (_params2 = params) === null || _params2 === void 0 ? void 0 : _params2.auth);
const config = (0, _Utils.mapToAxiosConfig)({ ...params,
url,
query,
auth,
headers,
options,
...rest
}).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response);
auth
});
return _languageCommon.http.get(config)(state).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response.data);
if (callback) return callback(nextState);
return nextState;
});
Expand All @@ -161,6 +199,7 @@ function get(path, params, callback) {
* @public
* @example
* post("/myendpoint", {
* @function
* body: {"foo": "bar"},
* headers: {"content-type": "application/json"},
* authentication: {username: "user", password: "pass"},
Expand All @@ -169,38 +208,32 @@ function get(path, params, callback) {
* return state;
* }
* )
* @function
* @param {string} path - Path to resource
* @param {object} params - Body, Query, Headers and Authentication parameters
* @param {function} callback - (Optional) Callback function
* @returns {Operation}
* @returns {operation}
*/


function post(path, params, callback) {
return state => {
var _params$authenticatio2, _params3, _params4;

path = (0, _languageCommon.expandReferences)(path)(state);
params = (0, _languageCommon.expandReferences)(params)(state);
const url = (0, _Utils.setUrl)(state.configuration, path);
const {
query,
headers,
authentication,
body,
formData,
options,
...rest
} = (0, _languageCommon.expandReferences)(params)(state);
const auth = (0, _Utils.setAuth)(state.configuration, authentication);
return (0, _Client.req)('POST', {
const auth = (0, _Utils.setAuth)(state.configuration, (_params$authenticatio2 = (_params3 = params) === null || _params3 === void 0 ? void 0 : _params3.authentication) !== null && _params$authenticatio2 !== void 0 ? _params$authenticatio2 : (_params4 = params) === null || _params4 === void 0 ? void 0 : _params4.auth);
const config = (0, _Utils.mapToAxiosConfig)({ ...params,
url,
query,
body,
auth,
headers,
formData,
options,
...rest
auth
}); // NOTE: that in order to use multipart/form submissions, we call axios.post
// directly so as to avoid calling 'expandReferences' on the config (in
// language-common.http.post) once we've set up the 'form-data' module.
// Elsewhere, calling expandReferences multiple times is harmless.

return axios.post(config.url, config.data, { ...config
}).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response);
const nextState = (0, _languageCommon.composeNextState)(state, response.data);
if (callback) return callback(nextState);
return nextState;
});
Expand Down Expand Up @@ -229,28 +262,18 @@ function post(path, params, callback) {

function put(path, params, callback) {
return state => {
var _params$authenticatio3, _params5, _params6;

path = (0, _languageCommon.expandReferences)(path)(state);
params = (0, _languageCommon.expandReferences)(params)(state);
const url = (0, _Utils.setUrl)(state.configuration, path);
const {
query,
headers,
authentication,
body,
formData,
options,
...rest
} = (0, _languageCommon.expandReferences)(params)(state);
const auth = (0, _Utils.setAuth)(state.configuration, authentication);
return (0, _Client.req)('PUT', {
const auth = (0, _Utils.setAuth)(state.configuration, (_params$authenticatio3 = (_params5 = params) === null || _params5 === void 0 ? void 0 : _params5.authentication) !== null && _params$authenticatio3 !== void 0 ? _params$authenticatio3 : (_params6 = params) === null || _params6 === void 0 ? void 0 : _params6.auth);
const config = (0, _Utils.mapToAxiosConfig)({ ...params,
url,
query,
body,
formData,
auth,
headers,
options,
...rest
}).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response);
auth
});
return _languageCommon.http.put(config)(state).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response.data);
if (callback) return callback(nextState);
return nextState;
});
Expand Down Expand Up @@ -279,28 +302,18 @@ function put(path, params, callback) {

function patch(path, params, callback) {
return state => {
var _params$authenticatio4, _params7, _params8;

path = (0, _languageCommon.expandReferences)(path)(state);
params = (0, _languageCommon.expandReferences)(params)(state);
const url = (0, _Utils.setUrl)(state.configuration, path);
const {
query,
headers,
authentication,
body,
formData,
options,
...rest
} = (0, _languageCommon.expandReferences)(params)(state);
const auth = (0, _Utils.setAuth)(state.configuration, authentication);
return (0, _Client.req)('PATCH', {
const auth = (0, _Utils.setAuth)(state.configuration, (_params$authenticatio4 = (_params7 = params) === null || _params7 === void 0 ? void 0 : _params7.authentication) !== null && _params$authenticatio4 !== void 0 ? _params$authenticatio4 : (_params8 = params) === null || _params8 === void 0 ? void 0 : _params8.auth);
const config = (0, _Utils.mapToAxiosConfig)({ ...params,
url,
query,
body,
formData,
options,
auth,
headers,
...rest
}).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response);
auth
});
return _languageCommon.http.patch(config)(state).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response.data);
if (callback) return callback(nextState);
return nextState;
});
Expand Down Expand Up @@ -329,28 +342,18 @@ function patch(path, params, callback) {

function del(path, params, callback) {
return state => {
var _params$authenticatio5, _params9, _params10;

path = (0, _languageCommon.expandReferences)(path)(state);
params = (0, _languageCommon.expandReferences)(params)(state);
const url = (0, _Utils.setUrl)(state.configuration, path);
const {
query,
headers,
authentication,
body,
formData,
options,
...rest
} = (0, _languageCommon.expandReferences)(params)(state);
const auth = (0, _Utils.setAuth)(state.configuration, authentication);
return (0, _Client.req)('DELETE', {
const auth = (0, _Utils.setAuth)(state.configuration, (_params$authenticatio5 = (_params9 = params) === null || _params9 === void 0 ? void 0 : _params9.authentication) !== null && _params$authenticatio5 !== void 0 ? _params$authenticatio5 : (_params10 = params) === null || _params10 === void 0 ? void 0 : _params10.auth);
const config = (0, _Utils.mapToAxiosConfig)({ ...params,
url,
query,
body,
formData,
options,
auth,
headers,
...rest
}).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response);
auth
});
return _languageCommon.http.delete(config)(state).then(response => {
const nextState = (0, _languageCommon.composeNextState)(state, response.data);
if (callback) return callback(nextState);
return nextState;
});
Expand Down
Loading

0 comments on commit b93f199

Please sign in to comment.