-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSettings.ts
More file actions
43 lines (33 loc) · 1.25 KB
/
getSettings.ts
File metadata and controls
43 lines (33 loc) · 1.25 KB
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
import type { Env } from '../types/env';
import { jsonResponse, errorResponse, optionsResponse } from '../services/vmClient';
const VM_URL = 'https://vmprev.adaseal.eu';
const CACHE_KEY = '__internal:settings_cache';
const CACHE_TTL = 3600;
export const onRequestGet: PagesFunction<Env> = async (context) => {
const { env } = context;
if (!env.VITE_VM_API_KEY || env.VITE_VM_API_KEY.trim() === '') {
return errorResponse('Server configuration error', 500);
}
try {
const cached = await env.VM_WEB_PROFILES.get(CACHE_KEY, { type: 'json' });
if (cached !== null) {
return jsonResponse(cached);
}
const url = `${VM_URL}/api.php?action=get_settings`;
const response = await fetch(url, {
headers: { 'X-API-Token': env.VITE_VM_API_KEY },
});
if (!response.ok) {
return errorResponse('Upstream service error', 502);
}
const settings = await response.json();
await env.VM_WEB_PROFILES.put(CACHE_KEY, JSON.stringify(settings), {
expirationTtl: CACHE_TTL,
});
return jsonResponse(settings);
} catch (error) {
console.error('getSettings error:', error);
return errorResponse('Failed to fetch settings');
}
};
export const onRequestOptions: PagesFunction = async () => optionsResponse();