official-site.ts
1.12 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
44
45
46
import type { HomeData, PositionTab, QrcodeStatus } from '../types/home';
import { createApiClient } from './client';
const apiClient = createApiClient();
const OFFICIAL_SITE_HOME_ENDPOINT = '/official-site/home';
type BackendQrcodeStatus = string | null | undefined;
type RawPositionTab = Omit<PositionTab, 'qrcode_status'> & {
qrcode_status: BackendQrcodeStatus;
};
type RawHomeData = Omit<HomeData, 'tabs'> & {
tabs: RawPositionTab[];
};
function normalizeQrcodeStatus(
qrcodeUrl: string | null,
qrcodeStatus: BackendQrcodeStatus
): QrcodeStatus {
if (!qrcodeUrl) {
return 'unavailable';
}
if (qrcodeStatus === 'available' || qrcodeStatus === 'enable') {
return 'available';
}
return 'unavailable';
}
function normalizeHomeData(data: RawHomeData): HomeData {
return {
...data,
tabs: data.tabs.map((tab) => ({
...tab,
qrcode_status: normalizeQrcodeStatus(tab.qrcode_url, tab.qrcode_status),
})),
};
}
export async function fetchOfficialSiteHome(): Promise<HomeData> {
const data = await apiClient.get<RawHomeData>(OFFICIAL_SITE_HOME_ENDPOINT);
return normalizeHomeData(data);
}