axios-wrapped
Version:
A wrapped version of Axios with additional functionality
3 lines (2 loc) • 6.65 kB
JavaScript
import t from"axios";const e="content-type",r='"name" should be type of string',s="'params' should be Array of key-value pairs, Map, object as dictionary";var n;function o(t){return Array.isArray(t)}function i(t){return!!a(t)||(o(t)||c(t)?0===t.length:!!u(t)&&0===Object.keys(t).length)}function a(t){return null==t}function u(t){const e=typeof t;return null!==t&&("object"===e||"function"===e)}function h(t,e){if(a(t))return[];if(o(t))return t.map(((r,s)=>e(r,s,t)));if(u(t)){const r=[],s=Object.keys(t);for(let n=0;n<s.length;n++){const o=s[n];r.push(e(t[o],o,t))}return r}return[]}function c(t){return"string"==typeof t}function l(t,e){return a(t)?[]:o(t)?function(t,e){return t.filter(((r,s)=>e(r,s,t)))}(t,e):u(t)?function(t,e){const r=[];for(const s in t)if(Object.prototype.hasOwnProperty.call(t,s)){const n=t[s];e(n,s,t)&&r.push(n)}return r}(t,e):[]}!function(t){t.Get="GET",t.Delete="DELETE",t.Head="HEAD",t.Options="OPTIONS",t.Post="POST",t.Put="PUT",t.Patch="PATCH",t.Link="LINK",t.Unlink="UNLINK"}(n||(n={}));const d=t=>a(t)||(function(t){return"object"==typeof t&&null!==t}(t)||c(t))&&i(t),y=t=>{const e={};for(const[r,s]of t)e[r]=s;return e};class f extends Error{payload;type;constructor(t,e){super("string"==typeof t?t:t?.message||""),this.payload=t,this.type=e??new.target.name}getPayload(){return this.payload}getType(){return this.type}static createPayload(t,e){return t?u(t)&&!o(t)?t:{message:t,error:e}:{message:e}}}class p extends f{constructor(t,e="Something Went Wrong!"){super(f.createPayload(t,e))}}class TypeMismatchException extends p{constructor(t,e="Type Mismatch"){super(t,e),Error.captureStackTrace&&Error.captureStackTrace(this,new.target)}}class m{url;endpoint;method;headers;params;query;body;successHooks;errorHooks;constructor(t){this.initialize(),this.url=t??null}getUrl(){return this.url}setUrl(t){return this.url=t,this}getEndpoint(){return this.endpoint}setEndpoint(t){return this.endpoint=t,this}getMethod(){return this.method}setMethod(t){return this.method=t,this}getContentType(){return this.headers.get(e)}setContentType(t){return this.headers.set(e,t),this}getHeader(t){return this.headers.get(t.toLowerCase())}hasHeader(t){return this.headers.has(t.toLowerCase())}addHeader(t,e,s){if(u(t)){const{key:e,value:r}=t;return this.headers.set(e.toLowerCase(),r),this}if("string"!=typeof t)throw new TypeMismatchException(r);return this.headers.set(t.toLowerCase(),this.normalizeValue(e,s)),this}removeHeader(t){return u(t)&&this.headers.delete(t.key.toLowerCase()),this.headers.delete(t.toLowerCase()),this}setHeaders(t){if(o(t)){for(const{key:e,value:r}of t)this.headers.set(e.toLowerCase(),r.toString());return this}if(t instanceof Map){for(const[e,r]of t)this.headers.set(e.toLowerCase(),r.toString());return this}if(u(t)){for(const e in t){const r=t[e];this.headers.set(e.toLowerCase(),r.toString())}return this}throw new TypeMismatchException("'headers' should be Array of key-value pairs, Map, object as dictionary")}getParam(t){return this.params.get(t)}hasParam(t){return this.params.has(t)}addParam(t,e,s){if(u(t)){const{key:e,value:r}=t;return this.params.set(e,r),this}if("string"!=typeof t)throw new TypeMismatchException(r);return this.params.set(t,this.normalizeValue(e,s)),this}removeParam(t){return u(t)&&this.params.delete(t.key),this.params.delete(t),this}setParams(t){if(o(t)){for(const{key:e,value:r}of t)this.params.set(e,r.toString());return this}if(t instanceof Map){for(const[e,r]of t)this.params.set(e,r.toString());return this}if(u(t)){for(const e in t){const r=t[e];this.params.set(e,r.toString())}return this}throw new TypeMismatchException(s)}getQueryParam(t){return this.query.get(t)}hasQueryParam(t){return!d(this.query.get(t))}addQueryParam(t,e,s){if(u(t)&&!o(t)){const{key:e,value:r}=t,s=this.query.get(e);return this.query.set(e,this.updatedValue(s,r)),this}if("string"!=typeof t)throw new TypeMismatchException(r);const n=this.query.get(t);return this.query.set(t,this.updatedValue(n,e,s)),this}removeQueryParam(t){return u(t)&&this.query.delete(t.key),this.query.delete(t),this}setQueryParams(t){if(o(t)){for(const{key:e,value:r}of t)this.query.set(e,r);return this}if(t instanceof Map){for(const[e,r]of t)this.query.set(e,r);return this}if(u(t)){for(const e in t){const r=t[e];this.query.set(e,r)}return this}throw new TypeMismatchException(s)}getBody(){return this.body}hasBody(){return!d(this.body)}setBody(t){return this.body=t,this}addOnSuccessHook(t){return this.successHooks.push(t),this}addOnErrorHook(t){return this.errorHooks.push(t),this}initialize(){this.url=null,this.endpoint="",this.headers=new Map,this.method=n.Get,this.params=new Map,this.query=new Map,this.body=null,this.successHooks=[],this.errorHooks=[]}normalizeValue(t,e){if(t instanceof Date||o(t)&&function(t){if(!a(t)&&0!==t.length)return t[0]}(t)instanceof Date)return e=e??(t=>t.toISOString()),o(t)?h(t,e):e(t);if(o(t)){const e=l(h(t,(t=>a(t)?t:t.toString())),(t=>!a(t)));return i(e)?null:e}return d(t)?null:t.toString()}updatedValue(t,e,r){const s=this.normalizeValue(e,r);return o(t)?o(s)?[...t,...s]:[...t,s]:a(t)?s:o(s)?[t,...s]:[t,s]}}const g=t=>(async(t,e={})=>{const{suppressErrors:r=!1}=e;return h(await Promise.allSettled(t),(t=>{if("fulfilled"===t.status)return t.value;if(!r)throw t.reason;return null}))})(t,{suppressErrors:!1});class w extends m{instance;abortController;constructor(t,e){super(t),this.init(e)}build(){return{execute:async()=>{try{const t=await this.createRequestObject();return await this.callSuccessHooks(t)?await this.buildAndExecWithoutRetry():t.data}catch(t){if(await this.callErrorHooks(t,!0))return await this.buildAndExecWithoutRetry()}},abort:()=>{this.abortController.abort()}}}async buildAndExecWithoutRetry(){let t=null;try{const e=await this.createRequestObject();await this.callSuccessHooks(e),t=e.data}catch(t){await this.callErrorHooks(t,!1)}return t}getInstance(){return this.instance}init(e){this.instance=e??t.create(),this.abortController=new AbortController}buildUrl=()=>{let t=this.url;d(this.endpoint)||(t+=this.endpoint);for(const[e,r]of this.params)t=t.replace(":"+e,r);return t};callSuccessHooks=async t=>(await g(h(this.successHooks,(e=>e(t,this))))).some((t=>!a(t)&&!!t.retry));callErrorHooks=async(t,e)=>(await g(h(this.errorHooks,(r=>r(t,this,e))))).some((t=>!a(t)&&t.retry));buildHeaders=()=>y(this.headers);buildQueryParams=()=>y(this.query);createRequestObject=()=>this.instance.request({url:this.buildUrl(),method:this.method.toString(),headers:this.buildHeaders(),params:this.buildQueryParams(),signal:this.abortController.signal,data:this.body??void 0})}export{n as EHttpMethod,w as Request};
//# sourceMappingURL=index.mjs.map