File size: 1,594 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const API_URL = `${import.meta.env.VITE_API_URL}/api/`;
const checkUpdateUrl = `${import.meta.env.VITE_API_URL}/api/check-cwe-update`;
const updateCWEModelUrl = `${import.meta.env.VITE_API_URL}/api/update-cwe-model`;

export const getSettings = async (): Promise<any> => {
  try {
    const response = await fetch(`${API_URL}settings`, {
      credentials: 'include',
    }); // Incluir token
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return await response.json();
  } catch (error) {
    console.error('Error fetching data:', error);

    throw error;
  }
};

/**
 * Retorna `true` si no hay actualización del modelo de CWE,
 * `false` de lo contrario.
 */
export const checkUpdateCWE = async (): Promise<boolean> => {
  try {
    const response = await fetch(checkUpdateUrl, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      credentials: 'include',
    });

    const data = await response.json();

    return data.checksum_match;
  } catch (error) {
    console.error(error);
    return false;
  }
};

type UpdateResponse = {
  status: 'success' | 'error';
  error?: string;
};

export const updateCWEModel = async (): Promise<UpdateResponse> => {
  try {
    const response = await fetch(updateCWEModelUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      credentials: 'include',
    });

    return await response.json();
  } catch (error) {
    console.error(error);
    return { status: 'error', error: 'Error updating CWE Model' };
  }
};