Refactor API structure and integrate new modular API client
- Replaced legacy apiService with a new modular api client structure. - Updated AuthContext, OAuthButtons, and AuthCallbackPage to use the new api client. - Created separate services for auth, sounds, playlists, and users. - Implemented centralized API configuration and error handling. - Added support for OAuth providers and token exchange. - Introduced a Toaster component for notifications in App. - Updated API endpoints and request handling for better maintainability.
This commit is contained in:
100
src/lib/api/services/sounds.ts
Normal file
100
src/lib/api/services/sounds.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { apiClient } from '../client'
|
||||
import { API_CONFIG } from '../config'
|
||||
import type { PaginatedResponse } from '../types'
|
||||
|
||||
export interface Sound {
|
||||
id: number
|
||||
title: string
|
||||
description?: string
|
||||
file_url: string
|
||||
duration: number
|
||||
file_size: number
|
||||
mime_type: string
|
||||
play_count: number
|
||||
user_id: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface CreateSoundRequest {
|
||||
title: string
|
||||
description?: string
|
||||
file: File
|
||||
}
|
||||
|
||||
export interface UpdateSoundRequest {
|
||||
title?: string
|
||||
description?: string
|
||||
}
|
||||
|
||||
export interface SoundsListParams {
|
||||
page?: number
|
||||
size?: number
|
||||
search?: string
|
||||
user_id?: number
|
||||
}
|
||||
|
||||
export class SoundsService {
|
||||
/**
|
||||
* Get list of sounds with pagination
|
||||
*/
|
||||
async list(params?: SoundsListParams): Promise<PaginatedResponse<Sound>> {
|
||||
return apiClient.get<PaginatedResponse<Sound>>(API_CONFIG.ENDPOINTS.SOUNDS.LIST, {
|
||||
params: params as Record<string, string | number | boolean | undefined>
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific sound by ID
|
||||
*/
|
||||
async get(id: string | number): Promise<Sound> {
|
||||
return apiClient.get<Sound>(API_CONFIG.ENDPOINTS.SOUNDS.GET(id))
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new sound
|
||||
*/
|
||||
async create(data: CreateSoundRequest): Promise<Sound> {
|
||||
const formData = new FormData()
|
||||
formData.append('title', data.title)
|
||||
if (data.description) {
|
||||
formData.append('description', data.description)
|
||||
}
|
||||
formData.append('file', data.file)
|
||||
|
||||
return apiClient.post<Sound>(API_CONFIG.ENDPOINTS.SOUNDS.CREATE, formData)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing sound
|
||||
*/
|
||||
async update(id: string | number, data: UpdateSoundRequest): Promise<Sound> {
|
||||
return apiClient.patch<Sound>(API_CONFIG.ENDPOINTS.SOUNDS.UPDATE(id), data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a sound
|
||||
*/
|
||||
async delete(id: string | number): Promise<void> {
|
||||
return apiClient.delete<void>(API_CONFIG.ENDPOINTS.SOUNDS.DELETE(id))
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a sound file
|
||||
*/
|
||||
async upload(file: File, metadata?: { title?: string; description?: string }): Promise<Sound> {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
if (metadata?.title) {
|
||||
formData.append('title', metadata.title)
|
||||
}
|
||||
if (metadata?.description) {
|
||||
formData.append('description', metadata.description)
|
||||
}
|
||||
|
||||
return apiClient.post<Sound>(API_CONFIG.ENDPOINTS.SOUNDS.UPLOAD, formData)
|
||||
}
|
||||
}
|
||||
|
||||
export const soundsService = new SoundsService()
|
||||
Reference in New Issue
Block a user