@ferry-core/ferry
Version:
common fetch tool for ferry service
7 lines • 4.04 kB
JavaScript
/*!
* @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
*/
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@ferry-core/oar");function t(e,t,r,n){return new(r||(r=Promise))((function(s,o){function a(e){try{c(n.next(e))}catch(e){o(e)}}function i(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const r="Request fail",n=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:u=[0]}=e,l=function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)t.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(r[n[s]]=e[n[s]])}return r}(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},l);return new Promise(((e,t)=>{this.oarClient(f).then((t=>{var n,s,o,a,i,c,d,l;const f=Object.assign(Object.assign({},t),{statusCode:t.status,code:null===(n=t.data)||void 0===n?void 0:n.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 n=null===(c=t.data)||void 0===c?void 0:c.code,s=Array.isArray(u)?u:[+u];null!=n&&s.includes(n)?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)||r,type:"fail"}))}else e(Object.assign(Object.assign({},f),{status:!1,msg:f.msg||(null===(l=t.data)||void 0===l?void 0:l.errorMessage)||r,type:"fail"}))})).catch((e=>{var r;const{status:s,config:o,data:a=null,code:i=n}=e.response,c=(null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.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,r,n=console.log)=>(...s)=>t(void 0,void 0,void 0,(function*(){try{return yield e.apply(r,s)}catch(e){n(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 u(e){return a(o.deleteReq,o,null==e?void 0:e.errorHandler)(e)}const l=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=u),f&&f.injectInfo&&!f.injectInfo.ferry&&(f.injectInfo.ferry=o),exports.default=l,exports.ferry=l,exports.ferryDel=u,exports.ferryGet=i,exports.ferryPst=c,exports.ferryPut=d;