Repository URL to install this package:
|
Version:
1.13.4 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
/**
* Fetch error handler
* @todo Do proper error handling here
* @param {*} error - Error response from the API
* @throws Will throw passed error
*/
var errorHandler = function errorHandler(error) {
throw error;
};
/**
* Fetch response handler
* @async
* @param {Response} response - Response from the API
* @return {Promise<*>} Parsed response data
*/
var responseHandler = /*#__PURE__*/function () {
var _ref = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(response) {
var headers, status, contentType, isJson, error;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
headers = response.headers, status = response.status;
contentType = headers.get('content-type');
isJson = contentType && contentType.includes('application/json');
if (!(status >= 200 && status < 300)) {
_context.next = 5;
break;
}
return _context.abrupt("return", isJson ? response.json() : response.text());
case 5:
_context.next = 7;
return isJson ? response.json() : response.text();
case 7:
error = _context.sent;
return _context.abrupt("return", Promise.reject(error));
case 9:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function responseHandler(_x) {
return _ref.apply(this, arguments);
};
}();
/**
* A custom Fetch wrapper implementation
* @async
* @param {Object} request - Request passed
* @param {string} request.url - Endpoint url
* @param {string} [request.method=GET] - Fetch method
* @param {Object} [request.headers={}] - Object with header attributes required for the request
* @param {?string} [request.body=null] - Body of the request. Note: Objects must be stringified in advance
* @param {?boolean} [request.asBeacon=false] - Tries to send a simpler beacon if API is available in browser
* @return {Promise<*|boolean>} Payload response in case of a request or queueing success flag in case of a beacon
*/
var fetchApi = /*#__PURE__*/function () {
var _ref3 = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref2) {
var url, _ref2$method, method, _ref2$headers, headers, _ref2$body, body, _ref2$asBeacon, asBeacon, contentTypeKey, type, restHeaders, formattedHeaders, beacon, isBeaconQueued, response;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
url = _ref2.url, _ref2$method = _ref2.method, method = _ref2$method === void 0 ? 'GET' : _ref2$method, _ref2$headers = _ref2.headers, headers = _ref2$headers === void 0 ? {} : _ref2$headers, _ref2$body = _ref2.body, body = _ref2$body === void 0 ? null : _ref2$body, _ref2$asBeacon = _ref2.asBeacon, asBeacon = _ref2$asBeacon === void 0 ? false : _ref2$asBeacon;
_context2.prev = 1;
if (!(asBeacon && navigator.sendBeacon)) {
_context2.next = 14;
break;
}
contentTypeKey = 'Content-Type';
type = headers[contentTypeKey], restHeaders = _rollupPluginBabelHelpers.objectWithoutProperties(headers, [contentTypeKey].map(_rollupPluginBabelHelpers.toPropertyKey));
formattedHeaders = _rollupPluginBabelHelpers.objectSpread2({
type: type
}, restHeaders);
beacon = new Blob([body], formattedHeaders);
_context2.prev = 7;
isBeaconQueued = navigator.sendBeacon(url, beacon);
return _context2.abrupt("return", isBeaconQueued);
case 12:
_context2.prev = 12;
_context2.t0 = _context2["catch"](7);
case 14:
_context2.next = 16;
return fetch(url, {
method: method,
headers: headers,
body: body
});
case 16:
response = _context2.sent;
_context2.next = 19;
return responseHandler(response);
case 19:
return _context2.abrupt("return", _context2.sent);
case 22:
_context2.prev = 22;
_context2.t1 = _context2["catch"](1);
return _context2.abrupt("return", errorHandler(_context2.t1));
case 25:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[1, 22], [7, 12]]);
}));
return function fetchApi(_x2) {
return _ref3.apply(this, arguments);
};
}();
exports.errorHandler = errorHandler;
exports.fetchApi = fetchApi;
exports.responseHandler = responseHandler;
//# sourceMappingURL=fetch.js.map