UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

169 lines (168 loc) • 5.03 kB
/** * DevExtreme (esm/__internal/core/utils/ajax.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import httpRequest from "../../../core/http_request"; import { evalCrossDomainScript, evalScript, getJsonpCallbackName as getJsonpOptions, getMethod, getRequestHeaders, getRequestOptions, isCrossDomain } from "../../../core/utils/ajax_utils"; import { Deferred } from "../../../core/utils/deferred"; import { isDefined } from "../../../core/utils/type"; import { getWindow } from "../../../core/utils/window"; import { injector } from "../../core/utils/dependency_injector"; const window = getWindow(); const SUCCESS = "success"; const ERROR = "error"; const TIMEOUT = "timeout"; const NO_CONTENT = "nocontent"; const PARSER_ERROR = "parsererror"; const isStatusSuccess = status => status >= 200 && status < 300; const hasContent = status => 204 !== status; const getDataFromResponse = xhr => xhr.responseType && "text" !== xhr.responseType || "string" !== typeof xhr.responseText ? xhr.response : xhr.responseText; const postProcess = (deferred, xhr, dataType) => { const data = getDataFromResponse(xhr); switch (dataType) { case "jsonp": evalScript(data); break; case "script": evalScript(data); deferred.resolve(data, SUCCESS, xhr); break; case "json": try { deferred.resolve(JSON.parse(data), SUCCESS, xhr) } catch (e) { deferred.reject(xhr, PARSER_ERROR, e) } break; default: deferred.resolve(data, SUCCESS, xhr) } }; const setHttpTimeout = (timeout, xhr) => { if (!timeout) { return } return setTimeout(() => { xhr.customStatus = TIMEOUT; xhr.abort() }, timeout) }; const sendRequest = options => { const xhr = httpRequest.getXhr(); const d = Deferred(); const result = d.promise(); const async = isDefined(options.async) ? options.async : true; const { dataType: dataType } = options; const timeout = options.timeout ?? 0; options.crossDomain = isCrossDomain(options.url); const needScriptEvaluation = "jsonp" === dataType || "script" === dataType; if (void 0 === options.cache) { options.cache = !needScriptEvaluation } const callbackName = getJsonpOptions(options); const headers = getRequestHeaders(options); const requestOptions = getRequestOptions(options, headers); const { url: url } = requestOptions; const { parameters: parameters } = requestOptions; if (callbackName) { window[callbackName] = data => { d.resolve(data, SUCCESS, xhr) } } if (options.crossDomain && needScriptEvaluation) { const reject = () => { d.reject(xhr, ERROR) }; const resolve = () => { if ("jsonp" === dataType) { return } d.resolve(null, SUCCESS, xhr) }; evalCrossDomainScript(url).then(resolve, reject); return result } if (options.crossDomain && !("withCredentials" in xhr)) { d.reject(xhr, ERROR); return result } xhr.open(getMethod(options), url, async, options.username, options.password); if (async) { xhr.timeout = timeout } const timeoutId = async ? setHttpTimeout(timeout, xhr) : void 0; xhr.onreadystatechange = () => { if (4 === xhr.readyState) { clearTimeout(timeoutId); if (isStatusSuccess(xhr.status)) { if (hasContent(xhr.status)) { postProcess(d, xhr, dataType) } else { d.resolve(null, NO_CONTENT, xhr) } } else { d.reject(xhr, xhr.customStatus ?? ERROR) } } }; if (options.upload) { xhr.upload.onprogress = options.upload.onprogress ?? null; xhr.upload.onloadstart = options.upload.onloadstart ?? null; xhr.upload.onabort = options.upload.onabort ?? null } if (options.xhrFields) { Object.entries(options.xhrFields).forEach(_ref => { let [field, value] = _ref; xhr[field] = value }) } if ("arraybuffer" === options.responseType) { xhr.responseType = options.responseType } Object.entries(headers).forEach(_ref2 => { let [name, value] = _ref2; if (isDefined(value)) { xhr.setRequestHeader(name, value) } }); if (options.beforeSend) { options.beforeSend(xhr) } xhr.send(parameters); result.abort = () => { xhr.abort() }; return result }; const Ajax = injector({ sendRequest: sendRequest }); export { Ajax };