@mee4dy/crud
Version:
Create a backend and frontend in 5 minutes! With our powerful full stack crud system, customize it to suit you.
3 lines (2 loc) • 2.01 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("lodash"),d=require("qs"),c=require("axios"),h=require("../orm/orm.cjs");require("deep-object-diff");function l(s){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const i in s)if(i!=="default"){const e=Object.getOwnPropertyDescriptor(s,i);Object.defineProperty(t,i,e.get?e:{enumerable:!0,get:()=>s[i]})}}return t.default=s,Object.freeze(t)}const u=l(a);class p{constructor(t){this.state={loading:!1},this.pk=t.pk||"id",this.endpoints=t.endpoints,this.orm=t.orm||!0,this.http={baseURL:void 0,headers:{},timeout:void 0,abort:!0,...t.http||{}},this.axios=c.create({baseURL:this.http.baseURL,headers:this.http.headers,timeout:this.http.timeout})}async fetch(t){var e,r;const i=this.getEndpoint("fetch");this.setLoading(!0),this.http.abort&&(this.abortController&&this.abortController.abort(),this.abortController=new AbortController);try{const n=await this.axios.get(i,{signal:this.abortController.signal,params:t,paramsSerializer:o=>d.stringify(o,{arrayFormat:"brackets"})});return this.prepareItems(((r=(e=n==null?void 0:n.data)==null?void 0:e.data)==null?void 0:r.items)||[])}catch(n){throw n}finally{this.setLoading(!1)}}async create(t){const i=this.getEndpoint("create"),e=await this.axios.post(i,{data:t});return e==null?void 0:e.data}async update(t,i){const e=this.getEndpoint("update",t),r=await this.axios.post(e,{pk:t,data:i});return r==null?void 0:r.data}async delete(t){const i=this.getEndpoint("delete",t);return await this.axios.post(i,{pk:t})}getEndpoint(t,i){let e=this.endpoints[t];if(!e)throw new Error("Endpoint fetch is not found!");return i&&(e=e.split(":pk").join(i.toString())),e}setLoading(t){this.state.loading=t}prepareItems(t){if(this.orm){const i=u.cloneDeep(t);return new h.ORM(i,this.pk,{update:({pk:r,data:n,level:o})=>this.update(r,n),delete:({pk:r,level:n})=>this.delete(r)}).getItems()}return t}}exports.CrudClient=p;
//# sourceMappingURL=index.cjs.map