Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios update #29

Merged
merged 8 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
module.exports = {
extends: [
'react-app',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
settings: {
react: {
version: '999.999.999',
},
},
rules: {
'prettier/prettier': 0,
'prettier/prettier': "error",
},
};
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['16.x', '18.x']
node: ["16.x", "18.x", "20.x"]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
3 changes: 1 addition & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"trailingComma": "all",
"arrowParens": "always",
"tabWidth": 4
"arrowParens": "always"
}
244 changes: 127 additions & 117 deletions README.md

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions dist/browser/index.global.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser/index.global.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser/index.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
var R=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var h=(u,e,t,r)=>{for(var n=r>1?void 0:r?b(e,t):e,s=u.length-1,o;s>=0;s--)(o=u[s])&&(n=(r?o(e,t,n):o(n))||n);return r&&n&&R(e,t,n),n};import{applyMagic as q}from"js-magic";import f from"axios";import{applyMagic as m}from"js-magic";var g=class{constructor(e,t){this.logger=e,this.httpRequestErrorService=t}process(e){this.logger&&this.logger.warn&&this.logger.warn("API ERROR",e);let t=e;typeof e=="string"&&(t=new Error(e)),this.httpRequestErrorService&&(typeof this.httpRequestErrorService.process<"u"?this.httpRequestErrorService.process(t):typeof this.httpRequestErrorService=="function"&&this.httpRequestErrorService(t))}};var c=class{constructor({baseURL:e="",timeout:t=null,cancellable:r=!1,strategy:n=null,flattenResponse:s=null,defaultResponse:o={},logger:i=null,onError:l=null,...a}){this.timeout=3e4;this.cancellable=!1;this.strategy="reject";this.flattenResponse=!0;this.defaultResponse=null;this.timeout=t!==null?t:this.timeout,this.strategy=n!==null?n:this.strategy,this.cancellable=r||this.cancellable,this.flattenResponse=s!==null?s:this.flattenResponse,this.defaultResponse=o,this.logger=i||global.console||window.console||null,this.httpRequestErrorService=l,this.requestsQueue=new Map,this.requestInstance=f.create({...a,baseURL:e,timeout:this.timeout})}getInstance(){return this.requestInstance}interceptRequest(e){this.getInstance().interceptors.request.use(e)}__get(e){return e in this?this[e]:this.prepareRequest.bind(this,e)}prepareRequest(e,t,r=null,n=null){return this.handleRequest({type:e,url:t,data:r,config:n})}buildRequestConfig(e,t,r,n){let s=e.toLowerCase();return{...n,url:t,method:s,[s==="get"||s==="head"?"params":"data"]:r||{}}}processRequestError(e,t){if(this.isRequestCancelled(e,t))return;t.onError&&typeof t.onError=="function"&&t.onError(e),new g(this.logger,this.httpRequestErrorService).process(e)}async outputErrorResponse(e,t){let r=this.isRequestCancelled(e,t),n=t.strategy||this.strategy;return r&&!t.rejectCancelled?this.defaultResponse:n==="silent"?(await new Promise(()=>null),this.defaultResponse):n==="reject"||n==="throwError"?Promise.reject(e):this.defaultResponse}isRequestCancelled(e,t){return f.isCancel(e)}addCancellationToken(e){if(!this.cancellable&&!e.cancellable)return{};if(typeof e.cancellable<"u"&&!e.cancellable)return{};if(typeof AbortController>"u")return console.error("AbortController is unavailable in your ENV."),{};let{method:t,baseURL:r,url:n,params:s,data:o}=e,i=JSON.stringify([t,r,n,s,o]).substring(0,55**5),l=this.requestsQueue.get(i);l&&l.abort();let a=new AbortController;return this.requestsQueue.set(i,a),{signal:a.signal}}async handleRequest({type:e,url:t,data:r=null,config:n=null}){let s=null,o=n||{},i=this.buildRequestConfig(e,t,r,o);i={...this.addCancellationToken(i),...i};try{s=await this.requestInstance.request(i)}catch(l){return this.processRequestError(l,i),this.outputErrorResponse(l,i)}return this.processResponseData(s)}processResponseData(e){return e.data?this.flattenResponse?typeof e.data=="object"&&typeof e.data.data<"u"&&Object.keys(e.data).length===1?e.data.data:e.data:e:this.defaultResponse}};c=h([m],c);var d=class{constructor({apiUrl:e,endpoints:t,timeout:r=null,cancellable:n=!1,strategy:s=null,flattenResponse:o=null,defaultResponse:i={},logger:l=null,onError:a=null,...p}){this.apiUrl="";this.apiUrl=e,this.endpoints=t,this.logger=l,this.httpRequestHandler=new c({...p,baseURL:this.apiUrl,timeout:r,cancellable:n,strategy:s,flattenResponse:o,defaultResponse:i,logger:l,onError:a})}getInstance(){return this.httpRequestHandler.getInstance()}__get(e){return e in this?this[e]:this.endpoints[e]?this.handleRequest.bind(this,e):this.handleNonImplemented.bind(this,e)}async handleRequest(...e){let t=e[0],r=this.endpoints[t],n=e[1]||{},s=e[2]||{},o=e[3]||{},i=r.url.replace(/:[a-z]+/gi,p=>s[p.substring(1)]?s[p.substring(1)]:p),l=null,a={...r};return delete a.url,delete a.method,l=await this.httpRequestHandler[(r.method||"get").toLowerCase()](i,n,{...o,...a}),l}handleNonImplemented(e){return this.logger&&this.logger.log&&this.logger.log(`${e} endpoint not implemented.`),Promise.resolve(null)}};d=h([q],d);var k=u=>new d(u);export{d as ApiHandler,g as HttpRequestErrorHandler,c as HttpRequestHandler,k as createApiFetcher};
var m=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var h=(u,e,t,r)=>{for(var n=r>1?void 0:r?R(e,t):e,s=u.length-1,o;s>=0;s--)(o=u[s])&&(n=(r?o(e,t,n):o(n))||n);return r&&n&&m(e,t,n),n};import{applyMagic as q}from"js-magic";import{applyMagic as b}from"js-magic";var g=class{logger;requestErrorService;constructor(e,t){this.logger=e,this.requestErrorService=t}process(e){var r;(r=this.logger)!=null&&r.warn&&this.logger.warn("API ERROR",e);let t=e;typeof e=="string"&&(t=new Error(e)),this.requestErrorService&&(typeof this.requestErrorService.process<"u"?this.requestErrorService.process(t):typeof this.requestErrorService=="function"&&this.requestErrorService(t))}};var p=class{requestInstance;timeout=3e4;cancellable=!1;strategy="reject";flattenResponse=!0;defaultResponse=null;axios;logger;requestErrorService;requestsQueue;constructor({axios:e,baseURL:t="",timeout:r=null,cancellable:n=!1,strategy:s=null,flattenResponse:o=null,defaultResponse:i={},logger:l=null,onError:a=null,...c}){this.axios=e,this.timeout=r!==null?r:this.timeout,this.strategy=s!==null?s:this.strategy,this.cancellable=n||this.cancellable,this.flattenResponse=o!==null?o:this.flattenResponse,this.defaultResponse=i,this.logger=l||global.console||window.console||null,this.requestErrorService=a,this.requestsQueue=new Map,this.requestInstance=e.create({...c,baseURL:t,timeout:this.timeout})}getInstance(){return this.requestInstance}__get(e){return e in this?this[e]:this.prepareRequest.bind(this,e)}prepareRequest(e,t,r=null,n=null){return this.handleRequest({type:e,url:t,data:r,config:n})}buildRequestConfig(e,t,r,n){let s=e.toLowerCase();return{...n,url:t,method:s,[s==="get"||s==="head"?"params":"data"]:r||{}}}processRequestError(e,t){if(this.isRequestCancelled(e,t))return;t.onError&&typeof t.onError=="function"&&t.onError(e),new g(this.logger,this.requestErrorService).process(e)}async outputErrorResponse(e,t){let r=this.isRequestCancelled(e,t),n=t.strategy||this.strategy;return r&&!t.rejectCancelled?this.defaultResponse:n==="silent"?(await new Promise(()=>null),this.defaultResponse):n==="reject"||n==="throwError"?Promise.reject(e):this.defaultResponse}isRequestCancelled(e,t){return this.axios.isCancel(e)}addCancellationToken(e){if(!this.cancellable&&!e.cancellable)return{};if(typeof e.cancellable<"u"&&!e.cancellable)return{};if(typeof AbortController>"u")return console.error("AbortController is unavailable in your ENV."),{};let{method:t,baseURL:r,url:n,params:s,data:o}=e,i=JSON.stringify([t,r,n,s,o]).substring(0,55**5),l=this.requestsQueue.get(i);l&&l.abort();let a=new AbortController;return this.requestsQueue.set(i,a),{signal:a.signal}}async handleRequest({type:e,url:t,data:r=null,config:n=null}){let s=null,o=n||{},i=this.buildRequestConfig(e,t,r,o);i={...this.addCancellationToken(i),...i};try{s=await this.requestInstance.request(i)}catch(l){return this.processRequestError(l,i),this.outputErrorResponse(l,i)}return this.processResponseData(s)}processResponseData(e){return e.data?this.flattenResponse?typeof e.data=="object"&&typeof e.data.data<"u"&&Object.keys(e.data).length===1?e.data.data:e.data:e:this.defaultResponse}};p=h([b],p);var d=class{requestHandler;endpoints;logger;constructor({axios:e,apiUrl:t,endpoints:r,timeout:n=null,cancellable:s=!1,strategy:o=null,flattenResponse:i=null,defaultResponse:l={},logger:a=null,onError:c=null,...f}){this.endpoints=r,this.logger=a,this.requestHandler=new p({...f,baseURL:t,axios:e,timeout:n,cancellable:s,strategy:o,flattenResponse:i,defaultResponse:l,logger:a,onError:c})}getInstance(){return this.requestHandler.getInstance()}__get(e){return e in this?this[e]:this.endpoints[e]?this.handleRequest.bind(this,e):this.handleNonImplemented.bind(this,e)}async handleRequest(...e){let t=e[0],r=this.endpoints[t],n=e[1]||{},s=e[2]||{},o=e[3]||{},i=r.url.replace(/:[a-z]+/gi,c=>s[c.substring(1)]?s[c.substring(1)]:c),l=null,a={...r};return delete a.url,delete a.method,l=await this.requestHandler[(r.method||"get").toLowerCase()](i,n,{...o,...a}),l}handleNonImplemented(e){var t;return(t=this.logger)!=null&&t.log&&this.logger.log(`${e} endpoint not implemented.`),Promise.resolve(null)}};d=h([q],d);var j=u=>new d(u);export{d as ApiHandler,g as RequestErrorHandler,p as RequestHandler,j as createApiFetcher};
//# sourceMappingURL=index.mjs.map
2 changes: 1 addition & 1 deletion dist/browser/index.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/node/index.js.map

Large diffs are not rendered by default.