@rpldy/sender
Advanced tools
Comparing version 0.11.4 to 0.11.5
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.XHR_SENDER_TYPE = void 0; | ||
var XHR_SENDER_TYPE = "rpldy-sender"; | ||
const XHR_SENDER_TYPE = "rpldy-sender"; | ||
exports.XHR_SENDER_TYPE = XHR_SENDER_TYPE; |
@@ -8,4 +8,4 @@ "use strict"; | ||
enumerable: true, | ||
get: function get() { | ||
return _xhrSender["default"]; | ||
get: function () { | ||
return _xhrSender.default; | ||
} | ||
@@ -15,3 +15,3 @@ }); | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _consts.XHR_SENDER_TYPE; | ||
@@ -22,7 +22,7 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
return _MissingUrlError["default"]; | ||
get: function () { | ||
return _MissingUrlError.default; | ||
} | ||
}); | ||
exports.send = exports["default"] = void 0; | ||
exports.send = exports.default = void 0; | ||
@@ -35,7 +35,7 @@ var _xhrSender = _interopRequireDefault(require("./xhrSender/xhrSender")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var send = (0, _xhrSender["default"])(); | ||
const send = (0, _xhrSender.default)(); | ||
exports.send = send; | ||
var _default = send; | ||
exports["default"] = _default; | ||
exports.default = _default; |
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
function _construct() { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function (o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var MissingUrlError = /*#__PURE__*/function (_Error) { | ||
_inherits(MissingUrlError, _Error); | ||
var _super = _createSuper(MissingUrlError); | ||
function MissingUrlError(senderType) { | ||
var _this; | ||
_classCallCheck(this, MissingUrlError); | ||
_this = _super.call(this, "".concat(senderType, " didn't receive upload URL")); | ||
_this.name = "MissingUrlError"; | ||
return _this; | ||
class MissingUrlError extends Error { | ||
constructor(senderType) { | ||
super(`${senderType} didn't receive upload URL`); | ||
this.name = "MissingUrlError"; | ||
} | ||
return MissingUrlError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
} | ||
exports["default"] = MissingUrlError; | ||
exports.default = MissingUrlError; |
@@ -6,18 +6,6 @@ "use strict"; | ||
}); | ||
exports["default"] = void 0; | ||
exports.default = void 0; | ||
var _shared = require("@rpldy/shared"); | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
/** | ||
@@ -27,25 +15,21 @@ * mimics FormData.set() when its not available (react-native) | ||
*/ | ||
var addToFormData = function (fd, name) { | ||
for (var _len = arguments.length, rest = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
rest[_key - 2] = arguments[_key]; | ||
} | ||
const addToFormData = (fd, name, ...rest) => { | ||
//rest = [value, fileName = undefined] | ||
if (fd.set) { | ||
// $FlowFixMe - ignore flow for not allowing FileLike here | ||
fd.set.apply(fd, [name].concat(rest)); | ||
fd.set(name, ...rest); | ||
} else { | ||
if (fd["delete"]) { | ||
fd["delete"](name); | ||
if (fd.delete) { | ||
fd.delete(name); | ||
} // $FlowFixMe - ignore flow for not allowing FileLike here | ||
fd.append.apply(fd, [name].concat(rest)); | ||
fd.append(name, ...rest); | ||
} | ||
}; | ||
var getFormFileField = function (fd, items, options) { | ||
var single = items.length === 1; | ||
items.forEach(function (item, i) { | ||
var name = single ? options.paramName : (0, _shared.isFunction)(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : "".concat(options.paramName, "[").concat(i, "]"); | ||
const getFormFileField = (fd, items, options) => { | ||
const single = items.length === 1; | ||
items.forEach((item, i) => { | ||
const name = single ? options.paramName : (0, _shared.isFunction)(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : `${options.paramName}[${i}]`; | ||
@@ -60,13 +44,7 @@ if (item.file) { | ||
var prepareFormData = function (items, options) { | ||
var fd = new FormData(); | ||
const prepareFormData = (items, options) => { | ||
const fd = new FormData(); | ||
if (options.params) { | ||
Object.entries(options.params).forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
key = _ref2[0], | ||
val = _ref2[1]; | ||
return addToFormData(fd, key, val); | ||
}); | ||
Object.entries(options.params).forEach(([key, val]) => addToFormData(fd, key, val)); | ||
} | ||
@@ -79,2 +57,2 @@ | ||
var _default = prepareFormData; | ||
exports["default"] = _default; | ||
exports.default = _default; |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports["default"] = exports.SUCCESS_CODES = void 0; | ||
exports.default = exports.SUCCESS_CODES = void 0; | ||
@@ -17,28 +17,22 @@ var _shared = require("@rpldy/shared"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
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; } | ||
var SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
const SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
exports.SUCCESS_CODES = SUCCESS_CODES; | ||
var getRequestData = function (items, options) { | ||
var data; | ||
const getRequestData = (items, options) => { | ||
let data; | ||
if (options.sendWithFormData) { | ||
_shared.logger.debugLog("uploady.sender: sending ".concat(items.length, " item(s) as form data")); | ||
_shared.logger.debugLog(`uploady.sender: sending ${items.length} item(s) as form data`); | ||
data = (0, _prepareFormData["default"])(items, options); | ||
data = (0, _prepareFormData.default)(items, options); | ||
} else { | ||
if (items.length > 1) { | ||
throw new Error("XHR Sender - Request without form data can only contain 1 item. received ".concat(items.length)); | ||
throw new Error(`XHR Sender - Request without form data can only contain 1 item. received ${items.length}`); | ||
} | ||
var item = items[0]; | ||
const item = items[0]; | ||
_shared.logger.debugLog("uploady.sender: sending item ".concat(item.id, " as request body")); | ||
_shared.logger.debugLog(`uploady.sender: sending item ${item.id} as request body`); | ||
@@ -51,13 +45,10 @@ data = item.file || item.url; | ||
var makeRequest = function (items, url, options, onProgress, config) { | ||
var xhr; | ||
var data = (config === null || config === void 0 ? void 0 : config.getRequestData) ? config.getRequestData(items, options) : getRequestData(items, options); | ||
const makeRequest = (items, url, options, onProgress, config) => { | ||
let xhr; | ||
const data = config !== null && config !== void 0 && config.getRequestData ? config.getRequestData(items, options) : getRequestData(items, options); | ||
var issueRequest = function () { | ||
var requestUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url; | ||
var requestData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : data; | ||
var requestOptions = arguments.length > 2 ? arguments[2] : undefined; | ||
requestOptions = (0, _shared.merge)(_objectSpread(_objectSpread({}, (0, _shared.pick)(options, ["method", "headers", "withCredentials"])), {}, { | ||
preSend: function preSend(req) { | ||
req.upload.onprogress = function (e) { | ||
const issueRequest = (requestUrl = url, requestData = data, requestOptions) => { | ||
requestOptions = (0, _shared.merge)({ ...(0, _shared.pick)(options, ["method", "headers", "withCredentials"]), | ||
preSend: req => { | ||
req.upload.onprogress = e => { | ||
if (e.lengthComputable && onProgress) { | ||
@@ -68,4 +59,4 @@ onProgress(e, items.slice()); | ||
} | ||
}), requestOptions); | ||
var realPXhr = (0, _shared.request)(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
}, requestOptions); | ||
const realPXhr = (0, _shared.request)(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
@@ -77,10 +68,8 @@ xhr = realPXhr.xhr; | ||
var pXhr = (config === null || config === void 0 ? void 0 : config.preRequestHandler) ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
const pXhr = config !== null && config !== void 0 && config.preRequestHandler ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
return { | ||
url: url, | ||
url, | ||
count: items.length, | ||
pXhr: pXhr, | ||
getXhr: function getXhr() { | ||
return xhr; | ||
}, | ||
pXhr, | ||
getXhr: () => xhr, | ||
aborted: false | ||
@@ -90,10 +79,10 @@ }; | ||
var parseResponseJson = function (response, headers, options) { | ||
var parsed = response; | ||
var ct = headers === null || headers === void 0 ? void 0 : headers["content-type"]; | ||
const parseResponseJson = (response, headers, options) => { | ||
let parsed = response; | ||
const ct = headers === null || headers === void 0 ? void 0 : headers["content-type"]; | ||
if (options.forceJsonResponse || (ct === null || ct === void 0 ? void 0 : ct.includes("json"))) { | ||
if (options.forceJsonResponse || ct !== null && ct !== void 0 && ct.includes("json")) { | ||
try { | ||
parsed = JSON.parse(response); | ||
} catch (_unused) {//silent fail | ||
} catch {//silent fail | ||
} | ||
@@ -105,52 +94,52 @@ } | ||
var processResponse = function (sendRequest, options) { | ||
return sendRequest.pXhr.then(function (xhr) { | ||
var _options$formatServer, _options$formatServer2; | ||
const processResponse = (sendRequest, options) => sendRequest.pXhr.then(xhr => { | ||
var _options$formatServer, _options$formatServer2; | ||
var state, response, status; | ||
let state, response, status; | ||
_shared.logger.debugLog("uploady.sender: received upload response ", xhr); | ||
_shared.logger.debugLog("uploady.sender: received upload response ", xhr); | ||
state = ~SUCCESS_CODES.indexOf(xhr.status) ? _shared.FILE_STATES.FINISHED : _shared.FILE_STATES.ERROR; | ||
status = xhr.status; | ||
var resHeaders = (0, _shared.parseResponseHeaders)(xhr); | ||
response = { | ||
data: (_options$formatServer = (_options$formatServer2 = options.formatServerResponse) === null || _options$formatServer2 === void 0 ? void 0 : _options$formatServer2.call(options, xhr.response, status, resHeaders)) !== null && _options$formatServer !== void 0 ? _options$formatServer : parseResponseJson(xhr.response, resHeaders, options), | ||
headers: resHeaders | ||
}; | ||
return { | ||
status: status, | ||
state: state, | ||
response: response | ||
}; | ||
})["catch"](function (error) { | ||
var state, response; | ||
state = ~SUCCESS_CODES.indexOf(xhr.status) ? _shared.FILE_STATES.FINISHED : _shared.FILE_STATES.ERROR; | ||
status = xhr.status; | ||
const resHeaders = (0, _shared.parseResponseHeaders)(xhr); | ||
response = { | ||
data: (_options$formatServer = (_options$formatServer2 = options.formatServerResponse) === null || _options$formatServer2 === void 0 ? void 0 : _options$formatServer2.call(options, xhr.response, status, resHeaders)) !== null && _options$formatServer !== void 0 ? _options$formatServer : parseResponseJson(xhr.response, resHeaders, options), | ||
headers: resHeaders | ||
}; | ||
return { | ||
status, | ||
state, | ||
response | ||
}; | ||
}).catch(error => { | ||
let state, response; | ||
if (sendRequest.aborted) { | ||
state = _shared.FILE_STATES.ABORTED; | ||
response = "aborted"; | ||
} else { | ||
_shared.logger.debugLog("uploady.sender: upload failed: ", error); | ||
if (sendRequest.aborted) { | ||
state = _shared.FILE_STATES.ABORTED; | ||
response = "aborted"; | ||
} else { | ||
_shared.logger.debugLog("uploady.sender: upload failed: ", error); | ||
state = _shared.FILE_STATES.ERROR; | ||
response = error; | ||
} | ||
state = _shared.FILE_STATES.ERROR; | ||
response = error; | ||
} | ||
return { | ||
error: true, | ||
state: state, | ||
response: response, | ||
status: 0 | ||
}; | ||
}); | ||
}; | ||
return { | ||
error: true, | ||
state, | ||
response, | ||
status: 0 | ||
}; | ||
}); | ||
var abortRequest = function (sendRequest) { | ||
var abortCalled = false; | ||
var aborted = sendRequest.aborted, | ||
getXhr = sendRequest.getXhr; | ||
var xhr = getXhr(); | ||
const abortRequest = sendRequest => { | ||
let abortCalled = false; | ||
const { | ||
aborted, | ||
getXhr | ||
} = sendRequest; | ||
const xhr = getXhr(); | ||
if (!aborted && xhr && xhr.readyState && xhr.readyState !== 4) { | ||
_shared.logger.debugLog("uploady.sender: cancelling request with ".concat(sendRequest.count, " items to: ").concat(sendRequest.url)); | ||
_shared.logger.debugLog(`uploady.sender: cancelling request with ${sendRequest.count} items to: ${sendRequest.url}`); | ||
@@ -165,22 +154,18 @@ xhr.abort(); | ||
var getXhrSend = function (config) { | ||
return function (items, url, options, onProgress) { | ||
if (!url) { | ||
throw new _MissingUrlError["default"](_consts.XHR_SENDER_TYPE); | ||
} | ||
const getXhrSend = config => (items, url, options, onProgress) => { | ||
if (!url) { | ||
throw new _MissingUrlError.default(_consts.XHR_SENDER_TYPE); | ||
} | ||
_shared.logger.debugLog("uploady.sender: sending file: ", { | ||
items: items, | ||
url: url, | ||
options: options | ||
}); | ||
_shared.logger.debugLog("uploady.sender: sending file: ", { | ||
items, | ||
url, | ||
options | ||
}); | ||
var sendRequest = makeRequest(items, url, options, onProgress, config); | ||
return { | ||
request: processResponse(sendRequest, options), | ||
abort: function abort() { | ||
return abortRequest(sendRequest); | ||
}, | ||
senderType: _consts.XHR_SENDER_TYPE | ||
}; | ||
const sendRequest = makeRequest(items, url, options, onProgress, config); | ||
return { | ||
request: processResponse(sendRequest, options), | ||
abort: () => abortRequest(sendRequest), | ||
senderType: _consts.XHR_SENDER_TYPE | ||
}; | ||
@@ -190,2 +175,2 @@ }; | ||
var _default = getXhrSend; | ||
exports["default"] = _default; | ||
exports.default = _default; |
@@ -1,1 +0,1 @@ | ||
export var XHR_SENDER_TYPE = "rpldy-sender"; | ||
export const XHR_SENDER_TYPE = "rpldy-sender"; |
import getXhrSend from "./xhrSender/xhrSender"; | ||
import { XHR_SENDER_TYPE } from "./consts"; | ||
var send = getXhrSend(); | ||
const send = getXhrSend(); | ||
export default send; | ||
export { send, getXhrSend, XHR_SENDER_TYPE }; | ||
export { default as MissingUrlError } from "./MissingUrlError"; |
@@ -1,43 +0,7 @@ | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
function _construct() { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function (o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var MissingUrlError = /*#__PURE__*/function (_Error) { | ||
_inherits(MissingUrlError, _Error); | ||
var _super = _createSuper(MissingUrlError); | ||
function MissingUrlError(senderType) { | ||
var _this; | ||
_classCallCheck(this, MissingUrlError); | ||
_this = _super.call(this, "".concat(senderType, " didn't receive upload URL")); | ||
_this.name = "MissingUrlError"; | ||
return _this; | ||
export default class MissingUrlError extends Error { | ||
constructor(senderType) { | ||
super(`${senderType} didn't receive upload URL`); | ||
this.name = "MissingUrlError"; | ||
} | ||
return MissingUrlError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
export { MissingUrlError as default }; | ||
} |
@@ -1,13 +0,1 @@ | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
import { isFunction } from "@rpldy/shared"; | ||
@@ -19,25 +7,21 @@ | ||
*/ | ||
var addToFormData = function (fd, name) { | ||
for (var _len = arguments.length, rest = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
rest[_key - 2] = arguments[_key]; | ||
} | ||
const addToFormData = (fd, name, ...rest) => { | ||
//rest = [value, fileName = undefined] | ||
if (fd.set) { | ||
// $FlowFixMe - ignore flow for not allowing FileLike here | ||
fd.set.apply(fd, [name].concat(rest)); | ||
fd.set(name, ...rest); | ||
} else { | ||
if (fd["delete"]) { | ||
fd["delete"](name); | ||
if (fd.delete) { | ||
fd.delete(name); | ||
} // $FlowFixMe - ignore flow for not allowing FileLike here | ||
fd.append.apply(fd, [name].concat(rest)); | ||
fd.append(name, ...rest); | ||
} | ||
}; | ||
var getFormFileField = function (fd, items, options) { | ||
var single = items.length === 1; | ||
items.forEach(function (item, i) { | ||
var name = single ? options.paramName : isFunction(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : "".concat(options.paramName, "[").concat(i, "]"); | ||
const getFormFileField = (fd, items, options) => { | ||
const single = items.length === 1; | ||
items.forEach((item, i) => { | ||
const name = single ? options.paramName : isFunction(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : `${options.paramName}[${i}]`; | ||
@@ -52,13 +36,7 @@ if (item.file) { | ||
var prepareFormData = function (items, options) { | ||
var fd = new FormData(); | ||
const prepareFormData = (items, options) => { | ||
const fd = new FormData(); | ||
if (options.params) { | ||
Object.entries(options.params).forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
key = _ref2[0], | ||
val = _ref2[1]; | ||
return addToFormData(fd, key, val); | ||
}); | ||
Object.entries(options.params).forEach(([key, val]) => addToFormData(fd, key, val)); | ||
} | ||
@@ -65,0 +43,0 @@ |
@@ -1,7 +0,1 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
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; } | ||
import { logger, FILE_STATES, request, parseResponseHeaders, pick, merge } from "@rpldy/shared"; | ||
@@ -11,17 +5,17 @@ import { XHR_SENDER_TYPE } from "../consts"; | ||
import prepareFormData from "./prepareFormData"; | ||
export var SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
export const SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
var getRequestData = function (items, options) { | ||
var data; | ||
const getRequestData = (items, options) => { | ||
let data; | ||
if (options.sendWithFormData) { | ||
logger.debugLog("uploady.sender: sending ".concat(items.length, " item(s) as form data")); | ||
logger.debugLog(`uploady.sender: sending ${items.length} item(s) as form data`); | ||
data = prepareFormData(items, options); | ||
} else { | ||
if (items.length > 1) { | ||
throw new Error("XHR Sender - Request without form data can only contain 1 item. received ".concat(items.length)); | ||
throw new Error(`XHR Sender - Request without form data can only contain 1 item. received ${items.length}`); | ||
} | ||
var item = items[0]; | ||
logger.debugLog("uploady.sender: sending item ".concat(item.id, " as request body")); | ||
const item = items[0]; | ||
logger.debugLog(`uploady.sender: sending item ${item.id} as request body`); | ||
data = item.file || item.url; | ||
@@ -33,13 +27,10 @@ } | ||
var makeRequest = function (items, url, options, onProgress, config) { | ||
var xhr; | ||
var data = (config === null || config === void 0 ? void 0 : config.getRequestData) ? config.getRequestData(items, options) : getRequestData(items, options); | ||
const makeRequest = (items, url, options, onProgress, config) => { | ||
let xhr; | ||
const data = config !== null && config !== void 0 && config.getRequestData ? config.getRequestData(items, options) : getRequestData(items, options); | ||
var issueRequest = function () { | ||
var requestUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url; | ||
var requestData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : data; | ||
var requestOptions = arguments.length > 2 ? arguments[2] : undefined; | ||
requestOptions = merge(_objectSpread(_objectSpread({}, pick(options, ["method", "headers", "withCredentials"])), {}, { | ||
preSend: function preSend(req) { | ||
req.upload.onprogress = function (e) { | ||
const issueRequest = (requestUrl = url, requestData = data, requestOptions) => { | ||
requestOptions = merge({ ...pick(options, ["method", "headers", "withCredentials"]), | ||
preSend: req => { | ||
req.upload.onprogress = e => { | ||
if (e.lengthComputable && onProgress) { | ||
@@ -50,4 +41,4 @@ onProgress(e, items.slice()); | ||
} | ||
}), requestOptions); | ||
var realPXhr = request(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
}, requestOptions); | ||
const realPXhr = request(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
@@ -59,10 +50,8 @@ xhr = realPXhr.xhr; | ||
var pXhr = (config === null || config === void 0 ? void 0 : config.preRequestHandler) ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
const pXhr = config !== null && config !== void 0 && config.preRequestHandler ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
return { | ||
url: url, | ||
url, | ||
count: items.length, | ||
pXhr: pXhr, | ||
getXhr: function getXhr() { | ||
return xhr; | ||
}, | ||
pXhr, | ||
getXhr: () => xhr, | ||
aborted: false | ||
@@ -72,10 +61,10 @@ }; | ||
var parseResponseJson = function (response, headers, options) { | ||
var parsed = response; | ||
var ct = headers === null || headers === void 0 ? void 0 : headers["content-type"]; | ||
const parseResponseJson = (response, headers, options) => { | ||
let parsed = response; | ||
const ct = headers === null || headers === void 0 ? void 0 : headers["content-type"]; | ||
if (options.forceJsonResponse || (ct === null || ct === void 0 ? void 0 : ct.includes("json"))) { | ||
if (options.forceJsonResponse || ct !== null && ct !== void 0 && ct.includes("json")) { | ||
try { | ||
parsed = JSON.parse(response); | ||
} catch (_unused) {//silent fail | ||
} catch {//silent fail | ||
} | ||
@@ -87,49 +76,49 @@ } | ||
var processResponse = function (sendRequest, options) { | ||
return sendRequest.pXhr.then(function (xhr) { | ||
var _options$formatServer, _options$formatServer2; | ||
const processResponse = (sendRequest, options) => sendRequest.pXhr.then(xhr => { | ||
var _options$formatServer, _options$formatServer2; | ||
var state, response, status; | ||
logger.debugLog("uploady.sender: received upload response ", xhr); | ||
state = ~SUCCESS_CODES.indexOf(xhr.status) ? FILE_STATES.FINISHED : FILE_STATES.ERROR; | ||
status = xhr.status; | ||
var resHeaders = parseResponseHeaders(xhr); | ||
response = { | ||
data: (_options$formatServer = (_options$formatServer2 = options.formatServerResponse) === null || _options$formatServer2 === void 0 ? void 0 : _options$formatServer2.call(options, xhr.response, status, resHeaders)) !== null && _options$formatServer !== void 0 ? _options$formatServer : parseResponseJson(xhr.response, resHeaders, options), | ||
headers: resHeaders | ||
}; | ||
return { | ||
status: status, | ||
state: state, | ||
response: response | ||
}; | ||
})["catch"](function (error) { | ||
var state, response; | ||
let state, response, status; | ||
logger.debugLog("uploady.sender: received upload response ", xhr); | ||
state = ~SUCCESS_CODES.indexOf(xhr.status) ? FILE_STATES.FINISHED : FILE_STATES.ERROR; | ||
status = xhr.status; | ||
const resHeaders = parseResponseHeaders(xhr); | ||
response = { | ||
data: (_options$formatServer = (_options$formatServer2 = options.formatServerResponse) === null || _options$formatServer2 === void 0 ? void 0 : _options$formatServer2.call(options, xhr.response, status, resHeaders)) !== null && _options$formatServer !== void 0 ? _options$formatServer : parseResponseJson(xhr.response, resHeaders, options), | ||
headers: resHeaders | ||
}; | ||
return { | ||
status, | ||
state, | ||
response | ||
}; | ||
}).catch(error => { | ||
let state, response; | ||
if (sendRequest.aborted) { | ||
state = FILE_STATES.ABORTED; | ||
response = "aborted"; | ||
} else { | ||
logger.debugLog("uploady.sender: upload failed: ", error); | ||
state = FILE_STATES.ERROR; | ||
response = error; | ||
} | ||
if (sendRequest.aborted) { | ||
state = FILE_STATES.ABORTED; | ||
response = "aborted"; | ||
} else { | ||
logger.debugLog("uploady.sender: upload failed: ", error); | ||
state = FILE_STATES.ERROR; | ||
response = error; | ||
} | ||
return { | ||
error: true, | ||
state: state, | ||
response: response, | ||
status: 0 | ||
}; | ||
}); | ||
}; | ||
return { | ||
error: true, | ||
state, | ||
response, | ||
status: 0 | ||
}; | ||
}); | ||
var abortRequest = function (sendRequest) { | ||
var abortCalled = false; | ||
var aborted = sendRequest.aborted, | ||
getXhr = sendRequest.getXhr; | ||
var xhr = getXhr(); | ||
const abortRequest = sendRequest => { | ||
let abortCalled = false; | ||
const { | ||
aborted, | ||
getXhr | ||
} = sendRequest; | ||
const xhr = getXhr(); | ||
if (!aborted && xhr && xhr.readyState && xhr.readyState !== 4) { | ||
logger.debugLog("uploady.sender: cancelling request with ".concat(sendRequest.count, " items to: ").concat(sendRequest.url)); | ||
logger.debugLog(`uploady.sender: cancelling request with ${sendRequest.count} items to: ${sendRequest.url}`); | ||
xhr.abort(); | ||
@@ -143,21 +132,17 @@ sendRequest.aborted = true; | ||
var getXhrSend = function (config) { | ||
return function (items, url, options, onProgress) { | ||
if (!url) { | ||
throw new MissingUrlError(XHR_SENDER_TYPE); | ||
} | ||
const getXhrSend = config => (items, url, options, onProgress) => { | ||
if (!url) { | ||
throw new MissingUrlError(XHR_SENDER_TYPE); | ||
} | ||
logger.debugLog("uploady.sender: sending file: ", { | ||
items: items, | ||
url: url, | ||
options: options | ||
}); | ||
var sendRequest = makeRequest(items, url, options, onProgress, config); | ||
return { | ||
request: processResponse(sendRequest, options), | ||
abort: function abort() { | ||
return abortRequest(sendRequest); | ||
}, | ||
senderType: XHR_SENDER_TYPE | ||
}; | ||
logger.debugLog("uploady.sender: sending file: ", { | ||
items, | ||
url, | ||
options | ||
}); | ||
const sendRequest = makeRequest(items, url, options, onProgress, config); | ||
return { | ||
request: processResponse(sendRequest, options), | ||
abort: () => abortRequest(sendRequest), | ||
senderType: XHR_SENDER_TYPE | ||
}; | ||
@@ -164,0 +149,0 @@ }; |
{ | ||
"version": "0.11.4", | ||
"version": "0.11.5", | ||
"name": "@rpldy/sender", | ||
@@ -21,6 +21,6 @@ "description": "react-uploady's default XHR sender", | ||
"dependencies": { | ||
"@rpldy/shared": "^0.11.4" | ||
"@rpldy/shared": "^0.11.5" | ||
}, | ||
"devDependencies": { | ||
"flow-bin": "^0.146.0" | ||
"flow-bin": "^0.147.0" | ||
}, | ||
@@ -30,3 +30,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "ffa687483c1ff9d6d2df3c992810b030943482b6" | ||
"gitHead": "39a78c4a9cf417433bd5989d65dc84f358952780" | ||
} |
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
18896
448
Updated@rpldy/shared@^0.11.5