File size: 1,146 Bytes
84121fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()