[FEAT] First functional version.
This commit is contained in:
113
partitions/src/lib/api.ts
Normal file
113
partitions/src/lib/api.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import axios, { type AxiosError } from 'axios';
|
||||
import { auth } from '$lib/stores/auth';
|
||||
import { browser } from '$app/environment';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
const API_BASE_URL = browser ? 'http://localhost:8000' : 'http://localhost:8000';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
export { api };
|
||||
|
||||
api.interceptors.request.use((config) => {
|
||||
const authState = get(auth);
|
||||
if (authState.token) {
|
||||
config.headers.Authorization = `Bearer ${authState.token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error: AxiosError) => {
|
||||
if (error.response?.status === 401) {
|
||||
auth.logout();
|
||||
if (browser) {
|
||||
window.location.href = '/';
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export interface Score {
|
||||
id: string;
|
||||
name: string;
|
||||
compositor: string;
|
||||
ressource?: string | null;
|
||||
instruments?: Instrument[];
|
||||
}
|
||||
|
||||
export interface Piece {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Instrument {
|
||||
id: string;
|
||||
title: string;
|
||||
piece: string;
|
||||
parts: Part[];
|
||||
}
|
||||
|
||||
export interface Part {
|
||||
id: string;
|
||||
files: PdfFile[];
|
||||
}
|
||||
|
||||
export interface PdfFile {
|
||||
name: string;
|
||||
filename: string;
|
||||
path: string;
|
||||
part: string | null;
|
||||
key: string | null;
|
||||
clef: string | null;
|
||||
variant: string | null;
|
||||
}
|
||||
|
||||
export const apiService = {
|
||||
async login(username: string, password: string): Promise<{ token: string; user: { username: string; role: string } }> {
|
||||
const response = await api.post('/login', { username, password });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
async getScores(): Promise<Score[]> {
|
||||
const response = await api.get('/scores');
|
||||
return response.data.scores;
|
||||
},
|
||||
|
||||
async getScore(id: string): Promise<Score> {
|
||||
const response = await api.get(`/scores/${id}`);
|
||||
return response.data.score;
|
||||
},
|
||||
|
||||
async getInstruments(scoreId: string): Promise<Instrument[]> {
|
||||
const response = await api.get(`/scores/${scoreId}/instruments`);
|
||||
return response.data.instruments;
|
||||
},
|
||||
|
||||
async getPieces(scoreId: string): Promise<Piece[]> {
|
||||
const response = await api.get(`/pieces/${scoreId}`);
|
||||
return response.data.pieces;
|
||||
},
|
||||
|
||||
async downloadPdf(path: string): Promise<Blob> {
|
||||
const response = await api.get(`/download/${path}`, {
|
||||
responseType: 'blob'
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getDownloadUrl(path: string): string {
|
||||
let token = '';
|
||||
auth.subscribe((state) => {
|
||||
token = state.token || '';
|
||||
})();
|
||||
return `${API_BASE_URL}/download/${path}?token=${token}`;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user