UNPKG

@haxtheweb/haxcms-nodejs

Version:

HAXcms single and multisite nodejs server, api, and administration

11 lines 4.74 kB
/** @license Copyright (c) 2015 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt */ import{Polymer as e}from"../polymer/lib/legacy/polymer-fn.js";import{Base as t}from"../polymer/polymer-legacy.js";e({is:"iron-request",hostAttributes:{hidden:!0},properties:{xhr:{type:Object,notify:!0,readOnly:!0,value:function(){return new XMLHttpRequest}},response:{type:Object,notify:!0,readOnly:!0,value:function(){return null}},status:{type:Number,notify:!0,readOnly:!0,value:0},statusText:{type:String,notify:!0,readOnly:!0,value:""},completes:{type:Object,readOnly:!0,notify:!0,value:function(){return new Promise(function(e,t){this.resolveCompletes=e,this.rejectCompletes=t}.bind(this))}},progress:{type:Object,notify:!0,readOnly:!0,value:function(){return{}}},aborted:{type:Boolean,notify:!0,readOnly:!0,value:!1},errored:{type:Boolean,notify:!0,readOnly:!0,value:!1},timedOut:{type:Boolean,notify:!0,readOnly:!0,value:!1}},get succeeded(){if(this.errored||this.aborted||this.timedOut)return!1;var e=this.xhr.status||0;return 0===e||e>=200&&e<300},send:function(e){var r=this.xhr;if(r.readyState>0)return null;r.addEventListener("progress",function(e){this._setProgress({lengthComputable:e.lengthComputable,loaded:e.loaded,total:e.total}),this.fire("iron-request-progress-changed",{value:this.progress})}.bind(this)),r.addEventListener("error",function(t){this._setErrored(!0),this._updateStatus();var r=e.rejectWithRequest?{error:t,request:this}:t;this.rejectCompletes(r)}.bind(this)),r.addEventListener("timeout",function(t){this._setTimedOut(!0),this._updateStatus();var r=e.rejectWithRequest?{error:t,request:this}:t;this.rejectCompletes(r)}.bind(this)),r.addEventListener("abort",function(){this._setAborted(!0),this._updateStatus();var t=new Error("Request aborted."),r=e.rejectWithRequest?{error:t,request:this}:t;this.rejectCompletes(r)}.bind(this)),r.addEventListener("loadend",function(){if(this._updateStatus(),this._setResponse(this.parseResponse()),this.succeeded)this.resolveCompletes(this);else{var t=new Error("The request failed with status code: "+this.xhr.status),r=e.rejectWithRequest?{error:t,request:this}:t;this.rejectCompletes(r)}}.bind(this)),this.url=e.url;var s=!1!==e.async;r.open(e.method||"GET",e.url,s);var n={json:"application/json",text:"text/plain",html:"text/html",xml:"application/xml",arraybuffer:"application/octet-stream"}[e.handleAs],o=e.headers||Object.create(null),i=Object.create(null);for(var a in o)i[a.toLowerCase()]=o[a];if(o=i,n&&!o.accept&&(o.accept=n),Object.keys(o).forEach((function(e){/[A-Z]/.test(e)&&t._error("Headers must be lower case, got",e),r.setRequestHeader(e,o[e])}),this),s){r.timeout=e.timeout;var u=e.handleAs;!e.jsonPrefix&&u||(u="text"),r.responseType=r._responseType=u,e.jsonPrefix&&(r._jsonPrefix=e.jsonPrefix)}r.withCredentials=!!e.withCredentials;var l=this._encodeBodyObject(e.body,o["content-type"]);return r.send(l),this.completes},parseResponse:function(){var e=this.xhr,t=e.responseType||e._responseType,r=!this.xhr.responseType,s=e._jsonPrefix&&e._jsonPrefix.length||0;try{switch(t){case"json":if(r||void 0===e.response)try{return JSON.parse(e.responseText)}catch(t){return console.warn("Failed to parse JSON sent from "+e.responseURL),null}return e.response;case"xml":return e.responseXML;case"blob":case"document":case"arraybuffer":return e.response;default:if(s)try{return JSON.parse(e.responseText.substring(s))}catch(t){return console.warn("Failed to parse JSON sent from "+e.responseURL),null}return e.responseText}}catch(e){this.rejectCompletes(new Error("Could not parse response. "+e.message))}},abort:function(){this._setAborted(!0),this.xhr.abort()},_encodeBodyObject:function(e,t){if("string"==typeof e)return e;var r=e;switch(t){case"application/json":return JSON.stringify(r);case"application/x-www-form-urlencoded":return this._wwwFormUrlEncode(r)}return e},_wwwFormUrlEncode:function(e){if(!e)return"";var t=[];return Object.keys(e).forEach((function(r){t.push(this._wwwFormUrlEncodePiece(r)+"="+this._wwwFormUrlEncodePiece(e[r]))}),this),t.join("&")},_wwwFormUrlEncodePiece:function(e){return null!=e&&e.toString?encodeURIComponent(e.toString().replace(/\r?\n/g,"\r\n")).replace(/%20/g,"+"):""},_updateStatus:function(){this._setStatus(this.xhr.status),this._setStatusText(void 0===this.xhr.statusText?"":this.xhr.statusText)}});