UNPKG

@ferry-core/ferry

Version:

common fetch tool for ferry service

7 lines 3.95 kB
/*! * @ferry-core/ferry with v0.1.2 * Author: yanpan * Built on 2023-09-03, 23:14:37 * Released under the MIT License Copyright (c) 2023 */ import e from"@ferry-core/oar";function t(e,t,n,r){return new(n||(n=Promise))((function(s,o){function a(e){try{c(r.next(e))}catch(e){o(e)}}function i(e){try{c(r.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,i)}c((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const n="Request fail",r=null;class s{constructor(t){this.baseURL="",this.headers={},this.oarClient=e.create(t)}static getInstance(e){return this.ferryInstance||(this.ferryInstance=new s(e)),this.ferryInstance}apiOar(e){var{baseURL:t=this.baseURL,headers:s=this.headers,method:o,url:a,data:i,params:c,responseType:d,customCodes:l=[0]}=e,u=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(r=Object.getOwnPropertySymbols(e);s<r.length;s++)t.indexOf(r[s])<0&&Object.prototype.propertyIsEnumerable.call(e,r[s])&&(n[r[s]]=e[r[s]])}return n}(e,["baseURL","headers","method","url","data","params","responseType","customCodes"]);const f=Object.assign({baseURL:t,headers:s,method:o,url:a,params:c,data:i,responseType:d},u);return new Promise(((e,t)=>{this.oarClient(f).then((t=>{var r,s,o,a,i,c,d,u;const f=Object.assign(Object.assign({},t),{statusCode:t.status,code:null===(r=t.data)||void 0===r?void 0:r.code,data:null===(s=t.data)||void 0===s?void 0:s.data,msg:(null===(o=t.data)||void 0===o?void 0:o.msg)||(null===(a=t.data)||void 0===a?void 0:a.message),ttl:null===(i=t.data)||void 0===i?void 0:i.ttl,origin:t.data,config:t.config});if([200,201,204].includes(null==t?void 0:t.status)){const r=null===(c=t.data)||void 0===c?void 0:c.code,s=Array.isArray(l)?l:[+l];null!=r&&s.includes(r)?e(Object.assign(Object.assign({},f),{status:!0,type:"success"})):e(Object.assign(Object.assign({},f),{status:!1,msg:f.msg||(null===(d=t.data)||void 0===d?void 0:d.errorMessage)||n,type:"fail"}))}else e(Object.assign(Object.assign({},f),{status:!1,msg:f.msg||(null===(u=t.data)||void 0===u?void 0:u.errorMessage)||n,type:"fail"}))})).catch((e=>{var n;const{status:s,config:o,data:a=null,code:i=r}=e.response,c=(null===(n=null==e?void 0:e.data)||void 0===n?void 0:n.errorMessage)||(null==e?void 0:e.msg)||(null==e?void 0:e.message)||"Network error or server crush";t(Object.assign(Object.assign({},e),{error:e,status:!1,statusCode:s,code:i,config:o||f,msg:c,data:a,type:"error"}))}))}))}oarInstance(){return this.oarClient}getReq(e){return this.apiOar(Object.assign(Object.assign({},e),{method:"GET"}))}postReq(e){return this.apiOar(Object.assign(Object.assign({},e),{method:"POST"}))}putReq(e){return this.apiOar(Object.assign(Object.assign({},e),{method:"PUT"}))}deleteReq(e){return this.apiOar(Object.assign(Object.assign({},e),{method:"DELETE"}))}}const o=s.getInstance({timeout:1e4,responseType:"json"}),a=(e,n,r=console.log)=>(...s)=>t(void 0,void 0,void 0,(function*(){try{return yield e.apply(n,s)}catch(e){r(e)}}));function i(e){return a(o.getReq,o,null==e?void 0:e.errorHandler)(e)}function c(e){return a(o.postReq,o,null==e?void 0:e.errorHandler)(e)}function d(e){return a(o.putReq,o,null==e?void 0:e.errorHandler)(e)}function l(e){return a(o.deleteReq,o,null==e?void 0:e.errorHandler)(e)}const u=o.oarInstance(),f="undefined"!=typeof window?window:void 0;f&&(f.injectInfo=f.injectInfo||{}),f&&f.injectInfo&&!f.injectInfo.ferryGet&&(f.injectInfo.ferryGet=i),f&&f.injectInfo&&!f.injectInfo.ferryPst&&(f.injectInfo.ferryPst=c),f&&f.injectInfo&&!f.injectInfo.ferryPut&&(f.injectInfo.ferryPut=d),f&&f.injectInfo&&!f.injectInfo.ferryDel&&(f.injectInfo.ferryDel=l),f&&f.injectInfo&&!f.injectInfo.ferry&&(f.injectInfo.ferry=o);export{u as default,u as ferry,l as ferryDel,i as ferryGet,c as ferryPst,d as ferryPut};