Skip to content

Commit

Permalink
Add timeout to settings and add defaultFunction to helper (#30)
Browse files Browse the repository at this point in the history
* Add timeout to settings and defaultFunction

* Add set function to set configurations easily
  • Loading branch information
enginustun authored Mar 15, 2020
1 parent 4dbb76c commit 9359478
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dev/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = {
isFunction: val =>
Object.prototype.toString.call(val) === '[object Function]',

defaultFunction: () => {},

pathJoin: (...paths) => {
const pathArray = Array.prototype.slice.call(paths);
let resultPath = '';
Expand Down
26 changes: 24 additions & 2 deletions dev/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,35 @@ const settings = {
);
}
},
setTimeout: duration => {
_settings.timeout = duration;
},
setHeader: _settings.setHeader,
set beforeEveryRequest(func = () => {}) {
set beforeEveryRequest(func = helper.defaultFunction) {
_settings.beforeEveryRequest = func;
},
set afterEveryRequest(func = () => {}) {
set afterEveryRequest(func = helper.defaultFunction) {
_settings.afterEveryRequest = func;
},
set: (configs = {}) => {
configs.endpoints && settings.addEndpoint(configs.endpoints);
configs.apiPaths && settings.addApiPath(configs.apiPaths);
configs.defaultEndpoint &&
settings.setDefaultEndpoint(configs.defaultEndpoint);
configs.defaultApiPath &&
settings.setDefaultApiPath(configs.defaultApiPath);
configs.timeout && settings.setTimeout(configs.timeout);
helper.isObject(configs.headers) &&
Object.entries(configs.headers).map(config =>
settings.setHeader(config[0], config[1])
);
if (configs.beforeEveryRequest) {
settings.beforeEveryRequest = beforeEveryRequest;
}
if (configs.afterEveryRequest) {
settings.afterEveryRequest = afterEveryRequest;
}
},
};

export { RestClient, RestBaseModel, settings };
1 change: 1 addition & 0 deletions dev/modules/settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const settings = {
timeout: 0,
endpoints: {},
defaultEndpoint: '',
apiPaths: {
Expand Down
1 change: 1 addition & 0 deletions dev/modules/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const isValid = value => {
class XHR {
constructor() {
this.xhr = new XMLHttpRequest();
this.xhr.timeout = settings.timeout;
this.method = 'GET';
this.async = true;
this.url = '';
Expand Down

0 comments on commit 9359478

Please sign in to comment.