Skip to content

Instantly share code, notes, and snippets.

@rezzzza
Created July 6, 2019 06:40
Show Gist options
  • Save rezzzza/1ad82293d29c0cc0a09a33989e7eb1fb to your computer and use it in GitHub Desktop.
Save rezzzza/1ad82293d29c0cc0a09a33989e7eb1fb to your computer and use it in GitHub Desktop.
export default function createClientApi(
axios,
apiBaseURL,
websiteBaseUrl,
userBaseUrl
) {
return {
ax: axios,
apiBaseURL,
userBaseUrl,
websiteBaseUrl,
saveCity: function(cityId, callback) {
const params = `?cityId=${cityId}&form_key=${this.formKey}`;
this.ax
.post(
this.websiteBaseUrl +
"/ajax/account/savevisitorcategoryinsession" +
params
)
.then(function(response) {
if (response.statusText !== "OK") {
if (window && window.localStorage) {
window.localStorage.clear();
}
callback(response, true);
return;
}
callback(response, false);
})
.catch(function(error) {
callback(error, true);
});
},
deleteCartItem: function(url, formKey, callback) {
const formData = new FormData();
formData.append("form_key", formKey);
this.ax
.post(this.websiteBaseUrl + url, formData)
.then(function(response) {
callback(response, false);
})
.catch(function(error) {
callback(error.response, true);
});
},
subscribeNewsletter: function(city, email, form_key, callback) {
let formData = new FormData();
formData.append("form_key", form_key);
formData.append("email", email);
formData.append("city", city);
this.ax
.post(
this.websiteBaseUrl + "/newsletter/subscriber/ajax/json/1",
formData
)
.then(function(response) {
if (response.statusText !== "OK") {
callback(response, true);
return;
}
callback(response, false);
})
.catch(function(error) {
callback(error, true);
});
},
getProductInfo: function(id, callback) {
this.ax
.get(this.apiBaseURL + `/rainman/products/${id}`)
.then(function(response) {
if (response.statusText !== "OK") {
callback(response, true);
return;
}
callback(response, false);
})
.catch(function(error) {
callback(error, true);
});
},
getOrders: function(jwt, page, callback) {
if (!jwt) {
callback({ response: { path: "/orders" } }, true);
return;
}
const limit = 10;
const offset = (page - 1) * limit;
this.ax
.get(
`${this.apiBaseURL}/rainman/orders?limit=${limit}&offset=${offset}`,
{
headers: {
Authorization: jwt
}
}
)
.then(response => {
if (typeof response === "undefined" || response.statusText !== "OK") {
callback(response, true);
return;
}
callback(response, false);
})
.catch(function(error) {
callback(error, true);
});
},
getTransactions: function(jwt, page, callback) {
const limit = 10;
const offset = (page - 1) * limit;
this.ax
.get(
`${
this.apiBaseURL
}/rainman/cash_out_requests?limit=${limit}&offset=${offset}`,
{
headers: {
Authorization: jwt
}
}
)
.then(response => {
if (typeof response === "undefined" || response.statusText !== "OK") {
callback(response, true);
return;
}
callback(response, false);
})
.catch(function(error) {
callback(error, true);
});
},
makeCashoutRequest(jwt, amount, request_type, bankAccount, callback) {
let params = {
request_type,
amount
};
if (bankAccount) {
params.bank_account_number = bankAccount;
}
const config = {
headers: {
Authorization: jwt
}
};
this.ax
.post(`${this.apiBaseURL}/rainman/cash_out_requests`, params, config)
.then(response => {
if (
typeof response === "undefined" ||
response.statusText !== "Created"
) {
callback(response.response, true);
return;
}
callback(response, false);
})
.catch(error => {
callback(error, true);
});
},
logout(jwt, callback) {
const config = {
headers: {
Authorization: jwt
}
};
return this.ax
.delete(`${this.userBaseUrl}/users/sign_out`, config)
.then(response => {
if (response === undefined || response.status !== 204) {
callback(response, true);
return response;
}
callback(response, false);
return response;
})
.catch(error => {
callback(error.response, true);
return error;
});
},
validate() {
var url = `${this.userBaseUrl}/users/validate`;
return this.ax.get(url);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment