@tma.js/sdk 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.iife.js +1 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +63 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/init/init.ts +89 -82
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tma.js/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "TypeScript Source Development Kit for Telegram Mini Apps client application.",
|
|
5
5
|
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
package/src/init/init.ts
CHANGED
|
@@ -30,96 +30,103 @@ export function init<O extends InitOptions>(options: O): ComputedInitResult<O> {
|
|
|
30
30
|
acceptCustomStyles = false,
|
|
31
31
|
} = options;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
try {
|
|
34
|
+
// Retrieve launch data.
|
|
35
|
+
const {
|
|
36
|
+
launchParams: {
|
|
37
|
+
initData,
|
|
38
|
+
initDataRaw,
|
|
39
|
+
version,
|
|
40
|
+
platform,
|
|
41
|
+
themeParams,
|
|
42
|
+
botInline = false,
|
|
43
|
+
},
|
|
44
|
+
isPageReload,
|
|
45
|
+
} = retrieveLaunchData();
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
const createRequestId = createRequestIdGenerator();
|
|
48
|
+
const postEvent = createPostEvent(version);
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// In Telegram web version we should listen to special event sent from the Telegram application
|
|
51
|
+
// to know, when we should reload the Mini App.
|
|
52
|
+
if (isIframe()) {
|
|
53
|
+
if (acceptCustomStyles) {
|
|
54
|
+
catchCustomStyles();
|
|
55
|
+
}
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
// Notify Telegram, iframe is ready. This will result in sending style tag html from native
|
|
58
|
+
// application which is used in catchCustomStyles function. We should call this method also
|
|
59
|
+
// to start receiving "reload_iframe" events from the Telegram application.
|
|
60
|
+
postEvent('iframe_ready', { reload_supported: true });
|
|
61
|
+
on('reload_iframe', () => window.location.reload());
|
|
62
|
+
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
const result: Omit<InitResult, 'viewport'> = {
|
|
65
|
+
backButton: createBackButton(isPageReload, version, postEvent),
|
|
66
|
+
closingBehavior: createClosingBehavior(isPageReload, postEvent),
|
|
67
|
+
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
|
|
68
|
+
createRequestId,
|
|
69
|
+
hapticFeedback: new HapticFeedback(version, postEvent),
|
|
70
|
+
invoice: new Invoice(version, postEvent),
|
|
71
|
+
mainButton: createMainButton(
|
|
72
|
+
isPageReload,
|
|
73
|
+
themeParams.buttonColor || '#000000',
|
|
74
|
+
themeParams.buttonTextColor || '#ffffff',
|
|
75
|
+
postEvent,
|
|
76
|
+
),
|
|
77
|
+
miniApp: createMiniApp(
|
|
78
|
+
isPageReload,
|
|
79
|
+
themeParams.backgroundColor || '#ffffff',
|
|
80
|
+
version,
|
|
81
|
+
botInline,
|
|
82
|
+
postEvent,
|
|
83
|
+
),
|
|
84
|
+
popup: new Popup(version, postEvent),
|
|
74
85
|
postEvent,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
themeParams: createThemeParams(themeParams),
|
|
87
|
-
utils: new Utils(version, createRequestId, postEvent),
|
|
88
|
-
...(initData
|
|
89
|
-
// Init data could be missing in case, application was launched via InlineKeyboardButton.
|
|
90
|
-
? {
|
|
91
|
-
initData: new InitData(initData),
|
|
92
|
-
initDataRaw,
|
|
93
|
-
}
|
|
94
|
-
: {}),
|
|
95
|
-
};
|
|
86
|
+
qrScanner: new QRScanner(version, postEvent),
|
|
87
|
+
themeParams: createThemeParams(themeParams),
|
|
88
|
+
utils: new Utils(version, createRequestId, postEvent),
|
|
89
|
+
...(initData
|
|
90
|
+
// Init data could be missing in case, application was launched via InlineKeyboardButton.
|
|
91
|
+
? {
|
|
92
|
+
initData: new InitData(initData),
|
|
93
|
+
initDataRaw,
|
|
94
|
+
}
|
|
95
|
+
: {}),
|
|
96
|
+
};
|
|
96
97
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
const viewport = async
|
|
99
|
+
? createViewportAsync(isPageReload, platform, postEvent)
|
|
100
|
+
: createViewportSync(isPageReload, platform, postEvent);
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
if (viewport instanceof Promise) {
|
|
103
|
+
return viewport.then((vp) => {
|
|
104
|
+
processCSSVars(
|
|
105
|
+
cssVars,
|
|
106
|
+
result.miniApp,
|
|
107
|
+
result.themeParams,
|
|
108
|
+
vp,
|
|
109
|
+
);
|
|
109
110
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
return {
|
|
112
|
+
...result,
|
|
113
|
+
viewport: vp,
|
|
114
|
+
};
|
|
115
|
+
}) as ComputedInitResult<O>;
|
|
116
|
+
}
|
|
116
117
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
processCSSVars(
|
|
119
|
+
cssVars,
|
|
120
|
+
result.miniApp,
|
|
121
|
+
result.themeParams,
|
|
122
|
+
viewport,
|
|
123
|
+
);
|
|
123
124
|
|
|
124
|
-
|
|
125
|
+
return { ...result, viewport } as ComputedInitResult<O>;
|
|
126
|
+
} catch (e) {
|
|
127
|
+
if (async) {
|
|
128
|
+
return Promise.reject(e) as unknown as ComputedInitResult<O>;
|
|
129
|
+
}
|
|
130
|
+
throw e;
|
|
131
|
+
}
|
|
125
132
|
}
|