UNPKG

react-accio

Version:

Declaratively fetch multiple APIs with a single React component.

3 lines (2 loc) 5.39 kB
var t,e=require("react"),r=(t=require("hoist-non-react-statics"))&&"object"==typeof t&&"default"in t?t.default:t;function o(t){return t.then(function(t){return[null,t]}).catch(function(t){return[t]})}function n(t,e){return o(fetch(t,e)).then(function(t){var e=t[0],r=t[1];if(e)throw new Error("Accio error: "+e.message);if(!1===r.ok)throw new Error("Accio failed to fetch: "+r.url+" "+r.status+" ("+r.statusText+")");return o(r.json())}).then(function(t){var e=t[0],r=t[1];if(e)throw new Error("Error parsing response to json: "+e.message);return r})}var i,s,c={resolver:n,method:"GET"},a={};function p(t,e){var r=t;return e.body&&(r+=JSON.stringify(e.body)),r}Object.defineProperties(a,{resolver:(i=c,s="resolver",{get:function(){return i[s]},set:function(t){if("function"!=typeof t)throw new TypeError("Expected resolver to be a function. But instead got "+typeof t+". Check your "+s+" assignment to Accio defaults.");i[s]=t}}),method:{get:function(){return c.method},set:function(t){if(!["GET","POST"].includes(t))throw new TypeError("Invalid method "+t+". Only GET & POST are supported. Check your method assignment to Accio defaults.");c.method=t}}});var u=e.createContext(null),h=u.Provider,l=u.Consumer,f=function(t){function r(){for(var e=[],r=arguments.length;r--;)e[r]=arguments[r];t.apply(this,e),this.cache=function(t){if("memory"===t)return new Map;if("localStorage"===t)throw new Error("Local storage cache support is incomplete. Please use memory in the meantime.");throw new Error("Unknown AccioCache storage type :/")}(this.props.use)}return t&&(r.__proto__=t),(r.prototype=Object.create(t&&t.prototype)).constructor=r,r.prototype.render=function(){return e.createElement(h,{value:this.cache},this.props.children)},r}(e.Component);f.defaultProps={use:"memory"};var d=l,m=new Set(["children","url","context","defer","ignoreCache","onComplete","onError","onShowLoading","onStartFetching","timeout","_cache"]);function y(t){for(var e={},r=Object.keys(t),o=0;o<r.length;o++){var n=r[o];m.has(n)||(e[n]=t[n])}return e}var v={PRELOAD_ERROR:-1,IDLE:0,PRELOADING:1,PRELOADED:2},E=function(t){function e(){for(var e=[],r=arguments.length;r--;)e[r]=arguments[r];t.apply(this,e),this.state={loading:!1,response:null,error:null,trigger:this.doWork.bind(this)},this.preloadStatus=v.IDLE,this.preloadError=null,this.requestId=0}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.preload=function(){var t=this;return Promise.resolve().then(function(){if(t.props._cache)return t.preloadStatus<v.PRELOADING?(t.preloadStatus=v.PRELOADING,o(t.doFetch.call(t)).then(function(e){var r=e[0],o=e[1];return r?(t.preloadStatus=v.PRELOAD_ERROR,void(t.preloadError=r)):(t.preloadStatus=v.PRELOADED,o)})):void 0;console.warn("Preloading without cache is not supported. This can be fixed by wrapping your app with <AccioCacheProvider />.")})},e.prototype.componentDidMount=function(){!0!==this.props.defer&&this.doWork.call(this)},e.prototype.componentWillUnmount=function(){this.timer&&clearTimeout(this.timer)},e.prototype.componentDidUpdate=function(t){var e=this.props.fetchKey;e&&e(this.props)!==e(t)&&this.doWork.call(this)},e.prototype.doWork=function(){var t=this,e=++this.requestId,r=this.props,n=r._cache,i=r.onStartFetching,s=r.timeout,c=p(r.url,y(this.props));if(!n||this.preloadStatus!==v.PRELOADED)return"number"==typeof s?this.timer=setTimeout(this.setLoading.bind(this,!0),s):this.setLoading.call(this,!0),"function"==typeof i&&i(),o(this.doFetch.call(this)).then(function(r){var o=r[0],n=r[1];e===t.requestId&&(o&&t.setError.call(t,o),t.timer&&clearTimeout(t.timer),t.setLoading.call(t,!1),t.setResponse.call(t,n))});var a=n.get(c);this.setResponse.call(this,a)},e.prototype.doFetch=function(){var t=this.props,r=t._cache,o=t.context,n=t.ignoreCache,i=t.url,s=e.defaults.resolver,c=y(this.props),a=function(){return s(i,c,o)};if(r&&!1===n){var u=p(i,c);if(r.has(u))return Promise.resolve(r.get(u));var h=a();return r.set(u,h),h.then(function(t){return r.set(u,t),t}).catch(function(t){throw r.delete(u),t})}return a()},e.prototype.setLoading=function(t){"function"==typeof this.props.onShowLoading&&this.props.onShowLoading(),this.setState({loading:t})},e.prototype.setResponse=function(t){"function"==typeof this.props.onComplete&&this.props.onComplete(t),this.setState({response:t})},e.prototype.setError=function(t){"function"==typeof this.props.onError&&this.props.onError(t),this.setState({error:t})},e.prototype.render=function(){return this.props.children(this.state)},e}(e.Component);E.defaults=a,E.defaultProps={defer:!1,ignoreCache:!1,context:{},method:E.defaults.method};var g=function(t){function r(){t.apply(this,arguments)}return t&&(r.__proto__=t),(r.prototype=Object.create(t&&t.prototype)).constructor=r,r.prototype.renderChild=function(t){var r=this.props,o=r.children,n=r.forwardedRef,i=function(t,e){var r={};for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&-1===e.indexOf(o)&&(r[o]=t[o]);return r}(r,["children","forwardedRef"]);return e.createElement(E,Object.assign({},i,{ref:n,_cache:t}),o)},r.prototype.render=function(){return e.createElement(d,null,this.renderChild.bind(this))},r}(e.Component),w=e.forwardRef(function(t,r){return e.createElement(g,Object.assign({},t,{forwardedRef:r}))});r(w,E),exports.Accio=w,exports.defaultResolver=n,exports.AccioCacheProvider=f; //# sourceMappingURL=index.js.map