Files
sbd2-frontend/src/lib/api/services/sounds.ts

127 lines
2.8 KiB
TypeScript

import { apiClient } from '../client'
export interface Sound {
id: number
name: string
filename: string
duration: number
size: number
hash: string
type: 'SDB' | 'TTS' | 'EXT'
play_count: number
is_normalized: boolean
normalized_filename?: string
normalized_duration?: number
normalized_size?: number
normalized_hash?: string
thumbnail?: string
is_music: boolean
is_deletable: boolean
is_favorited: boolean
favorite_count: number
created_at: string
updated_at: string
}
export type SoundSortField =
| 'name'
| 'filename'
| 'duration'
| 'size'
| 'type'
| 'play_count'
| 'created_at'
| 'updated_at'
export type SortOrder = 'asc' | 'desc'
export interface GetSoundsParams {
types?: string[]
search?: string
sort_by?: SoundSortField
sort_order?: SortOrder
limit?: number
offset?: number
favorites_only?: boolean
}
export interface GetSoundsResponse {
sounds: Sound[]
}
export class SoundsService {
/**
* Get all sounds with optional filtering, searching, and sorting
*/
async getSounds(params?: GetSoundsParams): Promise<Sound[]> {
const searchParams = new URLSearchParams()
// Handle multiple types
if (params?.types) {
params.types.forEach(type => {
searchParams.append('types', type)
})
}
// Handle other parameters
if (params?.search) {
searchParams.append('search', params.search)
}
if (params?.sort_by) {
searchParams.append('sort_by', params.sort_by)
}
if (params?.sort_order) {
searchParams.append('sort_order', params.sort_order)
}
if (params?.limit) {
searchParams.append('limit', params.limit.toString())
}
if (params?.offset) {
searchParams.append('offset', params.offset.toString())
}
if (params?.favorites_only) {
searchParams.append('favorites_only', 'true')
}
const url = searchParams.toString()
? `/api/v1/sounds/?${searchParams.toString()}`
: '/api/v1/sounds/'
const response = await apiClient.get<GetSoundsResponse>(url)
return response.sounds || []
}
/**
* Get sounds of a specific type
*/
async getSoundsByType(
type: string,
params?: Omit<GetSoundsParams, 'types'>,
): Promise<Sound[]> {
return this.getSounds({ ...params, types: [type] })
}
/**
* Get SDB type sounds
*/
async getSDBSounds(
params?: Omit<GetSoundsParams, 'types'>,
): Promise<Sound[]> {
return this.getSoundsByType('SDB', params)
}
/**
* Play a sound by ID
*/
async playSound(soundId: number): Promise<void> {
await apiClient.post(`/api/v1/sounds/play/${soundId}`)
}
/**
* Stop all playing sounds
*/
async stopSounds(): Promise<void> {
await apiClient.post('/api/v1/sounds/stop')
}
}
export const soundsService = new SoundsService()