@highpoint/js-fetch
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -0,29 +1,42 @@ | ||
# v2.1.0 | ||
- postJSON now accepts _also_ an object as the body | ||
# v2.0.1 | ||
* Retrieve `baseURI` before each request so if `<base href>` changes, the change | ||
- Retrieve `baseURI` before each request so if `<base href>` changes, the change | ||
will take effect. | ||
# v2.0.0 | ||
* Export ES modules instead of CommonJS | ||
- Export ES modules instead of CommonJS | ||
# v1.1.0 | ||
* Throw exception when device is offline | ||
- Throw exception when device is offline | ||
# v1.0.1 | ||
* Throw exception on authorization errors | ||
- Throw exception on authorization errors | ||
# v1.0.0 | ||
* Add function to post raw, non-JSON content. | ||
* Only parse JSON if response declares it to be JSON. Otherwise return body as | ||
- Add function to post raw, non-JSON content. | ||
- Only parse JSON if response declares it to be JSON. Otherwise return body as | ||
text. | ||
# v0.1.3 | ||
* Support IScript functions other than `IScript_Main` | ||
- Support IScript functions other than `IScript_Main` | ||
# v0.1.2 | ||
* Fix CSRF cookie name | ||
- Fix CSRF cookie name | ||
# v0.1.1 | ||
* Automatically add `postDataBin=y` to JSON requests (if it's not already there) | ||
- Automatically add `postDataBin=y` to JSON requests (if it's not already there) | ||
# v0.1.0 | ||
* Initial release | ||
- Initial release |
@@ -1,9 +0,13 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
var _this = this; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
var getBaseURI = function getBaseURI() { | ||
@@ -19,7 +23,7 @@ return (document.baseURI || document.querySelector('base').href).replace(/IScript_.*/, 'IScript_'); | ||
} | ||
}(); | ||
}(); // @link https://stackoverflow.com/a/25490531/719817 | ||
// @link https://stackoverflow.com/a/25490531/719817 | ||
var getCookieValue = function getCookieValue(name) { | ||
var match = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)'); | ||
var match = document.cookie.match("(^|;)\\s*".concat(name, "\\s*=\\s*([^;]+)")); | ||
return match ? match.pop() : ''; | ||
@@ -32,4 +36,8 @@ }; | ||
var checkResponse = function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(response) { | ||
var checkResponse = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee(response) { | ||
var isOK, responseText, authFailed; | ||
@@ -52,3 +60,3 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
return _context.abrupt('return', response.json()); | ||
return _context.abrupt("return", response.json()); | ||
@@ -84,3 +92,3 @@ case 4: | ||
case 14: | ||
return _context.abrupt('return', responseText); | ||
return _context.abrupt("return", responseText); | ||
@@ -91,7 +99,7 @@ case 15: | ||
case 16: | ||
case 'end': | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, _this); | ||
}, _callee, this); | ||
})); | ||
@@ -108,10 +116,10 @@ | ||
var _ref2$method = _ref2.method, | ||
method = _ref2$method === undefined ? 'GET' : _ref2$method, | ||
method = _ref2$method === void 0 ? 'GET' : _ref2$method, | ||
_ref2$credentials = _ref2.credentials, | ||
credentials = _ref2$credentials === undefined ? 'same-origin' : _ref2$credentials, | ||
credentials = _ref2$credentials === void 0 ? 'same-origin' : _ref2$credentials, | ||
_ref2$accept = _ref2.accept, | ||
accept = _ref2$accept === undefined ? 'application/json' : _ref2$accept, | ||
accept = _ref2$accept === void 0 ? 'application/json' : _ref2$accept, | ||
_ref2$headers = _ref2.headers, | ||
headers = _ref2$headers === undefined ? {} : _ref2$headers, | ||
otherArgs = _objectWithoutProperties(_ref2, ['method', 'credentials', 'accept', 'headers']); | ||
headers = _ref2$headers === void 0 ? {} : _ref2$headers, | ||
otherArgs = _objectWithoutProperties(_ref2, ["method", "credentials", "accept", "headers"]); | ||
@@ -121,7 +129,10 @@ if (isFramed) { | ||
} | ||
if (isOffline()) throw new Error('Network Error. Are you offline?'); | ||
return fetch(url.indexOf('http') === 0 ? url : getBaseURI() + url, _extends({ | ||
return fetch(url.indexOf('http') === 0 ? url : getBaseURI() + url, _objectSpread({ | ||
method: method, | ||
credentials: credentials, | ||
headers: _extends({ accept: accept }, headers) | ||
headers: _objectSpread({ | ||
accept: accept | ||
}, headers) | ||
}, otherArgs)); | ||
@@ -134,11 +145,12 @@ }; | ||
var _ref3$method = _ref3.method, | ||
method = _ref3$method === undefined ? 'POST' : _ref3$method, | ||
method = _ref3$method === void 0 ? 'POST' : _ref3$method, | ||
_ref3$headers = _ref3.headers, | ||
headers = _ref3$headers === undefined ? {} : _ref3$headers, | ||
otherArgs = _objectWithoutProperties(_ref3, ['method', 'headers']); | ||
headers = _ref3$headers === void 0 ? {} : _ref3$headers, | ||
otherArgs = _objectWithoutProperties(_ref3, ["method", "headers"]); | ||
return doFetch(url, _extends({ | ||
return doFetch(url, _objectSpread({ | ||
method: method, | ||
headers: _extends({ | ||
'X-CSRF-Token': getCookieValue('CSRFCookie') }, headers) | ||
headers: _objectSpread({ | ||
'X-CSRF-Token': getCookieValue('CSRFCookie') | ||
}, headers) | ||
}, otherArgs)); | ||
@@ -151,17 +163,17 @@ }; | ||
var _ref4$headers = _ref4.headers, | ||
headers = _ref4$headers === undefined ? {} : _ref4$headers, | ||
otherArgs = _objectWithoutProperties(_ref4, ['headers']); | ||
headers = _ref4$headers === void 0 ? {} : _ref4$headers, | ||
otherArgs = _objectWithoutProperties(_ref4, ["headers"]); | ||
return doPost(url, _extends({ | ||
headers: _extends({ | ||
return doPost(url, _objectSpread({ | ||
headers: _objectSpread({ | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | ||
}, headers) | ||
}, otherArgs)); | ||
}; | ||
}; // Set postDataBin if it has not already been set | ||
// Set postDataBin if it has not already been set | ||
var getBinaryURL = function getBinaryURL(url) { | ||
if (/[?&]postDataBin=/.test(url)) return url; | ||
var separator = url.indexOf('?') === -1 ? '?' : '&'; | ||
return url + separator + 'postDataBin=y'; | ||
return "".concat(url + separator, "postDataBin=y"); | ||
}; | ||
@@ -172,15 +184,21 @@ | ||
var _ref5$headers = _ref5.headers, | ||
headers = _ref5$headers === undefined ? {} : _ref5$headers, | ||
otherArgs = _objectWithoutProperties(_ref5, ['headers']); | ||
var body = _ref5.body, | ||
_ref5$headers = _ref5.headers, | ||
headers = _ref5$headers === void 0 ? {} : _ref5$headers, | ||
otherArgs = _objectWithoutProperties(_ref5, ["body", "headers"]); | ||
return doPost(getBinaryURL(url), _extends({ | ||
headers: _extends({ | ||
return doPost(getBinaryURL(url), _objectSpread({ | ||
headers: _objectSpread({ | ||
'Content-Type': 'application/json' | ||
}, headers) | ||
}, headers), | ||
body: typeof body === 'string' ? body : JSON.stringify(body) | ||
}, otherArgs)); | ||
}; | ||
export var json = function () { | ||
var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
export var json = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref6 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee2() { | ||
var response, | ||
@@ -193,14 +211,14 @@ _args2 = arguments; | ||
_context2.next = 2; | ||
return doFetch.apply(undefined, _args2); | ||
return doFetch.apply(void 0, _args2); | ||
case 2: | ||
response = _context2.sent; | ||
return _context2.abrupt('return', checkResponse(response)); | ||
return _context2.abrupt("return", checkResponse(response)); | ||
case 4: | ||
case 'end': | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, _this); | ||
}, _callee2, this); | ||
})); | ||
@@ -212,5 +230,8 @@ | ||
}(); | ||
export var postForm = function () { | ||
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
export var postForm = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref7 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee3() { | ||
var response, | ||
@@ -223,14 +244,14 @@ _args3 = arguments; | ||
_context3.next = 2; | ||
return doFormPost.apply(undefined, _args3); | ||
return doFormPost.apply(void 0, _args3); | ||
case 2: | ||
response = _context3.sent; | ||
return _context3.abrupt('return', checkResponse(response)); | ||
return _context3.abrupt("return", checkResponse(response)); | ||
case 4: | ||
case 'end': | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, _this); | ||
}, _callee3, this); | ||
})); | ||
@@ -242,5 +263,8 @@ | ||
}(); | ||
export var postJSON = function () { | ||
var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { | ||
export var postJSON = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref8 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee4() { | ||
var response, | ||
@@ -253,14 +277,14 @@ _args4 = arguments; | ||
_context4.next = 2; | ||
return doJSONPost.apply(undefined, _args4); | ||
return doJSONPost.apply(void 0, _args4); | ||
case 2: | ||
response = _context4.sent; | ||
return _context4.abrupt('return', checkResponse(response)); | ||
return _context4.abrupt("return", checkResponse(response)); | ||
case 4: | ||
case 'end': | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, _this); | ||
}, _callee4, this); | ||
})); | ||
@@ -272,5 +296,8 @@ | ||
}(); | ||
export var postRaw = function () { | ||
var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(url, args) { | ||
export var postRaw = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref9 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee5(url, args) { | ||
var response; | ||
@@ -286,15 +313,15 @@ return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
response = _context5.sent; | ||
return _context5.abrupt('return', checkResponse(response)); | ||
return _context5.abrupt("return", checkResponse(response)); | ||
case 4: | ||
case 'end': | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, _this); | ||
}, _callee5, this); | ||
})); | ||
return function postRaw(_x6, _x7) { | ||
return function postRaw(_x2, _x3) { | ||
return _ref9.apply(this, arguments); | ||
}; | ||
}(); |
{ | ||
"name": "@highpoint/js-fetch", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "HighPoint Fetch Library", | ||
@@ -15,11 +15,12 @@ "main": "dist/js-fetch.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@highpoint/eslint-config": "^0.1.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^8.2.6", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-eslint": "^10.0.1", | ||
"chai": "^4.1.2", | ||
"eslint": "^5.2.0", | ||
"eslint-config-prettier": "^2.9.0", | ||
"husky": "^0.14.3", | ||
"eslint-config-prettier": "^3.3.0", | ||
"husky": "^1.1.4", | ||
"karma": "^3.0.0", | ||
@@ -29,3 +30,3 @@ "karma-chai": "^0.1.0", | ||
"karma-mocha": "^1.3.0", | ||
"lint-staged": "^7.2.0", | ||
"lint-staged": "^8.0.4", | ||
"mkdirp": "^0.5.1", | ||
@@ -43,6 +44,10 @@ "mocha": "^5.2.0", | ||
"prebuild": "mkdirp dist", | ||
"precommit": "yarn test && lint-staged", | ||
"prepack": "run-s clean build", | ||
"test": "karma start karma.conf.js --single-run" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "yarn test && lint-staged" | ||
} | ||
} | ||
} |
@@ -60,6 +60,6 @@ # js-fetch | ||
const jsonResponse = await postJSON('https://api.example.com', { | ||
body: JSON.stringify({ | ||
body: { | ||
value1: 1, | ||
value2: 2 | ||
}) | ||
} | ||
}); | ||
@@ -66,0 +66,0 @@ console.log(jsonResponse); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15600
260
21
2