linkylinky-dashboard/src/lib/Apiclient.js

142 lines
3.6 KiB
JavaScript

import { data } from 'autoprefixer';
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 {
/**
* API-Getter for the linkylinky api stats endpoint
* @returns Current linkylinky stats (url count, total visits)
*/
static async getStats() {
return (
await axios.get('https://kauft.es/api/stats')
).data;
}
/**
* API-Getter for the linkylinky api all urls endpoint (needs auth)
* @returns All urls with shortcode, target, full url and visits in an array of objects
*/
static async getUrls() {
return (
await axios.get('https://kauft.es/api?showVisits=true', {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
/**
* API-Getter for the linkylinky api url details endpoint (needs auth)
* @param {*} shortcode The shortcode of your favourite url
* @returns Url shortcode, target, full url and visit count in an object
*/
static async getUrlDetails(shortcode) {
//TODO: Handle 404
return (
await axios.get(`https://kauft.es/api/${shortcode}`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
/**
* API-Getter for the linkylinky api url vists endpoint (needs auth)
* @param {*} shortcode The shortcode of your favourite url
* @returns Url visit details as an object for each visits (r/n they only contain timestamps)
*/
static async getUrlVisits(shortcode) {
//TODO: Handle 404
return (
await axios.get(`https://kauft.es/api/${shortcode}/visits`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
/**
* API-Getter for the linkylinky api all vists endpoint (needs auth)
* @returns Url visit details for each visit in an array of objects.
*/
static async getVisits() {
return (
await axios.get(`https://kauft.es/api/visits`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).data;
}
/**
* Create a new shorturl via the api
* @param {*} target The target (rediction) url for the new shorturl.
* @param {*} shortcode A custom shortcode (if needed)
* @returns The response data/error
*/
static async createUrl(target, shortcode) {
const res = (
await axios.post(`https://kauft.es/api`, {
target,
shortcode
}, {
validateStatus: null
})
);
return {
status: res.status,
data: res.data
}
}
/**
* API-Delet for the linkylinky api url deletion endpoint (needs auth)
* @param {*} shortcode The shortcode of your most hated url
* @returns Just a 204 (no matter if the url got deleted or didn't exist in the first place)
*/
static async deleteUrl(shortcode) {
return (
await axios.delete(`https://kauft.es/api/${shortcode}`, {
headers: { Authorization: `Bearer ${UserStore.state.token}` }
})
).status;
}
/**
* Login and receive a JWT for future auth.
* @param {*} username Your username (cleartext)
* @param {*} password Your password (cleartext)
* @returns A user login object containing your jwt
*/
static async login(username, password) {
return (
await axios.post(`https://kauft.es/api/auth/login`, {}, {
auth: {
username, password
}
})
).data;
}
/**
* Log yourself out -> Invalidates your current (and past) JWTs
* @returns Done!
*/
static async logout() {
return (
await axios.post(`https://kauft.es/api/auth/logout`, {}, {
headers: { Authorization: `Bearer ${UserStore.state.token}`,
validateStatus: null
}
})
).data;
}
}