File size: 2,659 Bytes
cb5d9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import type { ClientData } from "../types";

export const DEV_API_BASE = "http://localhost:8000";
export const API_BASE =
  import.meta.env.VITE_API_BASE_URL ??
  (import.meta.env.DEV ? DEV_API_BASE : "");
const DEV_CLIENT_ID = "ID_UNICO_DEL_CLIENTE_001";

export interface ClientConfigResponse {
  cliente: ClientData;
  query: string;
}

export function getApiBase(customApiBase?: string) {
  return customApiBase?.trim() ? customApiBase : API_BASE;
}

export function buildApiUrl(path: string, customApiBase?: string) {
  return `${getApiBase(customApiBase)}${path}`;
}

export async function fetchClientConfig(
  token: string,
  clientId: string,
  customApiBase?: string,
  useDevToken = true,
): Promise<ClientConfigResponse> {
  let finalToken = token;
  const finalClientId = clientId;

  if (!finalToken && !finalClientId) {
    if (!import.meta.env.DEV || !useDevToken) {
      throw new Error("No se encontró el token o key en la URL.");
    }

    const tokenRes = await fetch(buildApiUrl("/api/token", customApiBase), {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: `client_id=${encodeURIComponent(DEV_CLIENT_ID)}`,
    });

    if (!tokenRes.ok) {
      throw new Error("No se pudo generar el token de prueba en desarrollo.");
    }

    const tokenData = await tokenRes.json();
    finalToken = tokenData.token;
  }

  const query = finalToken
    ? `token=${encodeURIComponent(finalToken)}`
    : `client_id=${encodeURIComponent(finalClientId)}`;

  const res = await fetch(buildApiUrl(`/config?${query}`, customApiBase));
  if (!res.ok) {
    const text = await res.text();
    throw new Error(
      `No se pudo obtener la configuración del cliente. ${res.status} ${text}`,
    );
  }

  const cliente = await res.json();
  return { cliente, query };
}

export interface UploadImageResponse {
  filename: string;
  [key: string]: unknown;
}

export async function uploadRoomImage(
  file: File,
  customApiBase?: string,
): Promise<UploadImageResponse> {
  const formData = new FormData();
  formData.append("file", file);

  const response = await fetch(
    buildApiUrl("/api/upload-image", customApiBase),
    {
      method: "POST",
      body: formData,
    },
  );

  if (!response.ok) {
    const text = await response.text();
    throw new Error(text || "Error al subir la imagen");
  }

  return response.json();
}

export async function startSession(query: string, customApiBase?: string) {
  await fetch(buildApiUrl("/session/start", customApiBase), {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: query,
  });
}