A very performant and light (2mb in memory) link shortener and tracker. Written in Rust and React and uses Postgres/SQLite.
1import axios from 'axios';
2import { CreateLinkRequest, Link, AuthResponse, ClickStats, SourceStats } from '../types/api';
3
4// Create axios instance with default config
5const api = axios.create({
6 baseURL: '/api',
7});
8
9// Add a request interceptor to add the auth token to all requests
10api.interceptors.request.use((config) => {
11 const token = localStorage.getItem('token');
12 if (token) {
13 config.headers.Authorization = `Bearer ${token}`;
14 }
15 return config;
16});
17
18// Auth endpoints
19export const login = async (email: string, password: string) => {
20 const response = await api.post<AuthResponse>('/auth/login', {
21 email,
22 password,
23 });
24 return response.data;
25};
26
27export const register = async (email: string, password: string, adminToken: string) => {
28 const response = await api.post<AuthResponse>('/auth/register', {
29 email,
30 password,
31 admin_token: adminToken,
32 });
33 return response.data;
34};
35
36// Protected endpoints
37export const createShortLink = async (data: CreateLinkRequest) => {
38 const response = await api.post<Link>('/shorten', data);
39 return response.data;
40};
41
42export const getAllLinks = async () => {
43 const response = await api.get<Link[]>('/links');
44 return response.data;
45};
46
47export const deleteLink = async (id: number) => {
48 await api.delete(`/links/${id}`);
49};
50
51export const getLinkClickStats = async (id: number) => {
52 const response = await api.get<ClickStats[]>(`/links/${id}/clicks`);
53 return response.data;
54};
55
56export const getLinkSourceStats = async (id: number) => {
57 const response = await api.get<SourceStats[]>(`/links/${id}/sources`);
58 return response.data;
59};
60
61export { api };