react-request
Version:
Declarative HTTP requests with React.
2 lines (1 loc) • 7.02 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e.ReactRequest={},e.React)}(this,function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var k={};function c(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.url,r=e.method,n=e.responseType,n=void 0===n?"":n,e=e.body,e=void 0===e?"":e;return[void 0===t?"":t,(void 0===r?"":r).toUpperCase(),n,e].join("||")}function r(e){return!!k[e]}function p(e){var t=e.requestKey,r=e.res,n=e.err;(k[t]||[]).forEach(function(e){r?e.resolve(r):e.reject(n)}),k[t]=null}function j(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},r=arguments[2],n=void 0,o=void 0,o=r?(n=r,t):t.responseType?(n=t,{}):(n={},t),r=n.responseType,s=void 0===r?"":r,t=n.dedupe,i=void 0===t||t,u=n.requestKey||c({url:e.url||e,method:o.method||e.method||"",body:o.body||e.body||""}),r=void 0;if(i){k[u]||(k[u]=[]);var t=k[u],n=!!t.length,a={},r=new Promise(function(e,t){a.resolve=e,a.reject=t});if(t.push(a),n)return r}o=fetch(e,o).then(function(t){var e=void 0,e=s instanceof Function?s(t):s||(204===t.status?"text":"json");return t[e]().then(function(e){return t.data=e,i?void p({requestKey:u,res:t}):t},function(){return t.data=null,i?void p({requestKey:u,res:t}):t})},function(e){return i?void p({requestKey:u,err:e}):Promise.reject(e)});return i?r:o}var n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},o=function(e,t,r){return t&&s(e.prototype,t),r&&s(e,r),e};function s(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}var O=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r,n=arguments[t];for(r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},C={};(function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)})(u,t.Component),o(u,[{key:"render",value:function(){var e=this.props,t=e.children,r=this.state,n=r.response,o=r.error;return t&&t({requestName:e.requestName,url:r.url,fetching:r.fetching,failed:!!(o||n&&!n.ok),response:n,data:r.data,requestKey:r.requestKey,error:o,doFetch:this.fetchRenderProp})||null}}]),o(u,[{key:"componentDidMount",value:function(){this.isLazy()||this.fetchData()}},{key:"componentDidUpdate",value:function(e){var t=this.props.requestKey||c(O({},this.props,{method:this.props.method.toUpperCase()}));t===(e.requestKey||c(O({},e,{method:e.method.toUpperCase()})))||this.isLazy()||this.fetchData({requestKey:t})}},{key:"componentWillUnmount",value:function(){this.willUnmount=!0,this.cancelExistingRequest("Component unmounted")}}]),o=u;function u(e,t){n(this,u);var P=i(this,(u.__proto__||Object.getPrototypeOf(u)).call(this,e,t));return P.isReadRequest=function(e){e=e.toUpperCase();return"GET"==e||"HEAD"==e||"OPTIONS"==e},P.isLazy=function(){var e=P.props,t=e.lazy;return void 0===t?!P.isReadRequest(e.method):t},P.shouldCacheResponse=function(){var e=P.props,t=e.cacheResponse;return void 0===t?P.isReadRequest(e.method):t},P.getFetchPolicy=function(){var e=P.props,t=e.fetchPolicy;return void 0===t?P.isReadRequest(e.method)?"cache-first":"network-only":t},P.cancelExistingRequest=function(e){P.state.fetching&&null!==P._currentRequestKey&&((e=Error(e)).name="AbortError",P.onResponseReceived(O({},P.responseReceivedInfo,{error:e,hittingNetwork:!0})))},P.fetchRenderProp=function(t){return new Promise(function(e){setTimeout(function(){P.fetchData(t,!0,e)})})},P.getRequestKey=function(e){if(e&&e.requestKey)return e.requestKey;if(e){var t=Object.assign({},P.props,e);return c({url:t.url,body:t.body,method:t.method.toUpperCase()})}if(P.props.requestKey)return P.props.requestKey;t=P.props;return c({url:t.url,body:t.body,method:t.method.toUpperCase()})},P.fetchData=function(e,t,r){var n=P.props,o=n.requestName,s=n.dedupe,i=n.beforeFetch;P.cancelExistingRequest("New fetch initiated");var u=P.getRequestKey(e),a=Object.assign({},P.props,e);P._currentRequestKey=u;var c=a.url,p=a.body,d=a.credentials,l=a.headers,f=a.responseType,h=a.mode,y=a.cache,v=a.redirect,m=a.referrer,R=a.referrerPolicy,q=a.integrity,n=a.keepalive,e=a.signal,a=a.method.toUpperCase(),g=P.shouldCacheResponse(),n={body:p,credentials:d,headers:l,method:a,mode:h,cache:y,redirect:v,referrer:m,referrerPolicy:R,integrity:q,keepalive:n,signal:e},b={url:c,init:n,requestKey:u,responseType:f};P.responseReceivedInfo=b;var e=P.getFetchPolicy(),K=void 0;if("network-only"!==e&&!t)if(K=C[u]){if(P.onResponseReceived(O({},b,{response:K,hittingNetwork:!1,stillFetching:"cache-and-network"===e})),"cache-first"===e||"cache-only"===e)return Promise.resolve(K)}else if("cache-only"===e){o=Error('Response for "'+o+'" not found in cache.');return P.onResponseReceived(O({},b,{error:o,hittingNetwork:!1})),Promise.resolve(o)}P.setState({requestKey:u,url:c,error:null,failed:!1,fetching:!0});var w=!k[u]||!s;return w&&i({url:c,init:n,requestKey:u}),j(c,n,{requestKey:u,responseType:f,dedupe:s}).then(function(e){return g&&(C[u]=e),P._currentRequestKey===u&&P.onResponseReceived(O({},b,{response:e,hittingNetwork:w,resolve:r})),e},function(e){return P._currentRequestKey===u&&P.onResponseReceived(O({},b,{error:e,cachedResponse:K,hittingNetwork:w,resolve:r})),e})},P.onResponseReceived=function(e){var t=e.error,r=void 0===t?null:t,n=e.response,o=void 0===n?null:n,s=e.hittingNetwork,i=e.url,u=e.init,a=e.requestKey,c=e.cachedResponse,t=e.stillFetching,n=void 0!==t&&t,t=e.resolve;P.responseReceivedInfo=null,n||(P._currentRequestKey=null);e=void 0;o&&o.data?e=o.data:c&&c.data&&(e=c.data),e=e?P.props.transformData(e):null,r&&P.state.data&&(e=P.state.data);u={url:i,init:u,requestKey:a,error:r,failed:!!(r||o&&!o.ok),response:o,data:e,didUnmount:!!P.willUnmount};"function"==typeof t&&t(u),s&&P.props.afterFetch(u),P.willUnmount||P.setState({url:i,data:e,error:r,response:o,fetching:n,requestKey:a},function(){return P.props.onResponse(r,o)})},P.state={requestKey:e.requestKey||c(O({},e,{method:e.method.toUpperCase()})),requestName:e.requestName,fetching:!1,response:null,data:null,error:null,url:e.url},P}o.defaultProps={requestName:"anonymousRequest",onResponse:function(){},beforeFetch:function(){},afterFetch:function(){},transformData:function(e){return e},dedupe:!0,method:"get",referrerPolicy:"",integrity:"",referrer:"about:client"},e.Fetch=o,e.fetchDedupe=j,e.getRequestKey=c,e.isRequestInFlight=r,e.clearRequestCache=function(){k={}},e.clearResponseCache=function(){C={}},Object.defineProperty(e,"__esModule",{value:!0})});