rest-client-vue 1.1.1-b5 → 1.1.1-b6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/rest-client-vue.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
(function(v,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("lodash"),require("uuid"),require("vue"),require("axios"),require("jsog"),require("json-stringify-deterministic"),require("pinia")):typeof define=="function"&&define.amd?define(["exports","lodash","uuid","vue","axios","jsog","json-stringify-deterministic","pinia"],s):(v=typeof globalThis<"u"?globalThis:v||self,s(v["rest-client-vue"]={},v._,v.uuid,v.Vue,v.axios,v.jsog,v.stringify,v.pinia))})(this,function(v,s,I,a,$,oe,G,ce){"use strict";var Pe=Object.defineProperty;var Fe=(v,s,I)=>s in v?Pe(v,s,{enumerable:!0,configurable:!0,writable:!0,value:I}):v[s]=I;var ae=(v,s,I)=>(Fe(v,typeof s!="symbol"?s+"":s,I),I),Ne=(v,s,I)=>{if(!s.has(v))throw TypeError("Cannot "+I)};var j=(v,s,I)=>(Ne(v,s,"read from private field"),I?I.call(v):s.get(v)),Ie=(v,s,I)=>{if(s.has(v))throw TypeError("Cannot add the same private member more than once");s instanceof WeakSet?s.add(v):s.set(v,I)};var A;const ue={apiBaseUrl:"",useVueLogger:!1};class we extends Error{constructor(U,q=void 0,e=void 0){super(U);ae(this,"context");ae(this,"innerError");this.context=q,this.innerError=e}}class de{constructor(){Ie(this,A,{})}clearAllStores(){for(const n in j(this,A))j(this,A)[n].clear()}destroyStore(n){j(this,A)[n]&&(j(this,A)[n].$dispose(),delete j(this,A)[n])}getStore(n){return j(this,A)[n]}registerStore(n,U){j(this,A)[n]=U}}A=new WeakMap;function g(w,n){return s.get(w,(n==null?void 0:n.idProperty)||"_id")}function De(w,n){var U;return w&&((U=n.filter)!=null&&U.resourceIds)&&!n.order?n.filter.resourceIds.map(q=>w.find(e=>g(e,n)==q)).filter(q=>q!=null):w}const he={detail:{allowMultiple:!0,autoEdit:!1,autoFromMultipleSelection:!0,autoFromSingleInsertion:!0,autoFromSingleSelection:!0,constrainToSelection:!0},filter:{namedFilter:null,query:null,resourceIds:null},idProperty:"_id",limit:5e3,limitTransientDataToLocalCollection:!0,loading:{firstPageSize:500,pageSize:5e3},order:[[{path:"history.creation.timestamp"},"desc"],[{path:"history.creation.orderInBatch"},"desc"]]},K={resources:[],remoteCollectionSize:null,status:"NotLoaded",selection:[],detailSelection:[],invalidResources:[],transientData:{}},se={editingDetailSelection:!1,batchSaveAttempted:!1,editors:[],listNavigators:{}},Ce=(w,n)=>{n=s.merge({},he,n);const U=ce.defineStore(w,{state:()=>({idProperty:n.idProperty,filter:n.filter,order:n.order||null,propertyBlacklist:n.propertyBlacklist||null,referencePathsToExpand:n.referencePathsToExpand||null,...K,...se}),getters:{fixedQueryParams:e=>{var l,d,u,h;const r={};(l=e.filter)!=null&&l.namedFilter&&(r.namedFilter=e.filter.namedFilter);const t=[];if((d=e.filter)!=null&&d.resourceIds&&t.push({l:{path:e.idProperty},r:{constant:e.filter.resourceIds},operator:"in"}),(u=e.filter)!=null&&u.query&&t.push((h=e.filter)==null?void 0:h.query),t.length>0){const S=t.length==1?t[0]:{and:t};r.q=G(S)}return e.order&&(r.o=G(e.order)),e.propertyBlacklist&&(r.ex=G(e.propertyBlacklist)),e.referencePathsToExpand&&(r.r=G(e.referencePathsToExpand)),r}},actions:{reset(){for(const e in K)this[e]=K[e];for(const e in se)this[e]=se[e]},resetRetainingTransientData(){if(n.limitTransientDataToLocalCollection)this.reset();else{const e=this.transientData;this.reset(),this.transientData=e}},setEditingDetailSelection(e){this.editingDetailSelection=e},setFilter(e){s.isEqual(e,this.filter)||(this.filter=e,this.status="NotLoaded",this.resources=[],this.remoteCollectionSize=null,this.selection=[],this.detailSelection=[],this.invalidResources=[],this.editingDetailSelection=!1)},async setQuery(e){var r,t;if(!s.isEqual(e,(r=this.filter)==null?void 0:r.query)&&(e!=null||((t=this.filter)==null?void 0:t.query)!=null)){const l=s.cloneDeep(this.filter)||{};e!==null?l.query=e:s.unset(l,"query");const d=this.status;this.setFilter(l),d!="NotLoaded"&&await this.loadResources()}},async setFilterResourceIds(e){if(!s.isEqual(e,s.get(this.filter,"resourceIds"))){const r=s.cloneDeep(this.filter)||{};e!=null?r.resourceIds=e:s.unset(r,"resourceIds");const t=this.status;this.setFilter(r),t!="NotLoaded"&&await this.loadResources()}},setBatchSaveAttempted(e){e!=this.batchSaveAttempted&&(this.batchSaveAttempted=e)},clear(){for(const e in K)this[e]=K[e]},clearRetainingTransientData(){if(n.limitTransientDataToLocalCollection)this.reset();else{const e=this.transientData;this.reset(),this.transientData=e}},async ensureCollectionLoaded(){["Loaded","Loading","LoadingMore","Failed"].includes(this.status)||await this.loadResources()},async loadResources(e={}){this.resetRetainingTransientData();const r=I.v4();this._loadId=r,await this._loadResources(0,r,e)},async _loadResources(e,r,t){var b,N,k,B,Q;const l=e==0;if(this.status=l?"Loading":"LoadingMore",this._loadOffset=e,((b=this.filter)==null?void 0:b.query)===!1){this.resources=[],this.remoteCollectionSize=0;return}const d=this.fixedQueryParams,u=s.cloneDeep(d),h=l?(N=this.filter)!=null&&N.resourceIds&&!this.order?null:t.firstPageSize||((k=n.loading)==null?void 0:k.firstPageSize)||t.pageSize||((B=n.loading)==null?void 0:B.pageSize)||null:t.pageSize||((Q=n.loading)==null?void 0:Q.pageSize)||null,S=h!=null||n.limit!=null?s.min(s.filter([h,n.limit],p=>p!=null)):void 0;S!=null?(u.offset=e,u.limit=S):e!=0&&(u.offset=e);const D=s.map(u,(p,m)=>p!=null?`${encodeURIComponent(m)}=${encodeURIComponent(p)}`:null).filter(p=>p!=null).join("&"),L=s.isEmpty(D)?n.restCollectionUrl:`${n.restCollectionUrl}?${D}`;try{const p=await $.get(L),m=this.fixedQueryParams;if(r!=this._loadId||e!=this._loadOffset||!s.isEqual(d,m))console.log(`Discarding resources fetched by obsolete query from ${n.restCollectionUrl}.`);else{const C=oe.decode(s.get(p,"data.data",[])),T=(l?0:this.resources.length)+C.length,y=S==null||C.length<S||n.limit&&T>=n.limit;console.log(`Loaded ${C.length}${l?"":" more"} resources from ${n.restCollectionUrl}`),l&&(this.resources=[]);const H=[...this.resources,...C.map(Z=>Object.freeze(Z))];this.resources=De(H,this),s.get(p,"data.totalCollectionSize")?this.remoteCollectionSize=s.get(p,"data.totalCollectionSize"):this.remoteCollectionSize=null,this.status=y?"Loaded":"LoadingMore",y||this._loadResources(this.resources.length,r,t)}}catch(p){console.log(`Error while loading resources (URL="${L}"):`,p),this.clearRetainingTransientData(),this.status="Failed"}},async checkForDeletedResource(e){let r;try{r=await $.get(`${n.restCollectionUrl}/${e}`)}catch(t){$.isAxiosError(t)&&(r=t.response)}return r&&r.status==404?(this.recordDeletion(e),!0):!1},recordDeletion(e){var r;if(["Loaded","LoadingMore"].includes(this.status)){let t=this.resources.findIndex(l=>g(l,this)==e);t>=0&&this.resources.splice(t,1),t=this.selection.findIndex(l=>g(l,this)==e),t>=0&&this.selection.splice(t,1),t=this.detailSelection.findIndex(l=>g(l,this)==e),t>=0&&(this.detailSelection.splice(t,1),this.detailSelection.length==0&&(this.editingDetailSelection=((r=n.detail)==null?void 0:r.autoEdit)||!1)),n.limitTransientDataToLocalCollection&&delete this.transientData[e]}},recordInsertion(e,{insertAtBeginning:r,transientData:t}={}){var l;if(["Loaded","LoadingMore"].includes(this.status)){const d=Object.freeze(e);r?this.resources.unshift(d):this.resources.push(d),(l=n.detail)!=null&&l.autoFromSingleInsertion&&(this.detailSelection.length==0||this.detailSelection.length==1&&g(this.detailSelection[0],this)==null)&&(this.detailSelection=[d],this.editingDetailSelection=n.detail.autoEdit);const u=g(e,this);u&&(t===void 0?delete this.transientData[u]:this.transientData[u]=t)}},recordUpdate(e){const r=g(e,this);if(r&&["Loaded","LoadingMore"].includes(this.status)){const t=Object.freeze(e);let l=this.resources.findIndex(d=>g(d,this)==r);l>=0&&(this.resources[l]=t),l=this.selection.findIndex(d=>g(d,this)==r),l>=0&&(this.selection[l]=t),l=this.detailSelection.findIndex(d=>g(d,this)==r),l>=0&&(this.detailSelection[l]=t)}},async refreshResource(e){if(e){const r={};this.referencePathsToExpand&&(r.r=G(this.referencePathsToExpand));const t=s.map(r,(u,h)=>u!=null?`${encodeURIComponent(h)}=${encodeURIComponent(u)}`:null).filter(u=>u!=null).join("&"),l=s.isEmpty(t)?`${n.restCollectionUrl}/${e}`:`${n.restCollectionUrl}/${e}?${t}`,d=await $.get(l);if(d.status==200){const u=d.data;return this.recordUpdate(u),u}}return null},addResource(e={}){this.detailSelection=[s.omit(s.merge({},e),this.idProperty)],this.editingDetailSelection=!0},async deleteResource(e){if((await $.delete(`${n.restCollectionUrl}/${e}`)).status==200&&["Loaded","LoadingMore"].includes(this.status)){let t=this.resources.findIndex(l=>g(l,this)==e);t>=0&&this.resources.splice(t,1),t=this.selection.findIndex(l=>g(l,this)==e),t>=0&&this.selection.splice(t,1),t=this.detailSelection.findIndex(l=>g(l,this)==e),t>=0&&(this.detailSelection.splice(t,1),this.detailSelection.length==0&&(this.editingDetailSelection=n.detail.autoEdit)),n.limitTransientDataToLocalCollection&&delete this.transientData[e]}},async saveResource(e){const r=g(e,this),t=r==null,l=await $({method:t?"post":"put",url:t?n.restCollectionUrl:`${n.restCollectionUrl}/${r}`,data:e});if(l.status==200){const d=l.data,u=g(d,this),h=u?await this.refreshResource(u):null;return t&&this.recordInsertion(h||Object.freeze(d)),h||d}return null},async saveResources(e){const r=await $({method:"put",url:n.restCollectionUrl,data:e});if(r.status==200){const t=r.data||[],l=[];for(const[d,u]of t.entries()){const h=g(e[d],this)==null,S=g(u,this),D=S?await this.refreshResource(S):null;h&&this.recordInsertion(D||u),l.push(D||Object.freeze(u))}return l}return[]},clearTransientData(){this.transientData={}},setInvalidResourceIds(e){s.isEqual(e,this.invalidResources.map(r=>g(r,this)))||(this.invalidResources=e.map(r=>this.resources.find(t=>g(t,this)==r)).filter(r=>r!=null))},setTransientDataForResources(e){const{transientData:r,merge:t=!0}=e;t?s.merge(this.transientData,r):s.assign(this.transientData,r)},clearSelection(){this.setSelection([])},deselectResources(e){const r=this.selection.map(t=>g(t,this)).filter(t=>t!=null&&!e.includes(t));this.setSelection(r)},selectResources(e,r){const{addToSelection:t,edit:l}=r||{},d=this.selection.map(h=>g(h,this));let u=e;if(t){const h=s.difference(e,d);h.length>0&&(u=d.concat(h).filter(S=>S!=null))}s.isEqual(d,u)||this.setSelection(u,l)},setSelection(e,r){if(!s.isEqual(e,this.selection.map(t=>g(t,this)))){let t=!0;if(this.editors.length>0){if(n.detail.autoFromSingleSelection&&this.selection.length==1)t=!1;else if(n.detail.autoFromMultipleSelection&&n.detail.allowMultiple&&this.selection.length>1)t=!1;else if(n.detail.constrainToSelection){const l=this.selection.map(h=>g(h,this)),u=this.detailSelection.map(h=>g(h,this)).filter(h=>l.includes(h));this.detailSelection.filter(h=>!u.includes(g(h,this))).length>0&&(t=!1)}}if(!t)return;if(this.selection=e.map(l=>this.resources.find(d=>g(d,this)==l)).filter(l=>l!=null),n.detail.autoFromSingleSelection&&this.selection.length==1){const l=this.selection[0];(this.detailSelection.length!=1||g(this.detailSelection[0],this)!=g(l,this))&&(this.detailSelection=[l])}else if(n.detail.autoFromMultipleSelection&&n.detail.allowMultiple&&this.selection.length>1)s.isEqual(this.selection.map(l=>g(l,this)),this.detailSelection.map(l=>g(l,this)))||(this.detailSelection=s.clone(this.selection));else if(n.detail.constrainToSelection){const l=this.selection.map(h=>g(h,this)),d=this.detailSelection.map(h=>g(h,this)),u=d.filter(h=>l.includes(h));s.isEqual(u,d)||(this.detailSelection=this.detailSelection.filter(h=>u.includes(g(h,this))))}this.detailSelection.length==0?this.editingDetailSelection=!1:this.editingDetailSelection=r!==void 0?r:this.editingDetailSelection||n.detail.autoEdit}},hideDetail(){this.detailSelection.length>0&&(this.detailSelection=[],this.editingDetailSelection=!1)},showSelectionAsDetail(e){if(this.selection.length<=1||n.detail.allowMultiple){const r=this.selection.map(l=>g(l,this)),t=this.detailSelection.map(l=>g(l,this));s.isEqual(r,t)||(this.detailSelection=s.clone(this.selection)),this.detailSelection.length==0?this.editingDetailSelection=!1:this.editingDetailSelection=e!==void 0?e:this.editingDetailSelection||n.detail.autoEdit}},deregisterEditor(e){s.remove(this.editors,r=>r==e)},registerEditor(e){this.editors.includes(e)||this.editors.push(e)},deregisterListNavigator({name:e,listNavigator:r}){(r==null||this.listNavigators[e]==r)&&delete this.listNavigators[e]},registerListNavigator({name:e,listNavigator:r}){this.listNavigators[e]!=r&&(this.listNavigators[e]=r)}}});return(()=>{const e=(r,t)=>{const l=U(r,t);return ee.registerStore(l.$id,l),l};return e.$id=U.$id,e})()},ee=new de,Ee=(w={})=>{var ge,ve,pe,me;const{collectionId:n,draftBatchId:U,enabled:q,resourceType:e,options:r}=w,t=a.ref(void 0),l=a.ref(n),d=a.ref(U),u=a.ref(q??!0),h=a.ref([a.toRef(e)]),S=a.ref(r),D=a.computed(()=>d.value),L=a.ref(u.value),b=a.ref(I.v4()),N=a.ref((ve=(ge=S.value)==null?void 0:ge.filter)==null?void 0:ve.resourceIds),k=a.ref((me=(pe=S.value)==null?void 0:pe.filter)==null?void 0:me.query),B=a.computed(()=>d.value==null&&l.value!=null&&ee.getStore(l.value)!=null),Q=a.ref(B.value),p=a.computed(()=>T("status","Uninitialized"));function m(i){if(t.value)return i(t.value)}async function C(i){if(t.value)return await i(t.value)}function T(i,c){if(t.value)return t.value[i];if(c!==void 0)return c;throw"Attempted to use a REST collection store that has not been created, with no default value."}const y=(i={})=>{var ie,Re,Se,ye;const{collectionId:c,draftBatchId:E,enabled:F,resourceType:ne,options:te}=i;l.value=c,d.value=E,u.value=F??!0,h.value=[a.toRef(ne)],te!=null&&(S.value=te,N.value=(Re=(ie=S.value)==null?void 0:ie.filter)==null?void 0:Re.resourceIds,k.value=(ye=(Se=S.value)==null?void 0:Se.filter)==null?void 0:ye.query),b.value=I.v4(),H(u.value)},H=async i=>{i!=L.value&&(L.value=i,i&&(_(),z()))},Z=async i=>{N.value=i,await C(async c=>await c.setFilterResourceIds(N.value||null))},V=async i=>{if(k.value=i,p.value!="Uninitialized"){const c=i,E=a.computed(()=>s.isFunction(c)?R.value?c(R.value):null:c);await C(F=>F.setQuery(E.value||null))}},_=()=>{S.value=s.merge({},S.value||{},{filter:{resourceIds:N.value,query:k.value}})},R=a.computed(()=>h.value[0].value||void 0),O=function(i){const c=s.get(i,"filter.query"),E=s.get(i,"propertyBlacklist"),F=s.get(i,"referencePathsToExpand"),ne=s.isFunction(c)?c(R.value):c,te=R.value?s.isFunction(E)?E(R.value):E||[]:[],ie=R.value?s.isFunction(F)?F(R.value):F||[]:[];return{...s.cloneDeep(i||{}),filter:{...s.cloneDeep((i==null?void 0:i.filter)||{}),query:ne},propertyBlacklist:te,referencePathsToExpand:ie}},P=a.computed(()=>O(S.value)),M=a.computed(()=>{if(!R.value)return null;if(P.value.restCollectionUrl!==void 0)return P.value.restCollectionUrl;if(!Q.value)return!R.value||!R.value.collectionName?null:D.value?`${X.config.apiBaseUrl}/draft-batches/${D.value}/${R.value.collectionName}`:`${X.config.apiBaseUrl}/${R.value.collectionName}`}),o=a.computed(()=>{var i;if(R.value)return s.mergeWith(s.cloneDeep(he),P.value,{idProperty:(i=R.value)==null?void 0:i.idProperty,restCollectionUrl:M.value||""},(c,E)=>{if(s.isArray(E)&&E.length==0&&(c==null||s.isArray(c)))return E})}),f=a.ref(0),x=a.computed(()=>{var i;return f.value,l.value||R.value&&((i=R.value)==null?void 0:i.collectionName)&&(D.value?`draftBatches/${D.value}/${R.value.collectionName}/${b.value}`:`${R.value.collectionName}/${b.value}`)||null}),z=()=>{if(L.value){let i=!1;return R.value&&x.value&&(t.value=ee.getStore(x.value),t.value?i=!1:o.value&&(Q.value=!1,t.value=Ce(x.value,o.value)(),i=!0)),i}else return!1},J=()=>{x.value&&l.value==x.value?(t.value=void 0,z()):(t.value=void 0,f.value=f.value+1,z())};a.watch(D,(i,c)=>{s.isEqual(i,c)||J()}),a.watch(L,(i,c)=>{i!=c&&(i?z():t.value=void 0)}),a.watch(R,(i,c)=>{i&&!c?z():!i&&c||s.isEqual(i,c)||J()}),a.watch(o,(i,c)=>{s.isEqual(i,c)||J()}),a.watch(B,()=>{B.value||(Q.value=!1)}),a.watch(x,()=>z()),a.watch(M,(i,c)=>{s.isEqual(i,c)||J()});const W=[],qe=function(i){p.value!="Uninitialized"?a.nextTick(i):W.push(i)};return a.watch(p,(i,c)=>{if(i!="Uninitialized"&&c=="Uninitialized"){const E=s.clone(W);W.splice(0,W.length);for(const F of E)F()}}),z(),{collectionId:x,draftBatchId:D,resourceType:R,enabled:L,status:p,batchSaveAttempted:a.computed(()=>T("batchSaveAttempted",!1)),listNavigators:a.computed(()=>T("listNavigators",!1)),invalidResources:a.computed(()=>T("invalidResources",[])),resources:a.computed(()=>p.value!="Uninitialized"?(C(i=>i.ensureCollectionLoaded()),T("resources",[])):[]),remoteCollectionSize:a.computed(()=>T("remoteCollectionSize",null)),transientData:a.computed(()=>T("transientData",[])),detailSelection:a.computed(()=>T("detailSelection",[])),editingDetailSelection:a.computed(()=>T("editingDetailSelection",!1)),selection:a.computed(()=>T("selection",[])),reconfigureCollection:y,setEditingDetailSelection:i=>m(c=>c.setEditingDetailSelection(i)),setEnabled:H,setFilterResourceIds:Z,setQuery:V,ensureStore:z,setBatchSaveAttempted:i=>m(c=>c.setBatchSaveAttempted(i)),clear:()=>m(i=>i.clear()),ensureCollectionLoaded:async()=>await C(async i=>i.ensureCollectionLoaded()),loadResources:async()=>await C(async i=>await i.loadResources()),addResource:i=>m(c=>c.addResource(i)),checkForDeletedResource:async i=>await C(async c=>await c.checkForDeletedResource(i))==!0,recordDeletion:i=>m(c=>c.recordDeletion(i)),recordInsertion:(i,{insertAtBeginning:c=!1,transientData:E=void 0}={})=>m(F=>F.recordInsertion(i,{insertAtBeginning:c,transientData:E})),recordUpdate:i=>m(c=>c.recordUpdate(i)),refreshResource:async i=>await C(async c=>await c.refreshResource(i)),deleteResource:async i=>await C(async c=>await c.deleteResource(i)),saveResource:async i=>await C(async c=>await c.saveResource(i)),saveResources:async i=>await C(async c=>await c.saveResources(i)),clearTransientData:()=>m(i=>i.clearTransientData()),setInvalidResourceIds:i=>m(c=>c.setInvalidResourceIds(i)),setTransientDataForResources:i=>m(c=>c.setTransientDataForResources(i)),clearSelection:()=>m(i=>i.clearSelection()),deselectResources:i=>m(c=>c.deselectResources(i)),selectResources:(i,c)=>m(E=>E.selectResources(i,c)),hideDetail:()=>m(i=>i.hideDetail()),deregisterEditor:i=>m(c=>c.deregisterEditor(i)),registerEditor:i=>m(c=>c.registerEditor(i)),deregisterListNavigator:i=>m(c=>c.deregisterListNavigator(i)),registerListNavigator:i=>m(c=>c.registerListNavigator(i)),onItemsStoreReady:qe}},Ue=()=>{ee.clearAllStores()};function re(w,n){return s.get(w,(n==null?void 0:n.idProperty)||"_id")}const fe={idProperty:"_id"},Y={resource:null,status:"NotLoaded"},Te=(w,n)=>{n=s.merge({},fe,n);const U=ce.defineStore(w,{state:()=>({idProperty:n.idProperty,resourceId:n.resourceId||null,resourceUrl:n.resourceUrl||null,referencePathsToExpand:n.referencePathsToExpand||null,...Y}),getters:{currentResourceUrl:e=>e.resourceUrl||(n.restCollectionUrl&&e.resourceId?`${n.restCollectionUrl}/${e.resourceId}`:null)||n.restCollectionUrl||null,fixedQueryParams:e=>{const r={};return e.referencePathsToExpand&&(r.r=G(e.referencePathsToExpand)),r}},actions:{reset(){for(const e in Y)this[e]=Y[e]},async setResourceId(e){const r=this.currentResourceUrl;this.resourceId=e,!s.isEqual(this.currentResourceUrl,r)&&this.status!="NotLoaded"&&await this.loadResource()},async setResourceUrl(e){const r=this.currentResourceUrl;this.resourceUrl=e,!s.isEqual(this.currentResourceUrl,r)&&this.status!="NotLoaded"&&await this.loadResource()},clear(){for(const e in Y)this[e]=Y[e]},async ensureResourceLoaded(){["Loaded","Loading","Failed"].includes(this.status)||await this.loadResource()},async loadResource(){this.reset();const e=I.v4();if(this._loadId=e,this.currentResourceUrl){this.status="Loading";const r=this.fixedQueryParams,t=s.cloneDeep(r),l=s.map(t,(u,h)=>u!=null?`${encodeURIComponent(h)}=${encodeURIComponent(u)}`:null).filter(u=>u!=null).join("&"),d=s.isEmpty(l)?this.currentResourceUrl:`${this.currentResourceUrl}?${l}`;try{const u=await $.get(d),h=this.fixedQueryParams;e!=this._loadId||!s.isEqual(r,h)?console.log(`Discarding resource fetched by obsolete query from ${d}.`):(this.resource=oe.decode(s.get(u,"data",{})),this.status="Loaded")}catch(u){console.log(`Error while loading resource (URL="${d}")`,u),this.reset(),this.status="Failed"}}else throw"Cannot load a resource because its URL, or its ID and collection URL, are unknown."},async checkForDeletedResource(){if(this.currentResourceUrl){let e;try{e=await $.get(this.currentResourceUrl)}catch(r){$.isAxiosError(r)&&(e=r.response)}return e&&e.status==404?(this.recordDeletion(),!0):!1}else throw"Cannot check for a deleted resource because its URL, or its ID and collection URL, are unknown."},recordDeletion(){this.resource=null,this.status="Loaded"},recordInsertion(e){this.resourceId=re(e,this)||null,this.status=="Loaded"&&(this.resource=Object.freeze(e))},recordUpdate(e){this.resourceId=re(e,this)||null,this.status=="Loaded"&&(this.resource=Object.freeze(e))},async refreshResource(){return await this.loadResource(),this.resource},async deleteResource(){if(this.currentResourceUrl)(await $.delete(this.currentResourceUrl)).status==200&&this.recordDeletion();else throw"Cannot delete a resource because its URL, or its ID and collection URL, are unknown."},async saveResource(e){const r=re(e,this),t=r==null;if(!r)throw"The REST resource store cannot create new resources on the server.";if(!this.currentResourceUrl)throw"Cannot save a resource because its URL, or its ID and collection URL, are unknown.";const l=await $({method:t?"post":"put",url:this.currentResourceUrl,data:e});if(l.status==200){const d=l.data,u=await this.refreshResource();return t&&this.recordInsertion(u||d),u||Object.freeze(d)}return null},async makeCustomApiRequest(e){const{method:r,subpath:t,url:l,data:d}=e;let u=l;if(!u)if(this.currentResourceUrl)u=`${this.currentResourceUrl}/${t}`;else throw"Cannot make a custom API request for a resource because its URL, or its ID and collection URL, are unknown.";const h=await $({method:r||"get",url:u,data:d});return h.status>=200&&h.status<300?h.data:null}}});return(()=>{const e=(r,t)=>{const l=U(r,t);return le.registerStore(l.$id,l),l};return e.$id=U.$id,e})()},le=new de,Le=(w={})=>{const{resourceClientId:n,draftBatchId:U,enabled:q,resourceType:e,options:r}=w,t=a.ref(void 0),l=a.ref(n),d=a.ref(U),u=a.ref(q??!0),h=a.ref([a.toRef(e)]),S=a.ref(r),D=a.computed(()=>d.value),L=a.ref(u.value),b=a.ref(I.v4()),N=a.computed(()=>d.value==null&&l.value!=null&&le.getStore(l.value)!=null),k=a.ref(N.value),B=a.computed(()=>m("status","Uninitialized"));function Q(o){if(t.value)return o(t.value)}async function p(o){if(t.value)return await o(t.value)}function m(o,f){if(t.value)return t.value[o];if(f!==void 0)return f;throw"Attempted to use a REST resource store that has not been created, with no default value."}const C=(o={})=>{const{resourceClientId:f,draftBatchId:x,enabled:z,resourceType:J,options:W}=o;l.value=f,d.value=x,u.value=z??!0,h.value=[a.toRef(J)],W!=null&&(S.value=W),b.value=I.v4(),T(u.value)},T=async o=>{o!=L.value&&(L.value=o,o&&P())},y=a.computed(()=>h.value[0].value||void 0),H=function(o){const f=s.get(o,"referencePathsToExpand"),x=y.value?s.isFunction(f)?f(y.value):f||[]:[];return{...s.cloneDeep(o||{}),referencePathsToExpand:x}},Z=a.computed(()=>H(S.value)),V=a.computed(()=>{if(!y.value)return null;if(!k.value)return!y.value||!y.value.collectionName?null:D.value?`${X.config.apiBaseUrl}/draft-batches/${D.value}/${y.value.collectionName}`:`${X.config.apiBaseUrl}/${y.value.collectionName}`}),_=a.computed(()=>{if(y.value&&V.value)return s.mergeWith(s.cloneDeep(fe),Z.value,{idProperty:y.value.idProperty,restCollectionUrl:V.value},(o,f)=>{if(s.isArray(f)&&f.length==0&&(o==null||s.isArray(o)))return f})}),R=a.ref(0),O=a.computed(()=>{var o;return R.value,l.value||y.value&&((o=y.value)==null?void 0:o.collectionName)&&(D.value?`draftBatches/${D.value}/${y.value.collectionName}/${b.value}`:`${y.value.collectionName}/${b.value}`)||null}),P=()=>{if(L.value){let o=!1;return y.value&&O.value&&(t.value=le.getStore(O.value),t.value?o=!1:_.value&&(k.value=!1,t.value=Te(O.value,_.value)(),o=!0)),o}else return!1},M=()=>{O.value&&l.value==O.value?(t.value=void 0,P()):(t.value=void 0,R.value=R.value+1,P())};return a.watch(D,(o,f)=>{s.isEqual(o,f)||M()}),a.watch(L,(o,f)=>{o!=f&&(o?P():t.value=void 0)}),a.watch(y,(o,f)=>{o&&!f?P():!o&&f||s.isEqual(o,f)||M()}),a.watch(_,(o,f)=>{s.isEqual(o,f)||M()}),a.watch(N,()=>{N.value||(k.value=!1)}),a.watch(O,()=>P()),a.watch(V,(o,f)=>{s.isEqual(o,f)||M()}),P(),{resourceClientId:O,draftBatchId:D,enabled:L,resourceType:y,status:B,resource:a.computed(()=>B.value!="Uninitialized"?(p(o=>o.ensureResourceLoaded()),m("resource",[])):[]),resourceId:a.computed(()=>m("resourceId",null)),resourceUrl:a.computed(()=>m("resourceUrl",null)),reconfigureClient:C,setEnabled:T,setResourceId:async o=>await p(async f=>await f.setResourceId(o)),setResourceUrl:async o=>await p(async f=>await f.setResourceUrl(o)),ensureStore:P,clear:()=>Q(o=>o.clear()),ensureResourceLoaded:async()=>await p(async o=>o.ensureResourceLoaded()),loadResource:async()=>await p(async o=>await o.loadResource()),checkForDeletedResource:async()=>await p(async o=>await o.checkForDeletedResource())==!0,recordDeletion:()=>p(o=>o.recordDeletion()),recordInsertion:o=>p(f=>f.recordInsertion(o)),recordUpdate:o=>p(f=>f.recordUpdate(o)),refreshResource:async()=>await p(async o=>await o.refreshResource()),deleteResource:async()=>await p(async o=>await o.deleteResource()),saveResource:async o=>await p(async f=>await f.saveResource(o)),makeCustomApiRequest:async o=>await p(async f=>await f.makeCustomApiRequest(o))}},X={config:{...ue}};function $e(w){X.config=s.merge({},ue,w)}v.ApiClientError=we,v.clearAllRestCollections=Ue,v.initRestClient=$e,v.restClient=X,v.useRestCollection=Ee,v.useRestResource=Le,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|
1
|
+
(function(v,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("lodash"),require("uuid"),require("vue"),require("axios"),require("jsog"),require("json-stringify-deterministic"),require("pinia")):typeof define=="function"&&define.amd?define(["exports","lodash","uuid","vue","axios","jsog","json-stringify-deterministic","pinia"],s):(v=typeof globalThis<"u"?globalThis:v||self,s(v["rest-client-vue"]={},v._,v.uuid,v.Vue,v.axios,v.jsog,v.stringify,v.pinia))})(this,function(v,s,I,a,$,oe,G,ce){"use strict";var Pe=Object.defineProperty;var Fe=(v,s,I)=>s in v?Pe(v,s,{enumerable:!0,configurable:!0,writable:!0,value:I}):v[s]=I;var ae=(v,s,I)=>(Fe(v,typeof s!="symbol"?s+"":s,I),I),Ne=(v,s,I)=>{if(!s.has(v))throw TypeError("Cannot "+I)};var j=(v,s,I)=>(Ne(v,s,"read from private field"),I?I.call(v):s.get(v)),Ie=(v,s,I)=>{if(s.has(v))throw TypeError("Cannot add the same private member more than once");s instanceof WeakSet?s.add(v):s.set(v,I)};var A;const ue={apiBaseUrl:"",useVueLogger:!1};class we extends Error{constructor(U,q=void 0,e=void 0){super(U);ae(this,"context");ae(this,"innerError");this.context=q,this.innerError=e}}class de{constructor(){Ie(this,A,{})}clearAllStores(){for(const n in j(this,A))j(this,A)[n].clear()}destroyStore(n){j(this,A)[n]&&(j(this,A)[n].$dispose(),delete j(this,A)[n])}getStore(n){return j(this,A)[n]}registerStore(n,U){j(this,A)[n]=U}}A=new WeakMap;function g(w,n){return s.get(w,(n==null?void 0:n.idProperty)||"_id")}function De(w,n){var U;return w&&((U=n.filter)!=null&&U.resourceIds)&&!n.order?n.filter.resourceIds.map(q=>w.find(e=>g(e,n)==q)).filter(q=>q!=null):w}const he={detail:{allowMultiple:!0,autoEdit:!1,autoFromMultipleSelection:!0,autoFromSingleInsertion:!0,autoFromSingleSelection:!0,constrainToSelection:!0},filter:{namedFilter:null,query:null,resourceIds:null},idProperty:"_id",limit:5e3,limitTransientDataToLocalCollection:!0,loading:{firstPageSize:500,pageSize:5e3},order:[[{path:"history.creation.timestamp"},"desc"],[{path:"history.creation.indexInBatch"},"desc"]]},K={resources:[],remoteCollectionSize:null,status:"NotLoaded",selection:[],detailSelection:[],invalidResources:[],transientData:{}},se={editingDetailSelection:!1,batchSaveAttempted:!1,editors:[],listNavigators:{}},Ce=(w,n)=>{n=s.merge({},he,n);const U=ce.defineStore(w,{state:()=>({idProperty:n.idProperty,filter:n.filter,order:n.order||null,propertyBlacklist:n.propertyBlacklist||null,referencePathsToExpand:n.referencePathsToExpand||null,...K,...se}),getters:{fixedQueryParams:e=>{var l,d,u,h;const r={};(l=e.filter)!=null&&l.namedFilter&&(r.namedFilter=e.filter.namedFilter);const t=[];if((d=e.filter)!=null&&d.resourceIds&&t.push({l:{path:e.idProperty},r:{constant:e.filter.resourceIds},operator:"in"}),(u=e.filter)!=null&&u.query&&t.push((h=e.filter)==null?void 0:h.query),t.length>0){const S=t.length==1?t[0]:{and:t};r.q=G(S)}return e.order&&(r.o=G(e.order)),e.propertyBlacklist&&(r.ex=G(e.propertyBlacklist)),e.referencePathsToExpand&&(r.r=G(e.referencePathsToExpand)),r}},actions:{reset(){for(const e in K)this[e]=K[e];for(const e in se)this[e]=se[e]},resetRetainingTransientData(){if(n.limitTransientDataToLocalCollection)this.reset();else{const e=this.transientData;this.reset(),this.transientData=e}},setEditingDetailSelection(e){this.editingDetailSelection=e},setFilter(e){s.isEqual(e,this.filter)||(this.filter=e,this.status="NotLoaded",this.resources=[],this.remoteCollectionSize=null,this.selection=[],this.detailSelection=[],this.invalidResources=[],this.editingDetailSelection=!1)},async setQuery(e){var r,t;if(!s.isEqual(e,(r=this.filter)==null?void 0:r.query)&&(e!=null||((t=this.filter)==null?void 0:t.query)!=null)){const l=s.cloneDeep(this.filter)||{};e!==null?l.query=e:s.unset(l,"query");const d=this.status;this.setFilter(l),d!="NotLoaded"&&await this.loadResources()}},async setFilterResourceIds(e){if(!s.isEqual(e,s.get(this.filter,"resourceIds"))){const r=s.cloneDeep(this.filter)||{};e!=null?r.resourceIds=e:s.unset(r,"resourceIds");const t=this.status;this.setFilter(r),t!="NotLoaded"&&await this.loadResources()}},setBatchSaveAttempted(e){e!=this.batchSaveAttempted&&(this.batchSaveAttempted=e)},clear(){for(const e in K)this[e]=K[e]},clearRetainingTransientData(){if(n.limitTransientDataToLocalCollection)this.reset();else{const e=this.transientData;this.reset(),this.transientData=e}},async ensureCollectionLoaded(){["Loaded","Loading","LoadingMore","Failed"].includes(this.status)||await this.loadResources()},async loadResources(e={}){this.resetRetainingTransientData();const r=I.v4();this._loadId=r,await this._loadResources(0,r,e)},async _loadResources(e,r,t){var b,N,k,B,Q;const l=e==0;if(this.status=l?"Loading":"LoadingMore",this._loadOffset=e,((b=this.filter)==null?void 0:b.query)===!1){this.resources=[],this.remoteCollectionSize=0;return}const d=this.fixedQueryParams,u=s.cloneDeep(d),h=l?(N=this.filter)!=null&&N.resourceIds&&!this.order?null:t.firstPageSize||((k=n.loading)==null?void 0:k.firstPageSize)||t.pageSize||((B=n.loading)==null?void 0:B.pageSize)||null:t.pageSize||((Q=n.loading)==null?void 0:Q.pageSize)||null,S=h!=null||n.limit!=null?s.min(s.filter([h,n.limit],p=>p!=null)):void 0;S!=null?(u.offset=e,u.limit=S):e!=0&&(u.offset=e);const D=s.map(u,(p,m)=>p!=null?`${encodeURIComponent(m)}=${encodeURIComponent(p)}`:null).filter(p=>p!=null).join("&"),L=s.isEmpty(D)?n.restCollectionUrl:`${n.restCollectionUrl}?${D}`;try{const p=await $.get(L),m=this.fixedQueryParams;if(r!=this._loadId||e!=this._loadOffset||!s.isEqual(d,m))console.log(`Discarding resources fetched by obsolete query from ${n.restCollectionUrl}.`);else{const C=oe.decode(s.get(p,"data.data",[])),T=(l?0:this.resources.length)+C.length,y=S==null||C.length<S||n.limit&&T>=n.limit;console.log(`Loaded ${C.length}${l?"":" more"} resources from ${n.restCollectionUrl}`),l&&(this.resources=[]);const H=[...this.resources,...C.map(Z=>Object.freeze(Z))];this.resources=De(H,this),s.get(p,"data.totalCollectionSize")?this.remoteCollectionSize=s.get(p,"data.totalCollectionSize"):this.remoteCollectionSize=null,this.status=y?"Loaded":"LoadingMore",y||this._loadResources(this.resources.length,r,t)}}catch(p){console.log(`Error while loading resources (URL="${L}"):`,p),this.clearRetainingTransientData(),this.status="Failed"}},async checkForDeletedResource(e){let r;try{r=await $.get(`${n.restCollectionUrl}/${e}`)}catch(t){$.isAxiosError(t)&&(r=t.response)}return r&&r.status==404?(this.recordDeletion(e),!0):!1},recordDeletion(e){var r;if(["Loaded","LoadingMore"].includes(this.status)){let t=this.resources.findIndex(l=>g(l,this)==e);t>=0&&this.resources.splice(t,1),t=this.selection.findIndex(l=>g(l,this)==e),t>=0&&this.selection.splice(t,1),t=this.detailSelection.findIndex(l=>g(l,this)==e),t>=0&&(this.detailSelection.splice(t,1),this.detailSelection.length==0&&(this.editingDetailSelection=((r=n.detail)==null?void 0:r.autoEdit)||!1)),n.limitTransientDataToLocalCollection&&delete this.transientData[e]}},recordInsertion(e,{insertAtBeginning:r,transientData:t}={}){var l;if(["Loaded","LoadingMore"].includes(this.status)){const d=Object.freeze(e);r?this.resources.unshift(d):this.resources.push(d),(l=n.detail)!=null&&l.autoFromSingleInsertion&&(this.detailSelection.length==0||this.detailSelection.length==1&&g(this.detailSelection[0],this)==null)&&(this.detailSelection=[d],this.editingDetailSelection=n.detail.autoEdit);const u=g(e,this);u&&(t===void 0?delete this.transientData[u]:this.transientData[u]=t)}},recordUpdate(e){const r=g(e,this);if(r&&["Loaded","LoadingMore"].includes(this.status)){const t=Object.freeze(e);let l=this.resources.findIndex(d=>g(d,this)==r);l>=0&&(this.resources[l]=t),l=this.selection.findIndex(d=>g(d,this)==r),l>=0&&(this.selection[l]=t),l=this.detailSelection.findIndex(d=>g(d,this)==r),l>=0&&(this.detailSelection[l]=t)}},async refreshResource(e){if(e){const r={};this.referencePathsToExpand&&(r.r=G(this.referencePathsToExpand));const t=s.map(r,(u,h)=>u!=null?`${encodeURIComponent(h)}=${encodeURIComponent(u)}`:null).filter(u=>u!=null).join("&"),l=s.isEmpty(t)?`${n.restCollectionUrl}/${e}`:`${n.restCollectionUrl}/${e}?${t}`,d=await $.get(l);if(d.status==200){const u=d.data;return this.recordUpdate(u),u}}return null},addResource(e={}){this.detailSelection=[s.omit(s.merge({},e),this.idProperty)],this.editingDetailSelection=!0},async deleteResource(e){if((await $.delete(`${n.restCollectionUrl}/${e}`)).status==200&&["Loaded","LoadingMore"].includes(this.status)){let t=this.resources.findIndex(l=>g(l,this)==e);t>=0&&this.resources.splice(t,1),t=this.selection.findIndex(l=>g(l,this)==e),t>=0&&this.selection.splice(t,1),t=this.detailSelection.findIndex(l=>g(l,this)==e),t>=0&&(this.detailSelection.splice(t,1),this.detailSelection.length==0&&(this.editingDetailSelection=n.detail.autoEdit)),n.limitTransientDataToLocalCollection&&delete this.transientData[e]}},async saveResource(e){const r=g(e,this),t=r==null,l=await $({method:t?"post":"put",url:t?n.restCollectionUrl:`${n.restCollectionUrl}/${r}`,data:e});if(l.status==200){const d=l.data,u=g(d,this),h=u?await this.refreshResource(u):null;return t&&this.recordInsertion(h||Object.freeze(d)),h||d}return null},async saveResources(e){const r=await $({method:"put",url:n.restCollectionUrl,data:e});if(r.status==200){const t=r.data||[],l=[];for(const[d,u]of t.entries()){const h=g(e[d],this)==null,S=g(u,this),D=S?await this.refreshResource(S):null;h&&this.recordInsertion(D||u),l.push(D||Object.freeze(u))}return l}return[]},clearTransientData(){this.transientData={}},setInvalidResourceIds(e){s.isEqual(e,this.invalidResources.map(r=>g(r,this)))||(this.invalidResources=e.map(r=>this.resources.find(t=>g(t,this)==r)).filter(r=>r!=null))},setTransientDataForResources(e){const{transientData:r,merge:t=!0}=e;t?s.merge(this.transientData,r):s.assign(this.transientData,r)},clearSelection(){this.setSelection([])},deselectResources(e){const r=this.selection.map(t=>g(t,this)).filter(t=>t!=null&&!e.includes(t));this.setSelection(r)},selectResources(e,r){const{addToSelection:t,edit:l}=r||{},d=this.selection.map(h=>g(h,this));let u=e;if(t){const h=s.difference(e,d);h.length>0&&(u=d.concat(h).filter(S=>S!=null))}s.isEqual(d,u)||this.setSelection(u,l)},setSelection(e,r){if(!s.isEqual(e,this.selection.map(t=>g(t,this)))){let t=!0;if(this.editors.length>0){if(n.detail.autoFromSingleSelection&&this.selection.length==1)t=!1;else if(n.detail.autoFromMultipleSelection&&n.detail.allowMultiple&&this.selection.length>1)t=!1;else if(n.detail.constrainToSelection){const l=this.selection.map(h=>g(h,this)),u=this.detailSelection.map(h=>g(h,this)).filter(h=>l.includes(h));this.detailSelection.filter(h=>!u.includes(g(h,this))).length>0&&(t=!1)}}if(!t)return;if(this.selection=e.map(l=>this.resources.find(d=>g(d,this)==l)).filter(l=>l!=null),n.detail.autoFromSingleSelection&&this.selection.length==1){const l=this.selection[0];(this.detailSelection.length!=1||g(this.detailSelection[0],this)!=g(l,this))&&(this.detailSelection=[l])}else if(n.detail.autoFromMultipleSelection&&n.detail.allowMultiple&&this.selection.length>1)s.isEqual(this.selection.map(l=>g(l,this)),this.detailSelection.map(l=>g(l,this)))||(this.detailSelection=s.clone(this.selection));else if(n.detail.constrainToSelection){const l=this.selection.map(h=>g(h,this)),d=this.detailSelection.map(h=>g(h,this)),u=d.filter(h=>l.includes(h));s.isEqual(u,d)||(this.detailSelection=this.detailSelection.filter(h=>u.includes(g(h,this))))}this.detailSelection.length==0?this.editingDetailSelection=!1:this.editingDetailSelection=r!==void 0?r:this.editingDetailSelection||n.detail.autoEdit}},hideDetail(){this.detailSelection.length>0&&(this.detailSelection=[],this.editingDetailSelection=!1)},showSelectionAsDetail(e){if(this.selection.length<=1||n.detail.allowMultiple){const r=this.selection.map(l=>g(l,this)),t=this.detailSelection.map(l=>g(l,this));s.isEqual(r,t)||(this.detailSelection=s.clone(this.selection)),this.detailSelection.length==0?this.editingDetailSelection=!1:this.editingDetailSelection=e!==void 0?e:this.editingDetailSelection||n.detail.autoEdit}},deregisterEditor(e){s.remove(this.editors,r=>r==e)},registerEditor(e){this.editors.includes(e)||this.editors.push(e)},deregisterListNavigator({name:e,listNavigator:r}){(r==null||this.listNavigators[e]==r)&&delete this.listNavigators[e]},registerListNavigator({name:e,listNavigator:r}){this.listNavigators[e]!=r&&(this.listNavigators[e]=r)}}});return(()=>{const e=(r,t)=>{const l=U(r,t);return ee.registerStore(l.$id,l),l};return e.$id=U.$id,e})()},ee=new de,Ee=(w={})=>{var ge,ve,pe,me;const{collectionId:n,draftBatchId:U,enabled:q,resourceType:e,options:r}=w,t=a.ref(void 0),l=a.ref(n),d=a.ref(U),u=a.ref(q??!0),h=a.ref([a.toRef(e)]),S=a.ref(r),D=a.computed(()=>d.value),L=a.ref(u.value),b=a.ref(I.v4()),N=a.ref((ve=(ge=S.value)==null?void 0:ge.filter)==null?void 0:ve.resourceIds),k=a.ref((me=(pe=S.value)==null?void 0:pe.filter)==null?void 0:me.query),B=a.computed(()=>d.value==null&&l.value!=null&&ee.getStore(l.value)!=null),Q=a.ref(B.value),p=a.computed(()=>T("status","Uninitialized"));function m(i){if(t.value)return i(t.value)}async function C(i){if(t.value)return await i(t.value)}function T(i,c){if(t.value)return t.value[i];if(c!==void 0)return c;throw"Attempted to use a REST collection store that has not been created, with no default value."}const y=(i={})=>{var ie,Re,Se,ye;const{collectionId:c,draftBatchId:E,enabled:F,resourceType:ne,options:te}=i;l.value=c,d.value=E,u.value=F??!0,h.value=[a.toRef(ne)],te!=null&&(S.value=te,N.value=(Re=(ie=S.value)==null?void 0:ie.filter)==null?void 0:Re.resourceIds,k.value=(ye=(Se=S.value)==null?void 0:Se.filter)==null?void 0:ye.query),b.value=I.v4(),H(u.value)},H=async i=>{i!=L.value&&(L.value=i,i&&(_(),z()))},Z=async i=>{N.value=i,await C(async c=>await c.setFilterResourceIds(N.value||null))},V=async i=>{if(k.value=i,p.value!="Uninitialized"){const c=i,E=a.computed(()=>s.isFunction(c)?R.value?c(R.value):null:c);await C(F=>F.setQuery(E.value||null))}},_=()=>{S.value=s.merge({},S.value||{},{filter:{resourceIds:N.value,query:k.value}})},R=a.computed(()=>h.value[0].value||void 0),O=function(i){const c=s.get(i,"filter.query"),E=s.get(i,"propertyBlacklist"),F=s.get(i,"referencePathsToExpand"),ne=s.isFunction(c)?c(R.value):c,te=R.value?s.isFunction(E)?E(R.value):E||[]:[],ie=R.value?s.isFunction(F)?F(R.value):F||[]:[];return{...s.cloneDeep(i||{}),filter:{...s.cloneDeep((i==null?void 0:i.filter)||{}),query:ne},propertyBlacklist:te,referencePathsToExpand:ie}},P=a.computed(()=>O(S.value)),M=a.computed(()=>{if(!R.value)return null;if(P.value.restCollectionUrl!==void 0)return P.value.restCollectionUrl;if(!Q.value)return!R.value||!R.value.collectionName?null:D.value?`${X.config.apiBaseUrl}/draft-batches/${D.value}/${R.value.collectionName}`:`${X.config.apiBaseUrl}/${R.value.collectionName}`}),o=a.computed(()=>{var i;if(R.value)return s.mergeWith(s.cloneDeep(he),P.value,{idProperty:(i=R.value)==null?void 0:i.idProperty,restCollectionUrl:M.value||""},(c,E)=>{if(s.isArray(E)&&E.length==0&&(c==null||s.isArray(c)))return E})}),f=a.ref(0),x=a.computed(()=>{var i;return f.value,l.value||R.value&&((i=R.value)==null?void 0:i.collectionName)&&(D.value?`draftBatches/${D.value}/${R.value.collectionName}/${b.value}`:`${R.value.collectionName}/${b.value}`)||null}),z=()=>{if(L.value){let i=!1;return R.value&&x.value&&(t.value=ee.getStore(x.value),t.value?i=!1:o.value&&(Q.value=!1,t.value=Ce(x.value,o.value)(),i=!0)),i}else return!1},J=()=>{x.value&&l.value==x.value?(t.value=void 0,z()):(t.value=void 0,f.value=f.value+1,z())};a.watch(D,(i,c)=>{s.isEqual(i,c)||J()}),a.watch(L,(i,c)=>{i!=c&&(i?z():t.value=void 0)}),a.watch(R,(i,c)=>{i&&!c?z():!i&&c||s.isEqual(i,c)||J()}),a.watch(o,(i,c)=>{s.isEqual(i,c)||J()}),a.watch(B,()=>{B.value||(Q.value=!1)}),a.watch(x,()=>z()),a.watch(M,(i,c)=>{s.isEqual(i,c)||J()});const W=[],qe=function(i){p.value!="Uninitialized"?a.nextTick(i):W.push(i)};return a.watch(p,(i,c)=>{if(i!="Uninitialized"&&c=="Uninitialized"){const E=s.clone(W);W.splice(0,W.length);for(const F of E)F()}}),z(),{collectionId:x,draftBatchId:D,resourceType:R,enabled:L,status:p,batchSaveAttempted:a.computed(()=>T("batchSaveAttempted",!1)),listNavigators:a.computed(()=>T("listNavigators",!1)),invalidResources:a.computed(()=>T("invalidResources",[])),resources:a.computed(()=>p.value!="Uninitialized"?(C(i=>i.ensureCollectionLoaded()),T("resources",[])):[]),remoteCollectionSize:a.computed(()=>T("remoteCollectionSize",null)),transientData:a.computed(()=>T("transientData",[])),detailSelection:a.computed(()=>T("detailSelection",[])),editingDetailSelection:a.computed(()=>T("editingDetailSelection",!1)),selection:a.computed(()=>T("selection",[])),reconfigureCollection:y,setEditingDetailSelection:i=>m(c=>c.setEditingDetailSelection(i)),setEnabled:H,setFilterResourceIds:Z,setQuery:V,ensureStore:z,setBatchSaveAttempted:i=>m(c=>c.setBatchSaveAttempted(i)),clear:()=>m(i=>i.clear()),ensureCollectionLoaded:async()=>await C(async i=>i.ensureCollectionLoaded()),loadResources:async()=>await C(async i=>await i.loadResources()),addResource:i=>m(c=>c.addResource(i)),checkForDeletedResource:async i=>await C(async c=>await c.checkForDeletedResource(i))==!0,recordDeletion:i=>m(c=>c.recordDeletion(i)),recordInsertion:(i,{insertAtBeginning:c=!1,transientData:E=void 0}={})=>m(F=>F.recordInsertion(i,{insertAtBeginning:c,transientData:E})),recordUpdate:i=>m(c=>c.recordUpdate(i)),refreshResource:async i=>await C(async c=>await c.refreshResource(i)),deleteResource:async i=>await C(async c=>await c.deleteResource(i)),saveResource:async i=>await C(async c=>await c.saveResource(i)),saveResources:async i=>await C(async c=>await c.saveResources(i)),clearTransientData:()=>m(i=>i.clearTransientData()),setInvalidResourceIds:i=>m(c=>c.setInvalidResourceIds(i)),setTransientDataForResources:i=>m(c=>c.setTransientDataForResources(i)),clearSelection:()=>m(i=>i.clearSelection()),deselectResources:i=>m(c=>c.deselectResources(i)),selectResources:(i,c)=>m(E=>E.selectResources(i,c)),hideDetail:()=>m(i=>i.hideDetail()),deregisterEditor:i=>m(c=>c.deregisterEditor(i)),registerEditor:i=>m(c=>c.registerEditor(i)),deregisterListNavigator:i=>m(c=>c.deregisterListNavigator(i)),registerListNavigator:i=>m(c=>c.registerListNavigator(i)),onItemsStoreReady:qe}},Ue=()=>{ee.clearAllStores()};function re(w,n){return s.get(w,(n==null?void 0:n.idProperty)||"_id")}const fe={idProperty:"_id"},Y={resource:null,status:"NotLoaded"},Te=(w,n)=>{n=s.merge({},fe,n);const U=ce.defineStore(w,{state:()=>({idProperty:n.idProperty,resourceId:n.resourceId||null,resourceUrl:n.resourceUrl||null,referencePathsToExpand:n.referencePathsToExpand||null,...Y}),getters:{currentResourceUrl:e=>e.resourceUrl||(n.restCollectionUrl&&e.resourceId?`${n.restCollectionUrl}/${e.resourceId}`:null)||n.restCollectionUrl||null,fixedQueryParams:e=>{const r={};return e.referencePathsToExpand&&(r.r=G(e.referencePathsToExpand)),r}},actions:{reset(){for(const e in Y)this[e]=Y[e]},async setResourceId(e){const r=this.currentResourceUrl;this.resourceId=e,!s.isEqual(this.currentResourceUrl,r)&&this.status!="NotLoaded"&&await this.loadResource()},async setResourceUrl(e){const r=this.currentResourceUrl;this.resourceUrl=e,!s.isEqual(this.currentResourceUrl,r)&&this.status!="NotLoaded"&&await this.loadResource()},clear(){for(const e in Y)this[e]=Y[e]},async ensureResourceLoaded(){["Loaded","Loading","Failed"].includes(this.status)||await this.loadResource()},async loadResource(){this.reset();const e=I.v4();if(this._loadId=e,this.currentResourceUrl){this.status="Loading";const r=this.fixedQueryParams,t=s.cloneDeep(r),l=s.map(t,(u,h)=>u!=null?`${encodeURIComponent(h)}=${encodeURIComponent(u)}`:null).filter(u=>u!=null).join("&"),d=s.isEmpty(l)?this.currentResourceUrl:`${this.currentResourceUrl}?${l}`;try{const u=await $.get(d),h=this.fixedQueryParams;e!=this._loadId||!s.isEqual(r,h)?console.log(`Discarding resource fetched by obsolete query from ${d}.`):(this.resource=oe.decode(s.get(u,"data",{})),this.status="Loaded")}catch(u){console.log(`Error while loading resource (URL="${d}")`,u),this.reset(),this.status="Failed"}}else throw"Cannot load a resource because its URL, or its ID and collection URL, are unknown."},async checkForDeletedResource(){if(this.currentResourceUrl){let e;try{e=await $.get(this.currentResourceUrl)}catch(r){$.isAxiosError(r)&&(e=r.response)}return e&&e.status==404?(this.recordDeletion(),!0):!1}else throw"Cannot check for a deleted resource because its URL, or its ID and collection URL, are unknown."},recordDeletion(){this.resource=null,this.status="Loaded"},recordInsertion(e){this.resourceId=re(e,this)||null,this.status=="Loaded"&&(this.resource=Object.freeze(e))},recordUpdate(e){this.resourceId=re(e,this)||null,this.status=="Loaded"&&(this.resource=Object.freeze(e))},async refreshResource(){return await this.loadResource(),this.resource},async deleteResource(){if(this.currentResourceUrl)(await $.delete(this.currentResourceUrl)).status==200&&this.recordDeletion();else throw"Cannot delete a resource because its URL, or its ID and collection URL, are unknown."},async saveResource(e){const r=re(e,this),t=r==null;if(!r)throw"The REST resource store cannot create new resources on the server.";if(!this.currentResourceUrl)throw"Cannot save a resource because its URL, or its ID and collection URL, are unknown.";const l=await $({method:t?"post":"put",url:this.currentResourceUrl,data:e});if(l.status==200){const d=l.data,u=await this.refreshResource();return t&&this.recordInsertion(u||d),u||Object.freeze(d)}return null},async makeCustomApiRequest(e){const{method:r,subpath:t,url:l,data:d}=e;let u=l;if(!u)if(this.currentResourceUrl)u=`${this.currentResourceUrl}/${t}`;else throw"Cannot make a custom API request for a resource because its URL, or its ID and collection URL, are unknown.";const h=await $({method:r||"get",url:u,data:d});return h.status>=200&&h.status<300?h.data:null}}});return(()=>{const e=(r,t)=>{const l=U(r,t);return le.registerStore(l.$id,l),l};return e.$id=U.$id,e})()},le=new de,Le=(w={})=>{const{resourceClientId:n,draftBatchId:U,enabled:q,resourceType:e,options:r}=w,t=a.ref(void 0),l=a.ref(n),d=a.ref(U),u=a.ref(q??!0),h=a.ref([a.toRef(e)]),S=a.ref(r),D=a.computed(()=>d.value),L=a.ref(u.value),b=a.ref(I.v4()),N=a.computed(()=>d.value==null&&l.value!=null&&le.getStore(l.value)!=null),k=a.ref(N.value),B=a.computed(()=>m("status","Uninitialized"));function Q(o){if(t.value)return o(t.value)}async function p(o){if(t.value)return await o(t.value)}function m(o,f){if(t.value)return t.value[o];if(f!==void 0)return f;throw"Attempted to use a REST resource store that has not been created, with no default value."}const C=(o={})=>{const{resourceClientId:f,draftBatchId:x,enabled:z,resourceType:J,options:W}=o;l.value=f,d.value=x,u.value=z??!0,h.value=[a.toRef(J)],W!=null&&(S.value=W),b.value=I.v4(),T(u.value)},T=async o=>{o!=L.value&&(L.value=o,o&&P())},y=a.computed(()=>h.value[0].value||void 0),H=function(o){const f=s.get(o,"referencePathsToExpand"),x=y.value?s.isFunction(f)?f(y.value):f||[]:[];return{...s.cloneDeep(o||{}),referencePathsToExpand:x}},Z=a.computed(()=>H(S.value)),V=a.computed(()=>{if(!y.value)return null;if(!k.value)return!y.value||!y.value.collectionName?null:D.value?`${X.config.apiBaseUrl}/draft-batches/${D.value}/${y.value.collectionName}`:`${X.config.apiBaseUrl}/${y.value.collectionName}`}),_=a.computed(()=>{if(y.value&&V.value)return s.mergeWith(s.cloneDeep(fe),Z.value,{idProperty:y.value.idProperty,restCollectionUrl:V.value},(o,f)=>{if(s.isArray(f)&&f.length==0&&(o==null||s.isArray(o)))return f})}),R=a.ref(0),O=a.computed(()=>{var o;return R.value,l.value||y.value&&((o=y.value)==null?void 0:o.collectionName)&&(D.value?`draftBatches/${D.value}/${y.value.collectionName}/${b.value}`:`${y.value.collectionName}/${b.value}`)||null}),P=()=>{if(L.value){let o=!1;return y.value&&O.value&&(t.value=le.getStore(O.value),t.value?o=!1:_.value&&(k.value=!1,t.value=Te(O.value,_.value)(),o=!0)),o}else return!1},M=()=>{O.value&&l.value==O.value?(t.value=void 0,P()):(t.value=void 0,R.value=R.value+1,P())};return a.watch(D,(o,f)=>{s.isEqual(o,f)||M()}),a.watch(L,(o,f)=>{o!=f&&(o?P():t.value=void 0)}),a.watch(y,(o,f)=>{o&&!f?P():!o&&f||s.isEqual(o,f)||M()}),a.watch(_,(o,f)=>{s.isEqual(o,f)||M()}),a.watch(N,()=>{N.value||(k.value=!1)}),a.watch(O,()=>P()),a.watch(V,(o,f)=>{s.isEqual(o,f)||M()}),P(),{resourceClientId:O,draftBatchId:D,enabled:L,resourceType:y,status:B,resource:a.computed(()=>B.value!="Uninitialized"?(p(o=>o.ensureResourceLoaded()),m("resource",[])):[]),resourceId:a.computed(()=>m("resourceId",null)),resourceUrl:a.computed(()=>m("resourceUrl",null)),reconfigureClient:C,setEnabled:T,setResourceId:async o=>await p(async f=>await f.setResourceId(o)),setResourceUrl:async o=>await p(async f=>await f.setResourceUrl(o)),ensureStore:P,clear:()=>Q(o=>o.clear()),ensureResourceLoaded:async()=>await p(async o=>o.ensureResourceLoaded()),loadResource:async()=>await p(async o=>await o.loadResource()),checkForDeletedResource:async()=>await p(async o=>await o.checkForDeletedResource())==!0,recordDeletion:()=>p(o=>o.recordDeletion()),recordInsertion:o=>p(f=>f.recordInsertion(o)),recordUpdate:o=>p(f=>f.recordUpdate(o)),refreshResource:async()=>await p(async o=>await o.refreshResource()),deleteResource:async()=>await p(async o=>await o.deleteResource()),saveResource:async o=>await p(async f=>await f.saveResource(o)),makeCustomApiRequest:async o=>await p(async f=>await f.makeCustomApiRequest(o))}},X={config:{...ue}};function $e(w){X.config=s.merge({},ue,w)}v.ApiClientError=we,v.clearAllRestCollections=Ue,v.initRestClient=$e,v.restClient=X,v.useRestCollection=Ee,v.useRestResource=Le,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|