This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
appcat
/
sonist
Archived
1
0
Fork 0
sonist/dist/request/index.js

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)}for(let t in r){let e=r[t];t=t.toLowerCase(),this.opt.headers[t]=e}return this}send(t,e){if(!this.transport)return this;if(1===arguments.length)if("string"==typeof t)this.opt.data=t;else if("object"==typeof t){if("object"!=typeof this.opt.data)return this.defer.reject(error[10003]),this;Format.merge(this.opt.data,t)}else this.defer.reject(error[10004]);else if(2===arguments.length){if("string"!=typeof t)return this.defer.reject(error[10004]),this;void 0===e?delete this.opt.data[t]:this.opt.data[t]=e}else this.defer.reject(error[10005]);return this}field(t,e){return this.transport?(this.opt.formType="form-data",this.opt.method="POST",this.opt.data&&"object"==typeof this.opt.data||(this.opt.data={}),1===arguments.length&&"object"==typeof t?Format.merge(this.opt.data,t):2===arguments.length?this.opt.data[t]=e:this.defer.reject(error[10006]),this):this}cache(t){return this.transport?(noBodyMethods.indexOf(this.opt.method)>-1&&(this.opt.cache=!!t),this):this}abort(){return delete this.transport,this.opt.form||this.xhr.abort(),this}timeout(t){return"number"!=typeof t||t<1?this:(this.opt.timeout=t,this)}form(t){return"object"==typeof t&&"FORM"===t.nodeName&&(this.opt.method="POST",this.opt.form=t),this}then(t){if("function"!=typeof t)return this.defer.reject(error[10011]),this.defer.promise;if(!this.transport)return this.defer.promise;if(this.opt.url=this.opt.url.replace(/#.*$/,"").replace(/^\/\//,location.protocol+"//"),"boolean"!=typeof this.opt.crossDomain){var e=doc.createElement("a");try{e.href=this.opt.url;var r="1"[0]?e.href:e.getAttribute("href",4);e.href=r,e.async=!0,this.opt.crossDomain=originAnchor.protocol!==e.protocol||originAnchor.host!==e.host}catch(t){this.opt.crossDomain=!0}}if("JSONP"===this.opt.method&&(this.opt.crossDomain?(this.opt.data.callback=this.opt.data.callback||"jsonp"+request.cid++,this._jsonp(this.opt.data.callback)):this.opt.method="GET"),"JSONP"!==this.opt.method&&this.set("X-Requested-With","XMLHttpRequest"),this.opt.crossDomain&&supportCors&&(this.xhr.withCredentials=!0),this.opt.param=Format.param(this.opt.data),this.opt.formType||this.type("form"),noBodyMethods.indexOf(this.opt.method)<0?"form-data"===this.opt.formType?(delete this.opt.headers["content-type"],this.opt.param=this._formData()):"form"!==this.opt.formType&&("object"==typeof this.opt.data&&(this.opt.data=JSON.stringify(this.opt.data)),this.opt.param=this.opt.data):(this.opt.param&&(this.opt.url+=(/\?/.test(this.opt.url)?"&":"?")+this.opt.param),!1===this.opt.cache&&(this.opt.url+=(/\?/.test(this.opt.url)?"&":"?")+"_="+Math.random())),"JSONP"===this.opt.method)this.transport=doc.createElement("script"),this.transport.onerror=this.transport.onload=(()=>{this._dispatch()}),this.transport.src=this.opt.url,doc.head.insertBefore(this.transport,doc.head.firstChild),this.opt.timeout&&this.opt.timeout>0&&(this.opt.timeoutID=setTimeout(()=>{this.transport.onerror=this.transport.onload=null,this._dispatch(!0)},this.opt.timeout));else{for(var o in this.transport=this.xhr,this.xhr.onreadystatechange=(t=>{this.opt.timeout&&this.opt.timeout>0&&(this.opt["time"+this.xhr.readyState]=t.timeStamp,4===this.xhr.readyState&&(this.opt.isTimeout=this.opt.time4-this.opt.time1>this.opt.timeout)),4===this.xhr.readyState&&this._dispatch(this.opt.isTimeout)}),this.xhr.open(this.opt.method,this.opt.url,!0),this.opt.headers)this.xhr.setRequestHeader(o,this.opt.headers[o]);this.xhr.send(this.opt.param),this.opt.timeout&&this.opt.timeout>0&&(this.xhr.timeout=this.opt.timeout)}return this.defer.promise.then(e=>t(e))}}win.request||(win.request={get:t=>new _Request(t,"GET"),post:t=>new _Request(t,"POST"),jsonp:t=>new _Request(t,"JSONP"),open:(t,e="GET")=>new _Request(t,e),cache:{},cid:0,version:"1.1.0-normal"},Anot.ui.request=request.version);export default request;
一个音乐播放器, 主打本地音乐播放。支持 自动歌词/自动封面/均衡器等常见功能。
JavaScript 60.1%
SCSS 19.2%
HTML 16.9%
CSS 3.8%