1 line
7.7 KiB
JavaScript
1 line
7.7 KiB
JavaScript
|
"use strict";import Format from "./lib/format.js";const rlocalProtocol=/^(?:about|app|app-storage|.+-extension|file|res|widget):$/,rheaders=/^(.*?):[ \t]*([^\r\n]*)\r?$/gm,encode=encodeURIComponent,decode=decodeURIComponent,toS=Object.prototype.toString,win=window,doc=win.document,noop=function(t,e){this.defer.resolve(e)},Xhr=function(){return new XMLHttpRequest},supportCors="withCredentials"in Xhr();let isLocal=!1;try{isLocal=rlocalProtocol.test(location.ptyperotocol)}catch(t){}let originAnchor=doc.createElement("a");originAnchor.href=location.href;const noBodyMethods=["GET","HEAD","JSONP"],error={10001:"argument url is required",10002:'method "set" required an object or 2 args',10003:'method "send" can not call by different way',10004:'method "send" arguments error',10005:'method "send" required an object/string or 2 args',10006:'method "field" required an object or 2 args',10011:"Promise required a callback",10012:"Parse error",10104:"Request pending...",10200:"ok",10204:"no content",10304:"not modified",10500:"Internal Server Error",10504:"Connected timeout",form:"application/x-www-form-urlencoded; charset=UTF-8",json:"application/json; charset=UTF-8",text:"text/plain; charset=UTF-8"},convert={text:t=>t,xml:(t,e)=>void 0!==e?e:Format.parseXML(t),html:t=>Format.parseHTML(t),json:t=>JSON.parse(t),script:t=>Format.parseJS(t),jsonp(t){var e=request.cache[t];return delete request.cache[t],e}};class _Request{constructor(t="",e="GET"){if(!t)throw new Error(error[10001]);e=e.toUpperCase(),this.transport=Object.create(null),this.xhr=Xhr(),this.defer=Promise.defer(),this.opt={url:t,method:e,form:null,data:{},headers:{},timeoutID:0,uuid:Math.random().toString(16).slice(2)}}_formData(){if(this.opt.form){let t=Format.parseForm(this.opt.form);Format.merge(this.opt.data,t)}let t=new FormData;for(let e in this.opt.data){let r=this.opt.data[e];Array.isArray(r)?r.forEach(function(r){t.append(e+"[]",r)}):t.append(e,this.opt.data[e])}return t}_jsonp(t){win[t]=function(e){delete win[t],request.cache[t]=e}}_dispatch(t){if(!this.transport)return this.defer.reject(error[10104]);let e={response:{url:this.opt.url,headers:{"content-type":""}},request:{url:this.opt.url,headers:this.opt.headers},status:null===t?504:200,statusText:null===t?"Connected timeout":"ok",text:"",body:"",error:null};if(clearTimeout(this.opt.timeoutID),this.transport.nodeType&&"JSONP"===this.opt.method){if(this.transport.parentNode.removeChild(this.transport),!t){(!this.transport.readyState||"loaded"===this.transport.readyState||"complete"===this.transport.readyState)&&(e.body=convert.jsonp(this.opt.data.callback),e.text=JSON.stringify(e.body))}this.defer.resolve(e)}else{let o=!t&&(this.transport.status>=200&&this.transport.status<400);(!t&&this.transport.getAllResponseHeaders().split("\n")||[]).forEach(function(t,r){(t=t.trim())&&(t=t.split(":"),e.response.headers[t.shift().toLowerCase()]=t.join(":").trim())}),o?(e.status=this.transport.status,204===e.status?e.statusText=error[10204]:304===e.status&&(e.statusText=error[10304])):(e.status=t?504:this.transport.status||500,e.statusText=t?error[10504]:this.transport.statusText||error[10500],e.error=new Error(e.statusText));try{var r=e.response.headers["content-type"].match(/json|xml|script|html/i)||["text"];r=r[0].toLowerCase(),e.text=t?"":this.transport.responseText||this.transport.responseXML,e.body=convert[r](e.text,!t&&this.transport.responseXML)}catch(t){e.error=t,e.statusText=error[10012]}e.status>=200&&e.status<400?this.defer.resolve(e):this.defer.reject(e)}delete this.transport,delete this.opt,delete this.xhr}type(t){return"form-data"===this.opt.formType?this:(this.opt.formType=t||"form","form"===t||noBodyMethods.indexOf(this.opt.method)>-1?this.set("content-type",error.form):"json"===t?this.set("content-type",error.json):this.set("content-type",error.text),this)}set(t,e){if(!this.transport)return this;let r={};if(1===arguments.length){if("object"!=typeof t)return this.defer.reject(error[10002]),this;r=t}else{if(2!==arguments.length)return this.defer.reject(error[10002]),this;"string"==typeof t&&void 0!==e&&(r[t]=e
|