linkylinky-dashboard/src/lib/Apiclient.js

70 lines
1.5 KiB
JavaScript

import axios from 'axios';
import UserStore from './UserStore';
axios.interceptors.response.use(response => {
return response;
}, error => {
if (error.response.status === 401) {
UserStore.logout();
}
return error;
});
export default class Apiclient {
static async getStats() {
return (
await axios.get('https://kauft.es/api/stats')
).data;
}
static async getUrls() {
return (
await axios.get('https://kauft.es/api?showVisits=true', {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
static async getUrlDetails(shortcode) {
return (
await axios.get(`https://kauft.es/api/${shortcode}`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
static async getUrlVisits(shortcode) {
return (
await axios.get(`https://kauft.es/api/${shortcode}/visits`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
static async deleteUrl(shortcode) {
return (
await axios.delete(`https://kauft.es/api/${shortcode}`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).status;
}
static async login(username, password) {
return (
await axios.post(`https://kauft.es/api/auth/login`, {}, {
auth: {
username, password
}
})
).data;
}
static async logout() {
return (
await axios.post(`https://kauft.es/api/auth/logout`, {}, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
}