@datx/jsonapi
Advanced tools
| { | ||
| "workbench.colorCustomizations": {} | ||
| } |
| import { BaseRequest as BaseNetworkRequest } from '@datx/network'; | ||
| export declare class BaseJsonapiRequest extends BaseNetworkRequest { | ||
| constructor(baseUrl: string); | ||
| } |
| import { IType } from '@datx/core'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { Response } from './Response'; | ||
| import { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export interface ICache { | ||
| response: Response<IJsonapiModel>; | ||
| time: number; | ||
| types: Array<IType>; | ||
| url: string; | ||
| } | ||
| export interface ICacheInternal { | ||
| response: IResponseSnapshot; | ||
| collection?: IJsonapiCollection; | ||
| time: number; | ||
| types: Array<IType>; | ||
| url: string; | ||
| } | ||
| export declare function saveCache(url: string, response: Response<IJsonapiModel>): void; | ||
| export declare function getCache(url: string, maxAge: number, ResponseConstructor?: typeof Response): ICache | undefined; | ||
| export declare function clearAllCache(): void; | ||
| export declare function clearCacheByType(type: IType): void; | ||
| export declare function getCacheByCollection(collection: IJsonapiCollection): Array<Omit<ICacheInternal, 'collection'>>; | ||
| export declare function saveCacheForCollection(cacheItems: Array<Omit<ICacheInternal, 'collection'>>, collection: IJsonapiCollection): void; |
| export declare const MODEL_LINKS_FIELD = "jsonapiLinks"; | ||
| export declare const MODEL_REF_LINKS_FIELD = "jsonapiRefLinks"; | ||
| export declare const MODEL_REF_META_FIELD = "jsonapiRefMeta"; | ||
| export declare const MODEL_META_FIELD = "jsonapiMeta"; | ||
| export declare const MODEL_PERSISTED_FIELD = "networkPersisted"; | ||
| export declare const MODEL_PROP_FIELD = "jsonapiProp"; | ||
| export declare const MODEL_QUEUE_FIELD = "jsonapiQueue"; | ||
| export declare const MODEL_RELATED_FIELD = "jsonapiRelated"; | ||
| export declare const URL_REGEX: RegExp; | ||
| export declare const DATX_JSONAPI_CLASS: unique symbol; |
| import { ICollectionConstructor, PureCollection } from '@datx/core'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export declare function decorateCollection(BaseClass: typeof PureCollection): ICollectionConstructor<PureCollection & IJsonapiCollection>; |
| import { PureModel } from '@datx/core'; | ||
| export declare function decorateModel(BaseClass: typeof PureModel): typeof PureModel; |
| import { IViewConstructor, View } from '@datx/core'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| export declare function decorateView<U>(BaseClass: typeof View): IViewConstructor<IJsonapiModel, U & IJsonapiView>; |
| import { PureModel } from '@datx/core'; | ||
| export declare const DecoratedModel: typeof PureModel; | ||
| export declare class GenericModel extends DecoratedModel { | ||
| } |
| import { PureModel, IModelConstructor } from '@datx/core'; | ||
| import { IRawModel } from '@datx/utils'; | ||
| import { ILink, IRecord } from '@datx/jsonapi-types'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { IRequestOptions } from '../interfaces/IRequestOptions'; | ||
| import { Response } from '../Response'; | ||
| export declare function flattenModel(classRefs: any): null; | ||
| export declare function flattenModel(classRefs: any, data?: IRecord, modelClass?: IModelConstructor): IRawModel; | ||
| export declare function getModelMeta(model: PureModel): Record<string, any>; | ||
| export declare function getModelLinks(model: PureModel): Record<string, ILink>; | ||
| export declare function fetchModelLink<T extends IJsonapiModel = IJsonapiModel>(model: PureModel, key: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| export declare function getModelRefLinks(model: PureModel): Record<string, Record<string, ILink>>; | ||
| export declare function fetchModelRefLink<T extends IJsonapiModel = IJsonapiModel>(model: PureModel, ref: string, key: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| export declare function getModelRefMeta(model: PureModel): Record<string, any>; | ||
| export declare function isModelPersisted(model: PureModel): boolean; | ||
| export declare function modelToJsonApi(model: IJsonapiModel, onlyDirty?: boolean): IRecord; | ||
| export declare function getModelEndpointUrl(model: IJsonapiModel, options?: IRequestOptions): string; | ||
| export declare function saveModel(model: IJsonapiModel, options?: IRequestOptions): Promise<IJsonapiModel>; | ||
| export declare function removeModel<T extends IJsonapiModel>(model: T, options?: IRequestOptions): Promise<void>; | ||
| export declare function saveRelationship<T extends IJsonapiModel>(model: T, ref: string, options?: IRequestOptions): Promise<T>; |
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { Response } from '../Response'; | ||
| export declare function getResponseRawData<TModel extends IJsonapiModel = IJsonapiModel>(response: Response<TModel>): import("@datx/jsonapi-types").IDocument | undefined; |
| import { IType, PureCollection } from '@datx/core'; | ||
| import { IHeaders } from '../interfaces/IHeaders'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { IRequestOptions } from '../interfaces/IRequestOptions'; | ||
| import { IRequest } from '../interfaces/IRequest'; | ||
| export declare function buildUrl(url: string, data?: IRequest, options?: IRequestOptions, containsBase?: boolean): { | ||
| url: string; | ||
| data?: object; | ||
| headers: IHeaders; | ||
| }; | ||
| export declare function prepareQuery(type: IType, id?: number | string, data?: IRequest, options?: IRequestOptions, collection?: PureCollection, model?: IJsonapiModel): { | ||
| url: string; | ||
| data?: object; | ||
| headers: IHeaders; | ||
| }; |
| import { Collection, IReferenceDefinition, PureModel, View } from '@datx/core'; | ||
| import { IGetAllResponse } from '../interfaces/IGetAllResponse'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { Response } from '../Response'; | ||
| export declare const isBrowser: boolean; | ||
| /** | ||
| * Returns the value if it's not a function. If it's a function | ||
| * it calls it. | ||
| * | ||
| * @export | ||
| * @template T | ||
| * @param {(T|(() => T))} target can be anything or function | ||
| * @returns {T} value | ||
| */ | ||
| export declare function getValue<T>(target: T | (() => T)): T; | ||
| export declare function error(message: string): Error; | ||
| export declare function getModelClassRefs(type: typeof PureModel | PureModel): Record<string, IReferenceDefinition>; | ||
| export declare function getAllResponses<M extends IJsonapiModel = IJsonapiModel>(response: Response<M>, maxRequests?: number): Promise<IGetAllResponse<M>>; | ||
| export declare function isJsonApiClass(type: typeof PureModel | typeof Collection | typeof View): boolean; |
| "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(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}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="jsonapiLinks",l="jsonapiRefLinks",p="jsonapiRefMeta",d="jsonapiMeta",f="networkPersisted",h="jsonapiProp",y="jsonapiQueue",v="jsonapiRelated",g=/^((https?\:)?\/\/)/,_=Symbol("@datx/jsonapi class"),m="undefined"!=typeof window;function b(e){return new Error("[datx exception] ".concat(e))}function O(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 w(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 M=(m?"function"==typeof window.fetch&&window.fetch.bind(window):"function"==typeof globalThis.fetch&&globalThis.fetch.bind(globalThis))||void 0,j={baseUrl:"/",cache:m?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:M,paramArrayType:n.ParamArrayType.CommaSeparated,baseFetch:function(e,t,n,r,o){var i,a,s,c=this,u=Promise.resolve(),l=e.toUpperCase(),p="GET"!==l&&"HEAD"!==l;return u.then((function(){var o=j.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},j.defaultFetchOptions,{body:p&&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 k(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)||$)({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 P(n,r,o,i){var a,s;void 0===o&&(o=!1);var c=(null==r?void 0:r.Response)||$;return j.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=j.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);(ee=ee.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 x(e){var t,r,o,i,a,s,c,u,l,p,d,f,h,y,v,g,_,m=(null===(r=null===(t=e.options)||void 0===t?void 0:t.fetchOptions)||void 0===r?void 0:r.Response)||$,b=j.transformRequest(e),O=null===(o=null==b?void 0:b.collection)||void 0===o?void 0:o.constructor,w=O&&O.cache,M="GET"===b.method.toUpperCase(),x=(null===(a=null===(i=e.options)||void 0===i?void 0:i.cacheOptions)||void 0===a?void 0:a.skipCache)||!M?n.CachingStrategy.NetworkOnly:(null===(c=null===(s=e.options)||void 0===s?void 0:s.cacheOptions)||void 0===c?void 0:c.cachingStrategy)||w||j.cache,T=j.maxCacheAge||1/0;if(O&&void 0!==O.maxCacheAge&&(T=O.maxCacheAge),void 0!==(null===(l=null===(u=e.options)||void 0===u?void 0:u.cacheOptions)||void 0===l?void 0:l.maxAge)&&(T=null===(d=null===(p=e.options)||void 0===p?void 0:p.cacheOptions)||void 0===d?void 0:d.maxAge),x===n.CachingStrategy.NetworkOnly)return P(b,null===(f=e.options)||void 0===f?void 0:f.fetchOptions);var C=function(e,t,n){void 0===n&&(n=$);var r=Date.now()-1e3*t,o=ee.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}}}(b.url,T,m);if(x===n.CachingStrategy.NetworkFirst)return P(b,null===(h=e.options)||void 0===h?void 0:h.fetchOptions,!0).catch((function(e){if(C)return C.response;throw e}));if(x===n.CachingStrategy.StaleWhileRevalidate){var A=P(b,null===(y=e.options)||void 0===y?void 0:y.fetchOptions,!0);return C?(A.catch((function(){})),Promise.resolve(C.response)):A}if(x===n.CachingStrategy.CacheOnly)return C?Promise.resolve(C.response):Promise.reject(k("No cache for this request",e,null==b?void 0:b.collection));if(x===n.CachingStrategy.CacheFirst)return C?Promise.resolve(C.response):P(b,null===(v=e.options)||void 0===v?void 0:v.fetchOptions,!0);if(x===n.CachingStrategy.StaleAndUpdate){var E=null===(g=null==C?void 0:C.response)||void 0===g?void 0:g.clone();return A=P(b,null===(_=e.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(A.catch((function(){})),Promise.resolve(E)):A}return Promise.reject(k("Invalid caching strategy",e,null==b?void 0:b.collection))}function T(e,t,n,r){return x({collection:t,data:void 0,method:"GET",options:n,url:e,views:r})}function C(e,t,n,r,o){return x({collection:n,data:t,method:"POST",options:r,url:e,views:o})}function A(e,t,n,r,o){return x({collection:n,data:t,method:"PATCH",options:r,url:e,views:o})}function E(e,t,n,r,o){return x({collection:n,data:t,method:"PUT",options:r,url:e,views:o})}function S(e,t,n,r,o){if(void 0===o&&(o=$),e){var i="object"==typeof e?e.href:e;if(i)return T(i,t,n,r)}return Promise.resolve(new o({data:void 0},t))}function R(n,r){return t.mobx.action((function(o){if(o.error)throw o.error;if(204===o.status)return t.setMeta(n,f,!0),n;if(202===o.status){var i=o.data;return t.setMeta(i,h,r),t.setMeta(i,y,!0),t.setMeta(i,v,n),i}t.setMeta(n,f,!0);var a=o.replaceData(n).data;return e.commitModel(a),a}))}function I(e,t){void 0===t&&(t="");var r=[];return Object.keys(e).forEach((function(o){e[o]instanceof Array?j.paramArrayType===n.ParamArrayType.CommaSeparated?r.push({key:"".concat(t).concat(o),value:e[o].join(",")}):j.paramArrayType===n.ParamArrayType.MultipleParams?r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o),value:e}}))):j.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,I(e[o],"".concat(o,"."))):r.push({key:"".concat(t).concat(o),value:e[o]})})),r}function L(e){return I(e).map((function(e){return"filter[".concat(e.key,"]=").concat(e.value)}))}function D(e){return encodeURIComponent(e).replace(/%3D/,"=")}function q(e,t,n,r){var o,i,a,s,u,l,p,d,f,h=n&&n.networkConfig&&n.networkConfig.headers||{},y=[].concat(L((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(f=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(f)]:[],(d=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?j.sortParams&&Array.isArray(d)?["include=".concat(c([],d,!0).sort())]:["include=".concat(d)]:[],(l=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},p=[],Object.keys(l).forEach((function(e){p.push("fields[".concat(e,"]=").concat(l[e]))})),p),function(e){return e.map((function(e){return"string"==typeof e?e:"".concat(e.key,"=").concat(e.value)}))}((null===(u=null==n?void 0:n.queryParams)||void 0===u?void 0:u.custom)||[]));j.encodeQueryString&&(y=y.map(D));var v=function(e,t){var n=e;if(t.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+t.join("&")}return n}(function(e,t){return g.test(e)||t?e:"".concat(j.baseUrl).concat(e)}(e,r),y);return j.sortParams&&(v=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=j.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:t,headers:h,url:v}}function F(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(j.baseUrl)):c=s.endpoint||s.baseUrl||e.getModelType(s):c=t.toString(),q(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function N(n,r,o){var i,a;if(void 0===o&&(o=K),!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)]}))),h=((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[u]=r.links,a[d]=r.meta,a[f]=Boolean(r.id),a.type=r.type,a),i);if(r.relationships){var y={},v={},g={};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?(h[t]=i.data,n&&t in n||(g[t]={referenceDef:{model:(i.data instanceof Array?i.data[0].type:null===(o=i.data)||void 0===o?void 0:o.type)||K.type,type:i.data instanceof Array?e.ReferenceType.TO_MANY:e.ReferenceType.TO_ONE}})):h[t]=[]),i&&"links"in i&&(y[t]=i.links),i&&"meta"in i&&(v[t]=i.meta)})),Object.assign(h[t.META_FIELD].fields,g),h[t.META_FIELD][l]=y,h[t.META_FIELD][p]=v}return Object.assign(h,r.attributes)}function H(e){return t.getMeta(e,u,{})}function U(e){return t.getMeta(e,l,{})}function Q(t,n,r){if(!e.getModelCollection(t))throw b("The model needs to be in a collection");var o=U(t);if(!o||!(n in o))throw b("The reference ".concat(n," doesn't have any links"));var i=o[n];if(!i||!(r in i))throw b("Link ".concat(r," doesn't exist on the model"));return i[r]}function B(e){return t.getMeta(e,f,!1)}function J(n,r){var o=n.constructor,i=r?e.modelToDirtyJSON(n):e.modelToJSON(n),a=o.useAutogeneratedIds,s={attributes:i,id:B(n)||a?e.getModelId(n).toString():void 0,type:e.getModelType(n)},c=O(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 G(t,n){return F(e.getModelType(t),B(t)?e.getModelId(t):void 0,void 0,n,void 0,t).url}function z(t,n){var r,o=e.getModelCollection(t),i=null!==(r=null==j?void 0:j.usePatchWhenPossible)&&void 0!==r&&r,a=B(t),s=J(t,i&&a);return(a?i?A:E:C)(G(t,n),{data:s},o,n&&n.networkConfig&&n.networkConfig.headers).then(R(t)).then((function(n){return te(e.getModelType(t)),n}))}function W(n,r){var o=e.getModelCollection(n),i=B(n),a=G(n);return i?function(e,t,n,r){return x({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,f,!1)}(n),o&&o.__removeModel(n)})):(o&&o.__removeModel(n),Promise.resolve())}var V=[u,d,f,l,p];function Y(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=N(O(e),r)),i=n.call(this,a,o)||this;var s=(null==a?void 0:a[t.META_FIELD])||{};return V.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 z(this,e)},r.prototype.destroy=function(e){return W(this,e)},r[_]=!0,r.useAutogeneratedIds=e.useAutogeneratedIds||!1,r}(e)}var K=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t}(Y(e.PureModel));function X(e){if(e instanceof Array)return e;var t=[];return e.forEach?(e.forEach((function(e,n){t.push([n,e])})),t):t}function Z(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 K(N(void 0,i.data))}}}return new e.Bucket.ToOneOrMany(null,n,!0)}var $=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=Z(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&&X(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=Z(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 S(r,t.collection,o,t.views,n)}}}return this.__cache[e]},n}(),ee=[];function te(e){ee=ee.filter((function(t){return!t.types.includes(e)}))}function ne(e){if(e.error)throw e.error;return e}function re(e,n){return t.mapItems(e&&e.included||[],n),t.mapItems(e&&e.data||null,n)}function oe(r){return function(i){function c(e){void 0===e&&(e=[]);var t,n,r=i.call(this,e)||this;return e instanceof Array||!(null==e?void 0:e.cache)||(t=e.cache,n=r,ee.push.apply(ee,t.map((function(e){return Object.assign({collection:n},e)})))),r}return o(c,i),c.prototype.sync=function(e){var t=this;if(!e)return null;var n=re(e,(function(e){return t.__addRecord(e)}));return re(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:m?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:m?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,T(i.url,this,a).then(ne)},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,T(o.url,this,i).then(ne)},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,w(r.sent(),n)]}}))}))},c.prototype.request=function(e,t,n,r){return void 0===t&&(t="GET"),function(e){return x(e)}({url:q(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?W(a,"object"==typeof o?o:void 0):(a&&this.__removeModel(a),te(s),Promise.resolve())},c.prototype.removeAll=function(t){i.prototype.removeAll.call(this,t),te(e.getModelType(t))},c.prototype.reset=function(){i.prototype.reset.call(this),ee.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}))||K,s=N(O(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 K(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 F(e,t,n,r,this)},c.prototype.toJSON=function(){return Object.assign({},i.prototype.toJSON.call(this),{cache:(e=this,ee.filter((function(t){return t.collection===e})).map((function(e){return Object.assign({},e,{collection:void 0})})))});var e},c[_]=!0,c.types=r.types&&r.types.length?r.types.concat(K):[K],c.maxCacheAge=r.maxCacheAge,c.cache=r.cache,c.defaultModel=r.defaultModel||K,c}(r)}function ie(e){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,w(n.sent(),t)]}}))}))},t.prototype.__addFromResponse=function(e){return e.data&&this.add(e.data),e.views.push(this),e},t[_]=!0,t}(e)}var ae,se=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 S(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 ce(e,t){return(e&&t.collection?t.collection.sync(e):e)||{}}function ue(n){return i(i({},n),{data:{data:t.mapItems(n.data,(function(t){return t instanceof e.PureModel?J(t,!0):t}))||void 0}})}!function(e){e.Asc="",e.Desc="-"}(ae||(ae={}));var le=function(e){function t(t){var r,o=e.call(this,t)||this;return(r=o)._config.Response=se,n.header("content-type","application/vnd.api+json")(r),n.parser(ce)(r),n.serializer(ue)(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=le,exports.GenericModel=K,exports.MODEL_LINKS_FIELD=u,exports.MODEL_META_FIELD=d,exports.MODEL_PERSISTED_FIELD=f,exports.MODEL_REF_LINKS_FIELD=l,exports.MODEL_REF_META_FIELD=p,exports.NetworkResponse=se,exports.Response=$,exports.buildUrl=q,exports.config=j,exports.fetchLink=S,exports.fetchModelLink=function(n,r,o){var i=e.getModelCollection(n),a=H(n);if(!a||!(r in a))throw b("Link ".concat(r," doesn't exist on the model"));var s=S(a[r],i,o);return t.getMeta(n,y)?s.then((function(r){var o=t.getMeta(n,v),i=t.getMeta(n,h),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,f,!0),r.replaceData(o)):r})):s},exports.fetchModelRefLink=function(t,n,r,o){var i=e.getModelCollection(t);return S(Q(t,n,r),i,o)},exports.getModelEndpointUrl=G,exports.getModelLinks=H,exports.getModelMeta=function(e){return t.getMeta(e,d,{})},exports.getModelRefLinks=U,exports.getModelRefMeta=function(e){return t.getMeta(e,p,{})},exports.getResponseRawData=function(e){var t,n;return null===(n=null===(t=null==e?void 0:e.__internal)||void 0===t?void 0:t.response)||void 0===n?void 0:n.data},exports.isJsonApiClass=function(e){return _ in e},exports.isModelPersisted=B,exports.jsonapi=function(t){if(e.isModel(t))return Y(t);if(e.isCollection(t))return oe(t);if(e.isView(t))return ie(t);throw new Error("The instance needs to be a model, collection or a view")},exports.jsonapiCollection=function(e){return oe(e)},exports.jsonapiModel=function(e){return Y(e)},exports.jsonapiView=function(e){return ie(e)},exports.modelToJsonApi=J,exports.prepareQuery=F,exports.saveModel=z,exports.saveRelationship=function(n,r,o){var i,a=e.getModelCollection(n),s=Q(n,r,"self"),c=("object"==typeof s?null==s?void 0:s.href:s)||"",u=e.getRefId(n,r),l=null===(i=t.getMeta(n,"fields"))||void 0===i?void 0:i[r],p=(null==l?void 0:l.referenceDef)?l.referenceDef.model:null;return A(c,{data:t.mapItems(u,(function(e){return{id:e.id,type:e.type||p}}))},a,o&&o.networkConfig&&o.networkConfig.headers).then(R(n,r))}; | ||
| //# sourceMappingURL=index.cjs.js.map |
Sorry, the diff of this file is too big to display
| export { jsonapi, jsonapiModel, jsonapiCollection, jsonapiView } from './mixin'; | ||
| export { Response } from './Response'; | ||
| export { NetworkResponse } from './NetworkResponse'; | ||
| export { GenericModel } from './GenericModel'; | ||
| export { fetchModelLink, fetchModelRefLink, getModelLinks, getModelMeta, getModelRefLinks, getModelRefMeta, getModelEndpointUrl, modelToJsonApi, saveRelationship, isModelPersisted, saveModel, } from './helpers/model'; | ||
| export { prepareQuery, buildUrl } from './helpers/url'; | ||
| export { isJsonApiClass } from './helpers/utils'; | ||
| export { getResponseRawData } from './helpers/response'; | ||
| export { BaseJsonapiRequest } from './BaseRequest'; | ||
| export { ICollectionFetchOpts } from './interfaces/ICollectionFetchOpts'; | ||
| export { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| export { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| export { IRawResponse } from './interfaces/IRawResponse'; | ||
| export { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| export { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| export { | ||
| /** | ||
| * @deprecated import directly form `@datx/jsonapi-types`. Will be removed in v3 and renamed to `IResourceObject` | ||
| */ | ||
| IRecord, | ||
| /** | ||
| * @deprecated import directly form `@datx/jsonapi-types`. Will be removed in v3 and renamed to `IDocument` | ||
| */ | ||
| IResponse, | ||
| /** | ||
| * @deprecated import directly form `@datx/jsonapi-types`. Will be removed in v3 and renamed to `IResourceIdentifierObject` | ||
| */ | ||
| IDefinition, } from '@datx/jsonapi-types'; | ||
| export { config, fetchLink, IConfigType } from './NetworkUtils'; | ||
| export { BaseRequest, addInterceptor, cache, method, setUrl, body, query, header, params, fetchReference, encodeQueryString, paramArrayType, serializer, parser, collection, ParamArrayType, CachingStrategy, HttpMethod, IFetchOptions, IHeaders, IInterceptor, INetworkHandler, IPipeOperator, IResponseObject, clearAllCache, clearCacheByType, } from '@datx/network'; | ||
| export { MODEL_LINKS_FIELD, MODEL_META_FIELD, MODEL_PERSISTED_FIELD, MODEL_REF_LINKS_FIELD, MODEL_REF_META_FIELD, } from './consts'; |
| 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 T,header as S,parser as x,serializer as R,paramArrayType as q,encodeQueryString as M,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 H=function(){return H=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},H.apply(this,arguments)};function I(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 U(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(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=e.call(t,a)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function L(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="jsonapiLinks",G="jsonapiRefLinks",W="jsonapiRefMeta",B="jsonapiMeta",J="networkPersisted",Y="jsonapiProp",z="jsonapiQueue",V="jsonapiRelated",K=/^((https?\:)?\/\/)/,X=Symbol("@datx/jsonapi class"),Z="undefined"!=typeof window;function $(t){return new Error("[datx exception] ".concat(t))}function tt(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 et(t,e){return void 0===e&&(e=50),I(this,void 0,void 0,(function(){var n,r,o,i;return U(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}]}}))}))}function nt(t){return X in t}var rt=(Z?"function"==typeof window.fetch&&window.fetch.bind(window):"function"==typeof globalThis.fetch&&globalThis.fetch.bind(globalThis))||void 0,ot={baseUrl:"/",cache:Z?C.CacheFirst:C.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:rt,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=ot.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},ot.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 it(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)||Ht)({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 at(t,n,r,o){var i,a;void 0===r&&(r=!1);var s=(null==n?void 0:n.Response)||Ht;return ot.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=ot.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);(It=It.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 st(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)||Ht,m=ot.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||ot.cache,j=ot.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 at(m,null===(d=t.options)||void 0===d?void 0:d.fetchOptions);var P=function(t,e,n){void 0===n&&(n=Ht);var r=Date.now()-1e3*e,o=It.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 at(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=at(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(it("No cache for this request",t,null==m?void 0:m.collection));if(k===C.CacheFirst)return P?Promise.resolve(P.response):at(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=at(m,null===(_=t.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(A.catch((function(){})),Promise.resolve(E)):A}return Promise.reject(it("Invalid caching strategy",t,null==m?void 0:m.collection))}function ct(t,e,n,r){return st({collection:e,data:void 0,method:"GET",options:n,url:t,views:r})}function ut(t,e,n,r,o){return st({collection:n,data:e,method:"POST",options:r,url:t,views:o})}function lt(t,e,n,r,o){return st({collection:n,data:e,method:"PATCH",options:r,url:t,views:o})}function ft(t,e,n,r,o){return st({collection:n,data:e,method:"PUT",options:r,url:t,views:o})}function dt(t,e,n,r,o){if(void 0===o&&(o=Ht),t){var i="object"==typeof t?t.href:t;if(i)return ct(i,e,n,r)}return Promise.resolve(new o({data:void 0},e))}function pt(e,n){return g.action((function(r){if(r.error)throw r.error;if(204===r.status)return m(e,J,!0),e;if(202===r.status){var o=r.data;return m(o,Y,n),m(o,z,!0),m(o,V,e),o}m(e,J,!0);var i=r.replaceData(e).data;return t(i),i}))}function ht(t,e){void 0===e&&(e="");var n=[];return Object.keys(t).forEach((function(r){t[r]instanceof Array?ot.paramArrayType===E.CommaSeparated?n.push({key:"".concat(e).concat(r),value:t[r].join(",")}):ot.paramArrayType===E.MultipleParams?n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r),value:t}}))):ot.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,ht(t[r],"".concat(r,"."))):n.push({key:"".concat(e).concat(r),value:t[r]})})),n}function vt(t){return ht(t).map((function(t){return"filter[".concat(t.key,"]=").concat(t.value)}))}function yt(t){return encodeURIComponent(t).replace(/%3D/,"=")}function _t(t,e,n,r){var o,i,a,s,c,u,l,f,d,p=n&&n.networkConfig&&n.networkConfig.headers||{},h=[].concat(vt((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)?ot.sortParams&&Array.isArray(f)?["include=".concat(L([],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)||[]));ot.encodeQueryString&&(h=h.map(yt));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 K.test(t)||e?t:"".concat(ot.baseUrl).concat(t)}(t,r),h);return ot.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=ot.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:e,headers:p,url:v}}function gt(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(ot.baseUrl)):c=s.endpoint||s.baseUrl||e(s):c=t.toString(),_t(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function mt(t,e,n){var r,o;if(void 0===n&&(n=Nt),!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[Q]=e.links,o[B]=e.meta,o[J]=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)||Nt.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][G]=u,c[O][W]=l}return Object.assign(c,e.attributes)}function bt(t){return _(t,B,{})}function wt(t){return _(t,Q,{})}function Ot(t,r,o){var i=n(t),a=wt(t);if(!a||!(r in a))throw $("Link ".concat(r," doesn't exist on the model"));var s=dt(a[r],i,o);return _(t,z)?s.then((function(n){var r=_(t,V),o=_(t,Y),i=n.data,a=i&&e(i);return i&&a!==e(t)&&a===e(r)?o?(r[o]=i,n):(m(r,J,!0),n.replaceData(r)):n})):s}function kt(t){return _(t,G,{})}function jt(t,e,r){if(!n(t))throw $("The model needs to be in a collection");var o=kt(t);if(!o||!(e in o))throw $("The reference ".concat(e," doesn't have any links"));var i=o[e];if(!i||!(r in i))throw $("Link ".concat(r," doesn't exist on the model"));return i[r]}function Pt(t,e,r,o){var i=n(t);return dt(jt(t,e,r),i,o)}function At(t){return _(t,W,{})}function Ct(t){return _(t,J,!1)}function Et(t,n){var s=t.constructor,c=n?r(t):o(t),u=s.useAutogeneratedIds,l={attributes:c,id:Ct(t)||u?i(t).toString():void 0,type:e(t)},f=tt(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 Tt(t,n){return gt(e(t),Ct(t)?i(t):void 0,void 0,n,void 0,t).url}function St(t,r){var o,i=n(t),a=null!==(o=null==ot?void 0:ot.usePatchWhenPossible)&&void 0!==o&&o,s=Ct(t),c=Et(t,a&&s);return(s?a?lt:ft:ut)(Tt(t,r),{data:c},i,r&&r.networkConfig&&r.networkConfig.headers).then(pt(t)).then((function(n){return Ut(e(t)),n}))}function xt(t,e){var r=n(t),o=Ct(t),i=Tt(t);return o?function(t,e,n,r){return st({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,J,!1)}(t),r&&r.__removeModel(t)})):(r&&r.__removeModel(t),Promise.resolve())}function Rt(t,e,r){var o,i=n(t),s=jt(t,e,"self"),c=("object"==typeof s?null==s?void 0: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 lt(c,{data:w(u,(function(t){return{id:t.id,type:t.type||f}}))},i,r&&r.networkConfig&&r.networkConfig.headers).then(pt(t,e))}var qt=[Q,B,J,G,W];function Mt(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=mt(tt(t),n)),o=e.call(this,i,r)||this;var a=(null==i?void 0:i[O])||{};return qt.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 St(this,t)},n.prototype.destroy=function(t){return xt(this,t)},n[X]=!0,n.useAutogeneratedIds=t.useAutogeneratedIds||!1,n}(t)}var Nt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return F(e,t),e}(Mt(c));function Dt(t){if(t instanceof Array)return t;var e=[];return t.forEach?(t.forEach((function(t,n){e.push([n,t])})),e):e}function Ft(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 Nt(mt(void 0,o.data))}}}return new f.ToOneOrMany(null,e,!0)}var Ht=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=Ft(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&&Dt(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=Ft(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 dt(r,e.collection,o,e.views,n)}}}return this.__cache[t]},t}(),It=[];function Ut(t){It=It.filter((function(e){return!e.types.includes(t)}))}function Lt(t){return It.filter((function(e){return e.collection===t})).map((function(t){return Object.assign({},t,{collection:void 0})}))}function Qt(t){if(t.error)throw t.error;return t}function Gt(t,e){return w(t&&t.included||[],e),w(t&&t.data||null,e)}function Wt(n){return function(r){function o(t){void 0===t&&(t=[]);var e=r.call(this,t)||this;return t instanceof Array||!(null==t?void 0:t.cache)||function(t,e){It.push.apply(It,t.map((function(t){return Object.assign({collection:e},t)})))}(t.cache,e),e}return F(o,r),o.prototype.sync=function(t){var e=this;if(!t)return null;var n=Gt(t,(function(t){return e.__addRecord(t)}));return Gt(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:Z?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:Z?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,ct(i.url,this,a).then(Qt)},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,ct(o.url,this,i).then(Qt)},o.prototype.getAll=function(t,e,n){return void 0===n&&(n=50),I(this,void 0,void 0,(function(){return U(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,et(r.sent(),n)]}}))}))},o.prototype.request=function(t,e,n,r){return void 0===e&&(e="GET"),function(t){return st(t)}({url:_t(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?xt(s,"object"==typeof o?o:void 0):(s&&this.__removeModel(s),Ut(c),Promise.resolve())},o.prototype.removeAll=function(t){r.prototype.removeAll.call(this,t),Ut(e(t))},o.prototype.reset=function(){r.prototype.reset.call(this),It.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}))||Nt,c=mt(tt(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 Nt(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 gt(t,e,n,r,this)},o.prototype.toJSON=function(){return Object.assign({},r.prototype.toJSON.call(this),{cache:Lt(this)})},o[X]=!0,o.types=n.types&&n.types.length?n.types.concat(Nt):[Nt],o.maxCacheAge=n.maxCacheAge,o.cache=n.cache,o.defaultModel=n.defaultModel||Nt,o}(n)}function Bt(t){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),I(this,void 0,void 0,(function(){return U(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,et(n.sent(),e)]}}))}))},e.prototype.__addFromResponse=function(t){return t.data&&this.add(t.data),t.views.push(this),t},e[X]=!0,e}(t)}function Jt(t){if(h(t))return Mt(t);if(v(t))return Wt(t);if(y(t))return Bt(t);throw new Error("The instance needs to be a model, collection or a view")}function Yt(t){return Mt(t)}function zt(t){return Wt(t)}function Vt(t){return Bt(t)}var Kt,Xt=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 dt(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}(T);function Zt(t){var e,n;return null===(n=null===(e=null==t?void 0:t.__internal)||void 0===e?void 0:e.response)||void 0===n?void 0:n.data}function $t(t,e){return(t&&e.collection?e.collection.sync(t):t)||{}}function te(t){return H(H({},t),{data:{data:w(t.data,(function(t){return t instanceof c?Et(t,!0):t}))||void 0}})}!function(t){t.Asc="",t.Desc="-"}(Kt||(Kt={}));var ee=function(t){function e(e){var n,r=t.call(this,e)||this;return(n=r)._config.Response=Xt,S("content-type","application/vnd.api+json")(n),x($t)(n),R(te)(n),q(E.CommaSeparated)(n),M(!1)(n),r}return F(e,t),e}(N);export{ee as BaseJsonapiRequest,Nt as GenericModel,Q as MODEL_LINKS_FIELD,B as MODEL_META_FIELD,J as MODEL_PERSISTED_FIELD,G as MODEL_REF_LINKS_FIELD,W as MODEL_REF_META_FIELD,Xt as NetworkResponse,Ht as Response,_t as buildUrl,ot as config,dt as fetchLink,Ot as fetchModelLink,Pt as fetchModelRefLink,Tt as getModelEndpointUrl,wt as getModelLinks,bt as getModelMeta,kt as getModelRefLinks,At as getModelRefMeta,Zt as getResponseRawData,nt as isJsonApiClass,Ct as isModelPersisted,Jt as jsonapi,zt as jsonapiCollection,Yt as jsonapiModel,Vt as jsonapiView,Et as modelToJsonApi,gt as prepareQuery,St as saveModel,Rt as saveRelationship}; | ||
| //# sourceMappingURL=index.esm.js.map |
Sorry, the diff of this file is too big to display
| import { View } from '@datx/core'; | ||
| import { IJsonapiCollection } from './IJsonapiCollection'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface ICollectionFetchOpts { | ||
| url: string; | ||
| options?: IRequestOptions; | ||
| data?: object; | ||
| method: string; | ||
| collection?: IJsonapiCollection; | ||
| skipCache?: boolean; | ||
| views?: Array<View>; | ||
| } |
| export interface IFilters { | ||
| [key: string]: string | Array<string> | IFilters; | ||
| } |
| import { Response } from '../Response'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| export interface IGetAllResponse<T extends IJsonapiModel = IJsonapiModel> { | ||
| data: Array<T>; | ||
| responses: Array<Response<T>>; | ||
| lastResponse: Response<T>; | ||
| } |
| export declare type IHeaders = Record<string, string>; |
| import { IModelConstructor, IType, PureCollection, PureModel } from '@datx/core'; | ||
| import { IResponse } from '@datx/jsonapi-types'; | ||
| import { Response } from '../Response'; | ||
| import { IGetAllResponse } from './IGetAllResponse'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IJsonapiCollection extends PureCollection { | ||
| sync<T extends IJsonapiModel = IJsonapiModel>(body?: IResponse): T | Array<T> | null; | ||
| /** | ||
| * Fetch the records with the given type and id | ||
| * | ||
| * @param {string} type Record type | ||
| * @param {string} type Record id | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @returns {Promise<Response>} Resolves with the Response object or rejects with an error | ||
| */ | ||
| fetch<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| /** | ||
| * Fetch the first page of records of the given type | ||
| * | ||
| * @param {string} type Record type | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @returns {Promise<Response>} Resolves with the Response object or rejects with an error | ||
| */ | ||
| fetchAll<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions): Promise<Response<T>>; | ||
| getOne<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| getMany<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions): Promise<Response<T>>; | ||
| getAll<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions, maxRequests?: number): Promise<IGetAllResponse<T>>; | ||
| request<T extends IJsonapiModel = IJsonapiModel>(url: string, method?: string, data?: object, options?: IRequestOptions): Promise<Response<T>>; | ||
| removeOne(type: IType | typeof PureModel, id: string, options?: boolean | IRequestOptions): Promise<void>; | ||
| removeOne(model: PureModel, options?: boolean | IRequestOptions): Promise<void>; | ||
| } |
| import { PureModel } from '@datx/core'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IJsonapiModel extends PureModel { | ||
| save(options?: IRequestOptions): Promise<IJsonapiModel>; | ||
| destroy(options?: IRequestOptions): Promise<void>; | ||
| } |
| import { View } from '@datx/core'; | ||
| import type { IResponse } from '@datx/jsonapi-types'; | ||
| import { Response } from '../Response'; | ||
| import { IGetAllResponse } from './IGetAllResponse'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IJsonapiView<T extends IJsonapiModel = IJsonapiModel> extends View<T> { | ||
| sync(body?: IResponse): T | Array<T> | null; | ||
| getOne(id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| getMany(options?: IRequestOptions): Promise<Response<T>>; | ||
| getAll(options?: IRequestOptions, maxRequests?: number): Promise<IGetAllResponse<T>>; | ||
| } |
| import { Collection } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { IJsonApiObject } from '@datx/jsonapi-types'; | ||
| import type { IResponse } from '@datx/jsonapi-types'; | ||
| import { IHeaders } from './IHeaders'; | ||
| export interface IRawResponse { | ||
| data?: IResponse; | ||
| error?: Error; | ||
| headers?: IResponseHeaders; | ||
| requestHeaders?: IHeaders; | ||
| status?: number; | ||
| jsonapi?: IJsonApiObject; | ||
| collection?: Collection; | ||
| } |
| import { IDocument } from '@datx/jsonapi-types'; | ||
| export declare type IRequest = IDocument; |
| import { IFilters } from './IFilters'; | ||
| import { IHeaders } from './IHeaders'; | ||
| import { CachingStrategy } from '@datx/network'; | ||
| export interface IRequestOptions { | ||
| queryParams?: { | ||
| include?: string | ReadonlyArray<string>; | ||
| filter?: IFilters; | ||
| sort?: string | ReadonlyArray<string>; | ||
| fields?: Record<string, string | ReadonlyArray<string>>; | ||
| custom?: ReadonlyArray<{ | ||
| key: string; | ||
| value: string; | ||
| } | string>; | ||
| }; | ||
| cacheOptions?: { | ||
| cachingStrategy?: CachingStrategy; | ||
| maxAge?: number; | ||
| skipCache?: boolean; | ||
| }; | ||
| networkConfig?: { | ||
| headers?: IHeaders; | ||
| }; | ||
| /** If you don't know what this is for, you should probably not use it */ | ||
| fetchOptions?: object; | ||
| } |
| import type { IError, IJsonApiObject, ILink, IMeta, IRecord } from '@datx/jsonapi-types'; | ||
| export interface IResponse { | ||
| data?: IRecord | Array<IRecord>; | ||
| errors?: Array<IError>; | ||
| included?: Array<IRecord>; | ||
| meta?: IMeta; | ||
| links?: Record<string, ILink>; | ||
| jsonapi?: IJsonApiObject; | ||
| } |
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { IError, IJsonApiObject, ILink } from '@datx/jsonapi-types'; | ||
| import { IHeaders } from './IHeaders'; | ||
| import { IRawResponse } from './IRawResponse'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IResponseInternal { | ||
| meta?: object; | ||
| links?: Record<string, ILink>; | ||
| jsonapi?: IJsonApiObject; | ||
| headers?: IResponseHeaders; | ||
| requestHeaders?: IHeaders; | ||
| error?: Array<IError> | Error; | ||
| status?: number; | ||
| options?: IRequestOptions; | ||
| response: IRawResponse; | ||
| views: Array<View>; | ||
| } |
| import { IRawResponse } from './IRawResponse'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IResponseSnapshot { | ||
| response: Omit<IRawResponse, 'headers'> & { | ||
| headers?: Array<[string, string]>; | ||
| }; | ||
| options?: IRequestOptions; | ||
| } |
| import { ICollectionConstructor, IModelConstructor, IViewConstructor, PureCollection, PureModel } from '@datx/core'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureModel>(Base: IModelConstructor<T>): IModelConstructor<T & IJsonapiModel>; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureCollection>(Base: ICollectionConstructor<T>): ICollectionConstructor<T & IJsonapiCollection>; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureModel>(Base: IViewConstructor<T>): IViewConstructor<IJsonapiModel, T & IJsonapiView>; | ||
| export declare function jsonapiModel<T extends PureModel>(Base: IModelConstructor<T>): IModelConstructor<T & IJsonapiModel>; | ||
| export declare function jsonapiCollection<T extends PureCollection>(Base: ICollectionConstructor<T>): ICollectionConstructor<T & IJsonapiCollection>; | ||
| export declare function jsonapiView<T extends PureModel>(Base: IViewConstructor<T>): IViewConstructor<IJsonapiModel, T & IJsonapiView<IJsonapiModel>>; |
| import { View, PureCollection } from '@datx/core'; | ||
| import { Response as BaseResponse, IResponseObject } from '@datx/network'; | ||
| import { IJsonApiObject, ILink } from '@datx/jsonapi-types'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| export declare class NetworkResponse<T extends IJsonapiModel> extends BaseResponse<T> { | ||
| /** | ||
| * API response metadata | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get meta(): object | undefined; | ||
| /** | ||
| * API response links | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get links(): Record<string, ILink> | undefined; | ||
| /** | ||
| * The JSON API object returned by the server | ||
| * | ||
| * @type {JsonApi.IJsonApiObject} | ||
| * @memberOf Response | ||
| */ | ||
| get jsonapi(): IJsonApiObject | undefined; | ||
| /** | ||
| * First data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| first?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Previous data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| prev?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Next data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| next?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Last data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| last?: () => Promise<NetworkResponse<T>>; | ||
| get views(): Array<View>; | ||
| constructor(response: IResponseObject, collection?: PureCollection, overrideData?: T, views?: Array<View>); | ||
| /** | ||
| * Function called when a link is being fetched. The returned value is cached | ||
| * | ||
| * @private | ||
| * @param {string} name Link name | ||
| * @returns Promise that resolves with a Response object | ||
| * | ||
| * @memberOf NetworkResponse | ||
| */ | ||
| private __fetchLink; | ||
| } |
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { ILink, IResponse } from '@datx/jsonapi-types'; | ||
| import { ICollectionFetchOpts } from './interfaces/ICollectionFetchOpts'; | ||
| import { IHeaders } from './interfaces/IHeaders'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IRawResponse } from './interfaces/IRawResponse'; | ||
| import { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| import { Response as LibResponse } from './Response'; | ||
| import { CachingStrategy, ParamArrayType } from '@datx/network'; | ||
| export declare type FetchType = (method: string, url: string, body?: object, requestHeaders?: IHeaders, fetchOptions?: object) => Promise<IRawResponse>; | ||
| export declare type CollectionFetchType = <T extends IJsonapiModel>(options: ICollectionFetchOpts) => Promise<LibResponse<T>>; | ||
| export interface IResponseObject { | ||
| data: IResponse; | ||
| error?: Error; | ||
| headers: IResponseHeaders; | ||
| requestHeaders: IHeaders; | ||
| status: number; | ||
| } | ||
| export interface IConfigType { | ||
| baseFetch: FetchType; | ||
| baseUrl: string; | ||
| cache: CachingStrategy; | ||
| maxCacheAge: number; | ||
| defaultFetchOptions: Record<string, any>; | ||
| fetchReference?: typeof fetch; | ||
| paramArrayType: ParamArrayType; | ||
| encodeQueryString?: boolean; | ||
| onError(IResponseObject: any): IResponseObject; | ||
| transformRequest(options: ICollectionFetchOpts): ICollectionFetchOpts; | ||
| transformResponse(response: IRawResponse): IRawResponse; | ||
| usePatchWhenPossible: boolean; | ||
| /** | ||
| * Enable stable sort of url search params using `URLSearchParams.sort()` method. | ||
| * It will also sort include params using `Array.sort()` method. | ||
| * @default false | ||
| */ | ||
| sortParams?: boolean; | ||
| } | ||
| export declare const config: IConfigType; | ||
| export declare function libFetch<T extends IJsonapiModel = IJsonapiModel>(options: ICollectionFetchOpts): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to get data from the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function read<T extends IJsonapiModel = IJsonapiModel>(url: string, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to create data on the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function create<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to update data on the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function update<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to update data on the server with put | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function put<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to remove data from the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function remove<T extends IJsonapiModel = IJsonapiModel>(url: string, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * Fetch a link from the server | ||
| * | ||
| * @export | ||
| * @param {JsonApi.ILink} link Link URL or a link object | ||
| * @param {IJsonapiCollection} collection Store that will be used to save the response | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<LibResponse>} Response promise | ||
| */ | ||
| export declare function fetchLink<T extends IJsonapiModel = IJsonapiModel>(link: ILink, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>, ResponseConstructor?: typeof LibResponse): Promise<LibResponse<T>>; | ||
| export declare function handleResponse<T extends IJsonapiModel = IJsonapiModel>(record: T, prop?: string): (response: LibResponse<T>) => T; |
| import { BaseRequest } from '@datx/network'; | ||
| export declare function isJsonapi(): (pipeline: BaseRequest) => void; | ||
| export declare function filter(filter: Record<string, string | object>): (pipeline: BaseRequest) => void; | ||
| export declare enum Direction { | ||
| Asc = "", | ||
| Desc = "-" | ||
| } | ||
| export declare function sort(...params: Array<string | [string, Direction]>): (pipeline: BaseRequest) => void; | ||
| export declare function page(page: Record<string, string | object>): (pipeline: BaseRequest) => void; | ||
| export declare function include(...included: Array<string>): (pipeline: BaseRequest) => void; | ||
| export declare function sparse(sparse: Record<string, Array<string>>): (pipeline: BaseRequest) => void; |
| { | ||
| "name": "@datx/jsonapi", | ||
| "version": "2.5.0", | ||
| "description": "DatX mixin for JSON API support", | ||
| "main": "./index.cjs.js", | ||
| "module": "./index.esm.js", | ||
| "typings": "./index.d.ts", | ||
| "sideEffects": false, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/infinum/datx.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/infinum/datx/issues" | ||
| }, | ||
| "homepage": "https://github.com/infinum/datx#readme", | ||
| "author": "Infinum JavaScript Team <hello@infinum.com>", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "datx", | ||
| "mobx", | ||
| "jsonapi" | ||
| ], | ||
| "dependencies": { | ||
| "@datx/core": "2.5.0", | ||
| "@datx/network": "2.5.0", | ||
| "@datx/utils": "2.5.0" | ||
| }, | ||
| "gitHead": "ea4e53d6d63c73eb6f328ecafb88e704b655dc76" | ||
| } |
+110
| # @datx/jsonapi | ||
| DatX is an opinionated data store for use with the [MobX](https://mobx.js.org/) state management library. It features support for simple observable property definition, references to other models and first-class TypeScript support. | ||
| `@datx/jsonapi` is a datx mixin that adds [JSON API](https://jsonapi.org/) support. | ||
| --- | ||
| ## Basic usage | ||
| ```typescript | ||
| import { Collection, Model, Attribute } from '@datx/core'; | ||
| import { jsonapiCollection, jsonapiModel } from '@datx/jsonapi'; | ||
| import { computed } from 'mobx'; | ||
| class Person extends jsonapiModel(Model) { | ||
| public static type = 'person'; // Unique name of the model class | ||
| @Attribute() | ||
| public name: string; // A normal observable property without a default value | ||
| @Attribute() | ||
| public surname: string; | ||
| @Attribute({ toOne: Person }) | ||
| public spouse?: Person; // A reference to a Person model | ||
| @computed | ||
| public get fullName() { | ||
| // Standard MobX computed props | ||
| return `${this.name} ${this.surname}`; | ||
| } | ||
| } | ||
| class AppData extends jsonapiCollection(Collection) { | ||
| public static types = [Person]; // A list of models available in the collection | ||
| } | ||
| const store = new AppData(); | ||
| const john = store.add(new Person({ name: 'John', surname: 'Smith' })); // Add a model instance to the store | ||
| const jane = store.add({ name: 'Jane', surname: 'Smith', spouse: john }, Person); // Add a model to the store | ||
| await john.save(); // POST to the server | ||
| const people = await store.fetchAll(Person); // Get all people from the server | ||
| ``` | ||
| ## Getting started | ||
| Note: `@datx/jsonapi` has a peer dependency to `mobx@^4.2.0` or `mobx@^5.5.0`, so don't forget to install the latest MobX version: | ||
| ```bash | ||
| npm install --save @datx/jsonapi mobx | ||
| ``` | ||
| - [Basic configuration](https://datx.dev/docs/jsonapi/jsonapi-basic-configuration) | ||
| - [Network configuration](https://datx.dev/docs/jsonapi/jsonapi-network-configuration) | ||
| - [Network usage](https://datx.dev/docs/jsonapi/jsonapi-network-usage) | ||
| - [Spec compliance](https://datx.dev/docs/jsonapi/jsonapi-spec-compliance) | ||
| ### Polyfilling | ||
| The lib makes use of the following features that are not yet available everywhere. Based on your browser support, you might want to polyfill them: | ||
| - [Symbol.for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) | ||
| - [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) | ||
| - [Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) | ||
| - [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
| - [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) | ||
| [How to add the polyfills](https://datx.dev/docs/troubleshooting/known-issues#the-library-doesnt-work-in-internet-explorer-11). | ||
| Note: Fetch API is not included in the polyfills mentioned in the Troubleshooting page. Instead, you need to add it as a separate library. If you don't have any special requirements (like server-side rendering), you can use the [window.fetch polyfill](https://github.com/github/fetch#installation). | ||
| ## API reference | ||
| - [Model](https://datx.dev/docs/jsonapi/jsonapi-model) | ||
| - [Collection](https://datx.dev/docs/jsonapi/jsonapi-collection) | ||
| - [View](https://datx.dev/docs/jsonapi/jsonapi-view) | ||
| - [Response](https://datx.dev/docs/jsonapi/jsonapi-response) | ||
| - [Config](https://datx.dev/docs/jsonapi/jsonapi-config) | ||
| - [Utils](https://datx.dev/docs/jsonapi/jsonapi-utils) | ||
| - [TypeScript Interfaces](https://datx.dev/docs/jsonapi/jsonapi-typescript-interfaces) | ||
| ## Troubleshooting | ||
| Having issues with the library? Check out the [troubleshooting](https://datx.dev/docs/troubleshooting/known-issues) page or [open](https://github.com/infinum/datx/issues/new/choose) an issue. | ||
| --- | ||
| [](https://travis-ci.org/infinum/datx) | ||
| [](https://badge.fury.io/js/@datx/jsonapi) | ||
| [](https://david-dm.org/infinum/datx?path=packages/@datx/jsonapi) | ||
| [](https://david-dm.org/infinum/datx?path=packages/@datx/jsonapi#info=devDependencies) | ||
| ## License | ||
| The [MIT License](LICENSE) | ||
| ## Credits | ||
| datx-jsonapi is maintained and sponsored by | ||
| [Infinum](https://www.infinum.com). | ||
| <p align="center"> | ||
| <a href='https://infinum.com'> | ||
| <picture> | ||
| <source srcset="https://assets.infinum.com/brand/logo/static/white.svg" media="(prefers-color-scheme: dark)"> | ||
| <img src="https://assets.infinum.com/brand/logo/static/default.svg"> | ||
| </picture> | ||
| </a> | ||
| </p> |
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import type { IError, IJsonApiObject, ILink } from '@datx/jsonapi-types'; | ||
| import { IHeaders } from './interfaces/IHeaders'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IRawResponse } from './interfaces/IRawResponse'; | ||
| import { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| import { IResponseInternal } from './interfaces/IResponseInternal'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| declare type IAsync<T extends IJsonapiModel> = Promise<Response<T>>; | ||
| export declare class Response<T extends IJsonapiModel, P = IAsync<T>> { | ||
| private readonly __data; | ||
| protected __internal: IResponseInternal; | ||
| /** | ||
| * API response metadata | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get meta(): object | undefined; | ||
| /** | ||
| * API response links | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get links(): Record<string, ILink> | undefined; | ||
| /** | ||
| * The JSON API object returned by the server | ||
| * | ||
| * @type {JsonApi.IJsonApiObject} | ||
| * @memberOf Response | ||
| */ | ||
| get jsonapi(): IJsonApiObject | undefined; | ||
| /** | ||
| * Headers received from the API call | ||
| * | ||
| * @type {IResponseHeaders} | ||
| * @memberOf Response | ||
| */ | ||
| get headers(): IResponseHeaders | undefined; | ||
| /** | ||
| * Headers sent to the server | ||
| * | ||
| * @type {IHeaders} | ||
| * @memberOf Response | ||
| */ | ||
| get requestHeaders(): IHeaders | undefined; | ||
| /** | ||
| * Request error | ||
| * | ||
| * @type {(Array<JsonApi.IError>|Error)} | ||
| * @memberOf Response | ||
| */ | ||
| get error(): Array<IError> | Error | undefined; | ||
| /** | ||
| * First data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| first?: () => P; | ||
| /** | ||
| * Previous data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| prev?: () => P; | ||
| /** | ||
| * Next data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| next?: () => P; | ||
| /** | ||
| * Last data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| last?: () => P; | ||
| /** | ||
| * Received HTTP status | ||
| * | ||
| * @type {number} | ||
| * @memberOf Response | ||
| */ | ||
| get status(): number | undefined; | ||
| get views(): Array<View>; | ||
| /** | ||
| * Related Store | ||
| * | ||
| * @type {IJsonapiCollection} | ||
| * @memberOf Response | ||
| */ | ||
| readonly collection?: IJsonapiCollection; | ||
| /** | ||
| * Cache used for the link requests | ||
| * | ||
| * @protected | ||
| * @type {Record<string, P<Response>>} | ||
| * @memberOf Response | ||
| */ | ||
| protected readonly __cache: Record<string, () => P>; | ||
| constructor(response: IRawResponse, collection?: IJsonapiCollection, options?: IRequestOptions, overrideData?: T | Array<T>, views?: Array<View>); | ||
| get isSuccess(): boolean; | ||
| get data(): T | Array<T> | null; | ||
| private __updateInternal; | ||
| /** | ||
| * Replace the response record with a different record. Used to replace a record while keeping the same reference | ||
| * | ||
| * @param {PureModel} data New data | ||
| * @returns {Response} | ||
| * | ||
| * @memberOf Response | ||
| */ | ||
| replaceData(data: T): Response<T, P>; | ||
| clone(): Response<T>; | ||
| get snapshot(): IResponseSnapshot; | ||
| update(response: IRawResponse, views?: Array<View>): Response<T, P>; | ||
| /** | ||
| * Function called when a link is being fetched. The returned value is cached | ||
| * | ||
| * @protected | ||
| * @param {string} name Link name | ||
| * @returns P that resolves with a Response object | ||
| * | ||
| * @memberOf Response | ||
| */ | ||
| protected __fetchLink(name: string): () => P; | ||
| } | ||
| export {}; |
+21
| MIT License | ||
| Copyright (c) 2021 Infinum | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
+61
-9
| { | ||
| "name": "@datx/jsonapi", | ||
| "version": "2.5.0-beta.11", | ||
| "version": "2.5.0", | ||
| "description": "DatX mixin for JSON API support", | ||
| "main": "./index.cjs.js", | ||
| "module": "./index.esm.js", | ||
| "typings": "./index.d.ts", | ||
| "main": "dist/index.cjs.js", | ||
| "module": "dist/index.esm.js", | ||
| "typings": "dist/index.d.ts", | ||
| "sideEffects": false, | ||
@@ -24,8 +24,60 @@ "repository": { | ||
| ], | ||
| "devDependencies": { | ||
| "@rollup/plugin-commonjs": "^22.0.0", | ||
| "@rollup/plugin-node-resolve": "^13.3.0", | ||
| "@rollup/plugin-typescript": "^8.3.2", | ||
| "@types/jest": "^28.1.1", | ||
| "@types/lodash": "^4.14.182", | ||
| "@types/node": "^18.7.6", | ||
| "@types/uuid": "^8.3.4", | ||
| "isomorphic-fetch": "^3.0.0", | ||
| "jest": "^28.1.1", | ||
| "lodash": "^4.17.21", | ||
| "rollup": "^2.75.6", | ||
| "rollup-plugin-copy": "^3.4.0", | ||
| "rollup-plugin-exclude-dependencies-from-bundle": "^1.1.22", | ||
| "rollup-plugin-generate-package-json": "^3.2.0", | ||
| "rollup-plugin-terser": "^7.0.2", | ||
| "ts-jest": "^28.0.4", | ||
| "tslib": "~2.4.0", | ||
| "typescript": "~4.7.3" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest --coverage", | ||
| "test:watch": "jest --watch --coverage", | ||
| "prepublish": "npm run build", | ||
| "build": "rollup -c", | ||
| "watch": "rollup -c --watch", | ||
| "ts:check": "tsc --noEmit" | ||
| }, | ||
| "jest": { | ||
| "coveragePathIgnorePatterns": [ | ||
| "/test/", | ||
| "/node_modules/" | ||
| ], | ||
| "moduleFileExtensions": [ | ||
| "ts", | ||
| "js" | ||
| ], | ||
| "testEnvironment": "jsdom", | ||
| "testRegex": "test/(.*).test.ts$", | ||
| "globals": { | ||
| "ts-jest": { | ||
| "diagnostics": { | ||
| "warnOnly": true | ||
| } | ||
| } | ||
| }, | ||
| "preset": "ts-jest", | ||
| "testMatch": null, | ||
| "setupFilesAfterEnv": [ | ||
| "./test/setup.ts" | ||
| ] | ||
| }, | ||
| "dependencies": { | ||
| "@datx/core": "2.5.0-beta.11", | ||
| "@datx/network": "2.5.0-beta.11", | ||
| "@datx/utils": "2.5.0-beta.11" | ||
| }, | ||
| "gitHead": "d13cfe72a482e7e0307bd8276d8bfeeee5d12137" | ||
| "@datx/core": "2.5.0", | ||
| "@datx/jsonapi-types": "2.5.0", | ||
| "@datx/network": "2.5.0", | ||
| "@datx/utils": "2.5.0" | ||
| } | ||
| } |
+3
-3
@@ -13,6 +13,6 @@ # @datx/jsonapi | ||
| import { Collection, Model, Attribute } from '@datx/core'; | ||
| import { jsonapi } from '@datx/jsonapi'; | ||
| import { jsonapiCollection, jsonapiModel } from '@datx/jsonapi'; | ||
| import { computed } from 'mobx'; | ||
| class Person extends jsonapi(Model) { | ||
| class Person extends jsonapiModel(Model) { | ||
| public static type = 'person'; // Unique name of the model class | ||
@@ -36,3 +36,3 @@ | ||
| class AppData extends jsonapi(Collection) { | ||
| class AppData extends jsonapiCollection(Collection) { | ||
| public static types = [Person]; // A list of models available in the collection | ||
@@ -39,0 +39,0 @@ } |
| import { BaseRequest as BaseNetworkRequest } from '@datx/network'; | ||
| export declare class BaseJsonapiRequest extends BaseNetworkRequest { | ||
| constructor(baseUrl: string); | ||
| } |
-24
| import { IType } from '@datx/core'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { Response } from './Response'; | ||
| import { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export interface ICache { | ||
| response: Response<IJsonapiModel>; | ||
| time: number; | ||
| types: Array<IType>; | ||
| url: string; | ||
| } | ||
| export interface ICacheInternal { | ||
| response: IResponseSnapshot; | ||
| collection?: IJsonapiCollection; | ||
| time: number; | ||
| types: Array<IType>; | ||
| url: string; | ||
| } | ||
| export declare function saveCache(url: string, response: Response<IJsonapiModel>): void; | ||
| export declare function getCache(url: string, maxAge: number, ResponseConstructor?: typeof Response): ICache | undefined; | ||
| export declare function clearAllCache(): void; | ||
| export declare function clearCacheByType(type: IType): void; | ||
| export declare function getCacheByCollection(collection: IJsonapiCollection): Array<Omit<ICacheInternal, 'collection'>>; | ||
| export declare function saveCacheForCollection(cacheItems: Array<Omit<ICacheInternal, 'collection'>>, collection: IJsonapiCollection): void; |
-10
| export declare const MODEL_LINKS_FIELD = "jsonapiLinks"; | ||
| export declare const MODEL_REF_LINKS_FIELD = "jsonapiRefLinks"; | ||
| export declare const MODEL_REF_META_FIELD = "jsonapiRefMeta"; | ||
| export declare const MODEL_META_FIELD = "jsonapiMeta"; | ||
| export declare const MODEL_PERSISTED_FIELD = "networkPersisted"; | ||
| export declare const MODEL_PROP_FIELD = "jsonapiProp"; | ||
| export declare const MODEL_QUEUE_FIELD = "jsonapiQueue"; | ||
| export declare const MODEL_RELATED_FIELD = "jsonapiRelated"; | ||
| export declare const URL_REGEX: RegExp; | ||
| export declare const DATX_JSONAPI_CLASS: unique symbol; |
| import { ICollectionConstructor, PureCollection } from '@datx/core'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export declare function decorateCollection(BaseClass: typeof PureCollection): ICollectionConstructor<PureCollection & IJsonapiCollection>; |
| import { PureModel } from '@datx/core'; | ||
| export declare function decorateModel(BaseClass: typeof PureModel): typeof PureModel; |
| import { IViewConstructor, View } from '@datx/core'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| export declare function decorateView<U>(BaseClass: typeof View): IViewConstructor<IJsonapiModel, U & IJsonapiView>; |
| import { PureModel } from '@datx/core'; | ||
| export declare const DecoratedModel: typeof PureModel; | ||
| export declare class GenericModel extends DecoratedModel { | ||
| } |
| import { PureModel, IModelConstructor } from '@datx/core'; | ||
| import { IRawModel } from '@datx/utils'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { IRequestOptions } from '../interfaces/IRequestOptions'; | ||
| import { ILink, IRecord } from '../interfaces/JsonApi'; | ||
| import { Response } from '../Response'; | ||
| export declare function flattenModel(classRefs: any): null; | ||
| export declare function flattenModel(classRefs: any, data?: IRecord, modelClass?: IModelConstructor): IRawModel; | ||
| export declare function getModelMeta(model: PureModel): Record<string, any>; | ||
| export declare function getModelLinks(model: PureModel): Record<string, ILink>; | ||
| export declare function fetchModelLink<T extends IJsonapiModel = IJsonapiModel>(model: PureModel, key: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| export declare function getModelRefLinks(model: PureModel): Record<string, Record<string, ILink>>; | ||
| export declare function fetchModelRefLink<T extends IJsonapiModel = IJsonapiModel>(model: PureModel, ref: string, key: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| export declare function getModelRefMeta(model: PureModel): Record<string, any>; | ||
| export declare function isModelPersisted(model: PureModel): boolean; | ||
| export declare function modelToJsonApi(model: IJsonapiModel, onlyDirty?: boolean): IRecord; | ||
| export declare function getModelEndpointUrl(model: IJsonapiModel, options?: IRequestOptions): string; | ||
| export declare function saveModel(model: IJsonapiModel, options?: IRequestOptions): Promise<IJsonapiModel>; | ||
| export declare function removeModel<T extends IJsonapiModel>(model: T, options?: IRequestOptions): Promise<void>; | ||
| export declare function saveRelationship<T extends IJsonapiModel>(model: T, ref: string, options?: IRequestOptions): Promise<T>; |
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { Response } from '../Response'; | ||
| export declare function getResponseRawData<TModel extends IJsonapiModel = IJsonapiModel>(response: Response<TModel>): import("../interfaces/JsonApi").IResponse | undefined; |
| import { IType, PureCollection } from '@datx/core'; | ||
| import { IHeaders } from '../interfaces/IHeaders'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { IRequestOptions } from '../interfaces/IRequestOptions'; | ||
| import { IRequest } from '../interfaces/JsonApi'; | ||
| export declare function buildUrl(url: string, data?: IRequest, options?: IRequestOptions, containsBase?: boolean): { | ||
| url: string; | ||
| data?: object; | ||
| headers: IHeaders; | ||
| }; | ||
| export declare function prepareQuery(type: IType, id?: number | string, data?: IRequest, options?: IRequestOptions, collection?: PureCollection, model?: IJsonapiModel): { | ||
| url: string; | ||
| data?: object; | ||
| headers: IHeaders; | ||
| }; |
| import { Collection, IReferenceDefinition, PureModel, View } from '@datx/core'; | ||
| import { IGetAllResponse } from '../interfaces/IGetAllResponse'; | ||
| import { IJsonapiModel } from '../interfaces/IJsonapiModel'; | ||
| import { Response } from '../Response'; | ||
| export declare const isBrowser: boolean; | ||
| /** | ||
| * Returns the value if it's not a function. If it's a function | ||
| * it calls it. | ||
| * | ||
| * @export | ||
| * @template T | ||
| * @param {(T|(() => T))} target can be anything or function | ||
| * @returns {T} value | ||
| */ | ||
| export declare function getValue<T>(target: T | (() => T)): T; | ||
| export declare function error(message: string): Error; | ||
| export declare function getModelClassRefs(type: typeof PureModel | PureModel): Record<string, IReferenceDefinition>; | ||
| export declare function getAllResponses<M extends IJsonapiModel = IJsonapiModel>(response: Response<M>, maxRequests?: number): Promise<IGetAllResponse<M>>; | ||
| export declare function isJsonApiClass(type: typeof PureModel | typeof Collection | typeof View): boolean; |
| "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(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}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="jsonapiLinks",l="jsonapiRefLinks",d="jsonapiRefMeta",p="jsonapiMeta",f="networkPersisted",h="jsonapiProp",y="jsonapiQueue",v="jsonapiRelated",g=/^((https?\:)?\/\/)/,_=Symbol("@datx/jsonapi class"),m="undefined"!=typeof window;function b(e){return new Error("[datx exception] ".concat(e))}function O(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 w(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 M={baseUrl:"/",cache:m?n.CachingStrategy.CacheFirst:n.CachingStrategy.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:m&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||(null===globalThis||void 0===globalThis?void 0:globalThis.fetch),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=M.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},M.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 j(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)||Z)({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 k(n,r,o,i){var a,s;void 0===o&&(o=!1);var c=(null==r?void 0:r.Response)||Z;return M.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=M.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);($=$.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 P(e){var t,r,o,i,a,s,c,u,l,d,p,f,h,y,v,g,_,m=(null===(r=null===(t=e.options)||void 0===t?void 0:t.fetchOptions)||void 0===r?void 0:r.Response)||Z,b=M.transformRequest(e),O=null===(o=null==b?void 0:b.collection)||void 0===o?void 0:o.constructor,w=O&&O.cache,P="GET"===b.method.toUpperCase(),x=(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)||w||M.cache,T=M.maxCacheAge||1/0;if(O&&void 0!==O.maxCacheAge&&(T=O.maxCacheAge),void 0!==(null===(l=null===(u=e.options)||void 0===u?void 0:u.cacheOptions)||void 0===l?void 0:l.maxAge)&&(T=null===(p=null===(d=e.options)||void 0===d?void 0:d.cacheOptions)||void 0===p?void 0:p.maxAge),x===n.CachingStrategy.NetworkOnly)return k(b,null===(f=e.options)||void 0===f?void 0:f.fetchOptions);var C=function(e,t,n){void 0===n&&(n=Z);var r=Date.now()-1e3*t,o=$.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}}}(b.url,T,m);if(x===n.CachingStrategy.NetworkFirst)return k(b,null===(h=e.options)||void 0===h?void 0:h.fetchOptions,!0).catch((function(e){if(C)return C.response;throw e}));if(x===n.CachingStrategy.StaleWhileRevalidate){var A=k(b,null===(y=e.options)||void 0===y?void 0:y.fetchOptions,!0);return C?(A.catch((function(){})),Promise.resolve(C.response)):A}if(x===n.CachingStrategy.CacheOnly)return C?Promise.resolve(C.response):Promise.reject(j("No cache for this request",e,null==b?void 0:b.collection));if(x===n.CachingStrategy.CacheFirst)return C?Promise.resolve(C.response):k(b,null===(v=e.options)||void 0===v?void 0:v.fetchOptions,!0);if(x===n.CachingStrategy.StaleAndUpdate){var E=null===(g=null==C?void 0:C.response)||void 0===g?void 0:g.clone();return A=k(b,null===(_=e.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(A.catch((function(){})),Promise.resolve(E)):A}return Promise.reject(j("Invalid caching strategy",e,null==b?void 0:b.collection))}function x(e,t,n,r){return P({collection:t,data:void 0,method:"GET",options:n,url:e,views:r})}function T(e,t,n,r,o){return P({collection:n,data:t,method:"POST",options:r,url:e,views:o})}function C(e,t,n,r,o){return P({collection:n,data:t,method:"PATCH",options:r,url:e,views:o})}function A(e,t,n,r,o){return P({collection:n,data:t,method:"PUT",options:r,url:e,views:o})}function E(e,t,n,r,o){if(void 0===o&&(o=Z),e){var i="object"==typeof e?e.href:e;if(i)return x(i,t,n,r)}return Promise.resolve(new o({data:void 0},t))}function S(n,r){return t.mobx.action((function(o){if(o.error)throw o.error;if(204===o.status)return t.setMeta(n,f,!0),n;if(202===o.status){var i=o.data;return t.setMeta(i,h,r),t.setMeta(i,y,!0),t.setMeta(i,v,n),i}t.setMeta(n,f,!0);var a=o.replaceData(n).data;return e.commitModel(a),a}))}function R(e,t){void 0===t&&(t="");var r=[];return Object.keys(e).forEach((function(o){e[o]instanceof Array?M.paramArrayType===n.ParamArrayType.CommaSeparated?r.push({key:"".concat(t).concat(o),value:e[o].join(",")}):M.paramArrayType===n.ParamArrayType.MultipleParams?r.push.apply(r,e[o].map((function(e){return{key:"".concat(t).concat(o),value:e}}))):M.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,R(e[o],"".concat(o,"."))):r.push({key:"".concat(t).concat(o),value:e[o]})})),r}function I(e){return R(e).map((function(e){return"filter[".concat(e.key,"]=").concat(e.value)}))}function L(e){return encodeURIComponent(e).replace(/%3D/,"=")}function D(e,t,n,r){var o,i,a,s,u,l,d,p,f,h=n&&n.networkConfig&&n.networkConfig.headers||{},y=[].concat(I((null===(o=null==n?void 0:n.queryParams)||void 0===o?void 0:o.filter)||{}),(f=null===(i=null==n?void 0:n.queryParams)||void 0===i?void 0:i.sort)?["sort=".concat(f)]:[],(p=null===(a=null==n?void 0:n.queryParams)||void 0===a?void 0:a.include)?M.sortParams&&Array.isArray(p)?["include=".concat(c([],p,!0).sort())]:["include=".concat(p)]:[],(l=(null===(s=null==n?void 0:n.queryParams)||void 0===s?void 0:s.fields)||{},d=[],Object.keys(l).forEach((function(e){d.push("fields[".concat(e,"]=").concat(l[e]))})),d),function(e){return e.map((function(e){return"string"==typeof e?e:"".concat(e.key,"=").concat(e.value)}))}((null===(u=null==n?void 0:n.queryParams)||void 0===u?void 0:u.custom)||[]));M.encodeQueryString&&(y=y.map(L));var v=function(e,t){var n=e;if(t.length){var r=-1===n.indexOf("?")?"?":"&";n+=r+t.join("&")}return n}(function(e,t){return g.test(e)||t?e:"".concat(M.baseUrl).concat(e)}(e,r),y);return M.sortParams&&(v=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=M.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:t,headers:h,url:v}}function q(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(M.baseUrl)):c=s.endpoint||s.baseUrl||e.getModelType(s):c=t.toString(),D(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function F(n,r,o){var i,a;if(void 0===o&&(o=Y),!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)]}))),h=((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[u]=r.links,a[p]=r.meta,a[f]=Boolean(r.id),a.type=r.type,a),i);if(r.relationships){var y={},v={},g={};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?(h[t]=i.data,n&&t in n||(g[t]={referenceDef:{model:(i.data instanceof Array?i.data[0].type:null===(o=i.data)||void 0===o?void 0:o.type)||Y.type,type:i.data instanceof Array?e.ReferenceType.TO_MANY:e.ReferenceType.TO_ONE}})):h[t]=[]),i&&"links"in i&&(y[t]=i.links),i&&"meta"in i&&(v[t]=i.meta)})),Object.assign(h[t.META_FIELD].fields,g),h[t.META_FIELD][l]=y,h[t.META_FIELD][d]=v}return Object.assign(h,r.attributes)}function N(e){return t.getMeta(e,u,{})}function H(e){return t.getMeta(e,l,{})}function U(t,n,r){if(!e.getModelCollection(t))throw b("The model needs to be in a collection");var o=H(t);if(!o||!(n in o))throw b("The reference ".concat(n," doesn't have any links"));var i=o[n];if(!i||!(r in i))throw b("Link ".concat(r," doesn't exist on the model"));return i[r]}function Q(e){return t.getMeta(e,f,!1)}function B(n,r){var o=n.constructor,i=r?e.modelToDirtyJSON(n):e.modelToJSON(n),a=o.useAutogeneratedIds,s={attributes:i,id:Q(n)||a?e.getModelId(n).toString():void 0,type:e.getModelType(n)},c=O(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 J(t,n){return q(e.getModelType(t),Q(t)?e.getModelId(t):void 0,void 0,n,void 0,t).url}function G(t,n){var r,o=e.getModelCollection(t),i=null!==(r=null==M?void 0:M.usePatchWhenPossible)&&void 0!==r&&r,a=Q(t),s=B(t,i&&a);return(a?i?C:A:T)(J(t,n),{data:s},o,n&&n.networkConfig&&n.networkConfig.headers).then(S(t)).then((function(n){return ee(e.getModelType(t)),n}))}function z(n,r){var o=e.getModelCollection(n),i=Q(n),a=J(n);return i?function(e,t,n,r){return P({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,f,!1)}(n),o&&o.__removeModel(n)})):(o&&o.__removeModel(n),Promise.resolve())}var W=[u,p,f,l,d];function V(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=F(O(e),r)),i=n.call(this,a,o)||this;var s=(null==a?void 0:a[t.META_FIELD])||{};return W.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 G(this,e)},r.prototype.destroy=function(e){return z(this,e)},r[_]=!0,r.useAutogeneratedIds=e.useAutogeneratedIds||!1,r}(e)}var Y=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t}(V(e.PureModel));function K(e){if(e instanceof Array)return e;var t=[];return e.forEach?(e.forEach((function(e,n){t.push([n,e])})),t):t}function X(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 Y(F(void 0,i.data))}}}return new e.Bucket.ToOneOrMany(null,n,!0)}var Z=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=X(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&&K(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=X(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 E(r,t.collection,o,t.views,n)}}}return this.__cache[e]},n}(),$=[];function ee(e){$=$.filter((function(t){return!t.types.includes(e)}))}function te(e){if(e.error)throw e.error;return e}function ne(e,n){return t.mapItems(e&&e.included||[],n),t.mapItems(e&&e.data||null,n)}function re(r){return function(i){function c(e){void 0===e&&(e=[]);var t,n,r=i.call(this,e)||this;return e instanceof Array||!(null==e?void 0:e.cache)||(t=e.cache,n=r,$.push.apply($,t.map((function(e){return Object.assign({collection:n},e)})))),r}return o(c,i),c.prototype.sync=function(e){var t=this;if(!e)return null;var n=ne(e,(function(e){return t.__addRecord(e)}));return ne(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:m?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:m?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,x(i.url,this,a).then(te)},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,x(o.url,this,i).then(te)},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,w(r.sent(),n)]}}))}))},c.prototype.request=function(e,t,n,r){return void 0===t&&(t="GET"),function(e){return P(e)}({url:D(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?z(a,"object"==typeof o?o:void 0):(a&&this.__removeModel(a),ee(s),Promise.resolve())},c.prototype.removeAll=function(t){i.prototype.removeAll.call(this,t),ee(e.getModelType(t))},c.prototype.reset=function(){i.prototype.reset.call(this),$.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}))||Y,s=F(O(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 Y(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 q(e,t,n,r,this)},c.prototype.toJSON=function(){return Object.assign({},i.prototype.toJSON.call(this),{cache:(e=this,$.filter((function(t){return t.collection===e})).map((function(e){return Object.assign({},e,{collection:void 0})})))});var e},c[_]=!0,c.types=r.types&&r.types.length?r.types.concat(Y):[Y],c.maxCacheAge=r.maxCacheAge,c.cache=r.cache,c.defaultModel=r.defaultModel||Y,c}(r)}function oe(e){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,w(n.sent(),t)]}}))}))},t.prototype.__addFromResponse=function(e){return e.data&&this.add(e.data),e.views.push(this),e},t[_]=!0,t}(e)}var ie,ae=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 E(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 se(e,t){return(e&&t.collection?t.collection.sync(e):e)||{}}function ce(n){return i(i({},n),{data:{data:t.mapItems(n.data,(function(t){return t instanceof e.PureModel?B(t,!0):t}))||void 0}})}!function(e){e.Asc="",e.Desc="-"}(ie||(ie={}));var ue=function(e){function t(t){var r,o=e.call(this,t)||this;return(r=o)._config.Response=ae,n.header("content-type","application/vnd.api+json")(r),n.parser(se)(r),n.serializer(ce)(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=ue,exports.GenericModel=Y,exports.MODEL_LINKS_FIELD=u,exports.MODEL_META_FIELD=p,exports.MODEL_PERSISTED_FIELD=f,exports.MODEL_REF_LINKS_FIELD=l,exports.MODEL_REF_META_FIELD=d,exports.NetworkResponse=ae,exports.Response=Z,exports.buildUrl=D,exports.config=M,exports.fetchLink=E,exports.fetchModelLink=function(n,r,o){var i=e.getModelCollection(n),a=N(n);if(!a||!(r in a))throw b("Link ".concat(r," doesn't exist on the model"));var s=E(a[r],i,o);return t.getMeta(n,y)?s.then((function(r){var o=t.getMeta(n,v),i=t.getMeta(n,h),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,f,!0),r.replaceData(o)):r})):s},exports.fetchModelRefLink=function(t,n,r,o){var i=e.getModelCollection(t);return E(U(t,n,r),i,o)},exports.getModelEndpointUrl=J,exports.getModelLinks=N,exports.getModelMeta=function(e){return t.getMeta(e,p,{})},exports.getModelRefLinks=H,exports.getModelRefMeta=function(e){return t.getMeta(e,d,{})},exports.getResponseRawData=function(e){var t,n;return null===(n=null===(t=null==e?void 0:e.__internal)||void 0===t?void 0:t.response)||void 0===n?void 0:n.data},exports.isJsonApiClass=function(e){return _ in e},exports.isModelPersisted=Q,exports.jsonapi=function(t){if(e.isModel(t))return V(t);if(e.isCollection(t))return re(t);if(e.isView(t))return oe(t);throw new Error("The instance needs to be a model, collection or a view")},exports.jsonapiCollection=function(e){return re(e)},exports.jsonapiModel=function(e){return V(e)},exports.jsonapiView=function(e){return oe(e)},exports.modelToJsonApi=B,exports.prepareQuery=q,exports.saveModel=G,exports.saveRelationship=function(n,r,o){var i,a=e.getModelCollection(n),s=U(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 C(c,{data:t.mapItems(u,(function(e){return{id:e.id,type:e.type||d}}))},a,o&&o.networkConfig&&o.networkConfig.headers).then(S(n,r))}; | ||
| //# sourceMappingURL=index.cjs.js.map |
Sorry, the diff of this file is too big to display
-20
| export { jsonapi, jsonapiModel, jsonapiCollection, jsonapiView } from './mixin'; | ||
| export { Response } from './Response'; | ||
| export { NetworkResponse } from './NetworkResponse'; | ||
| export { GenericModel } from './GenericModel'; | ||
| export { fetchModelLink, fetchModelRefLink, getModelLinks, getModelMeta, getModelRefLinks, getModelRefMeta, getModelEndpointUrl, modelToJsonApi, saveRelationship, isModelPersisted, saveModel, } from './helpers/model'; | ||
| export { prepareQuery, buildUrl } from './helpers/url'; | ||
| export { isJsonApiClass } from './helpers/utils'; | ||
| export { getResponseRawData } from './helpers/response'; | ||
| export { BaseJsonapiRequest } from './BaseRequest'; | ||
| export { ICollectionFetchOpts } from './interfaces/ICollectionFetchOpts'; | ||
| export { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| export { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| export { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| export { IRawResponse } from './interfaces/IRawResponse'; | ||
| export { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| export { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| export { IResponse, IRecord, IDefinition } from './interfaces/JsonApi'; | ||
| export { config, fetchLink, IConfigType } from './NetworkUtils'; | ||
| export { BaseRequest, addInterceptor, cache, method, setUrl, body, query, header, params, fetchReference, encodeQueryString, paramArrayType, serializer, parser, collection, ParamArrayType, CachingStrategy, HttpMethod, IFetchOptions, IHeaders, IInterceptor, INetworkHandler, IPipeOperator, IResponseObject, clearAllCache, clearCacheByType, } from '@datx/network'; | ||
| export { MODEL_LINKS_FIELD, MODEL_META_FIELD, MODEL_PERSISTED_FIELD, MODEL_REF_LINKS_FIELD, MODEL_REF_META_FIELD, } from './consts'; |
| 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 T,header as S,parser as x,serializer as R,paramArrayType as q,encodeQueryString as M,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 H=function(){return H=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},H.apply(this,arguments)};function I(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 U(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(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=e.call(t,a)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function L(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="jsonapiLinks",G="jsonapiRefLinks",W="jsonapiRefMeta",B="jsonapiMeta",J="networkPersisted",Y="jsonapiProp",z="jsonapiQueue",V="jsonapiRelated",K=/^((https?\:)?\/\/)/,X=Symbol("@datx/jsonapi class"),Z="undefined"!=typeof window;function $(t){return new Error("[datx exception] ".concat(t))}function tt(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 et(t,e){return void 0===e&&(e=50),I(this,void 0,void 0,(function(){var n,r,o,i;return U(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}]}}))}))}function nt(t){return X in t}var rt={baseUrl:"/",cache:Z?C.CacheFirst:C.NetworkOnly,maxCacheAge:1/0,defaultFetchOptions:{headers:{"content-type":"application/vnd.api+json"}},encodeQueryString:!1,sortParams:!1,fetchReference:Z&&"fetch"in window&&"function"==typeof window.fetch&&window.fetch.bind(window)||(null===globalThis||void 0===globalThis?void 0:globalThis.fetch),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=rt.defaultFetchOptions.headers||{},i=Object.assign({},o,r),a=Object.assign({},rt.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 ot(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)||Ft)({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 it(t,n,r,o){var i,a;void 0===r&&(r=!1);var s=(null==n?void 0:n.Response)||Ft;return rt.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=rt.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);(Ht=Ht.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 at(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)||Ft,m=rt.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||rt.cache,j=rt.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 it(m,null===(d=t.options)||void 0===d?void 0:d.fetchOptions);var P=function(t,e,n){void 0===n&&(n=Ft);var r=Date.now()-1e3*e,o=Ht.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 it(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=it(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(ot("No cache for this request",t,null==m?void 0:m.collection));if(k===C.CacheFirst)return P?Promise.resolve(P.response):it(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=it(m,null===(_=t.options)||void 0===_?void 0:_.fetchOptions,!0,E),E?(A.catch((function(){})),Promise.resolve(E)):A}return Promise.reject(ot("Invalid caching strategy",t,null==m?void 0:m.collection))}function st(t,e,n,r){return at({collection:e,data:void 0,method:"GET",options:n,url:t,views:r})}function ct(t,e,n,r,o){return at({collection:n,data:e,method:"POST",options:r,url:t,views:o})}function ut(t,e,n,r,o){return at({collection:n,data:e,method:"PATCH",options:r,url:t,views:o})}function lt(t,e,n,r,o){return at({collection:n,data:e,method:"PUT",options:r,url:t,views:o})}function ft(t,e,n,r,o){if(void 0===o&&(o=Ft),t){var i="object"==typeof t?t.href:t;if(i)return st(i,e,n,r)}return Promise.resolve(new o({data:void 0},e))}function dt(e,n){return g.action((function(r){if(r.error)throw r.error;if(204===r.status)return m(e,J,!0),e;if(202===r.status){var o=r.data;return m(o,Y,n),m(o,z,!0),m(o,V,e),o}m(e,J,!0);var i=r.replaceData(e).data;return t(i),i}))}function pt(t,e){void 0===e&&(e="");var n=[];return Object.keys(t).forEach((function(r){t[r]instanceof Array?rt.paramArrayType===E.CommaSeparated?n.push({key:"".concat(e).concat(r),value:t[r].join(",")}):rt.paramArrayType===E.MultipleParams?n.push.apply(n,t[r].map((function(t){return{key:"".concat(e).concat(r),value:t}}))):rt.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,pt(t[r],"".concat(r,"."))):n.push({key:"".concat(e).concat(r),value:t[r]})})),n}function ht(t){return pt(t).map((function(t){return"filter[".concat(t.key,"]=").concat(t.value)}))}function vt(t){return encodeURIComponent(t).replace(/%3D/,"=")}function yt(t,e,n,r){var o,i,a,s,c,u,l,f,d,p=n&&n.networkConfig&&n.networkConfig.headers||{},h=[].concat(ht((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)?rt.sortParams&&Array.isArray(f)?["include=".concat(L([],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)||[]));rt.encodeQueryString&&(h=h.map(vt));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 K.test(t)||e?t:"".concat(rt.baseUrl).concat(t)}(t,r),h);return rt.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=rt.encodeQueryString?o.toString():decodeURIComponent(o.toString());return"".concat(n,"?").concat(i)}(v)),{data:e,headers:p,url:v}}function _t(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(rt.baseUrl)):c=s.endpoint||s.baseUrl||e(s):c=t.toString(),yt(n?"".concat(c,"/").concat(n):"".concat(c),r,o,u)}function gt(t,e,n){var r,o;if(void 0===n&&(n=Mt),!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[Q]=e.links,o[B]=e.meta,o[J]=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)||Mt.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][G]=u,c[O][W]=l}return Object.assign(c,e.attributes)}function mt(t){return _(t,B,{})}function bt(t){return _(t,Q,{})}function wt(t,r,o){var i=n(t),a=bt(t);if(!a||!(r in a))throw $("Link ".concat(r," doesn't exist on the model"));var s=ft(a[r],i,o);return _(t,z)?s.then((function(n){var r=_(t,V),o=_(t,Y),i=n.data,a=i&&e(i);return i&&a!==e(t)&&a===e(r)?o?(r[o]=i,n):(m(r,J,!0),n.replaceData(r)):n})):s}function Ot(t){return _(t,G,{})}function kt(t,e,r){if(!n(t))throw $("The model needs to be in a collection");var o=Ot(t);if(!o||!(e in o))throw $("The reference ".concat(e," doesn't have any links"));var i=o[e];if(!i||!(r in i))throw $("Link ".concat(r," doesn't exist on the model"));return i[r]}function jt(t,e,r,o){var i=n(t);return ft(kt(t,e,r),i,o)}function Pt(t){return _(t,W,{})}function At(t){return _(t,J,!1)}function Ct(t,n){var s=t.constructor,c=n?r(t):o(t),u=s.useAutogeneratedIds,l={attributes:c,id:At(t)||u?i(t).toString():void 0,type:e(t)},f=tt(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 Et(t,n){return _t(e(t),At(t)?i(t):void 0,void 0,n,void 0,t).url}function Tt(t,r){var o,i=n(t),a=null!==(o=null==rt?void 0:rt.usePatchWhenPossible)&&void 0!==o&&o,s=At(t),c=Ct(t,a&&s);return(s?a?ut:lt:ct)(Et(t,r),{data:c},i,r&&r.networkConfig&&r.networkConfig.headers).then(dt(t)).then((function(n){return It(e(t)),n}))}function St(t,e){var r=n(t),o=At(t),i=Et(t);return o?function(t,e,n,r){return at({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,J,!1)}(t),r&&r.__removeModel(t)})):(r&&r.__removeModel(t),Promise.resolve())}function xt(t,e,r){var o,i=n(t),s=kt(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 ut(c,{data:w(u,(function(t){return{id:t.id,type:t.type||f}}))},i,r&&r.networkConfig&&r.networkConfig.headers).then(dt(t,e))}var Rt=[Q,B,J,G,W];function qt(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=gt(tt(t),n)),o=e.call(this,i,r)||this;var a=(null==i?void 0:i[O])||{};return Rt.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 Tt(this,t)},n.prototype.destroy=function(t){return St(this,t)},n[X]=!0,n.useAutogeneratedIds=t.useAutogeneratedIds||!1,n}(t)}var Mt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return F(e,t),e}(qt(c));function Nt(t){if(t instanceof Array)return t;var e=[];return t.forEach?(t.forEach((function(t,n){e.push([n,t])})),e):e}function Dt(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 Mt(gt(void 0,o.data))}}}return new f.ToOneOrMany(null,e,!0)}var Ft=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=Dt(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&&Nt(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=Dt(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 ft(r,e.collection,o,e.views,n)}}}return this.__cache[t]},t}(),Ht=[];function It(t){Ht=Ht.filter((function(e){return!e.types.includes(t)}))}function Ut(t){return Ht.filter((function(e){return e.collection===t})).map((function(t){return Object.assign({},t,{collection:void 0})}))}function Lt(t){if(t.error)throw t.error;return t}function Qt(t,e){return w(t&&t.included||[],e),w(t&&t.data||null,e)}function Gt(n){return function(r){function o(t){void 0===t&&(t=[]);var e=r.call(this,t)||this;return t instanceof Array||!(null==t?void 0:t.cache)||function(t,e){Ht.push.apply(Ht,t.map((function(t){return Object.assign({collection:e},t)})))}(t.cache,e),e}return F(o,r),o.prototype.sync=function(t){var e=this;if(!t)return null;var n=Qt(t,(function(t){return e.__addRecord(t)}));return Qt(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:Z?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:Z?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,st(i.url,this,a).then(Lt)},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,st(o.url,this,i).then(Lt)},o.prototype.getAll=function(t,e,n){return void 0===n&&(n=50),I(this,void 0,void 0,(function(){return U(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,et(r.sent(),n)]}}))}))},o.prototype.request=function(t,e,n,r){return void 0===e&&(e="GET"),function(t){return at(t)}({url:yt(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?St(s,"object"==typeof o?o:void 0):(s&&this.__removeModel(s),It(c),Promise.resolve())},o.prototype.removeAll=function(t){r.prototype.removeAll.call(this,t),It(e(t))},o.prototype.reset=function(){r.prototype.reset.call(this),Ht.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}))||Mt,c=gt(tt(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 Mt(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 _t(t,e,n,r,this)},o.prototype.toJSON=function(){return Object.assign({},r.prototype.toJSON.call(this),{cache:Ut(this)})},o[X]=!0,o.types=n.types&&n.types.length?n.types.concat(Mt):[Mt],o.maxCacheAge=n.maxCacheAge,o.cache=n.cache,o.defaultModel=n.defaultModel||Mt,o}(n)}function Wt(t){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),I(this,void 0,void 0,(function(){return U(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,et(n.sent(),e)]}}))}))},e.prototype.__addFromResponse=function(t){return t.data&&this.add(t.data),t.views.push(this),t},e[X]=!0,e}(t)}function Bt(t){if(h(t))return qt(t);if(v(t))return Gt(t);if(y(t))return Wt(t);throw new Error("The instance needs to be a model, collection or a view")}function Jt(t){return qt(t)}function Yt(t){return Gt(t)}function zt(t){return Wt(t)}var Vt,Kt=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 ft(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}(T);function Xt(t){var e,n;return null===(n=null===(e=null==t?void 0:t.__internal)||void 0===e?void 0:e.response)||void 0===n?void 0:n.data}function Zt(t,e){return(t&&e.collection?e.collection.sync(t):t)||{}}function $t(t){return H(H({},t),{data:{data:w(t.data,(function(t){return t instanceof c?Ct(t,!0):t}))||void 0}})}!function(t){t.Asc="",t.Desc="-"}(Vt||(Vt={}));var te=function(t){function e(e){var n,r=t.call(this,e)||this;return(n=r)._config.Response=Kt,S("content-type","application/vnd.api+json")(n),x(Zt)(n),R($t)(n),q(E.CommaSeparated)(n),M(!1)(n),r}return F(e,t),e}(N);export{te as BaseJsonapiRequest,Mt as GenericModel,Q as MODEL_LINKS_FIELD,B as MODEL_META_FIELD,J as MODEL_PERSISTED_FIELD,G as MODEL_REF_LINKS_FIELD,W as MODEL_REF_META_FIELD,Kt as NetworkResponse,Ft as Response,yt as buildUrl,rt as config,ft as fetchLink,wt as fetchModelLink,jt as fetchModelRefLink,Et as getModelEndpointUrl,bt as getModelLinks,mt as getModelMeta,Ot as getModelRefLinks,Pt as getModelRefMeta,Xt as getResponseRawData,nt as isJsonApiClass,At as isModelPersisted,Bt as jsonapi,Yt as jsonapiCollection,Jt as jsonapiModel,zt as jsonapiView,Ct as modelToJsonApi,_t as prepareQuery,Tt as saveModel,xt as saveRelationship}; | ||
| //# sourceMappingURL=index.esm.js.map |
Sorry, the diff of this file is too big to display
| import { View } from '@datx/core'; | ||
| import { IJsonapiCollection } from './IJsonapiCollection'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface ICollectionFetchOpts { | ||
| url: string; | ||
| options?: IRequestOptions; | ||
| data?: object; | ||
| method: string; | ||
| collection?: IJsonapiCollection; | ||
| skipCache?: boolean; | ||
| views?: Array<View>; | ||
| } |
| export interface IFilters { | ||
| [key: string]: string | Array<string> | IFilters; | ||
| } |
| import { Response } from '../Response'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| export interface IGetAllResponse<T extends IJsonapiModel = IJsonapiModel> { | ||
| data: Array<T>; | ||
| responses: Array<Response<T>>; | ||
| lastResponse: Response<T>; | ||
| } |
| export declare type IHeaders = Record<string, string>; |
| import { IModelConstructor, IType, PureCollection, PureModel } from '@datx/core'; | ||
| import { Response } from '../Response'; | ||
| import { IGetAllResponse } from './IGetAllResponse'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| import { IResponse } from './JsonApi'; | ||
| export interface IJsonapiCollection extends PureCollection { | ||
| sync<T extends IJsonapiModel = IJsonapiModel>(body?: IResponse): T | Array<T> | null; | ||
| /** | ||
| * Fetch the records with the given type and id | ||
| * | ||
| * @param {string} type Record type | ||
| * @param {string} type Record id | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @returns {Promise<Response>} Resolves with the Response object or rejects with an error | ||
| */ | ||
| fetch<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| /** | ||
| * Fetch the first page of records of the given type | ||
| * | ||
| * @param {string} type Record type | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @returns {Promise<Response>} Resolves with the Response object or rejects with an error | ||
| */ | ||
| fetchAll<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions): Promise<Response<T>>; | ||
| getOne<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| getMany<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions): Promise<Response<T>>; | ||
| getAll<T extends IJsonapiModel = IJsonapiModel>(type: IType | IModelConstructor<T>, options?: IRequestOptions, maxRequests?: number): Promise<IGetAllResponse<T>>; | ||
| request<T extends IJsonapiModel = IJsonapiModel>(url: string, method?: string, data?: object, options?: IRequestOptions): Promise<Response<T>>; | ||
| removeOne(type: IType | typeof PureModel, id: string, options?: boolean | IRequestOptions): Promise<void>; | ||
| removeOne(model: PureModel, options?: boolean | IRequestOptions): Promise<void>; | ||
| } |
| import { PureModel } from '@datx/core'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IJsonapiModel extends PureModel { | ||
| save(options?: IRequestOptions): Promise<IJsonapiModel>; | ||
| destroy(options?: IRequestOptions): Promise<void>; | ||
| } |
| import { View } from '@datx/core'; | ||
| import { Response } from '../Response'; | ||
| import { IGetAllResponse } from './IGetAllResponse'; | ||
| import { IJsonapiModel } from './IJsonapiModel'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| import { IResponse } from './JsonApi'; | ||
| export interface IJsonapiView<T extends IJsonapiModel = IJsonapiModel> extends View<T> { | ||
| sync(body?: IResponse): T | Array<T> | null; | ||
| getOne(id: string, options?: IRequestOptions): Promise<Response<T>>; | ||
| getMany(options?: IRequestOptions): Promise<Response<T>>; | ||
| getAll(options?: IRequestOptions, maxRequests?: number): Promise<IGetAllResponse<T>>; | ||
| } |
| import { Collection } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { IHeaders } from './IHeaders'; | ||
| import { IJsonApiObject, IResponse } from './JsonApi'; | ||
| export interface IRawResponse { | ||
| data?: IResponse; | ||
| error?: Error; | ||
| headers?: IResponseHeaders; | ||
| requestHeaders?: IHeaders; | ||
| status?: number; | ||
| jsonapi?: IJsonApiObject; | ||
| collection?: Collection; | ||
| } |
| import { IFilters } from './IFilters'; | ||
| import { IHeaders } from './IHeaders'; | ||
| import { CachingStrategy } from '@datx/network'; | ||
| export interface IRequestOptions { | ||
| queryParams?: { | ||
| include?: string | ReadonlyArray<string>; | ||
| filter?: IFilters; | ||
| sort?: string | ReadonlyArray<string>; | ||
| fields?: Record<string, string | ReadonlyArray<string>>; | ||
| custom?: ReadonlyArray<{ | ||
| key: string; | ||
| value: string; | ||
| } | string>; | ||
| }; | ||
| cacheOptions?: { | ||
| cachingStrategy?: CachingStrategy; | ||
| maxAge?: number; | ||
| skipCache?: boolean; | ||
| }; | ||
| networkConfig?: { | ||
| headers?: IHeaders; | ||
| }; | ||
| /** If you don't know what this is for, you should probably not use it */ | ||
| fetchOptions?: object; | ||
| } |
| import { IJsonapiModel } from "./IJsonapiModel"; | ||
| export declare type IResponseData<TModel extends IJsonapiModel = IJsonapiModel> = TModel | Array<TModel> | null; |
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { IHeaders } from './IHeaders'; | ||
| import { IRawResponse } from './IRawResponse'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| import { IError, IJsonApiObject, ILink } from './JsonApi'; | ||
| export interface IResponseInternal { | ||
| meta?: object; | ||
| links?: Record<string, ILink>; | ||
| jsonapi?: IJsonApiObject; | ||
| headers?: IResponseHeaders; | ||
| requestHeaders?: IHeaders; | ||
| error?: Array<IError> | Error; | ||
| status?: number; | ||
| options?: IRequestOptions; | ||
| response: IRawResponse; | ||
| views: Array<View>; | ||
| } |
| import { IRawResponse } from './IRawResponse'; | ||
| import { IRequestOptions } from './IRequestOptions'; | ||
| export interface IResponseSnapshot { | ||
| response: Omit<IRawResponse, 'headers'> & { | ||
| headers?: Array<[string, string]>; | ||
| }; | ||
| options?: IRequestOptions; | ||
| } |
| interface IDefinition { | ||
| id?: string; | ||
| type: string; | ||
| } | ||
| interface IJsonApiObject { | ||
| version?: string; | ||
| meta?: Record<string, any>; | ||
| } | ||
| declare type ILink = string | { | ||
| href: string; | ||
| meta: Record<string, any>; | ||
| }; | ||
| interface IError { | ||
| id?: string | number; | ||
| links?: { | ||
| about: ILink; | ||
| }; | ||
| status?: number; | ||
| code?: string; | ||
| title?: string; | ||
| detail?: string; | ||
| source?: { | ||
| pointer?: string; | ||
| parameter?: string; | ||
| }; | ||
| meta?: Record<string, any>; | ||
| } | ||
| interface IRelationship { | ||
| data?: IDefinition | Array<IDefinition> | null; | ||
| links?: Record<string, ILink>; | ||
| meta?: Record<string, any>; | ||
| } | ||
| interface IRecord extends IDefinition { | ||
| attributes?: Record<string, any>; | ||
| relationships?: Record<string, IRelationship>; | ||
| links?: Record<string, ILink>; | ||
| meta?: Record<string, any>; | ||
| } | ||
| interface IResponse { | ||
| data?: IRecord | Array<IRecord>; | ||
| errors?: Array<IError>; | ||
| included?: Array<IRecord>; | ||
| meta?: Record<string, any>; | ||
| links?: Record<string, ILink>; | ||
| jsonapi?: IJsonApiObject; | ||
| } | ||
| declare type IRequest = IResponse; | ||
| export { IDefinition, IJsonApiObject, ILink, IError, IRelationship, IRecord, IResponse, IRequest }; |
-13
| import { ICollectionConstructor, IModelConstructor, IViewConstructor, PureCollection, PureModel } from '@datx/core'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IJsonapiView } from './interfaces/IJsonapiView'; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureModel>(Base: IModelConstructor<T>): IModelConstructor<T & IJsonapiModel>; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureCollection>(Base: ICollectionConstructor<T>): ICollectionConstructor<T & IJsonapiCollection>; | ||
| /** @deprecated Use `jsonapiModel`, `jsonapiCollection` or `jsonapiView` instead */ | ||
| export declare function jsonapi<T extends PureModel>(Base: IViewConstructor<T>): IViewConstructor<IJsonapiModel, T & IJsonapiView>; | ||
| export declare function jsonapiModel<T extends PureModel>(Base: IModelConstructor<T>): IModelConstructor<T & IJsonapiModel>; | ||
| export declare function jsonapiCollection<T extends PureCollection>(Base: ICollectionConstructor<T>): ICollectionConstructor<T & IJsonapiCollection>; | ||
| export declare function jsonapiView<T extends PureModel>(Base: IViewConstructor<T>): IViewConstructor<IJsonapiModel, T & IJsonapiView<IJsonapiModel>>; |
| import { View, PureCollection } from '@datx/core'; | ||
| import { Response as BaseResponse, IResponseObject } from '@datx/network'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IJsonApiObject, ILink } from './interfaces/JsonApi'; | ||
| export declare class NetworkResponse<T extends IJsonapiModel> extends BaseResponse<T> { | ||
| /** | ||
| * API response metadata | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get meta(): object | undefined; | ||
| /** | ||
| * API response links | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get links(): Record<string, ILink> | undefined; | ||
| /** | ||
| * The JSON API object returned by the server | ||
| * | ||
| * @type {JsonApi.IJsonApiObject} | ||
| * @memberOf Response | ||
| */ | ||
| get jsonapi(): IJsonApiObject | undefined; | ||
| /** | ||
| * First data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| first?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Previous data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| prev?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Next data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| next?: () => Promise<NetworkResponse<T>>; | ||
| /** | ||
| * Last data page | ||
| * | ||
| * @type {Promise<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| last?: () => Promise<NetworkResponse<T>>; | ||
| get views(): Array<View>; | ||
| constructor(response: IResponseObject, collection?: PureCollection, overrideData?: T, views?: Array<View>); | ||
| /** | ||
| * Function called when a link is being fetched. The returned value is cached | ||
| * | ||
| * @private | ||
| * @param {string} name Link name | ||
| * @returns Promise that resolves with a Response object | ||
| * | ||
| * @memberOf NetworkResponse | ||
| */ | ||
| private __fetchLink; | ||
| } |
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { ICollectionFetchOpts } from './interfaces/ICollectionFetchOpts'; | ||
| import { IHeaders } from './interfaces/IHeaders'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IRawResponse } from './interfaces/IRawResponse'; | ||
| import { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| import { ILink, IResponse } from './interfaces/JsonApi'; | ||
| import { Response as LibResponse } from './Response'; | ||
| import { CachingStrategy, ParamArrayType } from '@datx/network'; | ||
| export declare type FetchType = (method: string, url: string, body?: object, requestHeaders?: IHeaders, fetchOptions?: object) => Promise<IRawResponse>; | ||
| export declare type CollectionFetchType = <T extends IJsonapiModel>(options: ICollectionFetchOpts) => Promise<LibResponse<T>>; | ||
| export interface IResponseObject { | ||
| data: IResponse; | ||
| error?: Error; | ||
| headers: IResponseHeaders; | ||
| requestHeaders: IHeaders; | ||
| status: number; | ||
| } | ||
| export interface IConfigType { | ||
| baseFetch: FetchType; | ||
| baseUrl: string; | ||
| cache: CachingStrategy; | ||
| maxCacheAge: number; | ||
| defaultFetchOptions: Record<string, any>; | ||
| fetchReference?: typeof fetch; | ||
| paramArrayType: ParamArrayType; | ||
| encodeQueryString?: boolean; | ||
| onError(IResponseObject: any): IResponseObject; | ||
| transformRequest(options: ICollectionFetchOpts): ICollectionFetchOpts; | ||
| transformResponse(response: IRawResponse): IRawResponse; | ||
| usePatchWhenPossible: boolean; | ||
| /** | ||
| * Enable stable sort of url search params using `URLSearchParams.sort()` method. | ||
| * It will also sort include params using `Array.sort()` method. | ||
| * @default false | ||
| */ | ||
| sortParams?: boolean; | ||
| } | ||
| export declare const config: IConfigType; | ||
| export declare function libFetch<T extends IJsonapiModel = IJsonapiModel>(options: ICollectionFetchOpts): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to get data from the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function read<T extends IJsonapiModel = IJsonapiModel>(url: string, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to create data on the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function create<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to update data on the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function update<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to update data on the server with put | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {object} [data] Request body | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function put<T extends IJsonapiModel = IJsonapiModel>(url: string, data?: object, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * API call used to remove data from the server | ||
| * | ||
| * @export | ||
| * @param {IJsonapiCollection} collection Related collection | ||
| * @param {string} url API call URL | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<Response>} Resolves with a Response object | ||
| */ | ||
| export declare function remove<T extends IJsonapiModel = IJsonapiModel>(url: string, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>): Promise<LibResponse<T>>; | ||
| /** | ||
| * Fetch a link from the server | ||
| * | ||
| * @export | ||
| * @param {JsonApi.ILink} link Link URL or a link object | ||
| * @param {IJsonapiCollection} collection Store that will be used to save the response | ||
| * @param {IRequestOptions} [options] Server options | ||
| * @param {Array<View>} [views] Request view | ||
| * @returns {Promise<LibResponse>} Response promise | ||
| */ | ||
| export declare function fetchLink<T extends IJsonapiModel = IJsonapiModel>(link: ILink, collection?: IJsonapiCollection, options?: IRequestOptions, views?: Array<View>, ResponseConstructor?: typeof LibResponse): Promise<LibResponse<T>>; | ||
| export declare function handleResponse<T extends IJsonapiModel = IJsonapiModel>(record: T, prop?: string): (response: LibResponse<T>) => T; |
| import { BaseRequest } from '@datx/network'; | ||
| export declare function isJsonapi(): (pipeline: BaseRequest) => void; | ||
| export declare function filter(filter: Record<string, string | object>): (pipeline: BaseRequest) => void; | ||
| export declare enum Direction { | ||
| Asc = "", | ||
| Desc = "-" | ||
| } | ||
| export declare function sort(...params: Array<string | [string, Direction]>): (pipeline: BaseRequest) => void; | ||
| export declare function page(page: Record<string, string | object>): (pipeline: BaseRequest) => void; | ||
| export declare function include(...included: Array<string>): (pipeline: BaseRequest) => void; | ||
| export declare function sparse(sparse: Record<string, Array<string>>): (pipeline: BaseRequest) => void; |
-135
| import { View } from '@datx/core'; | ||
| import { IResponseHeaders } from '@datx/utils'; | ||
| import { IHeaders } from './interfaces/IHeaders'; | ||
| import { IJsonapiModel } from './interfaces/IJsonapiModel'; | ||
| import { IRawResponse } from './interfaces/IRawResponse'; | ||
| import { IRequestOptions } from './interfaces/IRequestOptions'; | ||
| import { IError, IJsonApiObject, ILink } from './interfaces/JsonApi'; | ||
| import { IResponseInternal } from './interfaces/IResponseInternal'; | ||
| import { IJsonapiCollection } from './interfaces/IJsonapiCollection'; | ||
| import { IResponseSnapshot } from './interfaces/IResponseSnapshot'; | ||
| declare type IAsync<T extends IJsonapiModel> = Promise<Response<T>>; | ||
| export declare class Response<T extends IJsonapiModel, P = IAsync<T>> { | ||
| private readonly __data; | ||
| protected __internal: IResponseInternal; | ||
| /** | ||
| * API response metadata | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get meta(): object | undefined; | ||
| /** | ||
| * API response links | ||
| * | ||
| * @type {object} | ||
| * @memberOf Response | ||
| */ | ||
| get links(): Record<string, ILink> | undefined; | ||
| /** | ||
| * The JSON API object returned by the server | ||
| * | ||
| * @type {JsonApi.IJsonApiObject} | ||
| * @memberOf Response | ||
| */ | ||
| get jsonapi(): IJsonApiObject | undefined; | ||
| /** | ||
| * Headers received from the API call | ||
| * | ||
| * @type {IResponseHeaders} | ||
| * @memberOf Response | ||
| */ | ||
| get headers(): IResponseHeaders | undefined; | ||
| /** | ||
| * Headers sent to the server | ||
| * | ||
| * @type {IHeaders} | ||
| * @memberOf Response | ||
| */ | ||
| get requestHeaders(): IHeaders | undefined; | ||
| /** | ||
| * Request error | ||
| * | ||
| * @type {(Array<JsonApi.IError>|Error)} | ||
| * @memberOf Response | ||
| */ | ||
| get error(): Array<IError> | Error | undefined; | ||
| /** | ||
| * First data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| first?: () => P; | ||
| /** | ||
| * Previous data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| prev?: () => P; | ||
| /** | ||
| * Next data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| next?: () => P; | ||
| /** | ||
| * Last data page | ||
| * | ||
| * @type {P<Response>} | ||
| * @memberOf Response | ||
| */ | ||
| last?: () => P; | ||
| /** | ||
| * Received HTTP status | ||
| * | ||
| * @type {number} | ||
| * @memberOf Response | ||
| */ | ||
| get status(): number | undefined; | ||
| get views(): Array<View>; | ||
| /** | ||
| * Related Store | ||
| * | ||
| * @type {IJsonapiCollection} | ||
| * @memberOf Response | ||
| */ | ||
| readonly collection?: IJsonapiCollection; | ||
| /** | ||
| * Cache used for the link requests | ||
| * | ||
| * @protected | ||
| * @type {Record<string, P<Response>>} | ||
| * @memberOf Response | ||
| */ | ||
| protected readonly __cache: Record<string, () => P>; | ||
| constructor(response: IRawResponse, collection?: IJsonapiCollection, options?: IRequestOptions, overrideData?: T | Array<T>, views?: Array<View>); | ||
| get isSuccess(): boolean; | ||
| get data(): T | Array<T> | null; | ||
| private __updateInternal; | ||
| /** | ||
| * Replace the response record with a different record. Used to replace a record while keeping the same reference | ||
| * | ||
| * @param {PureModel} data New data | ||
| * @returns {Response} | ||
| * | ||
| * @memberOf Response | ||
| */ | ||
| replaceData(data: T): Response<T, P>; | ||
| clone(): Response<T>; | ||
| get snapshot(): IResponseSnapshot; | ||
| update(response: IRawResponse, views?: Array<View>): Response<T, P>; | ||
| /** | ||
| * Function called when a link is being fetched. The returned value is cached | ||
| * | ||
| * @protected | ||
| * @param {string} name Link name | ||
| * @returns P that resolves with a Response object | ||
| * | ||
| * @memberOf Response | ||
| */ | ||
| protected __fetchLink(name: string): () => P; | ||
| } | ||
| export {}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
346858
2.39%40
11.11%2
-33.33%4
33.33%18
Infinity%844
-2.76%15
7.14%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
Updated
Updated
Updated