aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/config.js
blob: d034fac69ac9a4a9c2c95f362389af62bbd5f918 (plain) (blame)
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
/**
 * Environment Configuration for Baby Tracker App
 *
 * Loads from remote API with local fallback defaults.
 * Other modules should await window.envReady before reading window.env.
 */
 
const CONFIG_API_URL = 'https://config.ooyy.de5.net/api/kv/babyday';
const CONFIG_API_AUTH = 'c2RFV3dlMzIzMmZGVjplZGl0b3I=';
const CONFIG_TIMEOUT_MS = 5000;
 
// Default fallback values (used when remote config is unavailable)
window.env = {
  SUPABASE_URL: 'https://ovehbuxmhknxhuhetigc.supabase.co',
  SUPABASE_KEY: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im92ZWhidXhtaGtueGh1aGV0aWdjIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjM1OTU1NjMsImV4cCI6MjA3OTE3MTU2M30.XuThuzSrDGhgFK5d82lqVy_ClyDy0xkYUxKe1ego2gs',
  IMGBED_API: 'https://cf-imgbed-31z.pages.dev/upload?authCode=9YW0fVuUFAoF8KhGLrnTPCk',
  GROW_API_URL: 'https://jsondata.888653.xyz/grow',
  // Set to '' or null to disable AI assessment
  AI_API_URL: 'https://aiask.888653.xyz',
  // Baby birth date (YYYY/MM/DD)
  BIRTH_DATE: '2025/03/09'
};
 
const CONFIG_KEYS = [
  'SUPABASE_URL',
  'SUPABASE_KEY',
  'IMGBED_API',
  'GROW_API_URL',
  'AI_API_URL',
  'BIRTH_DATE'
];
 
async function loadRemoteConfig() {
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), CONFIG_TIMEOUT_MS);
 
  try {
    const response = await fetch(CONFIG_API_URL, {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${CONFIG_API_AUTH}`,
        'Content-Type': 'application/json'
      },
      signal: controller.signal
    });
 
    if (!response.ok) {
      console.warn('Remote config unavailable, using defaults:', response.status);
      return false;
    }
 
    const config = await response.json();
    let applied = 0;
 
    for (const key of CONFIG_KEYS) {
      if (config[key] !== undefined && config[key] !== null && config[key] !== '') {
        window.env[key] = config[key];
        applied++;
      }
    }
 
    console.log(`Remote config loaded (${applied} keys applied)`);
    return true;
  } catch (error) {
    if (error.name === 'AbortError') {
      console.warn('Remote config timed out, using defaults');
    } else {
      console.warn('Remote config failed, using defaults:', error.message);
    }
    return false;
  } finally {
    clearTimeout(timeoutId);
  }
}
 
// Modules should await this before depending on final env values
window.envReady = loadRemoteConfig();