Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/api.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions dist/api.min.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*! mg-api-js - v2.0.1
* Release on: 2020-04-27
* Copyright (c) 2020 Geotab Inc
* Licensed MIT */

/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
2 changes: 1 addition & 1 deletion dist/api.min.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/ApiHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ApiHelper {
call = call
.then( response => {
// Error handling has already taken errors out in either errorHandle() or AxiosCall.send()
if(response.data.result){
if(response && response.data && response.data.result){
return response.data.result;
}
});
Expand All @@ -144,13 +144,13 @@ class ApiHelper {
// User hasn't asked for the full axios object, so we should error check
call = call
.then( response => {
if(response.status !== 200){
if(response && response.status && response.statusText && response.status !== 200){
throw new Error(`Response ${response.status} - ${response.statusText}`);
}
return response;
})
.then( response => {
if(response.data.error){
if(response && response.data && response.data.error){
throw new Error(`${response.data.error.name} - ${response.data.error.message}`);
}
return response;
Expand Down Expand Up @@ -186,7 +186,7 @@ class ApiHelper {
this.clearLocalCredentials();
// Assuming there was a timeout, re-running authentication
let auth = await this.getAuthentication(authenticate, callbackError, rememberMe);
if(auth.result){ // Successful re-authentication
if(auth && auth.result){ // Successful re-authentication
params.credentials = auth.result.credentials;
return new AxiosCall(method, this.server, params, callbackSuccess, callbackError).send(this.options.timeout);
} else { // Unsuccessful
Expand All @@ -199,7 +199,7 @@ class ApiHelper {
})
} else {
call.then( response => {
if(response.data.result){
if(response && response.data && response.data.result){
let server = response.data.result.path === 'ThisServer' ? this.server : response.data.result.path;
this.server = server;
if(this.rememberMe){
Expand Down
37 changes: 15 additions & 22 deletions lib/AxiosCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,22 @@ class AxiosCall {
data: this.encode(this.body),
headers: this.config.headers,
timeout: timeout * 1000
});
// Normal callback behaviour if we have one
if (this.callbackSuccess) {
this.request
.then(response => {
let data = response.data;
if (data.error && this.callbackError) {
this.callbackError(data.error);
} else {
this.callbackSuccess(data.result);
}
}).then(response => {
let data = response.data;
if(this.callbackSuccess){
if(data.error && this.callbackError){
this.callbackError(data.error);
} else {
this.callbackSuccess(data.result);
}
);
}

// If we got a callbackError, we can catch the error and use it
if(this.callbackError){
this.request
.catch( error => {
this.callbackError('Request Failure', error);
}
);
}
}
return response;
}).catch(error => {
if(this.callbackError){
this.callbackError('Request Failure', error.toJSON());
}
return error;
});
// Returning promise
return this.request;
}
Expand Down