New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@datx/jsonapi

Package Overview
Dependencies
Maintainers
7
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@datx/jsonapi - npm Package Compare versions

Comparing version
2.4.11
to
2.4.12
+2
-2
helpers/model.d.ts

@@ -1,2 +0,2 @@

import { PureModel } from '@datx/core';
import { PureModel, IModelConstructor } from '@datx/core';
import { IRawModel } from '@datx/utils';

@@ -8,3 +8,3 @@ import { IJsonapiModel } from '../interfaces/IJsonapiModel';

export declare function flattenModel(classRefs: any): null;
export declare function flattenModel(classRefs: any, data?: IRecord): IRawModel;
export declare function flattenModel(classRefs: any, data?: IRecord, modelClass?: IModelConstructor): IRawModel;
export declare function getModelMeta(model: PureModel): Record<string, any>;

@@ -11,0 +11,0 @@ export declare function getModelLinks(model: PureModel): Record<string, ILink>;

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@datx/core"),t=require("@datx/utils"),n=require("@datx/network"),r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},r(e,t)};function o(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},i.apply(this,arguments)};function a(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function s(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}function s(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}var u=/^((https?\:)?\/\/)/,l="undefined"!=typeof window;function d(e){return new Error("[datx exception] ".concat(e))}function p(e){var n=t.getMeta(e,"fields",{},!0,!0),r={};return Object.keys(n).forEach((function(e){n[e].referenceDef&&(r[e]=n[e].referenceDef)})),r}function f(e,t){return void 0===t&&(t=50),a(this,void 0,void 0,(function(){var n,r,o,i;return s(this,(function(a){switch(a.label){case 0:r=[],o=e,i=1,(n=[]).push.apply(n,e.data),r.push(e),a.label=1;case 1:return e.next?++i>t?[3,3]:[4,e.next()]:[3,3];case 2:return e=a.sent(),r.push(e),n.push.apply(n,e.data),[3,1];case 3:return o=r[r.length-1],[2,{data:n,responses:r,lastResponse:o}]}}))}))}var h={baseUrl:"/",cache:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:l&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:n.ParamArrayType.CommaSeparated,baseFetch:function(e,t,n,r,o){var i,a,s,c=this,u=Promise.resolve(),l=e.toUpperCase(),d="GET"!==l&&"HEAD"!==l;return u.then((function(){var o=h.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},h.defaultFetchOptions,{body:d&&JSON.stringify(n)||void 0,headers:i,method:e});if(c.fetchReference)return c.fetchReference(t,a);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(e){return a=e.status,s=e.headers,e.json()})).catch((function(e){if(204===a)return null;throw e})).then((function(e){if(i=e,a>=400)throw{message:"Invalid HTTP status: ".concat(a),status:a};return{data:i,headers:s,requestHeaders:r,status:a}})).catch((function(e){return c.onError({data:i,error:e,headers:s,requestHeaders:r,status:a})}))},onError:function(e){return e},transformRequest:function(e){return e},transformResponse:function(e){return e},usePatchWhenPossible:!0};function y(e,t,n){var r,o,i,a;return new((null===(o=null===(r=t.options)||void 0===r?void 0:r.fetchOptions)||void 0===o?void 0:o.Response)||B)({error:new Error(e),requestHeaders:null===(a=null===(i=t.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers},n,t.options)}function v(n,r,o,i){var a,s;void 0===o&&(o=!1);var c=(null==r?void 0:r.Response)||B;return h.baseFetch(n.method,n.url,n.data,null===(s=null===(a=null==n?void 0:n.options)||void 0===a?void 0:a.networkConfig)||void 0===s?void 0:s.headers,r).then((function(e){var t=Object.assign({},e,{collection:n.collection}),r=h.transformResponse(t);return i?(i.update(r,n.views),i):new c(r,n.collection,n.options,void 0,n.views)})).then((function(r){return o&&function(n,r){if((null==r?void 0:r.isSuccess)&&-1!==r.status&&(r.data||null===r.data)){var o=t.mapItems(r.data||[],e.getModelType);(J=J.filter((function(e){return e.url!==n}))).unshift({response:r.snapshot,collection:r.collection,time:Date.now(),types:[].concat(o),url:n})}}(n.url,r),r}))}function g(e){var t,r,o,i,a,s,c,u,l,d,p,f,g,m,_,b,w,O=(null===(r=null===(t=e.options)||void 0===t?void 0:t.fetchOptions)||void 0===r?void 0:r.Response)||B,j=h.transformRequest(e),k=null===(o=null==j?void 0:j.collection)||void 0===o?void 0:o.constructor,M=k&&k.cache,P="GET"===j.method.toUpperCase(),C=(null===(a=null===(i=e.options)||void 0===i?void 0:i.cacheOptions)||void 0===a?void 0:a.skipCache)||!P?n.CachingStrategy.NetworkOnly:(null===(c=null===(s=e.options)||void 0===s?void 0:s.cacheOptions)||void 0===c?void 0:c.cachingStrategy)||M||h.cache,x=h.maxCacheAge||1/0;if(k&&void 0!==k.maxCacheAge&&(x=k.maxCacheAge),void 0!==(null===(l=null===(u=e.options)||void 0===u?void 0:u.cacheOptions)||void 0===l?void 0:l.maxAge)&&(x=null===(p=null===(d=e.options)||void 0===d?void 0:d.cacheOptions)||void 0===p?void 0:p.maxAge),C===n.CachingStrategy.NetworkOnly)return v(j,null===(f=e.options)||void 0===f?void 0:f.fetchOptions);var T=function(e,t,n){void 0===n&&(n=B);var r=Date.now()-1e3*t,o=J.find((function(t){return t.url===e&&t.time>r}));if(o){var i=o.response;return{response:new n(i.response,o.collection,i.options),time:o.time,types:o.types,url:o.url}}}(j.url,x,O);if(C===n.CachingStrategy.NetworkFirst)return v(j,null===(g=e.options)||void 0===g?void 0:g.fetchOptions,!0).catch((function(e){if(T)return T.response;throw e}));if(C===n.CachingStrategy.StaleWhileRevalidate){var A=v(j,null===(m=e.options)||void 0===m?void 0:m.fetchOptions,!0);return T?(A.catch((function(){})),Promise.resolve(T.response)):A}if(C===n.CachingStrategy.CacheOnly)return T?Promise.resolve(T.response):Promise.reject(y("No cache for this request",e,null==j?void 0:j.collection));if(C===n.CachingStrategy.CacheFirst)return T?Promise.resolve(T.response):v(j,null===(_=e.options)||void 0===_?void 0:_.fetchOptions,!0);if(C===n.CachingStrategy.StaleAndUpdate){var R=null===(b=null==T?void 0:T.response)||void 0===b?void 0:b.clone();return A=v(j,null===(w=e.options)||void 0===w?void 0:w.fetchOptions,!0,R),R?(A.catch((function(){})),Promise.resolve(R)):A}return Promise.reject(y("Invalid caching strategy",e,null==j?void 0:j.collection))}function m(e,t,n,r){return g({collection:t,data:void 0,method:"GET",options:n,url:e,views:r})}function _(e,t,n,r,o){return g({collection:n,data:t,method:"POST",options:r,url:e,views:o})}function b(e,t,n,r,o){return g({collection:n,data:t,method:"PATCH",options:r,url:e,views:o})}function w(e,t,n,r,o){return g({collection:n,data:t,method:"PUT",options:r,url:e,views:o})}function O(e,t,n,r,o){if(void 0===o&&(o=B),e){var i="object"==typeof e?e.href:e;if(i)return m(i,t,n,r)}return Promise.resolve(new o({data:void 0},t))}function j(n,r){return t.mobx.action((function(o){if(o.error)throw o.error;if(204===o.status)return t.setMeta(n,"networkPersisted",!0),n;if(202===o.status){var i=o.data;return t.setMeta(i,"jsonapiProp",r),t.setMeta(i,"jsonapiQueue",!0),t.setMeta(i,"jsonapiRelated",n),i}t.setMeta(n,"networkPersisted",!0);var a=o.replaceData(n).data;return e.commitModel(a),a}))}function k(e,t){void 0===t&&(t="");var r=[];return Object.keys(e).forEach((function(o){e[o]instanceof Array?h.paramArrayType===n.ParamArrayType.CommaSeparated?r.push({key:"".concat(t).concat(o),value:e[o].join(",")}):h.paramArrayType===n.ParamArrayType.MultipleParams?r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o),value:e}}))):h.paramArrayType===n.ParamArrayType.ParamArray&&r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o,"]["),value:e}}))):"object"==typeof e[o]?r.push.apply(r,k(e[o],"".concat(o,"."))):r.push({key:"".concat(t).concat(o),value:e[o]})})),r}function M(e){return k(e).map((function(e){return"filter[".concat(e.key,"]=").concat(e.value)}))}function P(e){return encodeURIComponent(e).replace(/%3D/,"=")}function C(e,t,n,r){var o,i,a,s,l,d,p,f,y,v=n&&n.networkConfig&&n.networkConfig.headers||{},g=[].concat(M((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(y=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(y)]:[],(f=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?h.sortParams&&Array.isArray(f)?["include=".concat(c([],f,!0).sort())]:["include=".concat(f)]:[],(d=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},p=[],Object.keys(d).forEach((function(e){p.push("fields[".concat(e,"]=").concat(d[e]))})),p),function(e){return e.map((function(e){return"string"==typeof e?e:"".concat(e.key,"=").concat(e.value)}))}((null===(l=null==n?void 0:n.queryParams)||void 0===l?void 0:l.custom)||[]));h.encodeQueryString&&(g=g.map(P));var m=function(e,t){var n=e;if(t.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+t.join("&")}return n}(function(e,t){return u.test(e)||t?e:"".concat(h.baseUrl).concat(e)}(e,r),g);return h.sortParams&&(m=function(e){var t=e.split("?"),n=t[0],r=t[1];if(!r)return e;var o=new URLSearchParams(r);o.sort();var i=h.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(m)),{data:t,headers:v,url:m}}function x(t,n,r,o,i,a){var s=a&&a.constructor;!s&&i&&(s=i.constructor.types.filter((function(e){return e.type===t}))[0]);var c,u=!1;return s?s.endpoint&&"function"==typeof s.endpoint?(u=!0,c=s.endpoint(h.baseUrl)):c=s.endpoint||s.baseUrl||e.getModelType(s):c=t.toString(),C(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function T(n,r){var o,i;if(!r)return null;var a=((o={})[t.META_FIELD]=((i={fields:Object.keys(r.attributes||{}).reduce((function(e,t){return e[t]={referenceDef:!1},e}),{}),id:r.id}).jsonapiLinks=r.links,i.jsonapiMeta=r.meta,i.networkPersisted=Boolean(r.id),i.type=r.type,i),o);if(r.relationships){var s={},c={},u={};Object.keys(r.relationships).forEach((function(t){var o,i=r.relationships[t];i&&"data"in i&&(i.data||null===i.data)&&(!(i.data instanceof Array)||i.data.length>0?(a[t]=i.data,n&&t in n||(u[t]={referenceDef:{model:(i.data instanceof Array?i.data[0].type:null===(o=i.data)||void 0===o?void 0:o.type)||H.type,type:i.data instanceof Array?e.ReferenceType.TO_MANY:e.ReferenceType.TO_ONE}})):a[t]=[]),i&&"links"in i&&(s[t]=i.links),i&&"meta"in i&&(c[t]=i.meta)})),Object.assign(a[t.META_FIELD].fields,u),a[t.META_FIELD].jsonapiRefLinks=s,a[t.META_FIELD].jsonapiRefMeta=c}return Object.assign(a,r.attributes)}function A(e){return t.getMeta(e,"jsonapiLinks",{})}function R(e){return t.getMeta(e,"jsonapiRefLinks",{})}function S(t,n,r){if(!e.getModelCollection(t))throw d("The model needs to be in a collection");var o=R(t);if(!o||!(n in o))throw d("The reference ".concat(n," doesn't have any links"));var i=o[n];if(!i||!(r in i))throw d("Link ".concat(r," doesn't exist on the model"));return i[r]}function E(e){return t.getMeta(e,"networkPersisted",!1)}function I(n,r){var o=n.constructor,i=r?e.modelToDirtyJSON(n):e.modelToJSON(n),a=o.useAutogeneratedIds,s={attributes:i,id:E(n)||a?e.getModelId(n).toString():void 0,type:e.getModelType(n)},c=p(n);return Object.keys(c).forEach((function(r){if(!c[r].property){s.relationships=s.relationships||{};var o=e.getRefId(n,r);s.relationships[r]={data:t.mapItems(o,(function(e){return{id:e.id.toString(),type:e.type}}))},s.attributes&&delete s.attributes[r]}})),s.attributes&&(delete s.attributes.id,delete s.attributes[t.META_FIELD]),s}function q(t,n){return x(e.getModelType(t),E(t)?e.getModelId(t):void 0,void 0,n,void 0,t).url}function L(t,n){var r,o=e.getModelCollection(t),i=null!==(r=null==h?void 0:h.usePatchWhenPossible)&&void 0!==r&&r,a=E(t),s=I(t,i&&a);return(a?i?b:w:_)(q(t,n),{data:s},o,n&&n.networkConfig&&n.networkConfig.headers).then(j(t)).then((function(n){return G(e.getModelType(t)),n}))}function D(n,r){var o=e.getModelCollection(n),i=E(n),a=q(n);return i?function(e,t,n,r){return g({collection:t,data:void 0,method:"DELETE",options:n,url:e,views:void 0})}(a,o,r&&r.networkConfig&&r.networkConfig.headers).then((function(e){if(e.error)throw e.error;!function(e,n){t.setMeta(e,"networkPersisted",!1)}(n),o&&o.__removeModel(n)})):(o&&o.__removeModel(n),Promise.resolve())}var F=["jsonapiLinks","jsonapiMeta","networkPersisted","jsonapiRefLinks","jsonapiRefMeta"];function N(e){return function(n){function r(r,o){void 0===r&&(r={});var i,a=r;r&&"type"in r&&("attributes"in r||"relationships"in r)&&(a=T(p(e),r)),i=n.call(this,a,o)||this;var s=(null==a?void 0:a[t.META_FIELD])||{};return F.forEach((function(e){e in s&&t.setMeta(i,e,s[e])})),i}return o(r,n),r.getAutoId=function(){return n.getAutoId.call(this).toString()},r.prototype.save=function(e){return L(this,e)},r.prototype.destroy=function(e){return D(this,e)},r.useAutogeneratedIds=e.useAutogeneratedIds||!1,r}(e)}var H=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t}(N(e.PureModel));function U(e){if(e instanceof Array)return e;var t=[];return e.forEach?(e.forEach((function(e,n){t.push([n,e])})),t):t}function Q(t,n,r){if(n&&t.data){var o=r||n.sync(t.data);return new e.Bucket.ToOneOrMany(o,n,!0)}if(t.data){var i=t.data;if(i.data){if(i.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:r||new H(T(void 0,i.data))}}}return new e.Bucket.ToOneOrMany(null,n,!0)}var B=function(){function n(e,t,n,r,o){var i=this;if(this.__internal={response:{},views:[]},this.__cache={},this.collection=t,this.__updateInternal(e,n,o),this.__data=Q(e,t,r),this.views.forEach((function(e){i.__data.value&&e.add(i.__data.value)})),Object.freeze(this),this.error)throw this}return Object.defineProperty(n.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),n.prototype.__updateInternal=function(e,n,r){var o,i,a,s,c,u=this;n&&(this.__internal.options=n),this.__internal.response=e,this.__internal.meta=(null===(o=e.data)||void 0===o?void 0:o.meta)||{},this.__internal.links=(null===(i=e.data)||void 0===i?void 0:i.links)||{},this.__internal.jsonapi=(null===(a=e.data)||void 0===a?void 0:a.jsonapi)||{},this.__internal.headers=e.headers&&((c=e.headers)instanceof Array?new t.Headers(c):c),this.__internal.requestHeaders=e.requestHeaders,this.__internal.error=(null===(s=e.data)||void 0===s?void 0:s.errors)||e.error,this.__internal.status=e.status,r&&(this.__internal.views=r),this.error||this.status||(this.__internal.error=new Error("Network not available")),this.links&&Object.keys(this.links).forEach((function(e){t.assignComputed(u,e,(function(){return u.__fetchLink(e)}))}))},n.prototype.replaceData=function(t){var n,r=this.data;if(r===t)return this;var o=e.getModelId(r).toString(),i=e.getModelType(r),a=this.views.map((function(e){return e.list.indexOf(r)}));this.collection&&(this.collection.removeOne(i,o),this.collection.add(t));var s=e.modelToJSON(r);return null===(n=null==s?void 0:s.__META__)||void 0===n||delete n.collection,e.updateModel(t,s),e.updateModelId(t,o),this.views.forEach((function(e,n){-1!==a[n]&&(e.list[a[n]]=t)})),new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,t)},n.prototype.clone=function(){return new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(n.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&U(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),n.prototype.update=function(e,t){this.__updateInternal(e,void 0,t);var n=Q(e,this.collection);return this.__data.__readonlyValue=n.value,this},n.prototype.__fetchLink=function(e){var t=this,n=this.constructor;if(!this.__cache[e]){var r=this.links&&e in this.links?this.links[e]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[e]=function(){return O(r,t.collection,o,t.views,n)}}}return this.__cache[e]},n}(),J=[];function G(e){J=J.filter((function(t){return!t.types.includes(e)}))}function z(e){if(e.error)throw e.error;return e}function W(e,n){return t.mapItems(e&&e.included||[],n),t.mapItems(e&&e.data||null,n)}var Y,V=function(e){function n(n,r,o,i){var a=e.call(this,n,r,o,i)||this;return a.links&&Object.keys(a.links).forEach((function(e){t.assignComputed(a,e,(function(){return a.__fetchLink(e)}))})),a}return o(n,e),Object.defineProperty(n.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),n.prototype.__fetchLink=function(e){var t=this;if(!this.__cache[e]){var r=this.links&&e in this.links?this.links[e]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[e]=function(){return O(r,t.collection,o,t.views).then((function(e){return new n(e.__internal.response,e.collection,void 0,e.views)}))}}}return this.__cache[e]},n}(n.Response);function K(e,t){return(e&&t.collection?t.collection.sync(e):e)||{}}function X(n){return i(i({},n),{data:{data:t.mapItems(n.data,(function(t){return t instanceof e.PureModel?I(t,!0):t}))||void 0}})}!function(e){e.Asc="",e.Desc="-"}(Y||(Y={}));var Z=function(e){function t(t){var r,o=e.call(this,t)||this;return(r=o)._config.Response=V,n.header("content-type","application/vnd.api+json")(r),n.parser(K)(r),n.serializer(X)(r),n.paramArrayType(n.ParamArrayType.CommaSeparated)(r),n.encodeQueryString(!1)(r),o}return o(t,e),t}(n.BaseRequest);Object.defineProperty(exports,"BaseRequest",{enumerable:!0,get:function(){return n.BaseRequest}}),Object.defineProperty(exports,"CachingStrategy",{enumerable:!0,get:function(){return n.CachingStrategy}}),Object.defineProperty(exports,"HttpMethod",{enumerable:!0,get:function(){return n.HttpMethod}}),Object.defineProperty(exports,"ParamArrayType",{enumerable:!0,get:function(){return n.ParamArrayType}}),Object.defineProperty(exports,"addInterceptor",{enumerable:!0,get:function(){return n.addInterceptor}}),Object.defineProperty(exports,"body",{enumerable:!0,get:function(){return n.body}}),Object.defineProperty(exports,"cache",{enumerable:!0,get:function(){return n.cache}}),Object.defineProperty(exports,"clearAllCache",{enumerable:!0,get:function(){return n.clearAllCache}}),Object.defineProperty(exports,"clearCacheByType",{enumerable:!0,get:function(){return n.clearCacheByType}}),Object.defineProperty(exports,"collection",{enumerable:!0,get:function(){return n.collection}}),Object.defineProperty(exports,"encodeQueryString",{enumerable:!0,get:function(){return n.encodeQueryString}}),Object.defineProperty(exports,"fetchReference",{enumerable:!0,get:function(){return n.fetchReference}}),Object.defineProperty(exports,"header",{enumerable:!0,get:function(){return n.header}}),Object.defineProperty(exports,"method",{enumerable:!0,get:function(){return n.method}}),Object.defineProperty(exports,"paramArrayType",{enumerable:!0,get:function(){return n.paramArrayType}}),Object.defineProperty(exports,"params",{enumerable:!0,get:function(){return n.params}}),Object.defineProperty(exports,"parser",{enumerable:!0,get:function(){return n.parser}}),Object.defineProperty(exports,"query",{enumerable:!0,get:function(){return n.query}}),Object.defineProperty(exports,"serializer",{enumerable:!0,get:function(){return n.serializer}}),Object.defineProperty(exports,"setUrl",{enumerable:!0,get:function(){return n.setUrl}}),exports.BaseJsonapiRequest=Z,exports.GenericModel=H,exports.NetworkResponse=V,exports.Response=B,exports.buildUrl=C,exports.config=h,exports.fetchLink=O,exports.fetchModelLink=function(n,r,o){var i=e.getModelCollection(n),a=A(n);if(!a||!(r in a))throw d("Link ".concat(r," doesn't exist on the model"));var s=O(a[r],i,o);return t.getMeta(n,"jsonapiQueue")?s.then((function(r){var o=t.getMeta(n,"jsonapiRelated"),i=t.getMeta(n,"jsonapiProp"),a=r.data,s=a&&e.getModelType(a);return a&&s!==e.getModelType(n)&&s===e.getModelType(o)?i?(o[i]=a,r):(t.setMeta(o,"networkPersisted",!0),r.replaceData(o)):r})):s},exports.fetchModelRefLink=function(t,n,r,o){var i=e.getModelCollection(t);return O(S(t,n,r),i,o)},exports.getModelEndpointUrl=q,exports.getModelLinks=A,exports.getModelMeta=function(e){return t.getMeta(e,"jsonapiMeta",{})},exports.getModelRefLinks=R,exports.getModelRefMeta=function(e){return t.getMeta(e,"jsonapiRefMeta",{})},exports.isModelPersisted=E,exports.jsonapi=function(r){if(e.isModel(r))return N(r);if(e.isCollection(r))return function(r){function c(e){void 0===e&&(e=[]);var t,n,o=r.call(this,e)||this;return e instanceof Array||!(null==e?void 0:e.cache)||(t=e.cache,n=o,J.push.apply(J,t.map((function(e){return Object.assign({collection:n},e)})))),o}return o(c,r),c.prototype.sync=function(e){var t=this;if(!e)return null;var n=W(e,(function(e){return t.__addRecord(e)}));return W(e,this.__updateRelationships.bind(this)),n},c.prototype.fetch=function(e,r,o){return t.deprecated("fetch is deprecated, use getOne instead"),this.getOne(e,r,Object.assign({},o,{cacheOptions:Object.assign({},(null==o?void 0:o.cacheOptions)||{},{cachingStrategy:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly})}))},c.prototype.fetchAll=function(e,r){return t.deprecated("fetchAll is deprecated, use getMany instead"),this.getMany(e,Object.assign({},r,{cacheOptions:Object.assign({},(null==r?void 0:r.cacheOptions)||{},{cachingStrategy:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly})}))},c.prototype.getOne=function(t,n,r){var o=e.getModelType(t),i=this.__prepareQuery(o,n,void 0,r),a=r||{};return a.networkConfig=a.networkConfig||{},a.networkConfig.headers=i.headers,m(i.url,this,a).then(z)},c.prototype.getMany=function(t,n){var r=e.getModelType(t),o=this.__prepareQuery(r,void 0,void 0,n),i=n||{};return i.networkConfig=i.networkConfig||{},i.networkConfig.headers=o.headers,m(o.url,this,i).then(z)},c.prototype.getAll=function(e,t,n){return void 0===n&&(n=50),a(this,void 0,void 0,(function(){return s(this,(function(r){switch(r.label){case 0:if(n<1)throw Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(e,t)];case 1:return[2,f(r.sent(),n)]}}))}))},c.prototype.request=function(e,t,n,r){return void 0===t&&(t="GET"),function(e){return g(e)}({url:C(e,n,r).url,options:r,data:n,method:t,collection:this})},c.prototype.removeOne=function(t,n,r){var o,i,a,s=e.getModelType(t);return"object"==typeof n||void 0===n||"boolean"==typeof n?(o=n,i=e.getModelId(t).toString(),a=t):(o=r,a=(i=e.getModelId(n).toString())?this.findOne(s,i):null),a&&o?D(a,"object"==typeof o?o:void 0):(a&&this.__removeModel(a),G(s),Promise.resolve())},c.prototype.removeAll=function(t){r.prototype.removeAll.call(this,t),G(e.getModelType(t))},c.prototype.reset=function(){r.prototype.reset.call(this),J.length=0},c.prototype.__addRecord=function(t){var n=this.constructor,r=t.type,o=t.id,i=void 0===o?null:this.findOne(r,o),a=T(p(n.types.find((function(t){return e.getModelType(t)===r}))||H),t);return i?(e.upsertModel(a,r,this),e.commitModel(i)):i=n.types.filter((function(e){return e.type===r})).length?this.add(a,r):this.add(new H(a,this)),i},c.prototype.__updateRelationships=function(n){var r=this,o=void 0===n.id?null:this.findOne(n.type,n.id);(n.relationships?Object.keys(n.relationships):[]).forEach((function(i){var a=n.relationships[i];if(a&&"data"in a){var s=a.data;if(!(t.isArrayLike(s)&&s.length<1)&&o)if(s){var c=t.mapItems(s,(function(e){return(void 0===e.id?null:r.findOne(e.type,e.id))||e}))||null,u=t.isArrayLike(s)?s[0].type:s.type;i in o?o[i]=c:e.initModelRef(o,i,{model:u,type:e.ReferenceType.TO_ONE_OR_MANY},c)}else{var l=t.getMeta(o,"refs");l&&i in l&&"__proto__"!==i&&(o[i]=l[i].type===e.ReferenceType.TO_MANY?[]:null)}}}))},c.prototype.__prepareQuery=function(e,t,n,r){return x(e,t,n,r,this)},c.prototype.toJSON=function(){return Object.assign({},r.prototype.toJSON.call(this),{cache:(e=this,J.filter((function(t){return t.collection===e})).map((function(e){return Object.assign({},e,{collection:void 0})})))});var e},c.types=i.types&&i.types.length?i.types.concat(H):[H],c.maxCacheAge=i.maxCacheAge,c.cache=i.cache,c.defaultModel=i.defaultModel||H,c}(i=r);var i;if(e.isView(r))return function(e){function t(t,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=e.call(this,t,n,r,o,i)||this;return a.__collection=n,a}return o(t,e),t.prototype.sync=function(e){var t=this.__collection.sync(e);return t&&this.add(t),t},t.prototype.getOne=function(e,t){return this.__collection.getOne(this.modelType,e,t).then(this.__addFromResponse.bind(this))},t.prototype.getMany=function(e){return this.__collection.getMany(this.modelType,e).then(this.__addFromResponse.bind(this))},t.prototype.getAll=function(e,t){return void 0===t&&(t=50),a(this,void 0,void 0,(function(){return s(this,(function(n){switch(n.label){case 0:if(t<1)throw new Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(e)];case 1:return[2,f(n.sent(),t)]}}))}))},t.prototype.__addFromResponse=function(e){return e.data&&this.add(e.data),e.views.push(this),e},t}(r);throw new Error("The instance needs to be a model, collection or a view")},exports.modelToJsonApi=I,exports.prepareQuery=x,exports.saveModel=L,exports.saveRelationship=function(n,r,o){var i,a=e.getModelCollection(n),s=S(n,r,"self"),c="object"==typeof s?s.href:s,u=e.getRefId(n,r),l=null===(i=t.getMeta(n,"fields"))||void 0===i?void 0:i[r],d=(null==l?void 0:l.referenceDef)?l.referenceDef.model:null;return b(c,{data:t.mapItems(u,(function(e){return{id:e.id,type:e.type||d}}))},a,o&&o.networkConfig&&o.networkConfig.headers).then(j(n,r))};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@datx/core"),t=require("@datx/utils"),n=require("@datx/network"),r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},r(e,t)};function o(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},i.apply(this,arguments)};function a(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function s(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}function s(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}function c(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}var u=/^((https?\:)?\/\/)/,l="undefined"!=typeof window;function d(e){return new Error("[datx exception] ".concat(e))}function p(e){var n=t.getMeta(e,"fields",{},!0,!0),r={};return Object.keys(n).forEach((function(e){n[e].referenceDef&&(r[e]=n[e].referenceDef)})),r}function f(e,t){return void 0===t&&(t=50),a(this,void 0,void 0,(function(){var n,r,o,i;return s(this,(function(a){switch(a.label){case 0:r=[],o=e,i=1,(n=[]).push.apply(n,e.data),r.push(e),a.label=1;case 1:return e.next?++i>t?[3,3]:[4,e.next()]:[3,3];case 2:return e=a.sent(),r.push(e),n.push.apply(n,e.data),[3,1];case 3:return o=r[r.length-1],[2,{data:n,responses:r,lastResponse:o}]}}))}))}var h={baseUrl:"/",cache:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:l&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:n.ParamArrayType.CommaSeparated,baseFetch:function(e,t,n,r,o){var i,a,s,c=this,u=Promise.resolve(),l=e.toUpperCase(),d="GET"!==l&&"HEAD"!==l;return u.then((function(){var o=h.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},h.defaultFetchOptions,{body:d&&JSON.stringify(n)||void 0,headers:i,method:e});if(c.fetchReference)return c.fetchReference(t,a);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(e){return a=e.status,s=e.headers,e.json()})).catch((function(e){if(204===a)return null;throw e})).then((function(e){if(i=e,a>=400)throw{message:"Invalid HTTP status: ".concat(a),status:a};return{data:i,headers:s,requestHeaders:r,status:a}})).catch((function(e){return c.onError({data:i,error:e,headers:s,requestHeaders:r,status:a})}))},onError:function(e){return e},transformRequest:function(e){return e},transformResponse:function(e){return e},usePatchWhenPossible:!0};function y(e,t,n){var r,o,i,a;return new((null===(o=null===(r=t.options)||void 0===r?void 0:r.fetchOptions)||void 0===o?void 0:o.Response)||B)({error:new Error(e),requestHeaders:null===(a=null===(i=t.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers},n,t.options)}function v(n,r,o,i){var a,s;void 0===o&&(o=!1);var c=(null==r?void 0:r.Response)||B;return h.baseFetch(n.method,n.url,n.data,null===(s=null===(a=null==n?void 0:n.options)||void 0===a?void 0:a.networkConfig)||void 0===s?void 0:s.headers,r).then((function(e){var t=Object.assign({},e,{collection:n.collection}),r=h.transformResponse(t);return i?(i.update(r,n.views),i):new c(r,n.collection,n.options,void 0,n.views)})).then((function(r){return o&&function(n,r){if((null==r?void 0:r.isSuccess)&&-1!==r.status&&(r.data||null===r.data)){var o=t.mapItems(r.data||[],e.getModelType);(J=J.filter((function(e){return e.url!==n}))).unshift({response:r.snapshot,collection:r.collection,time:Date.now(),types:[].concat(o),url:n})}}(n.url,r),r}))}function g(e){var t,r,o,i,a,s,c,u,l,d,p,f,g,m,_,b,O,w=(null===(r=null===(t=e.options)||void 0===t?void 0:t.fetchOptions)||void 0===r?void 0:r.Response)||B,j=h.transformRequest(e),k=null===(o=null==j?void 0:j.collection)||void 0===o?void 0:o.constructor,M=k&&k.cache,P="GET"===j.method.toUpperCase(),C=(null===(a=null===(i=e.options)||void 0===i?void 0:i.cacheOptions)||void 0===a?void 0:a.skipCache)||!P?n.CachingStrategy.NetworkOnly:(null===(c=null===(s=e.options)||void 0===s?void 0:s.cacheOptions)||void 0===c?void 0:c.cachingStrategy)||M||h.cache,x=h.maxCacheAge||1/0;if(k&&void 0!==k.maxCacheAge&&(x=k.maxCacheAge),void 0!==(null===(l=null===(u=e.options)||void 0===u?void 0:u.cacheOptions)||void 0===l?void 0:l.maxAge)&&(x=null===(p=null===(d=e.options)||void 0===d?void 0:d.cacheOptions)||void 0===p?void 0:p.maxAge),C===n.CachingStrategy.NetworkOnly)return v(j,null===(f=e.options)||void 0===f?void 0:f.fetchOptions);var T=function(e,t,n){void 0===n&&(n=B);var r=Date.now()-1e3*t,o=J.find((function(t){return t.url===e&&t.time>r}));if(o){var i=o.response;return{response:new n(i.response,o.collection,i.options),time:o.time,types:o.types,url:o.url}}}(j.url,x,w);if(C===n.CachingStrategy.NetworkFirst)return v(j,null===(g=e.options)||void 0===g?void 0:g.fetchOptions,!0).catch((function(e){if(T)return T.response;throw e}));if(C===n.CachingStrategy.StaleWhileRevalidate){var A=v(j,null===(m=e.options)||void 0===m?void 0:m.fetchOptions,!0);return T?(A.catch((function(){})),Promise.resolve(T.response)):A}if(C===n.CachingStrategy.CacheOnly)return T?Promise.resolve(T.response):Promise.reject(y("No cache for this request",e,null==j?void 0:j.collection));if(C===n.CachingStrategy.CacheFirst)return T?Promise.resolve(T.response):v(j,null===(_=e.options)||void 0===_?void 0:_.fetchOptions,!0);if(C===n.CachingStrategy.StaleAndUpdate){var R=null===(b=null==T?void 0:T.response)||void 0===b?void 0:b.clone();return A=v(j,null===(O=e.options)||void 0===O?void 0:O.fetchOptions,!0,R),R?(A.catch((function(){})),Promise.resolve(R)):A}return Promise.reject(y("Invalid caching strategy",e,null==j?void 0:j.collection))}function m(e,t,n,r){return g({collection:t,data:void 0,method:"GET",options:n,url:e,views:r})}function _(e,t,n,r,o){return g({collection:n,data:t,method:"POST",options:r,url:e,views:o})}function b(e,t,n,r,o){return g({collection:n,data:t,method:"PATCH",options:r,url:e,views:o})}function O(e,t,n,r,o){return g({collection:n,data:t,method:"PUT",options:r,url:e,views:o})}function w(e,t,n,r,o){if(void 0===o&&(o=B),e){var i="object"==typeof e?e.href:e;if(i)return m(i,t,n,r)}return Promise.resolve(new o({data:void 0},t))}function j(n,r){return t.mobx.action((function(o){if(o.error)throw o.error;if(204===o.status)return t.setMeta(n,"networkPersisted",!0),n;if(202===o.status){var i=o.data;return t.setMeta(i,"jsonapiProp",r),t.setMeta(i,"jsonapiQueue",!0),t.setMeta(i,"jsonapiRelated",n),i}t.setMeta(n,"networkPersisted",!0);var a=o.replaceData(n).data;return e.commitModel(a),a}))}function k(e,t){void 0===t&&(t="");var r=[];return Object.keys(e).forEach((function(o){e[o]instanceof Array?h.paramArrayType===n.ParamArrayType.CommaSeparated?r.push({key:"".concat(t).concat(o),value:e[o].join(",")}):h.paramArrayType===n.ParamArrayType.MultipleParams?r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o),value:e}}))):h.paramArrayType===n.ParamArrayType.ParamArray&&r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o,"]["),value:e}}))):"object"==typeof e[o]?r.push.apply(r,k(e[o],"".concat(o,"."))):r.push({key:"".concat(t).concat(o),value:e[o]})})),r}function M(e){return k(e).map((function(e){return"filter[".concat(e.key,"]=").concat(e.value)}))}function P(e){return encodeURIComponent(e).replace(/%3D/,"=")}function C(e,t,n,r){var o,i,a,s,l,d,p,f,y,v=n&&n.networkConfig&&n.networkConfig.headers||{},g=[].concat(M((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(y=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(y)]:[],(f=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?h.sortParams&&Array.isArray(f)?["include=".concat(c([],f,!0).sort())]:["include=".concat(f)]:[],(d=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},p=[],Object.keys(d).forEach((function(e){p.push("fields[".concat(e,"]=").concat(d[e]))})),p),function(e){return e.map((function(e){return"string"==typeof e?e:"".concat(e.key,"=").concat(e.value)}))}((null===(l=null==n?void 0:n.queryParams)||void 0===l?void 0:l.custom)||[]));h.encodeQueryString&&(g=g.map(P));var m=function(e,t){var n=e;if(t.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+t.join("&")}return n}(function(e,t){return u.test(e)||t?e:"".concat(h.baseUrl).concat(e)}(e,r),g);return h.sortParams&&(m=function(e){var t=e.split("?"),n=t[0],r=t[1];if(!r)return e;var o=new URLSearchParams(r);o.sort();var i=h.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(m)),{data:t,headers:v,url:m}}function x(t,n,r,o,i,a){var s=a&&a.constructor;!s&&i&&(s=i.constructor.types.filter((function(e){return e.type===t}))[0]);var c,u=!1;return s?s.endpoint&&"function"==typeof s.endpoint?(u=!0,c=s.endpoint(h.baseUrl)):c=s.endpoint||s.baseUrl||e.getModelType(s):c=t.toString(),C(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function T(n,r,o){var i,a;if(void 0===o&&(o=H),!r)return null;var s=t.getMetaObj(o),c=Object.fromEntries(Object.keys(s).filter((function(e){return e.startsWith("map_")})).map((function(e){return[s[e],e.slice(4)]}))),u=((i={})[t.META_FIELD]=(a={fields:Object.keys(r.attributes||{}).reduce((function(e,t){var n;return e[null!==(n=c[t])&&void 0!==n?n:t]={referenceDef:!1},e}),{}),id:r.id},a.jsonapiLinks=r.links,a.jsonapiMeta=r.meta,a.networkPersisted=Boolean(r.id),a.type=r.type,a),i);if(r.relationships){var l={},d={},p={};Object.keys(r.relationships).forEach((function(t){var o,i=r.relationships[t];i&&"data"in i&&(i.data||null===i.data)&&(!(i.data instanceof Array)||i.data.length>0?(u[t]=i.data,n&&t in n||(p[t]={referenceDef:{model:(i.data instanceof Array?i.data[0].type:null===(o=i.data)||void 0===o?void 0:o.type)||H.type,type:i.data instanceof Array?e.ReferenceType.TO_MANY:e.ReferenceType.TO_ONE}})):u[t]=[]),i&&"links"in i&&(l[t]=i.links),i&&"meta"in i&&(d[t]=i.meta)})),Object.assign(u[t.META_FIELD].fields,p),u[t.META_FIELD].jsonapiRefLinks=l,u[t.META_FIELD].jsonapiRefMeta=d}return Object.assign(u,r.attributes)}function A(e){return t.getMeta(e,"jsonapiLinks",{})}function R(e){return t.getMeta(e,"jsonapiRefLinks",{})}function S(t,n,r){if(!e.getModelCollection(t))throw d("The model needs to be in a collection");var o=R(t);if(!o||!(n in o))throw d("The reference ".concat(n," doesn't have any links"));var i=o[n];if(!i||!(r in i))throw d("Link ".concat(r," doesn't exist on the model"));return i[r]}function E(e){return t.getMeta(e,"networkPersisted",!1)}function I(n,r){var o=n.constructor,i=r?e.modelToDirtyJSON(n):e.modelToJSON(n),a=o.useAutogeneratedIds,s={attributes:i,id:E(n)||a?e.getModelId(n).toString():void 0,type:e.getModelType(n)},c=p(n),u=t.getMetaObj(n.constructor);return Object.keys(c).forEach((function(r){var o;if(!c[r].property){s.relationships=s.relationships||{};var i=e.getRefId(n,r),a=null!==(o=null==u?void 0:u["map_".concat(r)])&&void 0!==o?o:r;s.relationships[a]={data:t.mapItems(i,(function(e){return{id:e.id.toString(),type:e.type}}))},s.attributes&&delete s.attributes[r]}})),s.attributes&&(delete s.attributes.id,delete s.attributes[t.META_FIELD]),s}function q(t,n){return x(e.getModelType(t),E(t)?e.getModelId(t):void 0,void 0,n,void 0,t).url}function L(t,n){var r,o=e.getModelCollection(t),i=null!==(r=null==h?void 0:h.usePatchWhenPossible)&&void 0!==r&&r,a=E(t),s=I(t,i&&a);return(a?i?b:O:_)(q(t,n),{data:s},o,n&&n.networkConfig&&n.networkConfig.headers).then(j(t)).then((function(n){return G(e.getModelType(t)),n}))}function D(n,r){var o=e.getModelCollection(n),i=E(n),a=q(n);return i?function(e,t,n,r){return g({collection:t,data:void 0,method:"DELETE",options:n,url:e,views:void 0})}(a,o,r&&r.networkConfig&&r.networkConfig.headers).then((function(e){if(e.error)throw e.error;!function(e,n){t.setMeta(e,"networkPersisted",!1)}(n),o&&o.__removeModel(n)})):(o&&o.__removeModel(n),Promise.resolve())}var F=["jsonapiLinks","jsonapiMeta","networkPersisted","jsonapiRefLinks","jsonapiRefMeta"];function N(e){return function(n){function r(r,o){void 0===r&&(r={});var i,a=r;r&&"type"in r&&("attributes"in r||"relationships"in r)&&(a=T(p(e),r)),i=n.call(this,a,o)||this;var s=(null==a?void 0:a[t.META_FIELD])||{};return F.forEach((function(e){e in s&&t.setMeta(i,e,s[e])})),i}return o(r,n),r.getAutoId=function(){return n.getAutoId.call(this).toString()},r.prototype.save=function(e){return L(this,e)},r.prototype.destroy=function(e){return D(this,e)},r.useAutogeneratedIds=e.useAutogeneratedIds||!1,r}(e)}var H=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t}(N(e.PureModel));function U(e){if(e instanceof Array)return e;var t=[];return e.forEach?(e.forEach((function(e,n){t.push([n,e])})),t):t}function Q(t,n,r){if(n&&t.data){var o=r||n.sync(t.data);return new e.Bucket.ToOneOrMany(o,n,!0)}if(t.data){var i=t.data;if(i.data){if(i.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:r||new H(T(void 0,i.data))}}}return new e.Bucket.ToOneOrMany(null,n,!0)}var B=function(){function n(e,t,n,r,o){var i=this;if(this.__internal={response:{},views:[]},this.__cache={},this.collection=t,this.__updateInternal(e,n,o),this.__data=Q(e,t,r),this.views.forEach((function(e){i.__data.value&&e.add(i.__data.value)})),Object.freeze(this),this.error)throw this}return Object.defineProperty(n.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),n.prototype.__updateInternal=function(e,n,r){var o,i,a,s,c,u=this;n&&(this.__internal.options=n),this.__internal.response=e,this.__internal.meta=(null===(o=e.data)||void 0===o?void 0:o.meta)||{},this.__internal.links=(null===(i=e.data)||void 0===i?void 0:i.links)||{},this.__internal.jsonapi=(null===(a=e.data)||void 0===a?void 0:a.jsonapi)||{},this.__internal.headers=e.headers&&((c=e.headers)instanceof Array?new t.Headers(c):c),this.__internal.requestHeaders=e.requestHeaders,this.__internal.error=(null===(s=e.data)||void 0===s?void 0:s.errors)||e.error,this.__internal.status=e.status,r&&(this.__internal.views=r),this.error||this.status||(this.__internal.error=new Error("Network not available")),this.links&&Object.keys(this.links).forEach((function(e){t.assignComputed(u,e,(function(){return u.__fetchLink(e)}))}))},n.prototype.replaceData=function(t){var n,r=this.data;if(r===t)return this;var o=e.getModelId(r).toString(),i=e.getModelType(r),a=this.views.map((function(e){return e.list.indexOf(r)}));this.collection&&(this.collection.removeOne(i,o),this.collection.add(t));var s=e.modelToJSON(r);return null===(n=null==s?void 0:s.__META__)||void 0===n||delete n.collection,e.updateModel(t,s),e.updateModelId(t,o),this.views.forEach((function(e,n){-1!==a[n]&&(e.list[a[n]]=t)})),new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,t)},n.prototype.clone=function(){return new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(n.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&U(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),n.prototype.update=function(e,t){this.__updateInternal(e,void 0,t);var n=Q(e,this.collection);return this.__data.__readonlyValue=n.value,this},n.prototype.__fetchLink=function(e){var t=this,n=this.constructor;if(!this.__cache[e]){var r=this.links&&e in this.links?this.links[e]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[e]=function(){return w(r,t.collection,o,t.views,n)}}}return this.__cache[e]},n}(),J=[];function G(e){J=J.filter((function(t){return!t.types.includes(e)}))}function z(e){if(e.error)throw e.error;return e}function W(e,n){return t.mapItems(e&&e.included||[],n),t.mapItems(e&&e.data||null,n)}var Y,V=function(e){function n(n,r,o,i){var a=e.call(this,n,r,o,i)||this;return a.links&&Object.keys(a.links).forEach((function(e){t.assignComputed(a,e,(function(){return a.__fetchLink(e)}))})),a}return o(n,e),Object.defineProperty(n.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),n.prototype.__fetchLink=function(e){var t=this;if(!this.__cache[e]){var r=this.links&&e in this.links?this.links[e]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[e]=function(){return w(r,t.collection,o,t.views).then((function(e){return new n(e.__internal.response,e.collection,void 0,e.views)}))}}}return this.__cache[e]},n}(n.Response);function K(e,t){return(e&&t.collection?t.collection.sync(e):e)||{}}function X(n){return i(i({},n),{data:{data:t.mapItems(n.data,(function(t){return t instanceof e.PureModel?I(t,!0):t}))||void 0}})}!function(e){e.Asc="",e.Desc="-"}(Y||(Y={}));var Z=function(e){function t(t){var r,o=e.call(this,t)||this;return(r=o)._config.Response=V,n.header("content-type","application/vnd.api+json")(r),n.parser(K)(r),n.serializer(X)(r),n.paramArrayType(n.ParamArrayType.CommaSeparated)(r),n.encodeQueryString(!1)(r),o}return o(t,e),t}(n.BaseRequest);Object.defineProperty(exports,"BaseRequest",{enumerable:!0,get:function(){return n.BaseRequest}}),Object.defineProperty(exports,"CachingStrategy",{enumerable:!0,get:function(){return n.CachingStrategy}}),Object.defineProperty(exports,"HttpMethod",{enumerable:!0,get:function(){return n.HttpMethod}}),Object.defineProperty(exports,"ParamArrayType",{enumerable:!0,get:function(){return n.ParamArrayType}}),Object.defineProperty(exports,"addInterceptor",{enumerable:!0,get:function(){return n.addInterceptor}}),Object.defineProperty(exports,"body",{enumerable:!0,get:function(){return n.body}}),Object.defineProperty(exports,"cache",{enumerable:!0,get:function(){return n.cache}}),Object.defineProperty(exports,"clearAllCache",{enumerable:!0,get:function(){return n.clearAllCache}}),Object.defineProperty(exports,"clearCacheByType",{enumerable:!0,get:function(){return n.clearCacheByType}}),Object.defineProperty(exports,"collection",{enumerable:!0,get:function(){return n.collection}}),Object.defineProperty(exports,"encodeQueryString",{enumerable:!0,get:function(){return n.encodeQueryString}}),Object.defineProperty(exports,"fetchReference",{enumerable:!0,get:function(){return n.fetchReference}}),Object.defineProperty(exports,"header",{enumerable:!0,get:function(){return n.header}}),Object.defineProperty(exports,"method",{enumerable:!0,get:function(){return n.method}}),Object.defineProperty(exports,"paramArrayType",{enumerable:!0,get:function(){return n.paramArrayType}}),Object.defineProperty(exports,"params",{enumerable:!0,get:function(){return n.params}}),Object.defineProperty(exports,"parser",{enumerable:!0,get:function(){return n.parser}}),Object.defineProperty(exports,"query",{enumerable:!0,get:function(){return n.query}}),Object.defineProperty(exports,"serializer",{enumerable:!0,get:function(){return n.serializer}}),Object.defineProperty(exports,"setUrl",{enumerable:!0,get:function(){return n.setUrl}}),exports.BaseJsonapiRequest=Z,exports.GenericModel=H,exports.NetworkResponse=V,exports.Response=B,exports.buildUrl=C,exports.config=h,exports.fetchLink=w,exports.fetchModelLink=function(n,r,o){var i=e.getModelCollection(n),a=A(n);if(!a||!(r in a))throw d("Link ".concat(r," doesn't exist on the model"));var s=w(a[r],i,o);return t.getMeta(n,"jsonapiQueue")?s.then((function(r){var o=t.getMeta(n,"jsonapiRelated"),i=t.getMeta(n,"jsonapiProp"),a=r.data,s=a&&e.getModelType(a);return a&&s!==e.getModelType(n)&&s===e.getModelType(o)?i?(o[i]=a,r):(t.setMeta(o,"networkPersisted",!0),r.replaceData(o)):r})):s},exports.fetchModelRefLink=function(t,n,r,o){var i=e.getModelCollection(t);return w(S(t,n,r),i,o)},exports.getModelEndpointUrl=q,exports.getModelLinks=A,exports.getModelMeta=function(e){return t.getMeta(e,"jsonapiMeta",{})},exports.getModelRefLinks=R,exports.getModelRefMeta=function(e){return t.getMeta(e,"jsonapiRefMeta",{})},exports.isModelPersisted=E,exports.jsonapi=function(r){if(e.isModel(r))return N(r);if(e.isCollection(r))return function(r){function c(e){void 0===e&&(e=[]);var t,n,o=r.call(this,e)||this;return e instanceof Array||!(null==e?void 0:e.cache)||(t=e.cache,n=o,J.push.apply(J,t.map((function(e){return Object.assign({collection:n},e)})))),o}return o(c,r),c.prototype.sync=function(e){var t=this;if(!e)return null;var n=W(e,(function(e){return t.__addRecord(e)}));return W(e,this.__updateRelationships.bind(this)),n},c.prototype.fetch=function(e,r,o){return t.deprecated("fetch is deprecated, use getOne instead"),this.getOne(e,r,Object.assign({},o,{cacheOptions:Object.assign({},(null==o?void 0:o.cacheOptions)||{},{cachingStrategy:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly})}))},c.prototype.fetchAll=function(e,r){return t.deprecated("fetchAll is deprecated, use getMany instead"),this.getMany(e,Object.assign({},r,{cacheOptions:Object.assign({},(null==r?void 0:r.cacheOptions)||{},{cachingStrategy:l?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly})}))},c.prototype.getOne=function(t,n,r){var o=e.getModelType(t),i=this.__prepareQuery(o,n,void 0,r),a=r||{};return a.networkConfig=a.networkConfig||{},a.networkConfig.headers=i.headers,m(i.url,this,a).then(z)},c.prototype.getMany=function(t,n){var r=e.getModelType(t),o=this.__prepareQuery(r,void 0,void 0,n),i=n||{};return i.networkConfig=i.networkConfig||{},i.networkConfig.headers=o.headers,m(o.url,this,i).then(z)},c.prototype.getAll=function(e,t,n){return void 0===n&&(n=50),a(this,void 0,void 0,(function(){return s(this,(function(r){switch(r.label){case 0:if(n<1)throw Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(e,t)];case 1:return[2,f(r.sent(),n)]}}))}))},c.prototype.request=function(e,t,n,r){return void 0===t&&(t="GET"),function(e){return g(e)}({url:C(e,n,r).url,options:r,data:n,method:t,collection:this})},c.prototype.removeOne=function(t,n,r){var o,i,a,s=e.getModelType(t);return"object"==typeof n||void 0===n||"boolean"==typeof n?(o=n,i=e.getModelId(t).toString(),a=t):(o=r,a=(i=e.getModelId(n).toString())?this.findOne(s,i):null),a&&o?D(a,"object"==typeof o?o:void 0):(a&&this.__removeModel(a),G(s),Promise.resolve())},c.prototype.removeAll=function(t){r.prototype.removeAll.call(this,t),G(e.getModelType(t))},c.prototype.reset=function(){r.prototype.reset.call(this),J.length=0},c.prototype.__addRecord=function(t){var n=this.constructor,r=t.type,o=t.id,i=void 0===o?null:this.findOne(r,o),a=n.types.find((function(t){return e.getModelType(t)===r}))||H,s=T(p(a),t,a);return i?(e.upsertModel(s,r,this),e.commitModel(i)):i=n.types.filter((function(e){return e.type===r})).length?this.add(s,r):this.add(new H(s,this)),i},c.prototype.__updateRelationships=function(n){var r=this,o=void 0===n.id?null:this.findOne(n.type,n.id);(n.relationships?Object.keys(n.relationships):[]).forEach((function(i){var a=n.relationships[i];if(a&&"data"in a){var s=a.data;if(!(t.isArrayLike(s)&&s.length<1)&&o)if(s){var c=t.mapItems(s,(function(e){return(void 0===e.id?null:r.findOne(e.type,e.id))||e}))||null,u=t.isArrayLike(s)?s[0].type:s.type;i in o?o[i]=c:e.initModelRef(o,i,{model:u,type:e.ReferenceType.TO_ONE_OR_MANY},c)}else{var l=t.getMeta(o,"refs");l&&i in l&&"__proto__"!==i&&(o[i]=l[i].type===e.ReferenceType.TO_MANY?[]:null)}}}))},c.prototype.__prepareQuery=function(e,t,n,r){return x(e,t,n,r,this)},c.prototype.toJSON=function(){return Object.assign({},r.prototype.toJSON.call(this),{cache:(e=this,J.filter((function(t){return t.collection===e})).map((function(e){return Object.assign({},e,{collection:void 0})})))});var e},c.types=i.types&&i.types.length?i.types.concat(H):[H],c.maxCacheAge=i.maxCacheAge,c.cache=i.cache,c.defaultModel=i.defaultModel||H,c}(i=r);var i;if(e.isView(r))return function(e){function t(t,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=e.call(this,t,n,r,o,i)||this;return a.__collection=n,a}return o(t,e),t.prototype.sync=function(e){var t=this.__collection.sync(e);return t&&this.add(t),t},t.prototype.getOne=function(e,t){return this.__collection.getOne(this.modelType,e,t).then(this.__addFromResponse.bind(this))},t.prototype.getMany=function(e){return this.__collection.getMany(this.modelType,e).then(this.__addFromResponse.bind(this))},t.prototype.getAll=function(e,t){return void 0===t&&(t=50),a(this,void 0,void 0,(function(){return s(this,(function(n){switch(n.label){case 0:if(t<1)throw new Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(e)];case 1:return[2,f(n.sent(),t)]}}))}))},t.prototype.__addFromResponse=function(e){return e.data&&this.add(e.data),e.views.push(this),e},t}(r);throw new Error("The instance needs to be a model, collection or a view")},exports.modelToJsonApi=I,exports.prepareQuery=x,exports.saveModel=L,exports.saveRelationship=function(n,r,o){var i,a=e.getModelCollection(n),s=S(n,r,"self"),c="object"==typeof s?s.href:s,u=e.getRefId(n,r),l=null===(i=t.getMeta(n,"fields"))||void 0===i?void 0:i[r],d=(null==l?void 0:l.referenceDef)?l.referenceDef.model:null;return b(c,{data:t.mapItems(u,(function(e){return{id:e.id,type:e.type||d}}))},a,o&&o.networkConfig&&o.networkConfig.headers).then(j(n,r))};
//# sourceMappingURL=index.cjs.js.map

@@ -1,2 +0,2 @@

import{commitModel as t,getModelType as e,getModelCollection as n,modelToDirtyJSON as r,modelToJSON as o,getModelId as i,getRefId as a,ReferenceType as s,PureModel as c,updateModel as u,updateModelId as l,Bucket as f,upsertModel as d,initModelRef as p,isModel as h,isCollection as v,isView as y}from"@datx/core";import{getMeta as _,mobx as g,setMeta as m,mapItems as b,META_FIELD as w,assignComputed as O,Headers as k,deprecated as j,isArrayLike as P}from"@datx/utils";import{CachingStrategy as A,ParamArrayType as C,Response as E,header as R,parser as S,serializer as T,paramArrayType as x,encodeQueryString as M,BaseRequest as q}from"@datx/network";export{BaseRequest,CachingStrategy,HttpMethod,ParamArrayType,addInterceptor,body,cache,clearAllCache,clearCacheByType,collection,encodeQueryString,fetchReference,header,method,paramArrayType,params,parser,query,serializer,setUrl}from"@datx/network";var N=function(t,e){return N=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},N(t,e)};function D(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}N(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var F=function(){return F=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},F.apply(this,arguments)};function L(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{c(r.next(t))}catch(t){i(t)}}function s(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}c((r=r.apply(t,e||[])).next())}))}function H(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}function I(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}var U=/^((https?\:)?\/\/)/,Q="undefined"!=typeof window;function G(t){return new Error("[datx exception] ".concat(t))}function B(t){var e=_(t,"fields",{},!0,!0),n={};return Object.keys(e).forEach((function(t){e[t].referenceDef&&(n[t]=e[t].referenceDef)})),n}function J(t,e){return void 0===e&&(e=50),L(this,void 0,void 0,(function(){var n,r,o,i;return H(this,(function(a){switch(a.label){case 0:r=[],o=t,i=1,(n=[]).push.apply(n,t.data),r.push(t),a.label=1;case 1:return t.next?++i>e?[3,3]:[4,t.next()]:[3,3];case 2:return t=a.sent(),r.push(t),n.push.apply(n,t.data),[3,1];case 3:return o=r[r.length-1],[2,{data:n,responses:r,lastResponse:o}]}}))}))}var W={baseUrl:"/",cache:Q?A.CacheFirst:A.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:Q&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:C.CommaSeparated,baseFetch:function(t,e,n,r,o){var i,a,s,c=this,u=Promise.resolve(),l=t.toUpperCase(),f="GET"!==l&&"HEAD"!==l;return u.then((function(){var o=W.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},W.defaultFetchOptions,{body:f&&JSON.stringify(n)||void 0,headers:i,method:t});if(c.fetchReference)return c.fetchReference(e,a);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(t){return a=t.status,s=t.headers,t.json()})).catch((function(t){if(204===a)return null;throw t})).then((function(t){if(i=t,a>=400)throw{message:"Invalid HTTP status: ".concat(a),status:a};return{data:i,headers:s,requestHeaders:r,status:a}})).catch((function(t){return c.onError({data:i,error:t,headers:s,requestHeaders:r,status:a})}))},onError:function(t){return t},transformRequest:function(t){return t},transformResponse:function(t){return t},usePatchWhenPossible:!0};function Y(t,e,n){var r,o,i,a;return new((null===(o=null===(r=e.options)||void 0===r?void 0:r.fetchOptions)||void 0===o?void 0:o.Response)||At)({error:new Error(t),requestHeaders:null===(a=null===(i=e.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers},n,e.options)}function z(t,n,r,o){var i,a;void 0===r&&(r=!1);var s=(null==n?void 0:n.Response)||At;return W.baseFetch(t.method,t.url,t.data,null===(a=null===(i=null==t?void 0:t.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers,n).then((function(e){var n=Object.assign({},e,{collection:t.collection}),r=W.transformResponse(n);return o?(o.update(r,t.views),o):new s(r,t.collection,t.options,void 0,t.views)})).then((function(n){return r&&function(t,n){if((null==n?void 0:n.isSuccess)&&-1!==n.status&&(n.data||null===n.data)){var r=b(n.data||[],e);(Ct=Ct.filter((function(e){return e.url!==t}))).unshift({response:n.snapshot,collection:n.collection,time:Date.now(),types:[].concat(r),url:t})}}(t.url,n),n}))}function V(t){var e,n,r,o,i,a,s,c,u,l,f,d,p,h,v,y,_,g=(null===(n=null===(e=t.options)||void 0===e?void 0:e.fetchOptions)||void 0===n?void 0:n.Response)||At,m=W.transformRequest(t),b=null===(r=null==m?void 0:m.collection)||void 0===r?void 0:r.constructor,w=b&&b.cache,O="GET"===m.method.toUpperCase(),k=(null===(i=null===(o=t.options)||void 0===o?void 0:o.cacheOptions)||void 0===i?void 0:i.skipCache)||!O?A.NetworkOnly:(null===(s=null===(a=t.options)||void 0===a?void 0:a.cacheOptions)||void 0===s?void 0:s.cachingStrategy)||w||W.cache,j=W.maxCacheAge||1/0;if(b&&void 0!==b.maxCacheAge&&(j=b.maxCacheAge),void 0!==(null===(u=null===(c=t.options)||void 0===c?void 0:c.cacheOptions)||void 0===u?void 0:u.maxAge)&&(j=null===(f=null===(l=t.options)||void 0===l?void 0:l.cacheOptions)||void 0===f?void 0:f.maxAge),k===A.NetworkOnly)return z(m,null===(d=t.options)||void 0===d?void 0:d.fetchOptions);var P=function(t,e,n){void 0===n&&(n=At);var r=Date.now()-1e3*e,o=Ct.find((function(e){return e.url===t&&e.time>r}));if(o){var i=o.response;return{response:new n(i.response,o.collection,i.options),time:o.time,types:o.types,url:o.url}}}(m.url,j,g);if(k===A.NetworkFirst)return z(m,null===(p=t.options)||void 0===p?void 0:p.fetchOptions,!0).catch((function(t){if(P)return P.response;throw t}));if(k===A.StaleWhileRevalidate){var C=z(m,null===(h=t.options)||void 0===h?void 0:h.fetchOptions,!0);return P?(C.catch((function(){})),Promise.resolve(P.response)):C}if(k===A.CacheOnly)return P?Promise.resolve(P.response):Promise.reject(Y("No cache for this request",t,null==m?void 0:m.collection));if(k===A.CacheFirst)return P?Promise.resolve(P.response):z(m,null===(v=t.options)||void 0===v?void 0:v.fetchOptions,!0);if(k===A.StaleAndUpdate){var E=null===(y=null==P?void 0:P.response)||void 0===y?void 0:y.clone();return C=z(m,null===(_=t.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(C.catch((function(){})),Promise.resolve(E)):C}return Promise.reject(Y("Invalid caching strategy",t,null==m?void 0:m.collection))}function K(t,e,n,r){return V({collection:e,data:void 0,method:"GET",options:n,url:t,views:r})}function X(t,e,n,r,o){return V({collection:n,data:e,method:"POST",options:r,url:t,views:o})}function Z(t,e,n,r,o){return V({collection:n,data:e,method:"PATCH",options:r,url:t,views:o})}function $(t,e,n,r,o){return V({collection:n,data:e,method:"PUT",options:r,url:t,views:o})}function tt(t,e,n,r,o){if(void 0===o&&(o=At),t){var i="object"==typeof t?t.href:t;if(i)return K(i,e,n,r)}return Promise.resolve(new o({data:void 0},e))}function et(e,n){return g.action((function(r){if(r.error)throw r.error;if(204===r.status)return m(e,"networkPersisted",!0),e;if(202===r.status){var o=r.data;return m(o,"jsonapiProp",n),m(o,"jsonapiQueue",!0),m(o,"jsonapiRelated",e),o}m(e,"networkPersisted",!0);var i=r.replaceData(e).data;return t(i),i}))}function nt(t,e){void 0===e&&(e="");var n=[];return Object.keys(t).forEach((function(r){t[r]instanceof Array?W.paramArrayType===C.CommaSeparated?n.push({key:"".concat(e).concat(r),value:t[r].join(",")}):W.paramArrayType===C.MultipleParams?n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r),value:t}}))):W.paramArrayType===C.ParamArray&&n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r,"]["),value:t}}))):"object"==typeof t[r]?n.push.apply(n,nt(t[r],"".concat(r,"."))):n.push({key:"".concat(e).concat(r),value:t[r]})})),n}function rt(t){return nt(t).map((function(t){return"filter[".concat(t.key,"]=").concat(t.value)}))}function ot(t){return encodeURIComponent(t).replace(/%3D/,"=")}function it(t,e,n,r){var o,i,a,s,c,u,l,f,d,p=n&&n.networkConfig&&n.networkConfig.headers||{},h=[].concat(rt((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(d=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(d)]:[],(f=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?W.sortParams&&Array.isArray(f)?["include=".concat(I([],f,!0).sort())]:["include=".concat(f)]:[],(u=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},l=[],Object.keys(u).forEach((function(t){l.push("fields[".concat(t,"]=").concat(u[t]))})),l),function(t){return t.map((function(t){return"string"==typeof t?t:"".concat(t.key,"=").concat(t.value)}))}((null===(c=null==n?void 0:n.queryParams)||void 0===c?void 0:c.custom)||[]));W.encodeQueryString&&(h=h.map(ot));var v=function(t,e){var n=t;if(e.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+e.join("&")}return n}(function(t,e){return U.test(t)||e?t:"".concat(W.baseUrl).concat(t)}(t,r),h);return W.sortParams&&(v=function(t){var e=t.split("?"),n=e[0],r=e[1];if(!r)return t;var o=new URLSearchParams(r);o.sort();var i=W.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:e,headers:p,url:v}}function at(t,n,r,o,i,a){var s=a&&a.constructor;!s&&i&&(s=i.constructor.types.filter((function(e){return e.type===t}))[0]);var c,u=!1;return s?s.endpoint&&"function"==typeof s.endpoint?(u=!0,c=s.endpoint(W.baseUrl)):c=s.endpoint||s.baseUrl||e(s):c=t.toString(),it(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function st(t,e){var n,r;if(!e)return null;var o=((n={})[w]=((r={fields:Object.keys(e.attributes||{}).reduce((function(t,e){return t[e]={referenceDef:!1},t}),{}),id:e.id}).jsonapiLinks=e.links,r.jsonapiMeta=e.meta,r.networkPersisted=Boolean(e.id),r.type=e.type,r),n);if(e.relationships){var i={},a={},c={};Object.keys(e.relationships).forEach((function(n){var r,u=e.relationships[n];u&&"data"in u&&(u.data||null===u.data)&&(!(u.data instanceof Array)||u.data.length>0?(o[n]=u.data,t&&n in t||(c[n]={referenceDef:{model:(u.data instanceof Array?u.data[0].type:null===(r=u.data)||void 0===r?void 0:r.type)||kt.type,type:u.data instanceof Array?s.TO_MANY:s.TO_ONE}})):o[n]=[]),u&&"links"in u&&(i[n]=u.links),u&&"meta"in u&&(a[n]=u.meta)})),Object.assign(o[w].fields,c),o[w].jsonapiRefLinks=i,o[w].jsonapiRefMeta=a}return Object.assign(o,e.attributes)}function ct(t){return _(t,"jsonapiMeta",{})}function ut(t){return _(t,"jsonapiLinks",{})}function lt(t,r,o){var i=n(t),a=ut(t);if(!a||!(r in a))throw G("Link ".concat(r," doesn't exist on the model"));var s=tt(a[r],i,o);return _(t,"jsonapiQueue")?s.then((function(n){var r=_(t,"jsonapiRelated"),o=_(t,"jsonapiProp"),i=n.data,a=i&&e(i);return i&&a!==e(t)&&a===e(r)?o?(r[o]=i,n):(m(r,"networkPersisted",!0),n.replaceData(r)):n})):s}function ft(t){return _(t,"jsonapiRefLinks",{})}function dt(t,e,r){if(!n(t))throw G("The model needs to be in a collection");var o=ft(t);if(!o||!(e in o))throw G("The reference ".concat(e," doesn't have any links"));var i=o[e];if(!i||!(r in i))throw G("Link ".concat(r," doesn't exist on the model"));return i[r]}function pt(t,e,r,o){var i=n(t);return tt(dt(t,e,r),i,o)}function ht(t){return _(t,"jsonapiRefMeta",{})}function vt(t){return _(t,"networkPersisted",!1)}function yt(t,n){var s=t.constructor,c=n?r(t):o(t),u=s.useAutogeneratedIds,l={attributes:c,id:vt(t)||u?i(t).toString():void 0,type:e(t)},f=B(t);return Object.keys(f).forEach((function(e){if(!f[e].property){l.relationships=l.relationships||{};var n=a(t,e);l.relationships[e]={data:b(n,(function(t){return{id:t.id.toString(),type:t.type}}))},l.attributes&&delete l.attributes[e]}})),l.attributes&&(delete l.attributes.id,delete l.attributes[w]),l}function _t(t,n){return at(e(t),vt(t)?i(t):void 0,void 0,n,void 0,t).url}function gt(t,r){var o,i=n(t),a=null!==(o=null==W?void 0:W.usePatchWhenPossible)&&void 0!==o&&o,s=vt(t),c=yt(t,a&&s);return(s?a?Z:$:X)(_t(t,r),{data:c},i,r&&r.networkConfig&&r.networkConfig.headers).then(et(t)).then((function(n){return Et(e(t)),n}))}function mt(t,e){var r=n(t),o=vt(t),i=_t(t);return o?function(t,e,n,r){return V({collection:e,data:void 0,method:"DELETE",options:n,url:t,views:void 0})}(i,r,e&&e.networkConfig&&e.networkConfig.headers).then((function(e){if(e.error)throw e.error;!function(t,e){m(t,"networkPersisted",!1)}(t),r&&r.__removeModel(t)})):(r&&r.__removeModel(t),Promise.resolve())}function bt(t,e,r){var o,i=n(t),s=dt(t,e,"self"),c="object"==typeof s?s.href:s,u=a(t,e),l=null===(o=_(t,"fields"))||void 0===o?void 0:o[e],f=(null==l?void 0:l.referenceDef)?l.referenceDef.model:null;return Z(c,{data:b(u,(function(t){return{id:t.id,type:t.type||f}}))},i,r&&r.networkConfig&&r.networkConfig.headers).then(et(t,e))}var wt=["jsonapiLinks","jsonapiMeta","networkPersisted","jsonapiRefLinks","jsonapiRefMeta"];function Ot(t){return function(e){function n(n,r){void 0===n&&(n={});var o,i=n;n&&"type"in n&&("attributes"in n||"relationships"in n)&&(i=st(B(t),n)),o=e.call(this,i,r)||this;var a=(null==i?void 0:i[w])||{};return wt.forEach((function(t){t in a&&m(o,t,a[t])})),o}return D(n,e),n.getAutoId=function(){return e.getAutoId.call(this).toString()},n.prototype.save=function(t){return gt(this,t)},n.prototype.destroy=function(t){return mt(this,t)},n.useAutogeneratedIds=t.useAutogeneratedIds||!1,n}(t)}var kt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return D(e,t),e}(Ot(c));function jt(t){if(t instanceof Array)return t;var e=[];return t.forEach?(t.forEach((function(t,n){e.push([n,t])})),e):e}function Pt(t,e,n){if(e&&t.data){var r=n||e.sync(t.data);return new f.ToOneOrMany(r,e,!0)}if(t.data){var o=t.data;if(o.data){if(o.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:n||new kt(st(void 0,o.data))}}}return new f.ToOneOrMany(null,e,!0)}var At=function(){function t(t,e,n,r,o){var i=this;if(this.__internal={response:{},views:[]},this.__cache={},this.collection=e,this.__updateInternal(t,n,o),this.__data=Pt(t,e,r),this.views.forEach((function(t){i.__data.value&&t.add(i.__data.value)})),Object.freeze(this),this.error)throw this}return Object.defineProperty(t.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),t.prototype.__updateInternal=function(t,e,n){var r,o,i,a,s,c=this;e&&(this.__internal.options=e),this.__internal.response=t,this.__internal.meta=(null===(r=t.data)||void 0===r?void 0:r.meta)||{},this.__internal.links=(null===(o=t.data)||void 0===o?void 0:o.links)||{},this.__internal.jsonapi=(null===(i=t.data)||void 0===i?void 0:i.jsonapi)||{},this.__internal.headers=t.headers&&((s=t.headers)instanceof Array?new k(s):s),this.__internal.requestHeaders=t.requestHeaders,this.__internal.error=(null===(a=t.data)||void 0===a?void 0:a.errors)||t.error,this.__internal.status=t.status,n&&(this.__internal.views=n),this.error||this.status||(this.__internal.error=new Error("Network not available")),this.links&&Object.keys(this.links).forEach((function(t){O(c,t,(function(){return c.__fetchLink(t)}))}))},t.prototype.replaceData=function(t){var n,r=this.data;if(r===t)return this;var a=i(r).toString(),s=e(r),c=this.views.map((function(t){return t.list.indexOf(r)}));this.collection&&(this.collection.removeOne(s,a),this.collection.add(t));var f=o(r);return null===(n=null==f?void 0:f.__META__)||void 0===n||delete n.collection,u(t,f),l(t,a),this.views.forEach((function(e,n){-1!==c[n]&&(e.list[c[n]]=t)})),new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,t)},t.prototype.clone=function(){return new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(t.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&jt(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),t.prototype.update=function(t,e){this.__updateInternal(t,void 0,e);var n=Pt(t,this.collection);return this.__data.__readonlyValue=n.value,this},t.prototype.__fetchLink=function(t){var e=this,n=this.constructor;if(!this.__cache[t]){var r=this.links&&t in this.links?this.links[t]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[t]=function(){return tt(r,e.collection,o,e.views,n)}}}return this.__cache[t]},t}(),Ct=[];function Et(t){Ct=Ct.filter((function(e){return!e.types.includes(t)}))}function Rt(t){return Ct.filter((function(e){return e.collection===t})).map((function(t){return Object.assign({},t,{collection:void 0})}))}function St(t){if(t.error)throw t.error;return t}function Tt(t,e){return b(t&&t.included||[],e),b(t&&t.data||null,e)}function xt(n){if(h(n))return Ot(n);if(v(n))return function(n){function o(t){void 0===t&&(t=[]);var e=n.call(this,t)||this;return t instanceof Array||!(null==t?void 0:t.cache)||function(t,e){Ct.push.apply(Ct,t.map((function(t){return Object.assign({collection:e},t)})))}(t.cache,e),e}return D(o,n),o.prototype.sync=function(t){var e=this;if(!t)return null;var n=Tt(t,(function(t){return e.__addRecord(t)}));return Tt(t,this.__updateRelationships.bind(this)),n},o.prototype.fetch=function(t,e,n){return j("fetch is deprecated, use getOne instead"),this.getOne(t,e,Object.assign({},n,{cacheOptions:Object.assign({},(null==n?void 0:n.cacheOptions)||{},{cachingStrategy:Q?A.CacheFirst:A.NetworkOnly})}))},o.prototype.fetchAll=function(t,e){return j("fetchAll is deprecated, use getMany instead"),this.getMany(t,Object.assign({},e,{cacheOptions:Object.assign({},(null==e?void 0:e.cacheOptions)||{},{cachingStrategy:Q?A.CacheFirst:A.NetworkOnly})}))},o.prototype.getOne=function(t,n,r){var o=e(t),i=this.__prepareQuery(o,n,void 0,r),a=r||{};return a.networkConfig=a.networkConfig||{},a.networkConfig.headers=i.headers,K(i.url,this,a).then(St)},o.prototype.getMany=function(t,n){var r=e(t),o=this.__prepareQuery(r,void 0,void 0,n),i=n||{};return i.networkConfig=i.networkConfig||{},i.networkConfig.headers=o.headers,K(o.url,this,i).then(St)},o.prototype.getAll=function(t,e,n){return void 0===n&&(n=50),L(this,void 0,void 0,(function(){return H(this,(function(r){switch(r.label){case 0:if(n<1)throw Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(t,e)];case 1:return[2,J(r.sent(),n)]}}))}))},o.prototype.request=function(t,e,n,r){return void 0===e&&(e="GET"),function(t){return V(t)}({url:it(t,n,r).url,options:r,data:n,method:e,collection:this})},o.prototype.removeOne=function(t,n,r){var o,a,s,c=e(t);return"object"==typeof n||void 0===n||"boolean"==typeof n?(o=n,a=i(t).toString(),s=t):(o=r,s=(a=i(n).toString())?this.findOne(c,a):null),s&&o?mt(s,"object"==typeof o?o:void 0):(s&&this.__removeModel(s),Et(c),Promise.resolve())},o.prototype.removeAll=function(t){n.prototype.removeAll.call(this,t),Et(e(t))},o.prototype.reset=function(){n.prototype.reset.call(this),Ct.length=0},o.prototype.__addRecord=function(n){var r=this.constructor,o=n.type,i=n.id,a=void 0===i?null:this.findOne(o,i),s=st(B(r.types.find((function(t){return e(t)===o}))||kt),n);return a?(d(s,o,this),t(a)):a=r.types.filter((function(t){return t.type===o})).length?this.add(s,o):this.add(new kt(s,this)),a},o.prototype.__updateRelationships=function(t){var e=this,n=void 0===t.id?null:this.findOne(t.type,t.id);(t.relationships?Object.keys(t.relationships):[]).forEach((function(r){var o=t.relationships[r];if(o&&"data"in o){var i=o.data;if(!(P(i)&&i.length<1)&&n)if(i){var a=b(i,(function(t){return(void 0===t.id?null:e.findOne(t.type,t.id))||t}))||null,c=P(i)?i[0].type:i.type;r in n?n[r]=a:p(n,r,{model:c,type:s.TO_ONE_OR_MANY},a)}else{var u=_(n,"refs");u&&r in u&&"__proto__"!==r&&(n[r]=u[r].type===s.TO_MANY?[]:null)}}}))},o.prototype.__prepareQuery=function(t,e,n,r){return at(t,e,n,r,this)},o.prototype.toJSON=function(){return Object.assign({},n.prototype.toJSON.call(this),{cache:Rt(this)})},o.types=r.types&&r.types.length?r.types.concat(kt):[kt],o.maxCacheAge=r.maxCacheAge,o.cache=r.cache,o.defaultModel=r.defaultModel||kt,o}(r=n);var r;if(y(n))return function(t){function e(e,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=t.call(this,e,n,r,o,i)||this;return a.__collection=n,a}return D(e,t),e.prototype.sync=function(t){var e=this.__collection.sync(t);return e&&this.add(e),e},e.prototype.getOne=function(t,e){return this.__collection.getOne(this.modelType,t,e).then(this.__addFromResponse.bind(this))},e.prototype.getMany=function(t){return this.__collection.getMany(this.modelType,t).then(this.__addFromResponse.bind(this))},e.prototype.getAll=function(t,e){return void 0===e&&(e=50),L(this,void 0,void 0,(function(){return H(this,(function(n){switch(n.label){case 0:if(e<1)throw new Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(t)];case 1:return[2,J(n.sent(),e)]}}))}))},e.prototype.__addFromResponse=function(t){return t.data&&this.add(t.data),t.views.push(this),t},e}(n);throw new Error("The instance needs to be a model, collection or a view")}var Mt,qt=function(t){function e(e,n,r,o){var i=t.call(this,e,n,r,o)||this;return i.links&&Object.keys(i.links).forEach((function(t){O(i,t,(function(){return i.__fetchLink(t)}))})),i}return D(e,t),Object.defineProperty(e.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),e.prototype.__fetchLink=function(t){var n=this;if(!this.__cache[t]){var r=this.links&&t in this.links?this.links[t]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[t]=function(){return tt(r,n.collection,o,n.views).then((function(t){return new e(t.__internal.response,t.collection,void 0,t.views)}))}}}return this.__cache[t]},e}(E);function Nt(t,e){return(t&&e.collection?e.collection.sync(t):t)||{}}function Dt(t){return F(F({},t),{data:{data:b(t.data,(function(t){return t instanceof c?yt(t,!0):t}))||void 0}})}!function(t){t.Asc="",t.Desc="-"}(Mt||(Mt={}));var Ft=function(t){function e(e){var n,r=t.call(this,e)||this;return(n=r)._config.Response=qt,R("content-type","application/vnd.api+json")(n),S(Nt)(n),T(Dt)(n),x(C.CommaSeparated)(n),M(!1)(n),r}return D(e,t),e}(q);export{Ft as BaseJsonapiRequest,kt as GenericModel,qt as NetworkResponse,At as Response,it as buildUrl,W as config,tt as fetchLink,lt as fetchModelLink,pt as fetchModelRefLink,_t as getModelEndpointUrl,ut as getModelLinks,ct as getModelMeta,ft as getModelRefLinks,ht as getModelRefMeta,vt as isModelPersisted,xt as jsonapi,yt as modelToJsonApi,at as prepareQuery,gt as saveModel,bt as saveRelationship};
import{commitModel as t,getModelType as e,getModelCollection as n,modelToDirtyJSON as r,modelToJSON as o,getModelId as i,getRefId as a,ReferenceType as s,PureModel as c,updateModel as u,updateModelId as l,Bucket as f,upsertModel as d,initModelRef as p,isModel as h,isCollection as v,isView as y}from"@datx/core";import{getMeta as _,mobx as g,setMeta as m,getMetaObj as b,mapItems as w,META_FIELD as O,assignComputed as k,Headers as j,deprecated as P,isArrayLike as A}from"@datx/utils";import{CachingStrategy as C,ParamArrayType as E,Response as R,header as S,parser as T,serializer as x,paramArrayType as M,encodeQueryString as q,BaseRequest as N}from"@datx/network";export{BaseRequest,CachingStrategy,HttpMethod,ParamArrayType,addInterceptor,body,cache,clearAllCache,clearCacheByType,collection,encodeQueryString,fetchReference,header,method,paramArrayType,params,parser,query,serializer,setUrl}from"@datx/network";var D=function(t,e){return D=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},D(t,e)};function F(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}D(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var L=function(){return L=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},L.apply(this,arguments)};function H(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{c(r.next(t))}catch(t){i(t)}}function s(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}c((r=r.apply(t,e||[])).next())}))}function I(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}function U(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}var Q=/^((https?\:)?\/\/)/,G="undefined"!=typeof window;function W(t){return new Error("[datx exception] ".concat(t))}function B(t){var e=_(t,"fields",{},!0,!0),n={};return Object.keys(e).forEach((function(t){e[t].referenceDef&&(n[t]=e[t].referenceDef)})),n}function J(t,e){return void 0===e&&(e=50),H(this,void 0,void 0,(function(){var n,r,o,i;return I(this,(function(a){switch(a.label){case 0:r=[],o=t,i=1,(n=[]).push.apply(n,t.data),r.push(t),a.label=1;case 1:return t.next?++i>e?[3,3]:[4,t.next()]:[3,3];case 2:return t=a.sent(),r.push(t),n.push.apply(n,t.data),[3,1];case 3:return o=r[r.length-1],[2,{data:n,responses:r,lastResponse:o}]}}))}))}var Y={baseUrl:"/",cache:G?C.CacheFirst:C.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:G&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||void 0,paramArrayType:E.CommaSeparated,baseFetch:function(t,e,n,r,o){var i,a,s,c=this,u=Promise.resolve(),l=t.toUpperCase(),f="GET"!==l&&"HEAD"!==l;return u.then((function(){var o=Y.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},Y.defaultFetchOptions,{body:f&&JSON.stringify(n)||void 0,headers:i,method:t});if(c.fetchReference)return c.fetchReference(e,a);throw new Error("Fetch reference needs to be defined before using the network")})).then((function(t){return a=t.status,s=t.headers,t.json()})).catch((function(t){if(204===a)return null;throw t})).then((function(t){if(i=t,a>=400)throw{message:"Invalid HTTP status: ".concat(a),status:a};return{data:i,headers:s,requestHeaders:r,status:a}})).catch((function(t){return c.onError({data:i,error:t,headers:s,requestHeaders:r,status:a})}))},onError:function(t){return t},transformRequest:function(t){return t},transformResponse:function(t){return t},usePatchWhenPossible:!0};function z(t,e,n){var r,o,i,a;return new((null===(o=null===(r=e.options)||void 0===r?void 0:r.fetchOptions)||void 0===o?void 0:o.Response)||Ct)({error:new Error(t),requestHeaders:null===(a=null===(i=e.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers},n,e.options)}function V(t,n,r,o){var i,a;void 0===r&&(r=!1);var s=(null==n?void 0:n.Response)||Ct;return Y.baseFetch(t.method,t.url,t.data,null===(a=null===(i=null==t?void 0:t.options)||void 0===i?void 0:i.networkConfig)||void 0===a?void 0:a.headers,n).then((function(e){var n=Object.assign({},e,{collection:t.collection}),r=Y.transformResponse(n);return o?(o.update(r,t.views),o):new s(r,t.collection,t.options,void 0,t.views)})).then((function(n){return r&&function(t,n){if((null==n?void 0:n.isSuccess)&&-1!==n.status&&(n.data||null===n.data)){var r=w(n.data||[],e);(Et=Et.filter((function(e){return e.url!==t}))).unshift({response:n.snapshot,collection:n.collection,time:Date.now(),types:[].concat(r),url:t})}}(t.url,n),n}))}function K(t){var e,n,r,o,i,a,s,c,u,l,f,d,p,h,v,y,_,g=(null===(n=null===(e=t.options)||void 0===e?void 0:e.fetchOptions)||void 0===n?void 0:n.Response)||Ct,m=Y.transformRequest(t),b=null===(r=null==m?void 0:m.collection)||void 0===r?void 0:r.constructor,w=b&&b.cache,O="GET"===m.method.toUpperCase(),k=(null===(i=null===(o=t.options)||void 0===o?void 0:o.cacheOptions)||void 0===i?void 0:i.skipCache)||!O?C.NetworkOnly:(null===(s=null===(a=t.options)||void 0===a?void 0:a.cacheOptions)||void 0===s?void 0:s.cachingStrategy)||w||Y.cache,j=Y.maxCacheAge||1/0;if(b&&void 0!==b.maxCacheAge&&(j=b.maxCacheAge),void 0!==(null===(u=null===(c=t.options)||void 0===c?void 0:c.cacheOptions)||void 0===u?void 0:u.maxAge)&&(j=null===(f=null===(l=t.options)||void 0===l?void 0:l.cacheOptions)||void 0===f?void 0:f.maxAge),k===C.NetworkOnly)return V(m,null===(d=t.options)||void 0===d?void 0:d.fetchOptions);var P=function(t,e,n){void 0===n&&(n=Ct);var r=Date.now()-1e3*e,o=Et.find((function(e){return e.url===t&&e.time>r}));if(o){var i=o.response;return{response:new n(i.response,o.collection,i.options),time:o.time,types:o.types,url:o.url}}}(m.url,j,g);if(k===C.NetworkFirst)return V(m,null===(p=t.options)||void 0===p?void 0:p.fetchOptions,!0).catch((function(t){if(P)return P.response;throw t}));if(k===C.StaleWhileRevalidate){var A=V(m,null===(h=t.options)||void 0===h?void 0:h.fetchOptions,!0);return P?(A.catch((function(){})),Promise.resolve(P.response)):A}if(k===C.CacheOnly)return P?Promise.resolve(P.response):Promise.reject(z("No cache for this request",t,null==m?void 0:m.collection));if(k===C.CacheFirst)return P?Promise.resolve(P.response):V(m,null===(v=t.options)||void 0===v?void 0:v.fetchOptions,!0);if(k===C.StaleAndUpdate){var E=null===(y=null==P?void 0:P.response)||void 0===y?void 0:y.clone();return A=V(m,null===(_=t.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(A.catch((function(){})),Promise.resolve(E)):A}return Promise.reject(z("Invalid caching strategy",t,null==m?void 0:m.collection))}function X(t,e,n,r){return K({collection:e,data:void 0,method:"GET",options:n,url:t,views:r})}function Z(t,e,n,r,o){return K({collection:n,data:e,method:"POST",options:r,url:t,views:o})}function $(t,e,n,r,o){return K({collection:n,data:e,method:"PATCH",options:r,url:t,views:o})}function tt(t,e,n,r,o){return K({collection:n,data:e,method:"PUT",options:r,url:t,views:o})}function et(t,e,n,r,o){if(void 0===o&&(o=Ct),t){var i="object"==typeof t?t.href:t;if(i)return X(i,e,n,r)}return Promise.resolve(new o({data:void 0},e))}function nt(e,n){return g.action((function(r){if(r.error)throw r.error;if(204===r.status)return m(e,"networkPersisted",!0),e;if(202===r.status){var o=r.data;return m(o,"jsonapiProp",n),m(o,"jsonapiQueue",!0),m(o,"jsonapiRelated",e),o}m(e,"networkPersisted",!0);var i=r.replaceData(e).data;return t(i),i}))}function rt(t,e){void 0===e&&(e="");var n=[];return Object.keys(t).forEach((function(r){t[r]instanceof Array?Y.paramArrayType===E.CommaSeparated?n.push({key:"".concat(e).concat(r),value:t[r].join(",")}):Y.paramArrayType===E.MultipleParams?n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r),value:t}}))):Y.paramArrayType===E.ParamArray&&n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r,"]["),value:t}}))):"object"==typeof t[r]?n.push.apply(n,rt(t[r],"".concat(r,"."))):n.push({key:"".concat(e).concat(r),value:t[r]})})),n}function ot(t){return rt(t).map((function(t){return"filter[".concat(t.key,"]=").concat(t.value)}))}function it(t){return encodeURIComponent(t).replace(/%3D/,"=")}function at(t,e,n,r){var o,i,a,s,c,u,l,f,d,p=n&&n.networkConfig&&n.networkConfig.headers||{},h=[].concat(ot((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(d=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(d)]:[],(f=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?Y.sortParams&&Array.isArray(f)?["include=".concat(U([],f,!0).sort())]:["include=".concat(f)]:[],(u=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},l=[],Object.keys(u).forEach((function(t){l.push("fields[".concat(t,"]=").concat(u[t]))})),l),function(t){return t.map((function(t){return"string"==typeof t?t:"".concat(t.key,"=").concat(t.value)}))}((null===(c=null==n?void 0:n.queryParams)||void 0===c?void 0:c.custom)||[]));Y.encodeQueryString&&(h=h.map(it));var v=function(t,e){var n=t;if(e.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+e.join("&")}return n}(function(t,e){return Q.test(t)||e?t:"".concat(Y.baseUrl).concat(t)}(t,r),h);return Y.sortParams&&(v=function(t){var e=t.split("?"),n=e[0],r=e[1];if(!r)return t;var o=new URLSearchParams(r);o.sort();var i=Y.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:e,headers:p,url:v}}function st(t,n,r,o,i,a){var s=a&&a.constructor;!s&&i&&(s=i.constructor.types.filter((function(e){return e.type===t}))[0]);var c,u=!1;return s?s.endpoint&&"function"==typeof s.endpoint?(u=!0,c=s.endpoint(Y.baseUrl)):c=s.endpoint||s.baseUrl||e(s):c=t.toString(),at(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function ct(t,e,n){var r,o;if(void 0===n&&(n=jt),!e)return null;var i=b(n),a=Object.fromEntries(Object.keys(i).filter((function(t){return t.startsWith("map_")})).map((function(t){return[i[t],t.slice(4)]}))),c=((r={})[O]=(o={fields:Object.keys(e.attributes||{}).reduce((function(t,e){var n;return t[null!==(n=a[e])&&void 0!==n?n:e]={referenceDef:!1},t}),{}),id:e.id},o.jsonapiLinks=e.links,o.jsonapiMeta=e.meta,o.networkPersisted=Boolean(e.id),o.type=e.type,o),r);if(e.relationships){var u={},l={},f={};Object.keys(e.relationships).forEach((function(n){var r,o=e.relationships[n];o&&"data"in o&&(o.data||null===o.data)&&(!(o.data instanceof Array)||o.data.length>0?(c[n]=o.data,t&&n in t||(f[n]={referenceDef:{model:(o.data instanceof Array?o.data[0].type:null===(r=o.data)||void 0===r?void 0:r.type)||jt.type,type:o.data instanceof Array?s.TO_MANY:s.TO_ONE}})):c[n]=[]),o&&"links"in o&&(u[n]=o.links),o&&"meta"in o&&(l[n]=o.meta)})),Object.assign(c[O].fields,f),c[O].jsonapiRefLinks=u,c[O].jsonapiRefMeta=l}return Object.assign(c,e.attributes)}function ut(t){return _(t,"jsonapiMeta",{})}function lt(t){return _(t,"jsonapiLinks",{})}function ft(t,r,o){var i=n(t),a=lt(t);if(!a||!(r in a))throw W("Link ".concat(r," doesn't exist on the model"));var s=et(a[r],i,o);return _(t,"jsonapiQueue")?s.then((function(n){var r=_(t,"jsonapiRelated"),o=_(t,"jsonapiProp"),i=n.data,a=i&&e(i);return i&&a!==e(t)&&a===e(r)?o?(r[o]=i,n):(m(r,"networkPersisted",!0),n.replaceData(r)):n})):s}function dt(t){return _(t,"jsonapiRefLinks",{})}function pt(t,e,r){if(!n(t))throw W("The model needs to be in a collection");var o=dt(t);if(!o||!(e in o))throw W("The reference ".concat(e," doesn't have any links"));var i=o[e];if(!i||!(r in i))throw W("Link ".concat(r," doesn't exist on the model"));return i[r]}function ht(t,e,r,o){var i=n(t);return et(pt(t,e,r),i,o)}function vt(t){return _(t,"jsonapiRefMeta",{})}function yt(t){return _(t,"networkPersisted",!1)}function _t(t,n){var s=t.constructor,c=n?r(t):o(t),u=s.useAutogeneratedIds,l={attributes:c,id:yt(t)||u?i(t).toString():void 0,type:e(t)},f=B(t),d=b(t.constructor);return Object.keys(f).forEach((function(e){var n;if(!f[e].property){l.relationships=l.relationships||{};var r=a(t,e),o=null!==(n=null==d?void 0:d["map_".concat(e)])&&void 0!==n?n:e;l.relationships[o]={data:w(r,(function(t){return{id:t.id.toString(),type:t.type}}))},l.attributes&&delete l.attributes[e]}})),l.attributes&&(delete l.attributes.id,delete l.attributes[O]),l}function gt(t,n){return st(e(t),yt(t)?i(t):void 0,void 0,n,void 0,t).url}function mt(t,r){var o,i=n(t),a=null!==(o=null==Y?void 0:Y.usePatchWhenPossible)&&void 0!==o&&o,s=yt(t),c=_t(t,a&&s);return(s?a?$:tt:Z)(gt(t,r),{data:c},i,r&&r.networkConfig&&r.networkConfig.headers).then(nt(t)).then((function(n){return Rt(e(t)),n}))}function bt(t,e){var r=n(t),o=yt(t),i=gt(t);return o?function(t,e,n,r){return K({collection:e,data:void 0,method:"DELETE",options:n,url:t,views:void 0})}(i,r,e&&e.networkConfig&&e.networkConfig.headers).then((function(e){if(e.error)throw e.error;!function(t,e){m(t,"networkPersisted",!1)}(t),r&&r.__removeModel(t)})):(r&&r.__removeModel(t),Promise.resolve())}function wt(t,e,r){var o,i=n(t),s=pt(t,e,"self"),c="object"==typeof s?s.href:s,u=a(t,e),l=null===(o=_(t,"fields"))||void 0===o?void 0:o[e],f=(null==l?void 0:l.referenceDef)?l.referenceDef.model:null;return $(c,{data:w(u,(function(t){return{id:t.id,type:t.type||f}}))},i,r&&r.networkConfig&&r.networkConfig.headers).then(nt(t,e))}var Ot=["jsonapiLinks","jsonapiMeta","networkPersisted","jsonapiRefLinks","jsonapiRefMeta"];function kt(t){return function(e){function n(n,r){void 0===n&&(n={});var o,i=n;n&&"type"in n&&("attributes"in n||"relationships"in n)&&(i=ct(B(t),n)),o=e.call(this,i,r)||this;var a=(null==i?void 0:i[O])||{};return Ot.forEach((function(t){t in a&&m(o,t,a[t])})),o}return F(n,e),n.getAutoId=function(){return e.getAutoId.call(this).toString()},n.prototype.save=function(t){return mt(this,t)},n.prototype.destroy=function(t){return bt(this,t)},n.useAutogeneratedIds=t.useAutogeneratedIds||!1,n}(t)}var jt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return F(e,t),e}(kt(c));function Pt(t){if(t instanceof Array)return t;var e=[];return t.forEach?(t.forEach((function(t,n){e.push([n,t])})),e):e}function At(t,e,n){if(e&&t.data){var r=n||e.sync(t.data);return new f.ToOneOrMany(r,e,!0)}if(t.data){var o=t.data;if(o.data){if(o.data instanceof Array)throw new Error("A save/remove operation should not return an array of results");return{value:n||new jt(ct(void 0,o.data))}}}return new f.ToOneOrMany(null,e,!0)}var Ct=function(){function t(t,e,n,r,o){var i=this;if(this.__internal={response:{},views:[]},this.__cache={},this.collection=e,this.__updateInternal(t,n,o),this.__data=At(t,e,r),this.views.forEach((function(t){i.__data.value&&t.add(i.__data.value)})),Object.freeze(this),this.error)throw this}return Object.defineProperty(t.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"headers",{get:function(){return this.__internal.headers},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"requestHeaders",{get:function(){return this.__internal.requestHeaders},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"error",{get:function(){return this.__internal.error},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"status",{get:function(){return this.__internal.status},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isSuccess",{get:function(){return!this.error},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"data",{get:function(){return this.__data.value},enumerable:!1,configurable:!0}),t.prototype.__updateInternal=function(t,e,n){var r,o,i,a,s,c=this;e&&(this.__internal.options=e),this.__internal.response=t,this.__internal.meta=(null===(r=t.data)||void 0===r?void 0:r.meta)||{},this.__internal.links=(null===(o=t.data)||void 0===o?void 0:o.links)||{},this.__internal.jsonapi=(null===(i=t.data)||void 0===i?void 0:i.jsonapi)||{},this.__internal.headers=t.headers&&((s=t.headers)instanceof Array?new j(s):s),this.__internal.requestHeaders=t.requestHeaders,this.__internal.error=(null===(a=t.data)||void 0===a?void 0:a.errors)||t.error,this.__internal.status=t.status,n&&(this.__internal.views=n),this.error||this.status||(this.__internal.error=new Error("Network not available")),this.links&&Object.keys(this.links).forEach((function(t){k(c,t,(function(){return c.__fetchLink(t)}))}))},t.prototype.replaceData=function(t){var n,r=this.data;if(r===t)return this;var a=i(r).toString(),s=e(r),c=this.views.map((function(t){return t.list.indexOf(r)}));this.collection&&(this.collection.removeOne(s,a),this.collection.add(t));var f=o(r);return null===(n=null==f?void 0:f.__META__)||void 0===n||delete n.collection,u(t,f),l(t,a),this.views.forEach((function(e,n){-1!==c[n]&&(e.list[c[n]]=t)})),new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,t)},t.prototype.clone=function(){return new(0,this.constructor)(this.__internal.response,this.collection,this.__internal.options,this.data||void 0)},Object.defineProperty(t.prototype,"snapshot",{get:function(){return{response:Object.assign({},this.__internal.response,{headers:this.__internal.response.headers&&Pt(this.__internal.response.headers),collection:void 0}),options:this.__internal.options}},enumerable:!1,configurable:!0}),t.prototype.update=function(t,e){this.__updateInternal(t,void 0,e);var n=At(t,this.collection);return this.__data.__readonlyValue=n.value,this},t.prototype.__fetchLink=function(t){var e=this,n=this.constructor;if(!this.__cache[t]){var r=this.links&&t in this.links?this.links[t]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[t]=function(){return et(r,e.collection,o,e.views,n)}}}return this.__cache[t]},t}(),Et=[];function Rt(t){Et=Et.filter((function(e){return!e.types.includes(t)}))}function St(t){return Et.filter((function(e){return e.collection===t})).map((function(t){return Object.assign({},t,{collection:void 0})}))}function Tt(t){if(t.error)throw t.error;return t}function xt(t,e){return w(t&&t.included||[],e),w(t&&t.data||null,e)}function Mt(n){if(h(n))return kt(n);if(v(n))return function(n){function o(t){void 0===t&&(t=[]);var e=n.call(this,t)||this;return t instanceof Array||!(null==t?void 0:t.cache)||function(t,e){Et.push.apply(Et,t.map((function(t){return Object.assign({collection:e},t)})))}(t.cache,e),e}return F(o,n),o.prototype.sync=function(t){var e=this;if(!t)return null;var n=xt(t,(function(t){return e.__addRecord(t)}));return xt(t,this.__updateRelationships.bind(this)),n},o.prototype.fetch=function(t,e,n){return P("fetch is deprecated, use getOne instead"),this.getOne(t,e,Object.assign({},n,{cacheOptions:Object.assign({},(null==n?void 0:n.cacheOptions)||{},{cachingStrategy:G?C.CacheFirst:C.NetworkOnly})}))},o.prototype.fetchAll=function(t,e){return P("fetchAll is deprecated, use getMany instead"),this.getMany(t,Object.assign({},e,{cacheOptions:Object.assign({},(null==e?void 0:e.cacheOptions)||{},{cachingStrategy:G?C.CacheFirst:C.NetworkOnly})}))},o.prototype.getOne=function(t,n,r){var o=e(t),i=this.__prepareQuery(o,n,void 0,r),a=r||{};return a.networkConfig=a.networkConfig||{},a.networkConfig.headers=i.headers,X(i.url,this,a).then(Tt)},o.prototype.getMany=function(t,n){var r=e(t),o=this.__prepareQuery(r,void 0,void 0,n),i=n||{};return i.networkConfig=i.networkConfig||{},i.networkConfig.headers=o.headers,X(o.url,this,i).then(Tt)},o.prototype.getAll=function(t,e,n){return void 0===n&&(n=50),H(this,void 0,void 0,(function(){return I(this,(function(r){switch(r.label){case 0:if(n<1)throw Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(t,e)];case 1:return[2,J(r.sent(),n)]}}))}))},o.prototype.request=function(t,e,n,r){return void 0===e&&(e="GET"),function(t){return K(t)}({url:at(t,n,r).url,options:r,data:n,method:e,collection:this})},o.prototype.removeOne=function(t,n,r){var o,a,s,c=e(t);return"object"==typeof n||void 0===n||"boolean"==typeof n?(o=n,a=i(t).toString(),s=t):(o=r,s=(a=i(n).toString())?this.findOne(c,a):null),s&&o?bt(s,"object"==typeof o?o:void 0):(s&&this.__removeModel(s),Rt(c),Promise.resolve())},o.prototype.removeAll=function(t){n.prototype.removeAll.call(this,t),Rt(e(t))},o.prototype.reset=function(){n.prototype.reset.call(this),Et.length=0},o.prototype.__addRecord=function(n){var r=this.constructor,o=n.type,i=n.id,a=void 0===i?null:this.findOne(o,i),s=r.types.find((function(t){return e(t)===o}))||jt,c=ct(B(s),n,s);return a?(d(c,o,this),t(a)):a=r.types.filter((function(t){return t.type===o})).length?this.add(c,o):this.add(new jt(c,this)),a},o.prototype.__updateRelationships=function(t){var e=this,n=void 0===t.id?null:this.findOne(t.type,t.id);(t.relationships?Object.keys(t.relationships):[]).forEach((function(r){var o=t.relationships[r];if(o&&"data"in o){var i=o.data;if(!(A(i)&&i.length<1)&&n)if(i){var a=w(i,(function(t){return(void 0===t.id?null:e.findOne(t.type,t.id))||t}))||null,c=A(i)?i[0].type:i.type;r in n?n[r]=a:p(n,r,{model:c,type:s.TO_ONE_OR_MANY},a)}else{var u=_(n,"refs");u&&r in u&&"__proto__"!==r&&(n[r]=u[r].type===s.TO_MANY?[]:null)}}}))},o.prototype.__prepareQuery=function(t,e,n,r){return st(t,e,n,r,this)},o.prototype.toJSON=function(){return Object.assign({},n.prototype.toJSON.call(this),{cache:St(this)})},o.types=r.types&&r.types.length?r.types.concat(jt):[jt],o.maxCacheAge=r.maxCacheAge,o.cache=r.cache,o.defaultModel=r.defaultModel||jt,o}(r=n);var r;if(y(n))return function(t){function e(e,n,r,o,i){void 0===o&&(o=[]),void 0===i&&(i=!1);var a=t.call(this,e,n,r,o,i)||this;return a.__collection=n,a}return F(e,t),e.prototype.sync=function(t){var e=this.__collection.sync(t);return e&&this.add(e),e},e.prototype.getOne=function(t,e){return this.__collection.getOne(this.modelType,t,e).then(this.__addFromResponse.bind(this))},e.prototype.getMany=function(t){return this.__collection.getMany(this.modelType,t).then(this.__addFromResponse.bind(this))},e.prototype.getAll=function(t,e){return void 0===e&&(e=50),H(this,void 0,void 0,(function(){return I(this,(function(n){switch(n.label){case 0:if(e<1)throw new Error("Please enter a meaningful amount of max requests.");return[4,this.getMany(t)];case 1:return[2,J(n.sent(),e)]}}))}))},e.prototype.__addFromResponse=function(t){return t.data&&this.add(t.data),t.views.push(this),t},e}(n);throw new Error("The instance needs to be a model, collection or a view")}var qt,Nt=function(t){function e(e,n,r,o){var i=t.call(this,e,n,r,o)||this;return i.links&&Object.keys(i.links).forEach((function(t){k(i,t,(function(){return i.__fetchLink(t)}))})),i}return F(e,t),Object.defineProperty(e.prototype,"meta",{get:function(){return this.__internal.meta},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"links",{get:function(){return this.__internal.links},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"jsonapi",{get:function(){return this.__internal.jsonapi},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"views",{get:function(){return this.__internal.views},enumerable:!1,configurable:!0}),e.prototype.__fetchLink=function(t){var n=this;if(!this.__cache[t]){var r=this.links&&t in this.links?this.links[t]:null;if(r){var o=Object.assign({},this.__internal.options);o.networkConfig=o.networkConfig||{},o.networkConfig.headers=this.requestHeaders,this.__cache[t]=function(){return et(r,n.collection,o,n.views).then((function(t){return new e(t.__internal.response,t.collection,void 0,t.views)}))}}}return this.__cache[t]},e}(R);function Dt(t,e){return(t&&e.collection?e.collection.sync(t):t)||{}}function Ft(t){return L(L({},t),{data:{data:w(t.data,(function(t){return t instanceof c?_t(t,!0):t}))||void 0}})}!function(t){t.Asc="",t.Desc="-"}(qt||(qt={}));var Lt=function(t){function e(e){var n,r=t.call(this,e)||this;return(n=r)._config.Response=Nt,S("content-type","application/vnd.api+json")(n),T(Dt)(n),x(Ft)(n),M(E.CommaSeparated)(n),q(!1)(n),r}return F(e,t),e}(N);export{Lt as BaseJsonapiRequest,jt as GenericModel,Nt as NetworkResponse,Ct as Response,at as buildUrl,Y as config,et as fetchLink,ft as fetchModelLink,ht as fetchModelRefLink,gt as getModelEndpointUrl,lt as getModelLinks,ut as getModelMeta,dt as getModelRefLinks,vt as getModelRefMeta,yt as isModelPersisted,Mt as jsonapi,_t as modelToJsonApi,st as prepareQuery,mt as saveModel,wt as saveRelationship};
//# sourceMappingURL=index.esm.js.map
{
"name": "@datx/jsonapi",
"version": "2.4.11",
"version": "2.4.12",
"description": "DatX mixin for JSON API support",

@@ -25,7 +25,6 @@ "main": "./index.cjs.js",

"dependencies": {
"@datx/core": "2.4.11",
"@datx/network": "2.4.11",
"@datx/core": "2.4.12",
"@datx/network": "2.4.12",
"@datx/utils": "2.4.8"
},
"gitHead": "d8e93de8e2e87be4f40c67225720d676f4182c8f"
}
}
import { IJsonapiModel } from "./IJsonapiModel";
export declare type IResponseData<TModel extends IJsonapiModel = IJsonapiModel> = TModel | Array<TModel> | null;

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display