diff --git a/dist/cycle-http-driver.js b/dist/cycle-http-driver.js index 6b867c2..67d0cf4 100644 --- a/dist/cycle-http-driver.js +++ b/dist/cycle-http-driver.js @@ -1467,12 +1467,6 @@ function normalizeRequestOptions(reqOptions) { } } -function isolateSource(response$$, scope) { - return response$$.filter(function (res$) { - return Array.isArray(res$.request._namespace) && res$.request._namespace.indexOf(scope) !== -1; - }); -} - function isolateSink(request$, scope) { return request$.map(function (req) { if (typeof req === "string") { @@ -1484,6 +1478,15 @@ function isolateSink(request$, scope) { }); } +function isolateSource(response$$, scope) { + var isolatedResponse$$ = response$$.filter(function (res$) { + return Array.isArray(res$.request._namespace) && res$.request._namespace.indexOf(scope) !== -1; + }); + isolatedResponse$$.isolateSource = isolateSource; + isolatedResponse$$.isolateSink = isolateSink; + return isolatedResponse$$; +} + function makeHTTPDriver() { var _ref2 = arguments.length <= 0 || arguments[0] === undefined ? { eager: false } : arguments[0]; diff --git a/dist/cycle-http-driver.min.js b/dist/cycle-http-driver.min.js index c8d9090..85c8c60 100644 --- a/dist/cycle-http-driver.min.js +++ b/dist/cycle-http-driver.min.js @@ -1 +1 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.CycleHTTPDriver=f()}})(function(){var define,module,exports;return 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=200&&res.status<300){return self.callback(err,res)}var new_err=new Error(res.statusText||"Unsuccessful HTTP response");new_err.original=err;new_err.response=res;new_err.status=res.status;self.callback(new_err,res)})}Emitter(Request.prototype);Request.prototype.use=function(fn){fn(this);return this};Request.prototype.timeout=function(ms){this._timeout=ms;return this};Request.prototype.clearTimeout=function(){this._timeout=0;clearTimeout(this._timer);return this};Request.prototype.abort=function(){if(this.aborted)return;this.aborted=true;this.xhr.abort();this.clearTimeout();this.emit("abort");return this};Request.prototype.set=function(field,val){if(isObject(field)){for(var key in field){this.set(key,field[key])}return this}this._header[field.toLowerCase()]=val;this.header[field]=val;return this};Request.prototype.unset=function(field){delete this._header[field.toLowerCase()];delete this.header[field];return this};Request.prototype.getHeader=function(field){return this._header[field.toLowerCase()]};Request.prototype.type=function(type){this.set("Content-Type",request.types[type]||type);return this};Request.prototype.accept=function(type){this.set("Accept",request.types[type]||type);return this};Request.prototype.auth=function(user,pass){var str=btoa(user+":"+pass);this.set("Authorization","Basic "+str);return this};Request.prototype.query=function(val){if("string"!=typeof val)val=serialize(val);if(val)this._query.push(val);return this};Request.prototype.field=function(name,val){if(!this._formData)this._formData=new root.FormData;this._formData.append(name,val);return this};Request.prototype.attach=function(field,file,filename){if(!this._formData)this._formData=new root.FormData;this._formData.append(field,file,filename);return this};Request.prototype.send=function(data){var obj=isObject(data);var type=this.getHeader("Content-Type");if(obj&&isObject(this._data)){for(var key in data){this._data[key]=data[key]}}else if("string"==typeof data){if(!type)this.type("form");type=this.getHeader("Content-Type");if("application/x-www-form-urlencoded"==type){this._data=this._data?this._data+"&"+data:data}else{this._data=(this._data||"")+data}}else{this._data=data}if(!obj||isHost(data))return this;if(!type)this.type("json");return this};Request.prototype.callback=function(err,res){var fn=this._callback;this.clearTimeout();fn(err,res)};Request.prototype.crossDomainError=function(){var err=new Error("Origin is not allowed by Access-Control-Allow-Origin");err.crossDomain=true;this.callback(err)};Request.prototype.timeoutError=function(){var timeout=this._timeout;var err=new Error("timeout of "+timeout+"ms exceeded");err.timeout=timeout;this.callback(err)};Request.prototype.withCredentials=function(){this._withCredentials=true;return this};Request.prototype.end=function(fn){var self=this;var xhr=this.xhr=request.getXHR();var query=this._query.join("&");var timeout=this._timeout;var data=this._formData||this._data;this._callback=fn||noop;xhr.onreadystatechange=function(){if(4!=xhr.readyState)return;var status;try{status=xhr.status}catch(e){status=0}if(0==status){if(self.timedout)return self.timeoutError();if(self.aborted)return;return self.crossDomainError()}self.emit("end")};var handleProgress=function(e){if(e.total>0){e.percent=e.loaded/e.total*100}self.emit("progress",e)};if(this.hasListeners("progress")){xhr.onprogress=handleProgress}try{if(xhr.upload&&this.hasListeners("progress")){xhr.upload.onprogress=handleProgress}}catch(e){}if(timeout&&!this._timer){this._timer=setTimeout(function(){self.timedout=true;self.abort()},timeout)}if(query){query=request.serializeObject(query);this.url+=~this.url.indexOf("?")?"&"+query:"?"+query}xhr.open(this.method,this.url,true);if(this._withCredentials)xhr.withCredentials=true;if("GET"!=this.method&&"HEAD"!=this.method&&"string"!=typeof data&&!isHost(data)){var contentType=this.getHeader("Content-Type");var serialize=request.serialize[contentType?contentType.split(";")[0]:""];if(serialize)data=serialize(data)}for(var field in this.header){if(null==this.header[field])continue;xhr.setRequestHeader(field,this.header[field])}this.emit("request",this);xhr.send(data);return this};Request.prototype.then=function(fulfill,reject){return this.end(function(err,res){err?reject(err):fulfill(res)})};request.Request=Request;function request(method,url){if("function"==typeof url){return new Request("GET",method).end(url)}if(1==arguments.length){return new Request("GET",method)}return new Request(method,url)}request.get=function(url,data,fn){var req=request("GET",url);if("function"==typeof data)fn=data,data=null;if(data)req.query(data);if(fn)req.end(fn);return req};request.head=function(url,data,fn){var req=request("HEAD",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.del=function(url,fn){var req=request("DELETE",url);if(fn)req.end(fn);return req};request.patch=function(url,data,fn){var req=request("PATCH",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.post=function(url,data,fn){var req=request("POST",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.put=function(url,data,fn){var req=request("PUT",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};module.exports=request},{emitter:2,reduce:3}],2:[function(require,module,exports){module.exports=Emitter;function Emitter(obj){if(obj)return mixin(obj)}function mixin(obj){for(var key in Emitter.prototype){obj[key]=Emitter.prototype[key]}return obj}Emitter.prototype.on=Emitter.prototype.addEventListener=function(event,fn){this._callbacks=this._callbacks||{};(this._callbacks[event]=this._callbacks[event]||[]).push(fn);return this};Emitter.prototype.once=function(event,fn){var self=this;this._callbacks=this._callbacks||{};function on(){self.off(event,on);fn.apply(this,arguments)}on.fn=fn;this.on(event,on);return this};Emitter.prototype.off=Emitter.prototype.removeListener=Emitter.prototype.removeAllListeners=Emitter.prototype.removeEventListener=function(event,fn){this._callbacks=this._callbacks||{};if(0==arguments.length){this._callbacks={};return this}var callbacks=this._callbacks[event];if(!callbacks)return this;if(1==arguments.length){delete this._callbacks[event];return this}var cb;for(var i=0;i=0;i--){var a=attach[i];request=request.attach(a.name,a.path,a.filename)}}return request}function urlToSuperagent(url){return superagent.get(url)}function createResponse$(reqOptions){return Rx.Observable.create(function(observer){var request=optionsToSuperagent(reqOptions);try{request.end(function(err,res){if(err){observer.onError(err)}else{observer.onNext(res);observer.onCompleted()}})}catch(err){observer.onError(err)}return function onDispose(){request.abort()}})}function normalizeRequestOptions(reqOptions){if(typeof reqOptions==="string"){return{url:reqOptions}}else if(typeof reqOptions==="object"){return reqOptions}else{throw new Error("Observable of requests given to HTTP Driver must emit "+"either URL strings or objects with parameters.")}}function isolateSource(response$$,scope){return response$$.filter(function(res$){return Array.isArray(res$.request._namespace)&&res$.request._namespace.indexOf(scope)!==-1})}function isolateSink(request$,scope){return request$.map(function(req){if(typeof req==="string"){return{url:req,_namespace:[scope]}}req._namespace=req._namespace||[];req._namespace.push(scope);return req})}function makeHTTPDriver(){var _ref2=arguments.length<=0||arguments[0]===undefined?{eager:false}:arguments[0];var _ref2$eager=_ref2.eager;var eager=_ref2$eager===undefined?false:_ref2$eager;return function httpDriver(request$){var response$$=request$.map(function(request){var reqOptions=normalizeRequestOptions(request);var response$=createResponse$(reqOptions);if(eager||reqOptions.eager){response$=response$.replay(null,1);response$.connect()}response$.request=reqOptions;return response$}).replay(null,1);response$$.connect();response$$.isolateSource=isolateSource;response$$.isolateSink=isolateSink;return response$$}}module.exports={optionsToSuperagent:optionsToSuperagent,urlToSuperagent:urlToSuperagent,createResponse$:createResponse$,makeHTTPDriver:makeHTTPDriver}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{superagent:1}],5:[function(require,module,exports){"use strict";var _require=require("./http-driver");var makeHTTPDriver=_require.makeHTTPDriver;var CycleHTTPDriver={makeHTTPDriver:makeHTTPDriver};module.exports=CycleHTTPDriver},{"./http-driver":4}]},{},[5])(5)}); \ No newline at end of file +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.CycleHTTPDriver=f()}})(function(){var define,module,exports;return 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=200&&res.status<300){return self.callback(err,res)}var new_err=new Error(res.statusText||"Unsuccessful HTTP response");new_err.original=err;new_err.response=res;new_err.status=res.status;self.callback(new_err,res)})}Emitter(Request.prototype);Request.prototype.use=function(fn){fn(this);return this};Request.prototype.timeout=function(ms){this._timeout=ms;return this};Request.prototype.clearTimeout=function(){this._timeout=0;clearTimeout(this._timer);return this};Request.prototype.abort=function(){if(this.aborted)return;this.aborted=true;this.xhr.abort();this.clearTimeout();this.emit("abort");return this};Request.prototype.set=function(field,val){if(isObject(field)){for(var key in field){this.set(key,field[key])}return this}this._header[field.toLowerCase()]=val;this.header[field]=val;return this};Request.prototype.unset=function(field){delete this._header[field.toLowerCase()];delete this.header[field];return this};Request.prototype.getHeader=function(field){return this._header[field.toLowerCase()]};Request.prototype.type=function(type){this.set("Content-Type",request.types[type]||type);return this};Request.prototype.accept=function(type){this.set("Accept",request.types[type]||type);return this};Request.prototype.auth=function(user,pass){var str=btoa(user+":"+pass);this.set("Authorization","Basic "+str);return this};Request.prototype.query=function(val){if("string"!=typeof val)val=serialize(val);if(val)this._query.push(val);return this};Request.prototype.field=function(name,val){if(!this._formData)this._formData=new root.FormData;this._formData.append(name,val);return this};Request.prototype.attach=function(field,file,filename){if(!this._formData)this._formData=new root.FormData;this._formData.append(field,file,filename);return this};Request.prototype.send=function(data){var obj=isObject(data);var type=this.getHeader("Content-Type");if(obj&&isObject(this._data)){for(var key in data){this._data[key]=data[key]}}else if("string"==typeof data){if(!type)this.type("form");type=this.getHeader("Content-Type");if("application/x-www-form-urlencoded"==type){this._data=this._data?this._data+"&"+data:data}else{this._data=(this._data||"")+data}}else{this._data=data}if(!obj||isHost(data))return this;if(!type)this.type("json");return this};Request.prototype.callback=function(err,res){var fn=this._callback;this.clearTimeout();fn(err,res)};Request.prototype.crossDomainError=function(){var err=new Error("Origin is not allowed by Access-Control-Allow-Origin");err.crossDomain=true;this.callback(err)};Request.prototype.timeoutError=function(){var timeout=this._timeout;var err=new Error("timeout of "+timeout+"ms exceeded");err.timeout=timeout;this.callback(err)};Request.prototype.withCredentials=function(){this._withCredentials=true;return this};Request.prototype.end=function(fn){var self=this;var xhr=this.xhr=request.getXHR();var query=this._query.join("&");var timeout=this._timeout;var data=this._formData||this._data;this._callback=fn||noop;xhr.onreadystatechange=function(){if(4!=xhr.readyState)return;var status;try{status=xhr.status}catch(e){status=0}if(0==status){if(self.timedout)return self.timeoutError();if(self.aborted)return;return self.crossDomainError()}self.emit("end")};var handleProgress=function(e){if(e.total>0){e.percent=e.loaded/e.total*100}self.emit("progress",e)};if(this.hasListeners("progress")){xhr.onprogress=handleProgress}try{if(xhr.upload&&this.hasListeners("progress")){xhr.upload.onprogress=handleProgress}}catch(e){}if(timeout&&!this._timer){this._timer=setTimeout(function(){self.timedout=true;self.abort()},timeout)}if(query){query=request.serializeObject(query);this.url+=~this.url.indexOf("?")?"&"+query:"?"+query}xhr.open(this.method,this.url,true);if(this._withCredentials)xhr.withCredentials=true;if("GET"!=this.method&&"HEAD"!=this.method&&"string"!=typeof data&&!isHost(data)){var contentType=this.getHeader("Content-Type");var serialize=request.serialize[contentType?contentType.split(";")[0]:""];if(serialize)data=serialize(data)}for(var field in this.header){if(null==this.header[field])continue;xhr.setRequestHeader(field,this.header[field])}this.emit("request",this);xhr.send(data);return this};Request.prototype.then=function(fulfill,reject){return this.end(function(err,res){err?reject(err):fulfill(res)})};request.Request=Request;function request(method,url){if("function"==typeof url){return new Request("GET",method).end(url)}if(1==arguments.length){return new Request("GET",method)}return new Request(method,url)}request.get=function(url,data,fn){var req=request("GET",url);if("function"==typeof data)fn=data,data=null;if(data)req.query(data);if(fn)req.end(fn);return req};request.head=function(url,data,fn){var req=request("HEAD",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.del=function(url,fn){var req=request("DELETE",url);if(fn)req.end(fn);return req};request.patch=function(url,data,fn){var req=request("PATCH",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.post=function(url,data,fn){var req=request("POST",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.put=function(url,data,fn){var req=request("PUT",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};module.exports=request},{emitter:2,reduce:3}],2:[function(require,module,exports){module.exports=Emitter;function Emitter(obj){if(obj)return mixin(obj)}function mixin(obj){for(var key in Emitter.prototype){obj[key]=Emitter.prototype[key]}return obj}Emitter.prototype.on=Emitter.prototype.addEventListener=function(event,fn){this._callbacks=this._callbacks||{};(this._callbacks[event]=this._callbacks[event]||[]).push(fn);return this};Emitter.prototype.once=function(event,fn){var self=this;this._callbacks=this._callbacks||{};function on(){self.off(event,on);fn.apply(this,arguments)}on.fn=fn;this.on(event,on);return this};Emitter.prototype.off=Emitter.prototype.removeListener=Emitter.prototype.removeAllListeners=Emitter.prototype.removeEventListener=function(event,fn){this._callbacks=this._callbacks||{};if(0==arguments.length){this._callbacks={};return this}var callbacks=this._callbacks[event];if(!callbacks)return this;if(1==arguments.length){delete this._callbacks[event];return this}var cb;for(var i=0;i=0;i--){var a=attach[i];request=request.attach(a.name,a.path,a.filename)}}return request}function urlToSuperagent(url){return superagent.get(url)}function createResponse$(reqOptions){return Rx.Observable.create(function(observer){var request=optionsToSuperagent(reqOptions);try{request.end(function(err,res){if(err){observer.onError(err)}else{observer.onNext(res);observer.onCompleted()}})}catch(err){observer.onError(err)}return function onDispose(){request.abort()}})}function normalizeRequestOptions(reqOptions){if(typeof reqOptions==="string"){return{url:reqOptions}}else if(typeof reqOptions==="object"){return reqOptions}else{throw new Error("Observable of requests given to HTTP Driver must emit "+"either URL strings or objects with parameters.")}}function isolateSink(request$,scope){return request$.map(function(req){if(typeof req==="string"){return{url:req,_namespace:[scope]}}req._namespace=req._namespace||[];req._namespace.push(scope);return req})}function isolateSource(response$$,scope){var isolatedResponse$$=response$$.filter(function(res$){return Array.isArray(res$.request._namespace)&&res$.request._namespace.indexOf(scope)!==-1});isolatedResponse$$.isolateSource=isolateSource;isolatedResponse$$.isolateSink=isolateSink;return isolatedResponse$$}function makeHTTPDriver(){var _ref2=arguments.length<=0||arguments[0]===undefined?{eager:false}:arguments[0];var _ref2$eager=_ref2.eager;var eager=_ref2$eager===undefined?false:_ref2$eager;return function httpDriver(request$){var response$$=request$.map(function(request){var reqOptions=normalizeRequestOptions(request);var response$=createResponse$(reqOptions);if(eager||reqOptions.eager){response$=response$.replay(null,1);response$.connect()}response$.request=reqOptions;return response$}).replay(null,1);response$$.connect();response$$.isolateSource=isolateSource;response$$.isolateSink=isolateSink;return response$$}}module.exports={optionsToSuperagent:optionsToSuperagent,urlToSuperagent:urlToSuperagent,createResponse$:createResponse$,makeHTTPDriver:makeHTTPDriver}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{superagent:1}],5:[function(require,module,exports){"use strict";var _require=require("./http-driver");var makeHTTPDriver=_require.makeHTTPDriver;var CycleHTTPDriver={makeHTTPDriver:makeHTTPDriver};module.exports=CycleHTTPDriver},{"./http-driver":4}]},{},[5])(5)}); \ No newline at end of file diff --git a/package.json b/package.json index 8760de9..93c04c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cycle/http", - "version": "7.0.0-rc1", + "version": "7.0.0-rc2", "description": "A Cycle.js Driver for making HTTP requests", "main": "lib/index.js", "author": "Andre Staltz",