diff --git a/dist/karet.xhr.cjs.js b/dist/karet.xhr.cjs.js index 582c022..b5e9bf6 100644 --- a/dist/karet.xhr.cjs.js +++ b/dist/karet.xhr.cjs.js @@ -110,7 +110,10 @@ var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? I.id : end(emit(state = L.set(EVENT, event, state))); }); xhr.open(method, url, true, user, password); - if (responseType) xhr.responseType = responseType; + if (responseType) { + xhr.responseType = responseType; + if (responseType === 'json' && xhr.responseType !== 'json') state = L.set('parse', true, state); + } if (timeout) xhr.timeout = timeout; if (withCredentials) xhr.withCredentials = withCredentials; if (null != headers) { @@ -144,6 +147,14 @@ function perform(argsIn) { return (isObservable(args) ? args.flatMapLatest(performPlain) : performPlain(args)).toProperty(); } +function tryParse(json) { + try { + return JSON.parse(json); + } catch (_) { + return null; + } +} + var isOneOf = /*#__PURE__*/I.curry(function (values, value) { return values.includes(value); }); @@ -195,7 +206,13 @@ var headersReceived = /*#__PURE__*/I.defineNameU( /*#__PURE__*/L.get([XHR, 'read return 2 <= state; }]), 'headersReceived'); var isDone = /*#__PURE__*/I.defineNameU( /*#__PURE__*/L.get([EVENT, 'type', /*#__PURE__*/L.is('loadend')]), 'isDone'); -var response = /*#__PURE__*/setName( /*#__PURE__*/I.pipe2U( /*#__PURE__*/L.get([XHR, 'response']), /*#__PURE__*/skipDuplicates(I.acyclicEqualsU)), 'response'); +var response = /*#__PURE__*/setName( /*#__PURE__*/I.pipe2U( /*#__PURE__*/F.lift(function (_ref3) { + var xhr = _ref3.xhr, + parse = _ref3.parse; + + var response = xhr.response; + return parse ? tryParse(response) : response; +}), /*#__PURE__*/skipDuplicates(I.acyclicEqualsU)), 'response'); var responseFull = /*#__PURE__*/setName( /*#__PURE__*/getAfter(isDone, response), 'responseFull'); var responseType = /*#__PURE__*/setName( /*#__PURE__*/L.get([XHR, 'responseType']), 'responseType'); var responseURL = /*#__PURE__*/setName( /*#__PURE__*/L.get([XHR, 'responseURL']), 'responseURL'); diff --git a/dist/karet.xhr.es.js b/dist/karet.xhr.es.js index b256dcf..c54c77c 100644 --- a/dist/karet.xhr.es.js +++ b/dist/karet.xhr.es.js @@ -106,7 +106,10 @@ var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : va end(emit(state = set(EVENT, event, state))); }); xhr.open(method, url, true, user, password); - if (responseType) xhr.responseType = responseType; + if (responseType) { + xhr.responseType = responseType; + if (responseType === 'json' && xhr.responseType !== 'json') state = set('parse', true, state); + } if (timeout) xhr.timeout = timeout; if (withCredentials) xhr.withCredentials = withCredentials; if (null != headers) { @@ -140,6 +143,14 @@ function perform(argsIn) { return (isObservable(args) ? args.flatMapLatest(performPlain) : performPlain(args)).toProperty(); } +function tryParse(json) { + try { + return JSON.parse(json); + } catch (_) { + return null; + } +} + var isOneOf = /*#__PURE__*/curry(function (values, value) { return values.includes(value); }); @@ -191,7 +202,13 @@ var headersReceived = /*#__PURE__*/defineNameU( /*#__PURE__*/get([XHR, 'readySta return 2 <= state; }]), 'headersReceived'); var isDone = /*#__PURE__*/defineNameU( /*#__PURE__*/get([EVENT, 'type', /*#__PURE__*/is('loadend')]), 'isDone'); -var response = /*#__PURE__*/setName( /*#__PURE__*/pipe2U( /*#__PURE__*/get([XHR, 'response']), /*#__PURE__*/skipDuplicates(acyclicEqualsU)), 'response'); +var response = /*#__PURE__*/setName( /*#__PURE__*/pipe2U( /*#__PURE__*/lift(function (_ref3) { + var xhr = _ref3.xhr, + parse = _ref3.parse; + + var response = xhr.response; + return parse ? tryParse(response) : response; +}), /*#__PURE__*/skipDuplicates(acyclicEqualsU)), 'response'); var responseFull = /*#__PURE__*/setName( /*#__PURE__*/getAfter(isDone, response), 'responseFull'); var responseType = /*#__PURE__*/setName( /*#__PURE__*/get([XHR, 'responseType']), 'responseType'); var responseURL = /*#__PURE__*/setName( /*#__PURE__*/get([XHR, 'responseURL']), 'responseURL'); diff --git a/dist/karet.xhr.js b/dist/karet.xhr.js index 808e4d3..03404b9 100644 --- a/dist/karet.xhr.js +++ b/dist/karet.xhr.js @@ -102,7 +102,10 @@ end(emit(state = L.set(EVENT, event, state))); }); xhr.open(method, url, true, user, password); - if (responseType) xhr.responseType = responseType; + if (responseType) { + xhr.responseType = responseType; + if (responseType === 'json' && xhr.responseType !== 'json') state = L.set('parse', true, state); + } if (timeout) xhr.timeout = timeout; if (withCredentials) xhr.withCredentials = withCredentials; if (null != headers) { @@ -136,6 +139,14 @@ return (isObservable(args) ? args.flatMapLatest(performPlain) : performPlain(args)).toProperty(); } + function tryParse(json) { + try { + return JSON.parse(json); + } catch (_) { + return null; + } + } + var isOneOf = /*#__PURE__*/I.curry(function (values, value) { return values.includes(value); }); @@ -187,7 +198,13 @@ return 2 <= state; }]), 'headersReceived'); var isDone = /*#__PURE__*/I.defineNameU( /*#__PURE__*/L.get([EVENT, 'type', /*#__PURE__*/L.is('loadend')]), 'isDone'); - var response = /*#__PURE__*/setName( /*#__PURE__*/I.pipe2U( /*#__PURE__*/L.get([XHR, 'response']), /*#__PURE__*/skipDuplicates(I.acyclicEqualsU)), 'response'); + var response = /*#__PURE__*/setName( /*#__PURE__*/I.pipe2U( /*#__PURE__*/F.lift(function (_ref3) { + var xhr = _ref3.xhr, + parse = _ref3.parse; + + var response = xhr.response; + return parse ? tryParse(response) : response; + }), /*#__PURE__*/skipDuplicates(I.acyclicEqualsU)), 'response'); var responseFull = /*#__PURE__*/setName( /*#__PURE__*/getAfter(isDone, response), 'responseFull'); var responseType = /*#__PURE__*/setName( /*#__PURE__*/L.get([XHR, 'responseType']), 'responseType'); var responseURL = /*#__PURE__*/setName( /*#__PURE__*/L.get([XHR, 'responseURL']), 'responseURL'); diff --git a/dist/karet.xhr.min.js b/dist/karet.xhr.min.js index 8488770..df62dcd 100644 --- a/dist/karet.xhr.min.js +++ b/dist/karet.xhr.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("karet.lift"),require("infestines"),require("kefir"),require("kefir.partial.lenses")):"function"==typeof define&&define.amd?define(["exports","karet.lift","infestines","kefir","kefir.partial.lenses","partial.lenses.validation"],t):t((e.karet=e.karet||{},e.karet.xhr={}),e.karet.lift,e.I,e.Kefir,e.kefir.partial.lenses)}(this,function(e,r,v,o,w,t){"use strict";function n(e){var t=r.combine([e],f);return(s(t)?t.flatMapLatest(p):p(t)).toProperty()}var s=function(e){return e instanceof o.Observable},i=v.curry(function(e,t){return s(t)?t.skipDuplicates(e):t}),u=v.curry(function(e,t){if(s(t))return t.filter(e);if(e(t))return t;throw Error(e.name)}),a=function(e){return e},h={type:"initial"},H=["loadstart","progress","timeout","load","error"],d="xhr",T="up",k="down",E="event",p=(0,v.id)(function(e){var u=e.url,t=e.method,a=void 0===t?"GET":t,r=e.user,d=void 0===r?null:r,n=e.password,p=void 0===n?null:n,f=e.headers,c=e.overrideMimeType,s=e.body,l=void 0===s?null:s,y=e.responseType,g=e.timeout,m=e.withCredentials;return o.stream(function(e){var n=e.emit,t=e.end,r=new XMLHttpRequest,s={xhr:r,up:h,down:h},o=function(t,r){return function(e){n(s=w.set(t,{type:r,event:e},s))}};if(H.forEach(function(e){r.addEventListener(e,o(k,e)),r.upload.addEventListener(e,o(T,e))}),r.addEventListener("readystatechange",function(e){n(s=w.set(E,e,s))}),r.addEventListener("loadend",function(e){t(n(s=w.set(E,e,s)))}),r.open(a,u,!0,d,p),y&&(r.responseType=y),g&&(r.timeout=g),m&&(r.withCredentials=m),null!=f)if(v.isFunction(f.keys)&&(f=Array.from(f)),v.isArray(f))f.forEach(function(e){r.setRequestHeader(e[0],e[1])});else for(var i in f)r.setRequestHeader(i,f[i]);return c&&r.overrideMimeType(c),r.send(l),function(){r.status||r.abort()}})}),f=function(e){return v.isString(e)?{url:e}:e},c=v.curry(function(e,t){return e.includes(t)}),l=v.curry(function(e,t){return w.get([t,"type",c(e)])}),y=l(H),g=l(["progress","loadstart"]),m=l(["load"]),L=l(["error"]),x=l(["timeout"]),R=l(["load","error","timeout"]),S=v.curry(function(e,t){return w.get([t,E,e])}),q=S("loaded"),U=S("total"),b=S("error"),M=function(e){return 200<=e&&e<300},C=v.curryN(3,function(e,t){return v.pipe2U(u(e),t)}),F=a(y(T)),I=a(g(T)),N=a(m(T)),O=a(L(T)),P=a(x(T)),j=a(R(T)),A=a(q(T)),D=a(U(T)),X=a(b(T)),J=a(y(k)),_=a(g(k)),G=a(m(k)),K=a(L(k)),W=a(x(k)),z=a(R(k)),B=a(q(k)),Q=a(U(k)),V=a(b(k)),Y=a(w.get([d,"readyState"])),Z=v.defineNameU(w.get([d,"readyState",function(e){return 2<=e}]),"headersReceived"),$=v.defineNameU(w.get([E,"type",w.is("loadend")]),"isDone"),ee=a(v.pipe2U(w.get([d,"response"]),i(v.acyclicEqualsU))),te=a(C($,ee)),re=a(w.get([d,"responseType"])),ne=a(w.get([d,"responseURL"])),se=a(w.get([d,w.when(w.get(["responseType",c(["","text"])])),"responseText"])),oe=C($,a(w.get([d,w.when(w.get(["responseType",c(["","document"])])),"responseXML"]))),ie=a(w.get([d,"status"])),ue=a(w.get([d,"status",M])),ae=a(w.get([d,"statusText"])),de=v.curryN(2,function(t){return C(Z,a(w.get([d,w.reread(function(e){return e.getResponseHeader(t)})])))}),pe=C(Z,a(w.get([d,w.reread(function(e){return e.getAllResponseHeaders()})]))),fe=a(w.get([d,"timeout"])),ce=a(w.get([d,"withCredentials"])),le=r.lift(M),ye=r.lift(function(e,t){return v.assign({},f(e),f(t))}),ge=v.curry(function(e,t){return n(ye(e,t))}),me=a(ge({responseType:"json"})),ve=a(v.pipe2U(me,te));e.perform=n,e.upHasStarted=F,e.upIsProgressing=I,e.upHasSucceeded=N,e.upHasFailed=O,e.upHasTimedOut=P,e.upHasEnded=j,e.upLoaded=A,e.upTotal=D,e.upError=X,e.downHasStarted=J,e.downIsProgressing=_,e.downHasSucceeded=G,e.downHasFailed=K,e.downHasTimedOut=W,e.downHasEnded=z,e.downLoaded=B,e.downTotal=Q,e.downError=V,e.readyState=Y,e.headersReceived=Z,e.isDone=$,e.response=ee,e.responseFull=te,e.responseType=re,e.responseURL=ne,e.responseText=se,e.responseXML=oe,e.status=ie,e.statusIsHttpSuccess=ue,e.statusText=ae,e.responseHeader=de,e.allResponseHeaders=pe,e.timeout=fe,e.withCredentials=ce,e.isHttpSuccess=le,e.performWith=ge,e.performJson=me,e.getJson=ve,Object.defineProperty(e,"__esModule",{value:!0})}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("karet.lift"),require("infestines"),require("kefir"),require("kefir.partial.lenses")):"function"==typeof define&&define.amd?define(["exports","karet.lift","infestines","kefir","kefir.partial.lenses","partial.lenses.validation"],t):t((e.karet=e.karet||{},e.karet.xhr={}),e.karet.lift,e.I,e.Kefir,e.kefir.partial.lenses)}(this,function(e,r,v,o,h,t){"use strict";function n(e){var t=r.combine([e],f);return(s(t)?t.flatMapLatest(p):p(t)).toProperty()}var s=function(e){return e instanceof o.Observable},i=v.curry(function(e,t){return s(t)?t.skipDuplicates(e):t}),u=v.curry(function(e,t){if(s(t))return t.filter(e);if(e(t))return t;throw Error(e.name)}),a=function(e){return e},w={type:"initial"},H=["loadstart","progress","timeout","load","error"],d="xhr",T="up",k="down",E="event",p=(0,v.id)(function(e){var u=e.url,t=e.method,a=void 0===t?"GET":t,r=e.user,d=void 0===r?null:r,n=e.password,p=void 0===n?null:n,f=e.headers,c=e.overrideMimeType,s=e.body,l=void 0===s?null:s,y=e.responseType,g=e.timeout,m=e.withCredentials;return o.stream(function(e){var n=e.emit,t=e.end,r=new XMLHttpRequest,s={xhr:r,up:w,down:w},o=function(t,r){return function(e){n(s=h.set(t,{type:r,event:e},s))}};if(H.forEach(function(e){r.addEventListener(e,o(k,e)),r.upload.addEventListener(e,o(T,e))}),r.addEventListener("readystatechange",function(e){n(s=h.set(E,e,s))}),r.addEventListener("loadend",function(e){t(n(s=h.set(E,e,s)))}),r.open(a,u,!0,d,p),y&&"json"===(r.responseType=y)&&"json"!==r.responseType&&(s=h.set("parse",!0,s)),g&&(r.timeout=g),m&&(r.withCredentials=m),null!=f)if(v.isFunction(f.keys)&&(f=Array.from(f)),v.isArray(f))f.forEach(function(e){r.setRequestHeader(e[0],e[1])});else for(var i in f)r.setRequestHeader(i,f[i]);return c&&r.overrideMimeType(c),r.send(l),function(){r.status||r.abort()}})}),f=function(e){return v.isString(e)?{url:e}:e},c=v.curry(function(e,t){return e.includes(t)}),l=v.curry(function(e,t){return h.get([t,"type",c(e)])}),y=l(H),g=l(["progress","loadstart"]),m=l(["load"]),x=l(["error"]),L=l(["timeout"]),S=l(["load","error","timeout"]),R=v.curry(function(e,t){return h.get([t,E,e])}),q=R("loaded"),U=R("total"),b=R("error"),M=function(e){return 200<=e&&e<300},j=v.curryN(3,function(e,t){return v.pipe2U(u(e),t)}),N=a(y(T)),O=a(g(T)),C=a(m(T)),F=a(x(T)),I=a(L(T)),P=a(S(T)),A=a(q(T)),D=a(U(T)),J=a(b(T)),X=a(y(k)),_=a(g(k)),G=a(m(k)),K=a(x(k)),W=a(L(k)),z=a(S(k)),B=a(q(k)),Q=a(U(k)),V=a(b(k)),Y=a(h.get([d,"readyState"])),Z=v.defineNameU(h.get([d,"readyState",function(e){return 2<=e}]),"headersReceived"),$=v.defineNameU(h.get([E,"type",h.is("loadend")]),"isDone"),ee=a(v.pipe2U(r.lift(function(e){var t=e.xhr.response;return e.parse?function(e){try{return JSON.parse(e)}catch(e){return null}}(t):t}),i(v.acyclicEqualsU))),te=a(j($,ee)),re=a(h.get([d,"responseType"])),ne=a(h.get([d,"responseURL"])),se=a(h.get([d,h.when(h.get(["responseType",c(["","text"])])),"responseText"])),oe=j($,a(h.get([d,h.when(h.get(["responseType",c(["","document"])])),"responseXML"]))),ie=a(h.get([d,"status"])),ue=a(h.get([d,"status",M])),ae=a(h.get([d,"statusText"])),de=v.curryN(2,function(t){return j(Z,a(h.get([d,h.reread(function(e){return e.getResponseHeader(t)})])))}),pe=j(Z,a(h.get([d,h.reread(function(e){return e.getAllResponseHeaders()})]))),fe=a(h.get([d,"timeout"])),ce=a(h.get([d,"withCredentials"])),le=r.lift(M),ye=r.lift(function(e,t){return v.assign({},f(e),f(t))}),ge=v.curry(function(e,t){return n(ye(e,t))}),me=a(ge({responseType:"json"})),ve=a(v.pipe2U(me,te));e.perform=n,e.upHasStarted=N,e.upIsProgressing=O,e.upHasSucceeded=C,e.upHasFailed=F,e.upHasTimedOut=I,e.upHasEnded=P,e.upLoaded=A,e.upTotal=D,e.upError=J,e.downHasStarted=X,e.downIsProgressing=_,e.downHasSucceeded=G,e.downHasFailed=K,e.downHasTimedOut=W,e.downHasEnded=z,e.downLoaded=B,e.downTotal=Q,e.downError=V,e.readyState=Y,e.headersReceived=Z,e.isDone=$,e.response=ee,e.responseFull=te,e.responseType=re,e.responseURL=ne,e.responseText=se,e.responseXML=oe,e.status=ie,e.statusIsHttpSuccess=ue,e.statusText=ae,e.responseHeader=de,e.allResponseHeaders=pe,e.timeout=fe,e.withCredentials=ce,e.isHttpSuccess=le,e.performWith=ge,e.performJson=me,e.getJson=ve,Object.defineProperty(e,"__esModule",{value:!0})}); diff --git a/package-lock.json b/package-lock.json index 68c1505..27a8f0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "karet.xhr", - "version": "0.2.3-0", + "version": "0.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8b99c2f..dcf80fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "karet.xhr", - "version": "0.2.3-0", + "version": "0.2.3", "description": "An observable wrapper for XMLHttpRequest using Kefir", "module": "dist/karet.xhr.es.js", "main": "dist/karet.xhr.cjs.js",