rest-client-vue 1.2.0-b1 → 1.2.0-b2

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