axios-wrapped
Version:
A wrapped version of Axios with additional functionality
3 lines (2 loc) • 6.93 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("axios")):"function"==typeof define&&define.amd?define(["exports","axios"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).axiosWrapped={},t.axios)}(this,(function(t,e){"use strict";const r="content-type",s='"name" should be type of string',o="'params' should be Array of key-value pairs, Map, object as dictionary";var n;function i(t){return Array.isArray(t)}function a(t){return!!u(t)||(i(t)||l(t)?0===t.length:!!h(t)&&0===Object.keys(t).length)}function u(t){return null==t}function h(t){const e=typeof t;return null!==t&&("object"===e||"function"===e)}function c(t,e){if(u(t))return[];if(i(t))return t.map(((r,s)=>e(r,s,t)));if(h(t)){const r=[],s=Object.keys(t);for(let o=0;o<s.length;o++){const n=s[o];r.push(e(t[n],n,t))}return r}return[]}function l(t){return"string"==typeof t}function d(t,e){return u(t)?[]:i(t)?function(t,e){return t.filter(((r,s)=>e(r,s,t)))}(t,e):h(t)?function(t,e){const r=[];for(const s in t)if(Object.prototype.hasOwnProperty.call(t,s)){const o=t[s];e(o,s,t)&&r.push(o)}return r}(t,e):[]}t.EHttpMethod=void 0,(n=t.EHttpMethod||(t.EHttpMethod={})).Get="GET",n.Delete="DELETE",n.Head="HEAD",n.Options="OPTIONS",n.Post="POST",n.Put="PUT",n.Patch="PATCH",n.Link="LINK",n.Unlink="UNLINK";const f=t=>u(t)||(function(t){return"object"==typeof t&&null!==t}(t)||l(t))&&a(t),y=t=>{const e={};for(const[r,s]of t)e[r]=s;return e};class p 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?h(t)&&!i(t)?t:{message:t,error:e}:{message:e}}}class m extends p{constructor(t,e="Something Went Wrong!"){super(p.createPayload(t,e))}}class TypeMismatchException extends m{constructor(t,e="Type Mismatch"){super(t,e),Error.captureStackTrace&&Error.captureStackTrace(this,new.target)}}class g{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(r)}setContentType(t){return this.headers.set(r,t),this}getHeader(t){return this.headers.get(t.toLowerCase())}hasHeader(t){return this.headers.has(t.toLowerCase())}addHeader(t,e,r){if(h(t)){const{key:e,value:r}=t;return this.headers.set(e.toLowerCase(),r),this}if("string"!=typeof t)throw new TypeMismatchException(s);return this.headers.set(t.toLowerCase(),this.normalizeValue(e,r)),this}removeHeader(t){return h(t)&&this.headers.delete(t.key.toLowerCase()),this.headers.delete(t.toLowerCase()),this}setHeaders(t){if(i(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(h(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,r){if(h(t)){const{key:e,value:r}=t;return this.params.set(e,r),this}if("string"!=typeof t)throw new TypeMismatchException(s);return this.params.set(t,this.normalizeValue(e,r)),this}removeParam(t){return h(t)&&this.params.delete(t.key),this.params.delete(t),this}setParams(t){if(i(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(h(t)){for(const e in t){const r=t[e];this.params.set(e,r.toString())}return this}throw new TypeMismatchException(o)}getQueryParam(t){return this.query.get(t)}hasQueryParam(t){return!f(this.query.get(t))}addQueryParam(t,e,r){if(h(t)&&!i(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(s);const o=this.query.get(t);return this.query.set(t,this.updatedValue(o,e,r)),this}removeQueryParam(t){return h(t)&&this.query.delete(t.key),this.query.delete(t),this}setQueryParams(t){if(i(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(h(t)){for(const e in t){const r=t[e];this.query.set(e,r)}return this}throw new TypeMismatchException(o)}getBody(){return this.body}hasBody(){return!f(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=t.EHttpMethod.Get,this.params=new Map,this.query=new Map,this.body=null,this.successHooks=[],this.errorHooks=[]}normalizeValue(t,e){if(t instanceof Date||i(t)&&function(t){if(!u(t)&&0!==t.length)return t[0]}(t)instanceof Date)return e=e??(t=>t.toISOString()),i(t)?c(t,e):e(t);if(i(t)){const e=d(c(t,(t=>u(t)?t:t.toString())),(t=>!u(t)));return a(e)?null:e}return f(t)?null:t.toString()}updatedValue(t,e,r){const s=this.normalizeValue(e,r);return i(t)?i(s)?[...t,...s]:[...t,s]:u(t)?s:i(s)?[t,...s]:[t,s]}}const w=t=>(async(t,e={})=>{const{suppressErrors:r=!1}=e;return c(await Promise.allSettled(t),(t=>{if("fulfilled"===t.status)return t.value;if(!r)throw t.reason;return null}))})(t,{suppressErrors:!1});t.Request=class extends g{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(t){this.instance=t??e.create(),this.abortController=new AbortController}buildUrl=()=>{let t=this.url;f(this.endpoint)||(t+=this.endpoint);for(const[e,r]of this.params)t=t.replace(":"+e,r);return t};callSuccessHooks=async t=>(await w(c(this.successHooks,(e=>e(t,this))))).some((t=>!u(t)&&!!t.retry));callErrorHooks=async(t,e)=>(await w(c(this.errorHooks,(r=>r(t,this,e))))).some((t=>!u(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})}}));
//# sourceMappingURL=index.min.js.map