falcor-http-datasource
Version:
This package contains falcor components for use in browsers.
1 lines • 6.86 kB
JavaScript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){"use strict";module.exports=function buildQueryObject(url,method,queryData){var qData=[];var keys;var data={url:url};var isQueryParamUrl=url.indexOf("?")!==-1;var startUrl=isQueryParamUrl?"&":"?";if(typeof queryData==="string"){qData.push(queryData)}else{keys=Object.keys(queryData);keys.forEach(function(k){var value=typeof queryData[k]==="object"?JSON.stringify(queryData[k]):queryData[k];qData.push(k+"="+value)})}if(method==="GET"){data.url+=startUrl+qData.join("&")}else{data.data=qData.join("&")}return data}},{}],2:[function(require,module,exports){(function(global){"use strict";module.exports=function getCORSRequest(){var xhr=new global.XMLHttpRequest;if("withCredentials"in xhr){return xhr}else if(!!global.XDomainRequest){return new XDomainRequest}else{throw new Error("CORS is not supported by your browser")}}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],3:[function(require,module,exports){(function(global){"use strict";module.exports=function getXMLHttpRequest(){var progId,progIds,i;if(global.XMLHttpRequest){return new global.XMLHttpRequest}else{try{progIds=["Msxml2.XMLHTTP","Microsoft.XMLHTTP","Msxml2.XMLHTTP.4.0"];for(i=0;i<3;i++){try{progId=progIds[i];if(new global.ActiveXObject(progId)){break}}catch(e){}}return new global.ActiveXObject(progId)}catch(e){throw new Error("XMLHttpRequest is not supported by your browser")}}}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],4:[function(require,module,exports){"use strict";var getXMLHttpRequest=require("./getXMLHttpRequest");var getCORSRequest=require("./getCORSRequest");var hasOwnProp=Object.prototype.hasOwnProperty;function Observable(){}Observable.create=function(subscribe){var o=new Observable;o.subscribe=function(observer){var s=subscribe(observer);if(typeof s==="function"){return{dispose:s}}else{return s}};return o};function request(method,options,context){return Observable.create(function requestObserver(observer){var config={method:method||"GET",crossDomain:false,async:true,headers:{},responseType:"json"};var xhr,isDone,headers,header,prop;for(prop in options){if(hasOwnProp.call(options,prop)){config[prop]=options[prop]}}if(!config.crossDomain&&!config.headers["X-Requested-With"]){config.headers["X-Requested-With"]="XMLHttpRequest"}if(context.onBeforeRequest!=null){context.onBeforeRequest(config)}try{xhr=config.crossDomain?getCORSRequest():getXMLHttpRequest()}catch(err){observer.onError(err)}try{if(config.user){xhr.open(config.method,config.url,config.async,config.user,config.password)}else{xhr.open(config.method,config.url,config.async)}xhr.timeout=config.timeout;xhr.withCredentials=config.withCredentials!==false;headers=config.headers;for(header in headers){if(hasOwnProp.call(headers,header)){xhr.setRequestHeader(header,headers[header])}}if(config.responseType){try{xhr.responseType=config.responseType}catch(e){if(responseType!=="json"){throw e}}}xhr.onreadystatechange=function onreadystatechange(e){if(xhr.readyState===4){if(!isDone){isDone=true;onXhrLoad(observer,xhr,status,e)}}};xhr.ontimeout=function ontimeout(e){if(!isDone){isDone=true;onXhrError(observer,xhr,"timeout error",e)}};xhr.send(config.data)}catch(e){observer.onError(e)}return function dispose(){if(!isDone&&xhr.readyState!==4){isDone=true;xhr.abort()}}})}function _handleXhrError(observer,textStatus,errorThrown){if(!errorThrown){errorThrown=new Error(textStatus)}observer.onError(errorThrown)}function onXhrLoad(observer,xhr,status,e){var responseData,responseObject,responseType;if(xhr&&observer){responseType=xhr.responseType;responseData="response"in xhr?xhr.response:xhr.responseText;var status=xhr.status===1223?204:xhr.status;if(status>=200&&status<=399){try{if(responseType!=="json"&&typeof responseData==="string"){responseData=JSON.parse(responseData||"")}}catch(e){_handleXhrError(observer,"invalid json",e)}observer.onNext(responseData);observer.onCompleted();return}else if(status===401||status===403||status===407){return _handleXhrError(observer,responseData)}else if(status===410){return _handleXhrError(observer,responseData)}else if(status===408||status===504){return _handleXhrError(observer,responseData)}else{return _handleXhrError(observer,responseData||"Response code "+status)}}}function onXhrError(observer,xhr,status,e){_handleXhrError(observer,status||xhr.statusText||"request error",e)}module.exports=request},{"./getCORSRequest":2,"./getXMLHttpRequest":3}],5:[function(require,module,exports){"use strict";var request=require("./request");var buildQueryObject=require("./buildQueryObject");var isArray=Array.isArray;function simpleExtend(obj,obj2){var prop;for(prop in obj2){obj[prop]=obj2[prop]}return obj}function XMLHttpSource(jsongUrl,config){this._jsongUrl=jsongUrl;if(typeof config==="number"){var newConfig={timeout:config};config=newConfig}this._config=simpleExtend({timeout:15e3,headers:{}},config||{})}XMLHttpSource.prototype={constructor:XMLHttpSource,buildQueryObject:buildQueryObject,get:function httpSourceGet(pathSet){var method="GET";var queryObject=this.buildQueryObject(this._jsongUrl,method,{paths:pathSet,method:"get"});var config=simpleExtend(queryObject,this._config);var context=this;return request(method,config,context)},set:function httpSourceSet(jsongEnv){var method="POST";var queryObject=this.buildQueryObject(this._jsongUrl,method,{jsong:jsongEnv,method:"set"});var config=simpleExtend(queryObject,this._config);var context=this;return request(method,config,context)},call:function httpSourceCall(callPath,args,pathSuffix,paths){args=args||[];pathSuffix=pathSuffix||[];paths=paths||[];var method="POST";var queryData=[];queryData.push("method=call");queryData.push("callPath="+encodeURIComponent(JSON.stringify(callPath)));queryData.push("arguments="+encodeURIComponent(JSON.stringify(args)));queryData.push("pathSuffixes="+encodeURIComponent(JSON.stringify(pathSuffix)));queryData.push("paths="+encodeURIComponent(JSON.stringify(paths)));var queryObject=this.buildQueryObject(this._jsongUrl,method,queryData.join("&"));var config=simpleExtend(queryObject,this._config);var context=this;return request(method,config,context)}};XMLHttpSource.XMLHttpSource=XMLHttpSource;XMLHttpSource["default"]=XMLHttpSource;module.exports=XMLHttpSource},{"./buildQueryObject":1,"./request":4}]},{},[5]);