/
miniapp.ts
206 lines (181 loc) · 6.95 KB
/
miniapp.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
Reward,
DevicePermission,
CustomPermission,
CustomPermissionName,
CustomPermissionResult,
CustomPermissionStatus,
ShareInfoType,
ScreenOrientation,
Points,
} from '../../js-miniapp-bridge/src';
import { UserInfoProvider, UserInfo } from './modules/user-info';
import { ChatService } from './modules/chat-service';
import { getBridge } from './utils';
/**
* A module layer for webapps and mobile native interaction.
*/
interface MiniAppFeatures {
/**
* Request the mini app's unique id from the host app.
* @returns The Promise of provided id of mini app from injected side.
*/
getUniqueId(): Promise<string>;
/**
* Request the location permission from the host app.
* You must call this before using `navigator.geolocation`.
* This will request both the Android/iOS device permission for location (if not yet granted to the host app),
* and the custom permission for location {@link CustomPermissionName.LOCATION}.
* @param permissionDescription Description of location permission.
* @returns The Promise of permission result of mini app from injected side.
* Rejects the promise if the user denied the location permission (either the device permission or custom permission).
*/
requestLocationPermission(permissionDescription?: string): Promise<string>;
/**
*
* Request that the user grant custom permissions related to accessing user data.
* Typically, this will show a dialog in the Host App asking the user grant access to your Mini App.
* You can pass multiple permissions at once and the Host App will request all of those permissions within a single dialog.
*
* @param permissions An array containing CustomPermission objects - permission name and description
* @returns Promise with the custom permission results - "ALLOWED" or "DENIED" for each permission
*/
requestCustomPermissions(
permissions: CustomPermission[]
): Promise<CustomPermissionResult[]>;
/**
* Share text data with another App or with the host app.
* @param info The shared data must match the property in [ShareInfoType].
* @returns The Promise of share info action state from injected side.
*/
shareInfo(info: ShareInfoType): Promise<string>;
/**
* Swap and lock the screen orientation.
* There is no guarantee that all hostapps and devices allow the force screen change so MiniApp should not rely on this.
* @param screenOrientation The action that miniapp wants to request on device.
* @returns The Promise of screen action state from injected side.
*/
setScreenOrientation(screenOrientation: ScreenOrientation): Promise<string>;
/**
* Request the point balance from the host app.
* @returns Promise of the provided point balance from mini app.
*/
getPoints(): Promise<Points>;
}
/**
* A contract declaring the interaction mechanism between mini-apps and native host app to display ads.
*/
interface Ad {
/**
* Loads the specified Interstittial Ad Unit ID.
* Can be called multiple times to pre-load multiple ads.
* Promise is resolved when successfully loaded.
* @returns The Promise of load success response.
* Promise is rejected if failed to load.
*/
loadInterstitialAd(id: string): Promise<string>;
/**
* Loads the specified Rewarded Ad Unit ID.
* Can be called multiple times to pre-load multiple ads.
* Promise is resolved when successfully loaded.
* @returns The Promise of load success response.
* Promise is rejected if failed to load.
*/
loadRewardedAd(id: string): Promise<string>;
/**
* Shows the Interstitial Ad for the specified ID.
* Promise is resolved after the user closes the Ad.
* @returns The Promise of close success response.
* Promise is rejected if the Ad failed to display wasn't loaded first using MiniApp.loadInterstitialAd.
*/
showInterstitialAd(id: string): Promise<string>;
/**
* Shows the Rewarded Ad for the specified ID.
* Promise is resolved with an object after the user closes the Ad. The object contains the reward earned by the user.
* Reward will be null if the user did not earn the reward.
* @returns The Promise of Rewarded ad response result from injected side.
* Promise is rejected if the Ad failed to display wasn't loaded first using MiniApp.loadRewardedAds.
*/
showRewardedAd(id: string): Promise<Reward>;
}
interface Platform {
/**
* Detect which platform your mini app is running on.
* @returns `Android`, `iOS`, or `Unknown`
*/
getPlatform(): string;
}
export class MiniApp implements MiniAppFeatures, Ad, Platform {
user: UserInfoProvider = new UserInfo();
chatService = new ChatService();
private requestPermission(permissionType: DevicePermission): Promise<string> {
return getBridge().requestPermission(permissionType);
}
getUniqueId(): Promise<string> {
return getBridge().getUniqueId();
}
requestLocationPermission(permissionDescription = ''): Promise<string> {
const locationPermission = [
{
name: CustomPermissionName.LOCATION,
description: permissionDescription,
},
];
return this.requestCustomPermissions(locationPermission)
.then(permission =>
permission.find(
result =>
result.status === CustomPermissionStatus.ALLOWED ||
// Case where older Android SDK doesn't support the Location custom permission
result.status === CustomPermissionStatus.PERMISSION_NOT_AVAILABLE
)
)
.catch(error =>
// Case where older iOS SDK doesn't support the Location custom permission
typeof error === 'string' &&
error.startsWith('invalidCustomPermissionsList')
? Promise.resolve(true)
: Promise.reject(error)
)
.then(hasPermission =>
hasPermission
? this.requestPermission(DevicePermission.LOCATION)
: Promise.reject('User denied location permission to this mini app.')
);
}
requestCustomPermissions(
permissions: CustomPermission[]
): Promise<CustomPermissionResult[]> {
return getBridge()
.requestCustomPermissions(permissions)
.then(permissionResult => permissionResult.permissions);
}
loadInterstitialAd(id: string): Promise<string> {
return getBridge().loadInterstitialAd(id);
}
loadRewardedAd(id: string): Promise<string> {
return getBridge().loadRewardedAd(id);
}
showInterstitialAd(id: string): Promise<string> {
return getBridge().showInterstitialAd(id);
}
showRewardedAd(id: string): Promise<Reward> {
return getBridge().showRewardedAd(id);
}
shareInfo(info: ShareInfoType): Promise<string> {
return getBridge().shareInfo(info);
}
getPlatform(): string {
let platform = 'Unknown';
try {
platform = getBridge().platform;
} catch (e) {}
return platform;
}
setScreenOrientation(screenOrientation: ScreenOrientation): Promise<string> {
return getBridge().setScreenOrientation(screenOrientation);
}
getPoints(): Promise<Points> {
return getBridge().getPoints();
}
}