@marcopeg/utils
Advanced tools
Comparing version 1.8.9 to 1.9.0
@@ -336,2 +336,92 @@ 'use strict'; | ||
var deleteJSON = function () { | ||
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(url) { | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var headers, options, res, errMsg, error; | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.prev = 0; | ||
headers = Object.assign({}, config.headers || {}, { | ||
'content-type': 'application/json' | ||
}); | ||
options = Object.assign({}, config, { | ||
method: 'DELETE', | ||
headers: headers | ||
}); | ||
_context5.next = 5; | ||
return wrappedFetch(url, options); | ||
case 5: | ||
res = _context5.sent; | ||
if (!(res.status !== 200)) { | ||
_context5.next = 22; | ||
break; | ||
} | ||
errMsg = void 0; | ||
_context5.prev = 8; | ||
_context5.next = 11; | ||
return res.text(); | ||
case 11: | ||
errMsg = _context5.sent; | ||
_context5.next = 17; | ||
break; | ||
case 14: | ||
_context5.prev = 14; | ||
_context5.t0 = _context5['catch'](8); | ||
errMsg = res.statusText; | ||
case 17: | ||
error = new Error(res.status + ' - ' + errMsg); | ||
error.response = res; | ||
throw error; | ||
case 22: | ||
_context5.next = 24; | ||
return res.json(); | ||
case 24: | ||
return _context5.abrupt('return', _context5.sent); | ||
case 25: | ||
_context5.next = 30; | ||
break; | ||
case 27: | ||
_context5.prev = 27; | ||
_context5.t1 = _context5['catch'](0); | ||
throw _context5.t1; | ||
case 30: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, undefined, [[0, 27], [8, 14]]); | ||
})); | ||
return function deleteJSON(_x16) { | ||
return _ref5.apply(this, arguments); | ||
}; | ||
}(); | ||
var deleteJSONAuth = function deleteJSONAuth(url, token) { | ||
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = Object.assign({}, config.headers || {}, { | ||
Authorization: 'Bearer ' + token | ||
}); | ||
var options = Object.assign({}, config, { | ||
headers: headers | ||
}); | ||
return deleteJSON(url, options); | ||
}; | ||
// export sub modules as part of the main function | ||
@@ -345,3 +435,5 @@ wrappedFetch.setDevDelay = setDevDelay; | ||
wrappedFetch.putJSONAuth = putJSONAuth; | ||
wrappedFetch.deleteJSON = deleteJSON; | ||
wrappedFetch.deleteJSONAuth = deleteJSONAuth; | ||
module.exports = wrappedFetch; |
{ | ||
"name": "@marcopeg/utils", | ||
"version": "1.8.9", | ||
"version": "1.9.0", | ||
"description": "Javascript utils for server and client projects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -153,2 +153,44 @@ /* global fetch */ | ||
const deleteJSON = async (url, config = {}) => { | ||
try { | ||
const headers = Object.assign({}, config.headers || {}, { | ||
'content-type': 'application/json', | ||
}) | ||
const options = Object.assign({}, config, { | ||
method: 'DELETE', | ||
headers: headers, | ||
}) | ||
const res = await wrappedFetch(url, options) | ||
// status code error handling | ||
if (res.status !== 200) { | ||
let errMsg | ||
try { | ||
errMsg = await res.text() | ||
} catch (err) { | ||
errMsg = res.statusText | ||
} | ||
const error = new Error(`${res.status} - ${errMsg}`) | ||
error.response = res | ||
throw error | ||
} else { | ||
return await res.json() | ||
} | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
const deleteJSONAuth = (url, token, config = {}) => { | ||
const headers = Object.assign({}, config.headers || {}, { | ||
Authorization: `Bearer ${token}`, | ||
}) | ||
const options = Object.assign({}, config, { | ||
headers: headers, | ||
}) | ||
return deleteJSON(url, options) | ||
} | ||
// export sub modules as part of the main function | ||
@@ -162,3 +204,5 @@ wrappedFetch.setDevDelay = setDevDelay | ||
wrappedFetch.putJSONAuth = putJSONAuth | ||
wrappedFetch.deleteJSON = deleteJSON | ||
wrappedFetch.deleteJSONAuth = deleteJSONAuth | ||
module.exports = wrappedFetch |
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
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
77767
709