Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
export default class RestClient {
axios: any;
constructor(baseUrl: string) {
const axiosBase = require("axios");
const csrfToken = (<HTMLMetaElement>document.querySelector("meta[name=csrf-token]")).content;
this.axios = axiosBase.create({
baseURL: baseUrl,
timeout: 1000,
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": csrfToken,
},
});
}
get(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
) {
return this.axios
.get(path, { params: params })
.then((result: object) => {
console.log(`GET ${this.axios.baseURL}/${path}`);
console.log(`result: ${JSON.stringify(result)}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! GET ${this.axios.baseURL}/${path}`);
console.log(`error: ${JSON.stringify(error)}`);
errored(error);
})
.then(always());
}
post(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
) {
return this.axios
.post(path, params)
.then((result: object) => {
console.log(`POST ${this.axios.baseURL}/${path}`);
console.log(`result: ${JSON.stringify(result)}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! POST ${this.axios.baseURL}/${path}`);
console.log(`error: ${JSON.stringify(error)}`);
errored(error);
})
.then(always());
}
delete(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
) {
return this.axios
.delete(path, { data: { params } })
.then((result: object) => {
console.log(`DELETE ${this.axios.baseURL}/${path}`);
console.log(`result: ${JSON.stringify(result)}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! DELETE ${this.axios.baseURL}/${path}`);
console.log(`error: ${JSON.stringify(error)}`);
errored(error);
})
.then(always());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.