|
import { ChannelResponse, ViewerResponse } from '../types/channel' |
|
|
|
const API_BASE = '/api' |
|
|
|
class ChannelService { |
|
async getChannels(): Promise<ChannelResponse> { |
|
const token = localStorage.getItem('daddytv_token') |
|
const response = await fetch(`${API_BASE}/channels`, { |
|
headers: { |
|
'Authorization': `Bearer ${token}`, |
|
}, |
|
}) |
|
|
|
if (!response.ok) { |
|
throw new Error('Error al cargar canales') |
|
} |
|
|
|
return response.json() |
|
} |
|
|
|
async setViewing(channelUrl: string): Promise<ViewerResponse> { |
|
const token = localStorage.getItem('daddytv_token') |
|
const response = await fetch(`${API_BASE}/viewers`, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
'Authorization': `Bearer ${token}`, |
|
}, |
|
body: JSON.stringify({ channel_url: channelUrl }), |
|
}) |
|
|
|
if (!response.ok) { |
|
throw new Error('Error al registrar visualización') |
|
} |
|
|
|
return response.json() |
|
} |
|
|
|
getProxyUrl(originalUrl: string): string { |
|
return `${API_BASE}/proxy?url=${encodeURIComponent(originalUrl)}` |
|
} |
|
} |
|
|
|
export const channelService = new ChannelService() |