datx-network
Advanced tools
Comparing version 2.0.0-beta.5 to 2.0.0-beta.6
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,r=require("react"),n=require("mobx"),o=require("datx");(e=exports.CachingStrategy||(exports.CachingStrategy={}))[e.NetworkOnly=1]="NetworkOnly",e[e.NetworkFirst=2]="NetworkFirst",e[e.StaleWhileRevalidate=3]="StaleWhileRevalidate",e[e.CacheOnly=4]="CacheOnly",e[e.CacheFirst=5]="CacheFirst",e[e.StaleAndUpdate=6]="StaleAndUpdate",(t=exports.ParamArrayType||(exports.ParamArrayType={}))[t.MultipleParams=0]="MultipleParams",t[t.CommaSeparated=1]="CommaSeparated",t[t.ParamArray=2]="ParamArray";var i="undefined"!=typeof window;function a(e){var t,r;if("object"!=typeof e||null===e)return e;var n=Array.isArray(e)?[]:{};for(r in e)t=e[r],n[r]=a(t);return n}var s,c=/\{\s*([a-zA-Z0-9\-_]+)\s*\}/g;function u(e,t,r,n){return function(e,t){var r=e;if(t.length){var n="";-1===r.indexOf("?")?n="?":r.endsWith("&")||r.endsWith("?")||(n="&"),r+=n+t.join("&")}return r}(e,function e(t,r,n){void 0===n&&(n="");var o=[];return Object.keys(r).forEach((function(i){var a=""+n+(n?"["+i+"]":i);r[i]instanceof Array?t===exports.ParamArrayType.CommaSeparated?o.push({key:a,value:r[i].join(",")}):t===exports.ParamArrayType.MultipleParams?o.push.apply(o,r[i].map((function(e){return{key:a,value:e}}))):t===exports.ParamArrayType.ParamArray&&o.push.apply(o,r[i].map((function(e){return{key:a+"[]",value:e}}))):"object"==typeof r[i]?o.push.apply(o,e(t,r[i],a)):o.push({key:a,value:r[i]})})),o}(r,t).map((function(e){var t=e.key,r=e.value;return{key:t,value:n?encodeURIComponent(r):r}})).map((function(e){return e.key+"="+e.value})))}function p(e,t,r,n){var o,i=arguments.length,a=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,n);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,r,a):o(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a}function l(e){if(e instanceof Array)return e;var t=[];return e.forEach((function(e,r){t.push([r,e])})),t}function f(e,t,r){if(t&&e.data){var n=r||t.add(e.data);return new o.Bucket.ToOneOrMany(n,t,!0)}if(e.data&&e.data){if(e.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:new o.PureModel(e.data)}}return new o.Bucket.ToOneOrMany(null,t,!0)}(s=exports.HttpMethod||(exports.HttpMethod={})).Get="GET",s.Post="POST",s.Put="PUT",s.Patch="Patch",s.Delete="Delete",s.Options="Options",s.Head="Head";var d,h=function(){function e(e,t,r,o,i){var a=this;this.__internal={response:{},views:[]},this.collection=t,n.runInAction((function(){if(a.__updateInternal(e,r,i),a.__data=f(e,t,o),a.views.forEach((function(e){a.__data.value&&e.add(a.__data.value)})),Object.freeze(a),a.error)throw a}))}return Object.defineProperty(e.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),e.prototype.__updateInternal=function(e,t,r){var n;t&&(this.__internal.options=t),this.__internal.response=e,this.__internal.headers=e.headers&&((n=e.headers)instanceof Array?new Headers(n):n),this.__internal.requestHeaders=e.requestHeaders,this.__internal.error=e.error,this.__internal.status=e.status,r&&(this.__internal.views=r),this.error||this.status||(this.__internal.error=new Error("Network not available"))},e.prototype.replaceData=function(t){var r=this.data;if(r===t)return this;var n=o.getModelId(r).toString(),i=o.getModelType(r),a=this.views.map((function(e){return e.list.indexOf(r)}));return this.collection&&(this.collection.removeOne(i,n),this.collection.add(t)),o.updateModel(t,o.modelToJSON(r)),o.updateModelId(t,n),this.views.forEach((function(e,r){-1!==a[r]&&(e.list[a[r]]=t)})),new e(this.__internal.response,this.collection,this.__internal.options,t)},e.prototype.clone=function(){return new e(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(e.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&l(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),e.prototype.update=function(e,t){this.__updateInternal(e,void 0,t);var r=f(e,this.collection);return this.__data.__readonlyValue=r.value,this},p([n.action],e.prototype,"replaceData",null),p([n.action],e.prototype,"update",null),e}(),y=[];function v(e,t,r,i,a){return void 0===i&&(i=!1),t(e).then((function(t){var n=Object.assign({},t,{collection:e.collection});return a?(a.update(n,e.views),a):new h(r.config.parse(n),e.collection,e.options,void 0,e.views)})).then((function(t){return i&&function(e,t){if(t&&t.isSuccess&&(t.data||null===t.data)){var r=(i=t.data||[],a=o.getModelType,n.isArrayLike(i)?i.map((function(e){return a(e)})):null===i?null:a(i));(y=y.filter((function(t){return t.url!==e}))).unshift({response:t.snapshot,collection:t.collection,time:Date.now(),types:[].concat(r),url:e})}var i,a}(e.url,t),t})).catch((function(t){var n=Object.assign({},t,{collection:e.collection});throw new h(r.config.parse(n),e.collection,e.options,void 0,e.views)}))}function g(e,t,r){var n,o;return new h({error:new Error(e),requestHeaders:null===(o=null===(n=t.options)||void 0===n?void 0:n.networkConfig)||void 0===o?void 0:o.headers},r,t.options)}!function(e){e[e.Raw=0]="Raw",e[e.Json=1]="Json",e[e.Multipart=2]="Multipart",e[e.Urlencoded=3]="Urlencoded"}(d||(d={}));var _=function(){function e(e){this._config={baseUrl:"/",cache:i?exports.CachingStrategy.CacheFirst:exports.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!0,fetchReference:i&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:exports.ParamArrayType.ParamArray,serialize:function(e,t){return e},parse:function(e){return e}},this._options={method:exports.HttpMethod.Get,headers:{},query:{},params:{},bodyType:d.Json},this._interceptors=[],this._config.baseUrl=e}return Object.defineProperty(e.prototype,"config",{get:function(){return this._config},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"options",{get:function(){return this._options},enumerable:!1,configurable:!0}),e.prototype.baseFetch=function(e,t,r,n){return function(e,t,r,n,o){var i;void 0===o&&(o={});var a,s=0,c=Promise.resolve(),u=t.toUpperCase(),p="GET"!==u&&"HEAD"!==u;return c.then((function(){var i=e.config.defaultFetchOptions.headers||{},a=Object.assign({},i,o),s=Object.assign({},e.config.defaultFetchOptions,{body:p&&n||void 0,headers:a,method:t});if(e.config.fetchReference)return e.config.fetchReference(r,s);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(e){return s=e.status,a=e.headers,e.json()})).catch((function(e){if(204===s)return null;if(0===s)throw null;throw e})).then((function(e){if(i=e,s>=400)throw{message:"Invalid HTTP status: "+s,status:s};return{data:i,headers:a,requestHeaders:o,status:s}})).catch((function(e){throw{data:i,error:e,headers:a,requestHeaders:o,status:s}}))}(this,e,t,r,n)},e.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var r=this.clone();return e.forEach((function(e){return e(r)})),r},e.prototype.doRequest=function(e){return this.baseFetch(e.method,e.url,e.data,this.options.headers).then((function(e){return{data:e.data,status:e.status,headers:e.headers,requestHeaders:e.requestHeaders,error:e.error,collection:e.collection}}),(function(e){return Promise.reject({data:e.data,status:e.status,headers:e.headers,requestHeaders:e.requestHeaders,error:e.error,collection:e.collection})}))},e.prototype.processBody=function(){if(this._options.body)return this._options.bodyType===d.Json?(this._options.headers["content-type"]="application/json","string"==typeof this._options.body?this._options.body:JSON.stringify(this._options.body)):this._options.bodyType===d.Urlencoded?(this._options.headers["content-type"]="application/x-www-form-urlencoded","string"==typeof this._options.body?this._options.body:u("",this._options.body,this._config.paramArrayType,this._config.encodeQueryString).slice(1)):this._options.bodyType===d.Multipart?(this._options.headers["content-type"]="multipart/form-data",this._options.body instanceof FormData?this._options.body:new FormData(this._options.body)):"string"==typeof this._options.body?this._options.body:JSON.stringify(this._options.body)},e.prototype.fetch=function(e){var t=this;if(!this.options.url)throw new Error("URL should be defined");var r=Object.assign({},this.options.params,e),n=u(function(e,t){for(var r=e,n=c.exec(r),o=0;n;){var i=t[n[1]];void 0===i&&(i=n[0],o=c.lastIndex),r=r.replace(n[0],i),c.lastIndex=o,n=c.exec(r)}return r}(this.options.url,r),this.options.query,this.config.paramArrayType,this.config.encodeQueryString);return this._interceptors.reverse().reduce((function(e,t){return function(r){return t(r,e)}}),(function(e){return(r=t._config.cache,n=t._config.maxCacheAge,o=t,function(e,t){var i,a,s,c,u,p=e.method===exports.HttpMethod.Get,l=(null===(a=null===(i=e.options)||void 0===i?void 0:i.cacheOptions)||void 0===a?void 0:a.skipCache)||!p?exports.CachingStrategy.NetworkOnly:(null===(c=null===(s=e.options)||void 0===s?void 0:s.cacheOptions)||void 0===c?void 0:c.cachingStrategy)||r;if(l===exports.CachingStrategy.NetworkOnly)return v(e,t,o);var f=function(e,t){var r=Date.now()-1e3*t,n=y.find((function(t){return t.url===e&&t.time>r}));if(n){var o=n.response;return{response:new h(o.response,n.collection,o.options),time:n.time,types:n.types,url:n.url}}}(e.url,n);if(l===exports.CachingStrategy.NetworkFirst)return v(e,t,o,!0).catch((function(e){if(f)return f.response;throw e}));if(l===exports.CachingStrategy.StaleWhileRevalidate){var d=v(e,t,o,!0);return f?(d.catch((function(){})),Promise.resolve(f.response)):d}if(l===exports.CachingStrategy.CacheOnly)return f?Promise.resolve(f.response):Promise.reject(g("No cache for this request",e,null==e?void 0:e.collection));if(l===exports.CachingStrategy.CacheFirst)return f?Promise.resolve(f.response):v(e,t,o,!0);if(l===exports.CachingStrategy.StaleAndUpdate){var _=null===(u=null==f?void 0:f.response)||void 0===u?void 0:u.clone();return d=v(e,t,o,!0,_),_?(d.catch((function(){})),Promise.resolve(_)):d}return Promise.reject(g("Invalid caching strategy",e,null==e?void 0:e.collection))})(e,t.doRequest.bind(t));var r,n,o}))({url:n,method:this._options.method,data:this.processBody()})},e.prototype.useHook=function(e,t){var n=this,o=r.useState(null),i=o[0],a=o[1],s=r.useState(null),c=s[0],u=s[1],p=r.useState(null),l=p[0],f=p[1],d=r.useCallback((function(){var t=n.fetch(e);return a(t),u(null),f(null),t.then((function(e){u(e),a(null)})).catch((function(e){f(e),a(null)}))}),[this.fetch,e]);if(r.useEffect((function(){d()}),[d]),(null==t?void 0:t.suspense)&&i)throw i;return[c,Boolean(i),l]},e.prototype.clone=function(e){void 0===e&&(e=this.constructor);var t=new e(this._config.baseUrl);return t._config=a(this._config),t._interceptors=this._interceptors.slice(),t._options=a(this._options),t.config.fetchReference=this.config.fetchReference,t},e}();exports.BaseRequest=_,exports.Response=h,exports.addInterceptor=function(e){return function(t){t._interceptors.push(e)}},exports.body=function(e,t){return function(r){t||0===t?r.options.bodyType=t:e instanceof FormData?r.options.bodyType=d.Multipart:r.options.bodyType="object"==typeof e?d.Json:d.Raw,r.options.body=r.config.serialize(e,r.options.bodyType)}},exports.cache=function(e,t){return void 0===t&&(t=1/0),function(r){r.config.cache=e,r.config.maxCacheAge=t}},exports.encodeQueryString=function(e){return function(t){t.config.encodeQueryString=e}},exports.fetchReference=function(e){return function(t){t.config.fetchReference=e}},exports.header=function(e,t){return function(r){r.options.headers[e]=t}},exports.method=function(e){return function(t){t.options.method=e}},exports.paramArrayType=function(e){return function(t){t.config.paramArrayType=e}},exports.params=function(e,t){return function(r){"string"==typeof e?r.options.params[e]=t:Object.assign(r.options.params,e)}},exports.parser=function(e){return function(t){t.config.parse=e}},exports.query=function(e,t){return function(r){r.options.query[e]=t}},exports.serializer=function(e){return function(t){t.config.serialize=e}},exports.setUrl=function(e){return function(t){t.options.url=e}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,r=require("react"),n=require("mobx"),o=require("datx");(e=exports.CachingStrategy||(exports.CachingStrategy={}))[e.NetworkOnly=1]="NetworkOnly",e[e.NetworkFirst=2]="NetworkFirst",e[e.StaleWhileRevalidate=3]="StaleWhileRevalidate",e[e.CacheOnly=4]="CacheOnly",e[e.CacheFirst=5]="CacheFirst",e[e.StaleAndUpdate=6]="StaleAndUpdate",(t=exports.ParamArrayType||(exports.ParamArrayType={}))[t.MultipleParams=0]="MultipleParams",t[t.CommaSeparated=1]="CommaSeparated",t[t.ParamArray=2]="ParamArray";var i="undefined"!=typeof window;function a(e){var t,r;if("object"!=typeof e||null===e)return e;var n=Array.isArray(e)?[]:{};for(r in e)t=e[r],n[r]=a(t);return n}var s,c=/\{\s*([a-zA-Z0-9\-_]+)\s*\}/g;function u(e,t,r,n){return function(e,t){var r=e;if(t.length){var n="";-1===r.indexOf("?")?n="?":r.endsWith("&")||r.endsWith("?")||(n="&"),r+=n+t.join("&")}return r}(e,function e(t,r,n){void 0===n&&(n="");var o=[];return Object.keys(r).forEach((function(i){var a=""+n+(n?"["+i+"]":i);r[i]instanceof Array?t===exports.ParamArrayType.CommaSeparated?o.push({key:a,value:r[i].join(",")}):t===exports.ParamArrayType.MultipleParams?o.push.apply(o,r[i].map((function(e){return{key:a,value:e}}))):t===exports.ParamArrayType.ParamArray&&o.push.apply(o,r[i].map((function(e){return{key:a+"[]",value:e}}))):"object"==typeof r[i]?o.push.apply(o,e(t,r[i],a)):o.push({key:a,value:r[i]})})),o}(r,t).map((function(e){var t=e.key,r=e.value;return{key:t,value:n?encodeURIComponent(r):r}})).map((function(e){return e.key+"="+e.value})))}function p(e,t,r,n){var o,i=arguments.length,a=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,n);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,r,a):o(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a}function l(e){if(e instanceof Array)return e;var t=[];return e.forEach((function(e,r){t.push([r,e])})),t}function f(e,t,r){if(t&&e.data){var n=r||t.add(e.data);return new o.Bucket.ToOneOrMany(n,t,!0)}if(e.data&&e.data){if(e.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:new o.PureModel(e.data)}}return new o.Bucket.ToOneOrMany(null,t,!0)}(s=exports.HttpMethod||(exports.HttpMethod={})).Get="GET",s.Post="POST",s.Put="PUT",s.Patch="Patch",s.Delete="Delete",s.Options="Options",s.Head="Head";var d,h=function(){function e(e,t,r,o,i){var a=this;this.__internal={response:{},views:[]},this.collection=t,n.runInAction((function(){if(a.__updateInternal(e,r,i),a.__data=f(e,t,o),a.views.forEach((function(e){a.__data.value&&e.add(a.__data.value)})),Object.freeze(a),a.error)throw a}))}return Object.defineProperty(e.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),e.prototype.__updateInternal=function(e,t,r){var n;t&&(this.__internal.options=t),this.__internal.response=e,this.__internal.headers=e.headers&&((n=e.headers)instanceof Array?new Headers(n):n),this.__internal.requestHeaders=e.requestHeaders,this.__internal.error=e.error,this.__internal.status=e.status,r&&(this.__internal.views=r),this.error||this.status||(this.__internal.error=new Error("Network not available"))},e.prototype.replaceData=function(t){var r=this.data;if(r===t)return this;var n=o.getModelId(r).toString(),i=o.getModelType(r),a=this.views.map((function(e){return e.list.indexOf(r)}));return this.collection&&(this.collection.removeOne(i,n),this.collection.add(t)),o.updateModel(t,o.modelToJSON(r)),o.updateModelId(t,n),this.views.forEach((function(e,r){-1!==a[r]&&(e.list[a[r]]=t)})),new e(this.__internal.response,this.collection,this.__internal.options,t)},e.prototype.clone=function(){return new e(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(e.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&l(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),e.prototype.update=function(e,t){this.__updateInternal(e,void 0,t);var r=f(e,this.collection);return this.__data.__readonlyValue=r.value,this},p([n.action],e.prototype,"replaceData",null),p([n.action],e.prototype,"update",null),e}(),y=[];function v(e,t,r,i,a){return void 0===i&&(i=!1),t(e).then((function(t){var n=Object.assign({},t,{collection:e.collection});return a?(a.update(n,e.views),a):new h(r.config.parse(n),e.collection,e.options,void 0,e.views)})).then((function(t){return i&&function(e,t){if(t&&t.isSuccess&&(t.data||null===t.data)){var r=(i=t.data||[],a=o.getModelType,n.isArrayLike(i)?i.map((function(e){return a(e)})):null===i?null:a(i));(y=y.filter((function(t){return t.url!==e}))).unshift({response:t.snapshot,collection:t.collection,time:Date.now(),types:[].concat(r),url:e})}var i,a}(e.url,t),t})).catch((function(t){var n=Object.assign({},t,{collection:e.collection});throw new h(r.config.parse(n),e.collection,e.options,void 0,e.views)}))}function g(e,t,r){var n,o;return new h({error:new Error(e),requestHeaders:null===(o=null===(n=t.options)||void 0===n?void 0:n.networkConfig)||void 0===o?void 0:o.headers},r,t.options)}!function(e){e[e.Raw=0]="Raw",e[e.Json=1]="Json",e[e.Multipart=2]="Multipart",e[e.Urlencoded=3]="Urlencoded"}(d||(d={}));var _=function(){function e(e){this._config={baseUrl:"/",cache:i?exports.CachingStrategy.CacheFirst:exports.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!0,fetchReference:i&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:exports.ParamArrayType.ParamArray,serialize:function(e,t){return e},parse:function(e){return e}},this._options={method:exports.HttpMethod.Get,headers:{},query:{},params:{},bodyType:d.Json},this._interceptors=[],this._config.baseUrl=e}return Object.defineProperty(e.prototype,"config",{get:function(){return this._config},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"options",{get:function(){return this._options},enumerable:!1,configurable:!0}),e.prototype.baseFetch=function(e,t,r,n){return function(e,t,r,n,o){var i;void 0===o&&(o={});var a,s=0,c=Promise.resolve(),u=t.toUpperCase(),p="GET"!==u&&"HEAD"!==u;return c.then((function(){var i=e.config.defaultFetchOptions.headers||{},a=Object.assign({},i,o),s=Object.assign({},e.config.defaultFetchOptions,{body:p&&n||void 0,headers:a,method:t});if(e.config.fetchReference)return e.config.fetchReference(r,s);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(e){return s=e.status,a=e.headers,e.json()})).catch((function(e){if(204===s)return null;if(0===s)throw null;throw e})).then((function(t){if(i=t,s>=400)throw{message:"Invalid HTTP status: "+s,status:s};return{data:i,headers:a,requestHeaders:o,status:s,collection:e.config.collection}})).catch((function(t){throw{data:i,error:t,headers:a,requestHeaders:o,status:s,collection:e.config.collection}}))}(this,e,t,r,n)},e.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var r=this.clone();return e.forEach((function(e){return e(r)})),r},e.prototype.doRequest=function(e){return this.baseFetch(e.method,e.url,e.data,this.options.headers).then((function(e){return{data:e.data,status:e.status,headers:e.headers,requestHeaders:e.requestHeaders,error:e.error,collection:e.collection}}),(function(e){return Promise.reject({data:e.data,status:e.status,headers:e.headers,requestHeaders:e.requestHeaders,error:e.error,collection:e.collection})}))},e.prototype.processBody=function(){if(this._options.body)return this._options.bodyType===d.Json?(this._options.headers["content-type"]="application/json","string"==typeof this._options.body?this._options.body:JSON.stringify(this._options.body)):this._options.bodyType===d.Urlencoded?(this._options.headers["content-type"]="application/x-www-form-urlencoded","string"==typeof this._options.body?this._options.body:u("",this._options.body,this._config.paramArrayType,this._config.encodeQueryString).slice(1)):this._options.bodyType===d.Multipart?(this._options.headers["content-type"]="multipart/form-data",this._options.body instanceof FormData?this._options.body:new FormData(this._options.body)):"string"==typeof this._options.body?this._options.body:JSON.stringify(this._options.body)},e.prototype.fetch=function(e){var t=this;if(!this.options.url)throw new Error("URL should be defined");var r=Object.assign({},this.options.params,e),n=u(function(e,t){for(var r=e,n=c.exec(r),o=0;n;){var i=t[n[1]];void 0===i&&(i=n[0],o=c.lastIndex),r=r.replace(n[0],i),c.lastIndex=o,n=c.exec(r)}return r}(this.options.url,r),this.options.query,this.config.paramArrayType,this.config.encodeQueryString);return this._interceptors.reverse().reduce((function(e,t){return function(r){return t(r,e)}}),(function(e){return(r=t._config.cache,n=t._config.maxCacheAge,o=t,function(e,t){var i,a,s,c,u,p=e.method===exports.HttpMethod.Get,l=(null===(a=null===(i=e.options)||void 0===i?void 0:i.cacheOptions)||void 0===a?void 0:a.skipCache)||!p?exports.CachingStrategy.NetworkOnly:(null===(c=null===(s=e.options)||void 0===s?void 0:s.cacheOptions)||void 0===c?void 0:c.cachingStrategy)||r;if(l===exports.CachingStrategy.NetworkOnly)return v(e,t,o);var f=function(e,t){var r=Date.now()-1e3*t,n=y.find((function(t){return t.url===e&&t.time>r}));if(n){var o=n.response;return{response:new h(o.response,n.collection,o.options),time:n.time,types:n.types,url:n.url}}}(e.url,n);if(l===exports.CachingStrategy.NetworkFirst)return v(e,t,o,!0).catch((function(e){if(f)return f.response;throw e}));if(l===exports.CachingStrategy.StaleWhileRevalidate){var d=v(e,t,o,!0);return f?(d.catch((function(){})),Promise.resolve(f.response)):d}if(l===exports.CachingStrategy.CacheOnly)return f?Promise.resolve(f.response):Promise.reject(g("No cache for this request",e,null==e?void 0:e.collection));if(l===exports.CachingStrategy.CacheFirst)return f?Promise.resolve(f.response):v(e,t,o,!0);if(l===exports.CachingStrategy.StaleAndUpdate){var _=null===(u=null==f?void 0:f.response)||void 0===u?void 0:u.clone();return d=v(e,t,o,!0,_),_?(d.catch((function(){})),Promise.resolve(_)):d}return Promise.reject(g("Invalid caching strategy",e,null==e?void 0:e.collection))})(e,t.doRequest.bind(t));var r,n,o}))({url:n,method:this._options.method,data:this.processBody()})},e.prototype.useHook=function(e,t){var n=this,o=r.useState(null),i=o[0],a=o[1],s=r.useState(null),c=s[0],u=s[1],p=r.useState(null),l=p[0],f=p[1],d=r.useCallback((function(){var t=n.fetch(e);return a(t),u(null),f(null),t.then((function(e){u(e),a(null)})).catch((function(e){f(e),a(null)}))}),[this.fetch,e]);if(r.useEffect((function(){d()}),[d]),(null==t?void 0:t.suspense)&&i)throw i;return[c,Boolean(i),l]},e.prototype.clone=function(e){void 0===e&&(e=this.constructor);var t=new e(this._config.baseUrl);return t._config=a(this._config),t._interceptors=this._interceptors.slice(),t._options=a(this._options),t.config.fetchReference=this.config.fetchReference,t},e}();exports.BaseRequest=_,exports.Response=h,exports.addInterceptor=function(e){return function(t){t._interceptors.push(e)}},exports.body=function(e,t){return function(r){t||0===t?r.options.bodyType=t:e instanceof FormData?r.options.bodyType=d.Multipart:r.options.bodyType="object"==typeof e?d.Json:d.Raw,r.options.body=r.config.serialize(e,r.options.bodyType)}},exports.cache=function(e,t){return void 0===t&&(t=1/0),function(r){r.config.cache=e,r.config.maxCacheAge=t}},exports.encodeQueryString=function(e){return function(t){t.config.encodeQueryString=e}},exports.fetchReference=function(e){return function(t){t.config.fetchReference=e}},exports.header=function(e,t){return function(r){r.options.headers[e]=t}},exports.method=function(e){return function(t){t.options.method=e}},exports.paramArrayType=function(e){return function(t){t.config.paramArrayType=e}},exports.params=function(e,t){return function(r){"string"==typeof e?r.options.params[e]=t:Object.assign(r.options.params,e)}},exports.parser=function(e){return function(t){t.config.parse=e}},exports.query=function(e,t){return function(r){r.options.query[e]=t}},exports.serializer=function(e){return function(t){t.config.serialize=e}},exports.setUrl=function(e){return function(t){t.options.url=e}}; |
@@ -175,6 +175,13 @@ import { useState, useCallback, useEffect } from 'react'; | ||
} | ||
return { data: data, headers: headers, requestHeaders: requestHeaders, status: status }; | ||
return { data: data, headers: headers, requestHeaders: requestHeaders, status: status, collection: requestObj.config.collection }; | ||
}) | ||
.catch(function (error) { | ||
throw { data: data, error: error, headers: headers, requestHeaders: requestHeaders, status: status }; | ||
throw { | ||
data: data, | ||
error: error, | ||
headers: headers, | ||
requestHeaders: requestHeaders, | ||
status: status, | ||
collection: requestObj.config.collection, | ||
}; | ||
}); | ||
@@ -181,0 +188,0 @@ } |
@@ -5,2 +5,3 @@ import { CachingStrategy } from '../enums/CachingStrategy'; | ||
import { BodyType } from '../enums/BodyType'; | ||
import { PureCollection } from 'datx'; | ||
export interface IConfigType { | ||
@@ -16,2 +17,3 @@ baseUrl: string; | ||
parse(data: IResponseObject): IResponseObject; | ||
collection?: PureCollection; | ||
} |
@@ -7,2 +7,3 @@ import { BaseRequest } from './BaseRequest'; | ||
import { ParamArrayType } from './enums/ParamArrayType'; | ||
import { PureCollection } from 'datx'; | ||
export declare function setUrl(url: string): (pipeline: BaseRequest) => void; | ||
@@ -22,1 +23,2 @@ export declare function addInterceptor(interceptor: IInterceptor): (pipeline: BaseRequest) => void; | ||
export declare function parser(parse: (data: object) => object): (pipeline: BaseRequest) => void; | ||
export declare function collection(collection?: PureCollection): (pipeline: BaseRequest) => void; |
{ | ||
"name": "datx-network", | ||
"version": "2.0.0-beta.5", | ||
"version": "2.0.0-beta.6", | ||
"description": "DatX network layer", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67243
1287