UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 4.66 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import t from"../request.js";import{isTimeoutError as e}from"../support/requestUtils.js";class s extends EventTarget{constructor(){super(...arguments),this._readyState=0,this._responseType="",this.timeout=0,this._withCredentials=!1,this.onabort=null,this.onerror=null,this.onload=null,this.onloadend=null,this.onloadstart=null,this.onprogress=null,this.ontimeout=null,this.onreadystatechange=null,this.UNSENT=0,this.OPENED=1,this.HEADERS_RECEIVED=2,this.LOADING=3,this.DONE=4,this._abortController=null,this._error=null,this._options={},this._response=null,this._sendFlag=!1,this._url=""}get readyState(){return this._readyState}get response(){return""===this.responseType||"text"===this.responseType?this.responseText:this.readyState!==this.DONE||this._error||!this._response?null:this._response.data}get responseText(){return""!==this.responseType&&"text"!==this.responseType&&this._throwDOMException(),this.readyState!==this.DONE?"":this._response?this._response.data:null!=this._error?.raw?"string"==typeof this._error.raw?this._error.raw:JSON.stringify(this._error.raw):""}get responseType(){return this._responseType}set responseType(t){("document"!==t||globalThis instanceof Window)&&(this.readyState!==this.LOADING&&this.readyState!==this.DONE||this._throwDOMException(),this._responseType=t)}get responseURL(){return(this._response||this._error)?.url??""}get responseXML(){return""!==this.responseType&&"document"!==this.responseType&&this._throwDOMException(),this.readyState!==this.DONE?null:this._response?.data??null}get status(){return(this._response||this._error)?.httpStatus??0}get statusText(){return""}get upload(){return console.warn("upload not implemented"),null}get withCredentials(){return this._withCredentials}set withCredentials(t){this.readyState!==this.UNSENT&&this.readyState!==this.OPENED&&this._throwDOMException(),this._withCredentials=t}static{this.UNSENT=0}static{this.OPENED=1}static{this.HEADERS_RECEIVED=2}static{this.LOADING=3}static{this.DONE=4}abort(){this.readyState===this.OPENED&&this._sendFlag||this.readyState===this.HEADERS_RECEIVED||this.readyState===this.LOADING?(this._sendFlag=!1,this._error=null,this._response=null,this._abortController?.abort(),this._updateReadyState(this.DONE),this._dispatchProgressEvent("abort"),this._dispatchProgressEvent("loadend")):this.readyState===this.DONE&&this._updateReadyState(this.UNSENT)}getAllResponseHeaders(){let t="";const e=(this._response||this._error)?.getAllHeaders?.();if(e)for(const[s,r]of e)t+=`${s}: ${r}\r\n`;return t}getResponseHeader(t){return(this._response||this._error)?.getHeader?.(t)??null}open(t,e){switch(this._sendFlag=!1,this._options={},t){case"GET":this._options.method="auto";break;case"POST":this._options.method="post";break;case"HEAD":this._options.method="head";break;case"DELETE":this._options.method="delete";break;case"PUT":this._options.method="put"}this._url=e,this._error=null,this._response=null,this._abortController=new AbortController,this._updateReadyState(this.OPENED)}overrideMimeType(t){throw new Error("overrideMimeType not implemented")}send(s){(this.readyState!==this.OPENED||this._sendFlag)&&this._throwDOMException(),this._sendFlag=!0,this._updateReadyState(this.LOADING),this._dispatchProgressEvent("loadstart");const r=this._options;s instanceof URLSearchParams?r.body=s.toString():r.body=s,r.responseType=""===this.responseType?"text":"arraybuffer"===this.responseType?"array-buffer":this.responseType,r.signal=this._abortController?.signal,this.timeout&&(r.timeout=this.timeout),this.withCredentials&&(r.withCredentials=!0),t(this._url,r).then((t=>{this._response=t,this._updateReadyState(this.DONE),this._dispatchProgressEvent("load")})).catch((t=>{this._sendFlag&&(this._error=t.details,this._updateReadyState(this.DONE),e(t)?this._dispatchProgressEvent("timeout"):"AbortError"===t.name?this._dispatchProgressEvent("abort"):this._dispatchProgressEvent("error"))})).finally((()=>{this._sendFlag&&this._dispatchProgressEvent("loadend")}))}setRequestHeader(t,e){(this.readyState!==this.OPENED||this._sendFlag)&&this._throwDOMException(),this._options.headers??={},this._options.headers[t]=e}_dispatchProgressEvent(t){const e=new ProgressEvent(t,{loaded:0,total:0});this.dispatchEvent(e),this[`on${t}`]?.(e)}_throwDOMException(){throw new DOMException("","InvalidStateError")}_updateReadyState(t){if(this._readyState!==t&&(this._readyState=t,t!==this.UNSENT)){const t=new Event("readystatechange");this.dispatchEvent(t),this.onreadystatechange?.(t)}}}export{s as XHRAdapter};