adds type generation
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
"use client";
|
||||
import { Fetcher } from "openapi-typescript-fetch";
|
||||
import { getUser } from "./fetch";
|
||||
import { paths, components } from "muzak/data/types";
|
||||
|
||||
interface GenericPaths {
|
||||
[path: string]: {
|
||||
[method: string]: any;
|
||||
};
|
||||
}
|
||||
const fetcher = Fetcher.for<GenericPaths>();
|
||||
const fetcher = Fetcher.for<paths>();
|
||||
const BASE_URL = process.env.BACKEND_HOST || "http://127.0.0.1:8001";
|
||||
|
||||
// Configure the fetcher
|
||||
@@ -19,7 +15,7 @@ fetcher.configure({
|
||||
},
|
||||
},
|
||||
use: [
|
||||
// Authentication middleware
|
||||
// Add authentication token to request
|
||||
async (url, init, next) => {
|
||||
const user = getUser();
|
||||
if (user?.access_token) {
|
||||
@@ -30,18 +26,14 @@ fetcher.configure({
|
||||
}
|
||||
return next(url, init);
|
||||
},
|
||||
// Error handling middleware
|
||||
// Handle errors
|
||||
async (url, init, next) => {
|
||||
const response = await next(url, init);
|
||||
|
||||
if (!response.ok) {
|
||||
// Handle authentication errors
|
||||
if (response.status === 401) {
|
||||
console.warn("Authentication failed - token may be expired");
|
||||
// You might want to trigger a redirect to login or token refresh here
|
||||
}
|
||||
|
||||
// Log other errors
|
||||
console.error(`API Error: ${response.status} ${response.statusText}`, {
|
||||
url,
|
||||
method: init.method,
|
||||
@@ -53,7 +45,6 @@ fetcher.configure({
|
||||
],
|
||||
});
|
||||
|
||||
// Create your API methods
|
||||
export const api = {
|
||||
tracks: {
|
||||
list: fetcher.path("/api/track/").method("get").create(),
|
||||
@@ -88,4 +79,9 @@ export const api = {
|
||||
},
|
||||
};
|
||||
|
||||
export type User = components["schemas"]["User"];
|
||||
export type Track = components["schemas"]["Track"];
|
||||
export type Album = components["schemas"]["Album"];
|
||||
export type Vote = components["schemas"]["Vote"];
|
||||
|
||||
export default api;
|
||||
|
||||
Reference in New Issue
Block a user