@rpldy/sender
Advanced tools
Comparing version 1.3.1 to 1.4.0-rc.0
@@ -26,11 +26,6 @@ "use strict"; | ||
exports.send = void 0; | ||
var _xhrSender = _interopRequireDefault(require("./xhrSender/xhrSender")); | ||
var _consts = require("./consts"); | ||
var _MissingUrlError = _interopRequireDefault(require("./MissingUrlError")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const send = (0, _xhrSender.default)(); | ||
@@ -37,0 +32,0 @@ exports.send = send; |
@@ -7,3 +7,2 @@ "use strict"; | ||
exports.default = void 0; | ||
class MissingUrlError extends Error { | ||
@@ -14,5 +13,3 @@ constructor(senderType) { | ||
} | ||
} | ||
exports.default = MissingUrlError; |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.default = void 0; | ||
var _shared = require("@rpldy/shared"); | ||
/** | ||
@@ -19,6 +17,4 @@ * mimics FormData.set() when its not available (react-native) | ||
} | ||
//rest = [value, fileName = undefined] | ||
if ("set" in fd) { | ||
//$FlowExpectedError[speculation-ambiguous] - ignore flow for not allowing FileLike here | ||
fd.set(name, ...rest); | ||
@@ -28,9 +24,6 @@ } else { | ||
fd.delete(name); | ||
} //$FlowExpectedError[speculation-ambiguous] - ignore flow for not allowing FileLike here | ||
} | ||
fd.append(name, ...rest); | ||
} | ||
}; | ||
const getFormFileField = (fd, items, options) => { | ||
@@ -40,3 +33,2 @@ const single = items.length === 1; | ||
const name = single ? options.paramName : (0, _shared.isFunction)(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : `${options.paramName}[${i}]`; | ||
if (item.file) { | ||
@@ -49,10 +41,7 @@ addToFormData(fd, name, item.file, item.file.name); | ||
}; | ||
const prepareFormData = (items, options) => { | ||
const fd = new FormData(); | ||
if (options.params) { | ||
Object.entries(options.params).forEach(_ref => { | ||
let [key, val] = _ref; | ||
if (options.formDataAllowUndefined || val !== undefined) { | ||
@@ -63,8 +52,6 @@ addToFormData(fd, key, val); | ||
} | ||
getFormFileField(fd, items, options); | ||
return fd; | ||
}; | ||
var _default = prepareFormData; | ||
exports.default = _default; |
@@ -7,22 +7,13 @@ "use strict"; | ||
exports.default = exports.SUCCESS_CODES = void 0; | ||
var _shared = require("@rpldy/shared"); | ||
var _consts = require("../consts"); | ||
var _MissingUrlError = _interopRequireDefault(require("../MissingUrlError")); | ||
var _prepareFormData = _interopRequireDefault(require("./prepareFormData")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
exports.SUCCESS_CODES = SUCCESS_CODES; | ||
const getRequestData = (items, options) => { | ||
let data; | ||
if (options.sendWithFormData) { | ||
_shared.logger.debugLog(`uploady.sender: sending ${items.length} item(s) as form data`); | ||
data = (0, _prepareFormData.default)(items, options); | ||
@@ -33,17 +24,11 @@ } else { | ||
} | ||
const item = items[0]; | ||
_shared.logger.debugLog(`uploady.sender: sending item ${item.id} as request body`); | ||
data = item.file || item.url; | ||
} | ||
return data; | ||
}; | ||
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); | ||
const issueRequest = function () { | ||
@@ -53,3 +38,4 @@ let requestUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url; | ||
let requestOptions = arguments.length > 2 ? arguments[2] : undefined; | ||
requestOptions = (0, _shared.merge)({ ...(0, _shared.pick)(options, ["method", "headers", "withCredentials"]), | ||
const resolvedRequestOptions = (0, _shared.merge)({ | ||
...(0, _shared.pick)(options, ["method", "headers", "withCredentials"]), | ||
preSend: req => { | ||
@@ -63,9 +49,8 @@ req.upload.onprogress = e => { | ||
}, requestOptions); | ||
const realPXhr = (0, _shared.request)(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
const realPXhr = (0, _shared.request)(requestUrl, requestData, resolvedRequestOptions); | ||
xhr = realPXhr.xhr; | ||
return realPXhr; | ||
}; //pXhr is a promise that resolves to the upload XHR | ||
}; | ||
//pXhr is a promise that resolves to the upload XHR | ||
const pXhr = config !== null && config !== void 0 && config.preRequestHandler ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
@@ -80,7 +65,5 @@ return { | ||
}; | ||
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 && ct.includes("json")) { | ||
@@ -92,6 +75,4 @@ try { | ||
} | ||
return parsed; | ||
}; | ||
const checkIsResponseSuccessful = (xhr, options) => { | ||
@@ -101,9 +82,6 @@ const isSuccess = options.isSuccessfulCall ? options.isSuccessfulCall(xhr) : SUCCESS_CODES.includes(xhr.status); | ||
}; | ||
const processResponse = (sendRequest, options) => sendRequest.pXhr.then(xhr => { | ||
_shared.logger.debugLog("uploady.sender: received upload response ", xhr); | ||
return checkIsResponseSuccessful(xhr, options).then(isSuccess => { | ||
var _options$formatServer, _options$formatServer2; | ||
const state = isSuccess ? _shared.FILE_STATES.FINISHED : _shared.FILE_STATES.ERROR; | ||
@@ -124,3 +102,2 @@ const status = xhr.status; | ||
let state, response; | ||
if (sendRequest.aborted) { | ||
@@ -131,7 +108,5 @@ state = _shared.FILE_STATES.ABORTED; | ||
_shared.logger.debugLog("uploady.sender: upload failed: ", error); | ||
state = _shared.FILE_STATES.ERROR; | ||
response = error; | ||
} | ||
return { | ||
@@ -144,3 +119,2 @@ error: true, | ||
}); | ||
const abortRequest = sendRequest => { | ||
@@ -153,6 +127,4 @@ let abortCalled = false; | ||
const xhr = getXhr(); | ||
if (!aborted && xhr && xhr.readyState && xhr.readyState !== 4) { | ||
_shared.logger.debugLog(`uploady.sender: cancelling request with ${sendRequest.count} items to: ${sendRequest.url}`); | ||
xhr.abort(); | ||
@@ -162,6 +134,4 @@ sendRequest.aborted = true; | ||
} | ||
return abortCalled; | ||
}; | ||
const getXhrSend = config => (items, url, options, onProgress) => { | ||
@@ -171,3 +141,2 @@ if (!url) { | ||
} | ||
_shared.logger.debugLog("uploady.sender: sending file: ", { | ||
@@ -178,3 +147,2 @@ items, | ||
}); | ||
const sendRequest = makeRequest(items, url, options, onProgress, config); | ||
@@ -187,4 +155,3 @@ return { | ||
}; | ||
var _default = getXhrSend; | ||
exports.default = _default; |
@@ -6,3 +6,2 @@ export default class MissingUrlError extends Error { | ||
} | ||
} |
import { isFunction } from "@rpldy/shared"; | ||
/** | ||
@@ -11,6 +10,4 @@ * mimics FormData.set() when its not available (react-native) | ||
} | ||
//rest = [value, fileName = undefined] | ||
if ("set" in fd) { | ||
//$FlowExpectedError[speculation-ambiguous] - ignore flow for not allowing FileLike here | ||
fd.set(name, ...rest); | ||
@@ -20,9 +17,6 @@ } else { | ||
fd.delete(name); | ||
} //$FlowExpectedError[speculation-ambiguous] - ignore flow for not allowing FileLike here | ||
} | ||
fd.append(name, ...rest); | ||
} | ||
}; | ||
const getFormFileField = (fd, items, options) => { | ||
@@ -32,3 +26,2 @@ const single = items.length === 1; | ||
const name = single ? options.paramName : isFunction(options.formatGroupParamName) ? options.formatGroupParamName(i, options.paramName) : `${options.paramName}[${i}]`; | ||
if (item.file) { | ||
@@ -41,10 +34,7 @@ addToFormData(fd, name, item.file, item.file.name); | ||
}; | ||
const prepareFormData = (items, options) => { | ||
const fd = new FormData(); | ||
if (options.params) { | ||
Object.entries(options.params).forEach(_ref => { | ||
let [key, val] = _ref; | ||
if (options.formDataAllowUndefined || val !== undefined) { | ||
@@ -55,7 +45,5 @@ addToFormData(fd, key, val); | ||
} | ||
getFormFileField(fd, items, options); | ||
return fd; | ||
}; | ||
export default prepareFormData; |
@@ -6,6 +6,4 @@ import { logger, FILE_STATES, request, parseResponseHeaders, pick, merge, isPromise } from "@rpldy/shared"; | ||
export const SUCCESS_CODES = [200, 201, 202, 203, 204]; | ||
const getRequestData = (items, options) => { | ||
let data; | ||
if (options.sendWithFormData) { | ||
@@ -18,3 +16,2 @@ logger.debugLog(`uploady.sender: sending ${items.length} item(s) as form data`); | ||
} | ||
const item = items[0]; | ||
@@ -24,10 +21,7 @@ logger.debugLog(`uploady.sender: sending item ${item.id} as request body`); | ||
} | ||
return data; | ||
}; | ||
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); | ||
const issueRequest = function () { | ||
@@ -37,3 +31,4 @@ let requestUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url; | ||
let requestOptions = arguments.length > 2 ? arguments[2] : undefined; | ||
requestOptions = merge({ ...pick(options, ["method", "headers", "withCredentials"]), | ||
const resolvedRequestOptions = merge({ | ||
...pick(options, ["method", "headers", "withCredentials"]), | ||
preSend: req => { | ||
@@ -47,9 +42,8 @@ req.upload.onprogress = e => { | ||
}, requestOptions); | ||
const realPXhr = request(requestUrl, requestData, requestOptions); // $FlowFixMe - | ||
const realPXhr = request(requestUrl, requestData, resolvedRequestOptions); | ||
xhr = realPXhr.xhr; | ||
return realPXhr; | ||
}; //pXhr is a promise that resolves to the upload XHR | ||
}; | ||
//pXhr is a promise that resolves to the upload XHR | ||
const pXhr = config !== null && config !== void 0 && config.preRequestHandler ? config.preRequestHandler(issueRequest, items, url, options, onProgress, config) : issueRequest(); | ||
@@ -64,7 +58,5 @@ return { | ||
}; | ||
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 && ct.includes("json")) { | ||
@@ -76,6 +68,4 @@ try { | ||
} | ||
return parsed; | ||
}; | ||
const checkIsResponseSuccessful = (xhr, options) => { | ||
@@ -85,3 +75,2 @@ const isSuccess = options.isSuccessfulCall ? options.isSuccessfulCall(xhr) : SUCCESS_CODES.includes(xhr.status); | ||
}; | ||
const processResponse = (sendRequest, options) => sendRequest.pXhr.then(xhr => { | ||
@@ -91,3 +80,2 @@ logger.debugLog("uploady.sender: received upload response ", xhr); | ||
var _options$formatServer, _options$formatServer2; | ||
const state = isSuccess ? FILE_STATES.FINISHED : FILE_STATES.ERROR; | ||
@@ -108,3 +96,2 @@ const status = xhr.status; | ||
let state, response; | ||
if (sendRequest.aborted) { | ||
@@ -118,3 +105,2 @@ state = FILE_STATES.ABORTED; | ||
} | ||
return { | ||
@@ -127,3 +113,2 @@ error: true, | ||
}); | ||
const abortRequest = sendRequest => { | ||
@@ -136,3 +121,2 @@ let abortCalled = false; | ||
const xhr = getXhr(); | ||
if (!aborted && xhr && xhr.readyState && xhr.readyState !== 4) { | ||
@@ -144,6 +128,4 @@ logger.debugLog(`uploady.sender: cancelling request with ${sendRequest.count} items to: ${sendRequest.url}`); | ||
} | ||
return abortCalled; | ||
}; | ||
const getXhrSend = config => (items, url, options, onProgress) => { | ||
@@ -153,3 +135,2 @@ if (!url) { | ||
} | ||
logger.debugLog("uploady.sender: sending file: ", { | ||
@@ -167,3 +148,2 @@ items, | ||
}; | ||
export default getXhrSend; |
{ | ||
"version": "1.3.1", | ||
"version": "1.4.0-rc.0", | ||
"name": "@rpldy/sender", | ||
@@ -26,6 +26,6 @@ "description": "react-uploady's default XHR sender", | ||
"dependencies": { | ||
"@rpldy/shared": "^1.3.1" | ||
"@rpldy/shared": "^1.4.0-rc.0" | ||
}, | ||
"devDependencies": { | ||
"flow-bin": "^0.182.0" | ||
"flow-bin": "^0.199.1" | ||
}, | ||
@@ -35,3 +35,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "8a2473ae6d9521c5f16f5ae525466075ac02fd1f" | ||
"gitHead": "96a9141aa17e74962f1606d836f8f9f5c4b7aef0" | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
474
20109
1
Updated@rpldy/shared@^1.4.0-rc.0