@flumens/models 0.15.0 → 0.16.1

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),s=require("axios"),i=require("@flumens/utils/dist/errors"),r=require("../Collection.js"),o=require("./Group.js"),a=require("./helpers.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=n(s);class d extends r.default{constructor(e){super(Object.assign({id:"groups",Model:o.default},e)),this.remote=t.observable({synchronising:!1,url:e.url,getAccessToken:e.getAccessToken})}fetchRemote(){return e.__awaiter(this,arguments,void 0,(function*(e={}){console.log(`📚 Collection: ${this.id} collection fetching`),this.remote.synchronising=!0;try{const{type:t,form:s,location:i}=e,r=yield this.remote.getAccessToken(),o=`${this.remote.url}/index.php/services/rest/groups`,n={};t&&(n.view=t),s&&(n.page=s.join(",")),Number.isFinite(i)&&(n.indexed_location_ids=i);const d={params:n,headers:{Authorization:`Bearer ${r}`},timeout:8e4},u=yield l.default.get(o,d),c=e=>!e.toDate||new Date(e.toDate)>new Date,h=e=>!e.fromDate||new Date(e.fromDate)<new Date,m=u.data.map((({values:e})=>e)).map(a.getCamelCaseObj).filter(c).filter(h),f=m.map((e=>this.Model.fromDTO(e,{skipStore:"joinable"===t})));if(t){if("member"===t)for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&"t"===t.data.userIsMember&&(t.destroy(),this.data.splice(e,1),e--)}else if("pending"===t)for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&"t"===t.data.userIsPending&&(t.destroy(),this.data.splice(e,1),e--)}else if("joinable"===t)for(let e=0;e<this.data.length;e++){this.data[e].isStored||(this.data.splice(e,1),e--)}}else for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&t.destroy(),this.data.splice(e,1),e--}"member"===t&&(yield Promise.all(f.map((e=>e.save())))),"pending"===t&&(yield Promise.all(f.map((e=>e.save())))),this.data.push(...f),console.log(`📚 Collection: ${this.id} collection fetching done ${m.length} documents`)}catch(e){if(l.default.isCancel(e))return;if(i.isAxiosNetworkError(e))throw new i.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in e){throw new Error(e.issues.map((e=>`${e.path.join(" ")} ${e.message}`)).join(" "))}throw this.remote.synchronising=!1,e}this.remote.synchronising=!1}))}get isSynchronising(){return this.remote.synchronising}}exports.byGroupMembershipStatus=function(e){return t=>{const s=t.data.userIsMember||t.data.userIsPending;return"member"===e?s:!s}},exports.default=d;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),s=require("axios"),i=require("@flumens/utils/dist/errors"),r=require("../Collection.js"),o=require("./Group.js"),a=require("./helpers.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=n(s);class d extends r.default{constructor(e){super(Object.assign({id:"groups",Model:o.default},e)),this.remote=t.observable({synchronising:!1,url:e.url,getAccessToken:e.getAccessToken})}fetchRemote(){return e.__awaiter(this,arguments,void 0,(function*(e={}){console.log(`📚 Collection: ${this.id} collection fetching`),this.remote.synchronising=!0;try{const{type:t,form:s,location:i}=e,r=yield this.remote.getAccessToken(),o=`${this.remote.url}/index.php/services/rest/groups`,n={};t&&(n.view=t),s&&(n.page=s.join(",")),Number.isFinite(i)&&(n.indexed_location_ids=i);const d={params:n,headers:{Authorization:`Bearer ${r}`},timeout:8e4},u=yield l.default.get(o,d),c=e=>!e.toDate||new Date(e.toDate)>new Date,h=e=>!e.fromDate||new Date(e.fromDate)<new Date,m=u.data.map((({values:e})=>e)).map(a.getCamelCaseObj).filter(c).filter(h),f=m.map((e=>this.Model.fromDTO(e,{skipStore:"joinable"===t})));if(t){if("member"===t)for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&"t"===t.data.userIsMember&&(yield t.destroy(),this.data.splice(e,1),e--)}else if("pending"===t)for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&"t"===t.data.userIsPending&&(yield t.destroy(),this.data.splice(e,1),e--)}else if("joinable"===t)for(let e=0;e<this.data.length;e++){this.data[e].isStored||(this.data.splice(e,1),e--)}}else for(let e=0;e<this.data.length;e++){const t=this.data[e];t.isStored&&(yield t.destroy()),this.data.splice(e,1),e--}"member"===t&&(yield Promise.all(f.map((e=>e.save())))),"pending"===t&&(yield Promise.all(f.map((e=>e.save())))),this.data.push(...f),console.log(`📚 Collection: ${this.id} collection fetching done ${m.length} documents`)}catch(e){if(l.default.isCancel(e))return;if(i.isAxiosNetworkError(e))throw new i.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in e){throw new Error(e.issues.map((e=>`${e.path.join(" ")} ${e.message}`)).join(" "))}throw this.remote.synchronising=!1,e}this.remote.synchronising=!1}))}get isSynchronising(){return this.remote.synchronising}}exports.byGroupMembershipStatus=function(e){return t=>{const s=t.data.userIsMember||t.data.userIsPending;return"member"===e?s:!s}},exports.default=d;
package/dist/Model.d.ts CHANGED
@@ -20,7 +20,7 @@ export default class Model<T extends Data = Data> {
20
20
  /**
21
21
  * Remote server document ID.
22
22
  */
23
- id: string;
23
+ id?: string;
24
24
  /**
25
25
  * Key name of the model used to save and fetch the model from.
26
26
  */
@@ -77,7 +77,7 @@ export default class Model<T extends Data = Data> {
77
77
  * Returns a clean (no observables) JSON representation of the model.
78
78
  */
79
79
  toJSON(): {
80
- id: string;
80
+ id: string | undefined;
81
81
  cid: string;
82
82
  createdAt: number;
83
83
  updatedAt: number;
package/dist/Model.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),s=require("mobx-utils"),i=require("@flumens/utils");const d=t=>JSON.parse(JSON.stringify(t)),r={};exports.copyObject=d,exports.default=class{constructor({id:t="",cid:a="",createdAt:o,updatedAt:h,syncedAt:c,data:n={},attrs:u={},store:l}){this.ready=new i.Deferred,this.debouncedValue=3e3,this._observerPaused=!1,this.id=t,this.cid=a||t||i.UUIDv7();const v=Date.now();let y;this.createdAt=o||v,this.updatedAt=h||v,this.syncedAt=c,this.data=d(Object.assign(Object.assign(Object.assign({},r),n),u)),e.makeObservable(this,{createdAt:e.observable,updatedAt:e.observable,syncedAt:e.observable,data:e.observable}),this.store=l;const b=(...t)=>{clearTimeout(y),y=setTimeout((()=>this.sync.apply(this,t)),this.debouncedValue)};s.deepObserve(this.data,(()=>{this._observerPaused||(this.updatedAt=Date.now(),b())}))}setUpdatedAtTimestamp(t){this.updatedAt=t}fetch(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)return void this.ready.resolve(!1);const t=yield this.store.find(this.cid);if(!t)return yield this.save(),void this.ready.resolve(!0);t.id&&(this.id=t.id),t.cid&&(this.cid=t.cid),this._observerPaused=!0,e.set(this.data,t.data),this._observerPaused=!1,this.createdAt=t.createdAt,this.updatedAt=t.updatedAt,this.syncedAt=t.syncedAt,this.ready.resolve(!0)}))}save(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to save locally without a store");const t=this.toJSON();yield this.store.save(t)}))}destroy(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to delete locally without a store");yield this.store.delete(this.cid)}))}sync(...e){return t.__awaiter(this,void 0,void 0,(function*(){this.store&&(yield this.save())}))}toJSON(){return{id:this.id,cid:this.cid,createdAt:this.createdAt,updatedAt:this.updatedAt,syncedAt:this.syncedAt,data:e.toJS(this.data)}}reset(t){this.id="";const s=JSON.parse(JSON.stringify(Object.assign(Object.assign({},r),t)));e.set(this.data,s);const i=Object.keys(s);return Object.keys(this.data).forEach((t=>{i.includes(t)||(this.data[t]=null,delete this.data[t])})),this.save()}get isStored(){return!!this.store}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),s=require("mobx-utils"),i=require("@flumens/utils");const d=t=>JSON.parse(JSON.stringify(t)),r={};exports.copyObject=d,exports.default=class{constructor({id:t,cid:a="",createdAt:o,updatedAt:h,syncedAt:c,data:n={},attrs:u={},store:l}){this.ready=new i.Deferred,this.debouncedValue=3e3,this._observerPaused=!1,this.id=t,this.cid=a||t||i.UUIDv7();const v=Date.now();let y;this.createdAt=o||v,this.updatedAt=h||v,this.syncedAt=c,this.data=d(Object.assign(Object.assign(Object.assign({},r),n),u)),e.makeObservable(this,{createdAt:e.observable,updatedAt:e.observable,syncedAt:e.observable,data:e.observable}),this.store=l;const b=(...t)=>{clearTimeout(y),y=setTimeout((()=>this.sync.apply(this,t)),this.debouncedValue)};s.deepObserve(this.data,(()=>{this._observerPaused||(this.updatedAt=Date.now(),b())}))}setUpdatedAtTimestamp(t){this.updatedAt=t}fetch(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)return void this.ready.resolve(!1);const t=yield this.store.find(this.cid);if(!t)return yield this.save(),void this.ready.resolve(!0);t.id&&(this.id=t.id),t.cid&&(this.cid=t.cid),this._observerPaused=!0,e.set(this.data,t.data),this._observerPaused=!1,this.createdAt=t.createdAt,this.updatedAt=t.updatedAt,this.syncedAt=t.syncedAt,this.ready.resolve(!0)}))}save(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to save locally without a store");const t=this.toJSON();yield this.store.save(t)}))}destroy(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to delete locally without a store");yield this.store.delete(this.cid)}))}sync(...e){return t.__awaiter(this,void 0,void 0,(function*(){this.store&&(yield this.save())}))}toJSON(){return{id:this.id,cid:this.cid,createdAt:this.createdAt,updatedAt:this.updatedAt,syncedAt:this.syncedAt,data:e.toJS(this.data)}}reset(t){this.id="";const s=JSON.parse(JSON.stringify(Object.assign(Object.assign({},r),t)));e.set(this.data,s);const i=Object.keys(s);return Object.keys(this.data).forEach((t=>{i.includes(t)||(this.data[t]=null,delete this.data[t])})),this.save()}get isStored(){return!!this.store}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.15.0",
3
+ "version": "0.16.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {