forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-app-initialization-data.ts
More file actions
101 lines (89 loc) · 2.33 KB
/
Copy pathget-app-initialization-data.ts
File metadata and controls
101 lines (89 loc) · 2.33 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
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
'use server';
import envConfig from '@/src/constants/envConfig';
export interface Category {
title: string;
id: string;
}
export interface TriggerEvent {
title: string;
id: string;
}
export interface NotificationScope {
title: string;
id: string;
}
export interface AppCapability {
title: string;
id: string;
triggers?: TriggerEvent[];
scopes?: NotificationScope[];
actions?: unknown[];
}
export interface PaymentPlan {
title: string;
id: string;
}
export interface AppInitializationData {
categories: Category[];
capabilities: AppCapability[];
paymentPlans: PaymentPlan[];
}
export default async function getAppInitializationData(
token?: string,
): Promise<AppInitializationData> {
const apiUrl = envConfig.API_URL || 'http://localhost:8000';
try {
// Fetch categories and capabilities in parallel (no auth required)
const [categoriesResponse, capabilitiesResponse] = await Promise.all([
fetch(`${apiUrl}/v1/app-categories`, {
headers: {
'Content-Type': 'application/json',
},
cache: 'no-cache',
}),
fetch(`${apiUrl}/v1/app-capabilities`, {
headers: {
'Content-Type': 'application/json',
},
cache: 'no-cache',
}),
]);
// Parse categories and capabilities
const categories: Category[] = categoriesResponse.ok
? await categoriesResponse.json()
: [];
const capabilities: AppCapability[] = capabilitiesResponse.ok
? await capabilitiesResponse.json()
: [];
// Fetch payment plans if token is provided
let paymentPlans: PaymentPlan[] = [];
if (token) {
try {
const paymentPlansResponse = await fetch(`${apiUrl}/v1/app/plans`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
cache: 'no-cache',
});
if (paymentPlansResponse.ok) {
paymentPlans = await paymentPlansResponse.json();
}
} catch (error) {
console.warn('Failed to fetch payment plans:', error);
}
}
return {
categories,
capabilities,
paymentPlans,
};
} catch (error) {
console.error('Error fetching app initialization data:', error);
return {
categories: [],
capabilities: [],
paymentPlans: [],
};
}
}