UNPKG

podchat-browser

Version:

Javascript SDK to use POD's Chat Service - Browser Only

362 lines (317 loc) 12.8 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _constants = require("../constants"); function HTTPRequestsManager(app) { function cancelFileDownload(params, callback) { if (params) { if (typeof params.uniqueId == 'string') { var uniqueId = params.uniqueId; if (app.store.resumableRequestsObject[params.uniqueId]) { app.store.resumableRequestsObject[params.uniqueId].abort(); } else { app.store.httpRequestObject[eval('uniqueId')] && app.store.httpRequestObject[eval('uniqueId')].abort(); app.store.httpRequestObject[eval('uniqueId')] && delete app.store.httpRequestObject[eval('uniqueId')]; } callback && callback(uniqueId); } } } function cancelFileUpload(params, callback) { if (params) { if (typeof params.uniqueId == 'string') { var uniqueId = params.uniqueId; app.uploads.abort(uniqueId); app.store.httpRequestObject[eval('uniqueId')] && app.store.httpRequestObject[eval('uniqueId')].abort(); app.store.httpRequestObject[eval('uniqueId')] && delete app.store.httpRequestObject[eval('uniqueId')]; app.messageQueues.deleteFromChatUploadQueue({ message: { uniqueId: uniqueId } }, callback); } } } /** * HTTP Request class * * Manages all HTTP Requests * * @access private * * @param {object} params Given parameters including (Headers, ...) * @param {function} callback The callback function to run after * * @return {undefined} */ function httpRequest(params, callback) { var url = params.url, xhrResponseType = params.responseType || 'text', fileSize, originalFileName, threadId, fileUniqueId, fileObject, reqBody = params.body, data = params.data, method = typeof params.method == 'string' ? params.method : 'GET', fileUploadUniqueId = typeof params.uniqueId == 'string' ? params.uniqueId : 'uniqueId', hasError = false; if (!url) { callback({ hasError: true, errorCode: 6201, errorMessage: _constants.CHAT_ERRORS[6201] }); return; } var hasFile = false; app.store.httpRequestObject[eval('fileUploadUniqueId')] = new XMLHttpRequest(); var settings = params.settings; app.store.httpRequestObject[eval('fileUploadUniqueId')].responseType = xhrResponseType; if (data && (0, _typeof2["default"])(data) === 'object' && (data.hasOwnProperty('image') || data.hasOwnProperty('file'))) { app.store.httpRequestObject[eval('fileUploadUniqueId')].timeout = settings && (0, _typeof2["default"])(parseInt(settings.uploadTimeout)) > 0 && settings.uploadTimeout > 0 ? settings.uploadTimeout : app.sdkParams.httpUploadRequestTimeout; } else { app.store.httpRequestObject[eval('fileUploadUniqueId')].timeout = settings && (0, _typeof2["default"])(parseInt(settings.timeout)) > 0 && settings.timeout > 0 ? settings.timeout : app.sdkParams.httpRequestTimeout; } app.store.httpRequestObject[eval('fileUploadUniqueId')].addEventListener('error', function (event) { if (callback && method === 'POST') { if (hasFile) { hasError = true; app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: fileUniqueId, state: 'UPLOAD_ERROR', progress: 0, fileInfo: { fileName: originalFileName, fileSize: fileSize }, fileObject: fileObject, errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (XMLHttpRequest Error Event Listener)' }); } callback({ hasError: true, errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (XMLHttpRequest Error Event Listener)' }); } else { if (callback) { callback({ hasError: true, errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (XMLHttpRequest Error Event Listener)' }); } if (params.enableDownloadProgressEvents) { app.chatEvents.fireEvent('fileDownloadEvents', { hashCode: params.hashCode, state: 'DOWNLOAD_ERROR', errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (XMLHttpRequest Error Event Listener)' }); } } }, false); if (params.enableDownloadProgressEvents) { app.store.httpRequestObject[eval('fileUploadUniqueId')].onprogress = function (event) { app.chatEvents.fireEvent('fileDownloadEvents', { hashCode: params.hashCode, state: 'DOWNLOADING', progress: Math.round(event.loaded / event.total * 100) }); }; } app.store.httpRequestObject[eval('fileUploadUniqueId')].addEventListener('abort', function (event) { if (callback) { if (hasFile) { hasError = true; app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: fileUniqueId, state: 'UPLOAD_CANCELED', progress: 0, fileInfo: { fileName: originalFileName, fileSize: fileSize }, fileObject: fileObject, errorCode: 6303, errorMessage: _constants.CHAT_ERRORS[6303] }); } callback({ hasError: true, errorCode: 6303, errorMessage: _constants.CHAT_ERRORS[6303] }); } }, false); try { if (method === 'GET') { if ((0, _typeof2["default"])(data) === 'object' && data !== null) { var keys = Object.keys(data); if (keys.length > 0) { url += '?'; for (var i = 0; i < keys.length; i++) { var key = keys[i]; url += key + '=' + data[key]; if (i < keys.length - 1) { url += '&'; } } } } else if (typeof data === 'string') { url += '?' + data; } app.store.httpRequestObject[eval('fileUploadUniqueId')].open(method, url, true); if ((0, _typeof2["default"])(params.headers) === 'object') { for (var key in params.headers) { if (params.headers.hasOwnProperty(key)) app.store.httpRequestObject[eval('fileUploadUniqueId')].setRequestHeader(key, params.headers[key]); } } app.store.httpRequestObject[eval('fileUploadUniqueId')].send(); } if (method === 'POST' && data) { app.store.httpRequestObject[eval('fileUploadUniqueId')].open(method, url, true); if ((0, _typeof2["default"])(params.headers) === 'object') { for (var key in params.headers) { if (params.headers.hasOwnProperty(key)) app.store.httpRequestObject[eval('fileUploadUniqueId')].setRequestHeader(key, params.headers[key]); } } if (reqBody) { var _data = reqBody; app.store.httpRequestObject[eval('fileUploadUniqueId')].send(_data); } else if ((0, _typeof2["default"])(data) == 'object') { if (data.hasOwnProperty('image') || data.hasOwnProperty('file')) { hasFile = true; var formData = new FormData(); for (var key in data) { if (data.hasOwnProperty(key)) formData.append(key, data[key]); } fileSize = data.fileSize; originalFileName = data.originalFileName; threadId = data.threadId; fileUniqueId = data.uniqueId; fileObject = data['image'] ? data['image'] : data['file']; app.store.httpRequestObject[eval('fileUploadUniqueId')].upload.onprogress = function (event) { if (event.lengthComputable && !hasError) { app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: fileUniqueId, state: 'UPLOADING', progress: Math.round(event.loaded / event.total * 100), fileInfo: { fileName: originalFileName, fileSize: fileSize }, fileObject: fileObject }); } }; app.store.httpRequestObject[eval('fileUploadUniqueId')].send(formData); } else { app.store.httpRequestObject[eval('fileUploadUniqueId')].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var keys = Object.keys(data); if (keys.length > 0) { var sendData = ''; for (var i = 0; i < keys.length; i++) { var key = keys[i]; sendData += key + '=' + data[key]; if (i < keys.length - 1) { sendData += '&'; } } } app.store.httpRequestObject[eval('fileUploadUniqueId')].send(sendData); } } else { app.store.httpRequestObject[eval('fileUploadUniqueId')].send(data); } } } catch (e) { callback && callback({ hasError: true, cache: false, errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (Request Catch Error)' + e }); } app.store.httpRequestObject[eval('fileUploadUniqueId')].onreadystatechange = function () { if (app.store.httpRequestObject[eval('fileUploadUniqueId')].readyState === 4) { if (app.store.httpRequestObject[eval('fileUploadUniqueId')].status === 200) { if (hasFile) { hasError = false; var fileHashCode = ''; try { var fileUploadResult = JSON.parse(app.store.httpRequestObject[eval('fileUploadUniqueId')].response); if (!!fileUploadResult && fileUploadResult.hasOwnProperty('result')) { fileHashCode = fileUploadResult.result.hashCode; } } catch (e) { app.sdkParams.consoleLogging && console.log(e); } app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: fileUniqueId, fileHash: fileHashCode, state: 'UPLOADED', progress: 100, fileInfo: { fileName: originalFileName, fileSize: fileSize }, fileObject: fileObject }); } callback && callback({ hasError: false, cache: false, result: { response: app.store.httpRequestObject[eval('fileUploadUniqueId')].response, responseText: xhrResponseType === 'text' ? app.store.httpRequestObject[eval('fileUploadUniqueId')].responseText : '', responseHeaders: app.store.httpRequestObject[eval('fileUploadUniqueId')].getAllResponseHeaders(), responseContentType: app.store.httpRequestObject[eval('fileUploadUniqueId')].getResponseHeader('content-type') } }); } else { if (hasFile) { hasError = true; app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: fileUniqueId, state: 'UPLOAD_ERROR', progress: 0, fileInfo: { fileName: originalFileName, fileSize: fileSize }, fileObject: fileObject, errorCode: 6200, errorMessage: _constants.CHAT_ERRORS[6200] + ' (Request Status != 200)', statusCode: app.store.httpRequestObject[eval('fileUploadUniqueId')].status }); } callback && callback({ hasError: true, errorMessage: xhrResponseType === 'text' ? app.store.httpRequestObject[eval('fileUploadUniqueId')].responseText : 'ُAn error accoured!', errorCode: app.store.httpRequestObject[eval('fileUploadUniqueId')].status }); } } }; } return { httpRequest: httpRequest, cancelFileUpload: cancelFileUpload, cancelFileDownload: cancelFileDownload }; } var _default = HTTPRequestsManager; exports["default"] = _default;