@tma.js/sdk 1.4.3 → 1.4.6
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/README.md +2 -2
- package/dist/dts/bridge/events/events.d.ts +16 -16
- package/dist/dts/bridge/methods/methods.d.ts +27 -27
- package/dist/dts/bridge/methods/popup.d.ts +1 -1
- package/dist/dts/{init/css → css}/bindMiniAppCSSVars.d.ts +2 -2
- package/dist/dts/{init/css → css}/bindThemeCSSVars.d.ts +1 -1
- package/dist/dts/{init/css → css}/bindViewportCSSVars.d.ts +1 -1
- package/dist/dts/css/index.d.ts +4 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/init-data/types.d.ts +3 -3
- package/dist/dts/launch-params/types.d.ts +1 -1
- package/dist/dts/theme-params/types.d.ts +1 -1
- package/dist/index.cjs +1 -2
- package/dist/index.iife.js +1 -2
- package/dist/index.mjs +126 -115
- package/package.json +2 -3
- package/dist/index.cjs.map +0 -1
- package/dist/index.iife.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/__tests__/globals.ts +0 -39
- package/src/back-button/BackButton.ts +0 -90
- package/src/back-button/__tests__/BackButton.ts +0 -129
- package/src/back-button/index.ts +0 -2
- package/src/back-button/types.ts +0 -14
- package/src/bridge/__tests__/parseMessage.ts +0 -23
- package/src/bridge/__tests__/request.ts +0 -236
- package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
- package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
- package/src/bridge/env/__tests__/isIframe.ts +0 -30
- package/src/bridge/env/hasExternalNotify.ts +0 -19
- package/src/bridge/env/hasWebviewProxy.ts +0 -19
- package/src/bridge/env/index.ts +0 -3
- package/src/bridge/env/isIframe.ts +0 -11
- package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
- package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
- package/src/bridge/errors/index.ts +0 -2
- package/src/bridge/events/__tests__/createEmitter.ts +0 -143
- package/src/bridge/events/__tests__/off.ts +0 -34
- package/src/bridge/events/__tests__/on.ts +0 -49
- package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
- package/src/bridge/events/__tests__/once.ts +0 -64
- package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
- package/src/bridge/events/__tests__/subscribe.ts +0 -49
- package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
- package/src/bridge/events/createEmitter.ts +0 -108
- package/src/bridge/events/events.ts +0 -170
- package/src/bridge/events/index.ts +0 -9
- package/src/bridge/events/off.ts +0 -14
- package/src/bridge/events/on.ts +0 -19
- package/src/bridge/events/onTelegramEvent.ts +0 -81
- package/src/bridge/events/once.ts +0 -18
- package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
- package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
- package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
- package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
- package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
- package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
- package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
- package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
- package/src/bridge/events/parsers/index.ts +0 -9
- package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
- package/src/bridge/events/parsers/phoneRequested.ts +0 -14
- package/src/bridge/events/parsers/popupClosed.ts +0 -19
- package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
- package/src/bridge/events/parsers/theme-changed.ts +0 -58
- package/src/bridge/events/parsers/viewportChanged.ts +0 -33
- package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
- package/src/bridge/events/singletonEmitter.ts +0 -19
- package/src/bridge/events/subscribe.ts +0 -15
- package/src/bridge/events/unsubscribe.ts +0 -10
- package/src/bridge/index.ts +0 -7
- package/src/bridge/invokeCustomMethod.ts +0 -56
- package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
- package/src/bridge/methods/__tests__/postEvent.ts +0 -137
- package/src/bridge/methods/createPostEvent.ts +0 -40
- package/src/bridge/methods/custom-methods.ts +0 -68
- package/src/bridge/methods/haptic.ts +0 -52
- package/src/bridge/methods/index.ts +0 -6
- package/src/bridge/methods/methods.ts +0 -370
- package/src/bridge/methods/popup.ts +0 -53
- package/src/bridge/methods/postEvent.ts +0 -101
- package/src/bridge/parseMessage.ts +0 -28
- package/src/bridge/request.ts +0 -176
- package/src/classnames/__tests__/classNames.ts +0 -20
- package/src/classnames/__tests__/mergeClassNames.ts +0 -21
- package/src/classnames/classNames.ts +0 -34
- package/src/classnames/index.ts +0 -2
- package/src/classnames/mergeClassNames.ts +0 -60
- package/src/closing-behavior/ClosingBehavior.ts +0 -64
- package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
- package/src/closing-behavior/index.ts +0 -2
- package/src/closing-behavior/types.ts +0 -12
- package/src/cloud-storage/CloudStorage.ts +0 -138
- package/src/cloud-storage/index.ts +0 -1
- package/src/colors/__tests__/isColorDark.ts +0 -12
- package/src/colors/__tests__/isRGB.ts +0 -13
- package/src/colors/__tests__/isRGBShort.ts +0 -13
- package/src/colors/__tests__/toRGB.ts +0 -23
- package/src/colors/index.ts +0 -5
- package/src/colors/isColorDark.ts +0 -22
- package/src/colors/isRGB.ts +0 -9
- package/src/colors/isRGBShort.ts +0 -9
- package/src/colors/toRGB.ts +0 -49
- package/src/colors/types.ts +0 -9
- package/src/event-emitter/EventEmitter.ts +0 -146
- package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
- package/src/event-emitter/index.ts +0 -2
- package/src/event-emitter/types.ts +0 -60
- package/src/globals.ts +0 -38
- package/src/haptic-feedback/HapticFeedback.ts +0 -70
- package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
- package/src/haptic-feedback/index.ts +0 -1
- package/src/index.ts +0 -179
- package/src/init/catchCustomStyles.ts +0 -17
- package/src/init/creators/__tests__/createViewport.ts +0 -96
- package/src/init/creators/createBackButton.ts +0 -25
- package/src/init/creators/createClosingBehavior.ts +0 -24
- package/src/init/creators/createMainButton.ts +0 -51
- package/src/init/creators/createMiniApp.ts +0 -48
- package/src/init/creators/createRequestIdGenerator.ts +0 -13
- package/src/init/creators/createSettingsButton.ts +0 -25
- package/src/init/creators/createThemeParams.ts +0 -11
- package/src/init/creators/createViewport.ts +0 -94
- package/src/init/creators/index.ts +0 -8
- package/src/init/css/bindMiniAppCSSVars.ts +0 -48
- package/src/init/css/bindThemeCSSVars.ts +0 -31
- package/src/init/css/bindViewportCSSVars.ts +0 -36
- package/src/init/css/index.ts +0 -1
- package/src/init/css/processCSSVarsOption.ts +0 -57
- package/src/init/css/setCSSVar.ts +0 -8
- package/src/init/index.ts +0 -2
- package/src/init/init.ts +0 -134
- package/src/init/types.ts +0 -94
- package/src/init-data/InitData.ts +0 -96
- package/src/init-data/__tests__/InitData.ts +0 -98
- package/src/init-data/__tests__/chatParser.ts +0 -102
- package/src/init-data/__tests__/initDataParser.ts +0 -136
- package/src/init-data/__tests__/parseInitData.ts +0 -136
- package/src/init-data/__tests__/userParser.ts +0 -96
- package/src/init-data/chatParser.ts +0 -19
- package/src/init-data/index.ts +0 -6
- package/src/init-data/initDataParser.ts +0 -41
- package/src/init-data/parseInitData.ts +0 -10
- package/src/init-data/types.ts +0 -164
- package/src/init-data/userParser.ts +0 -45
- package/src/invoice/Invoice.ts +0 -123
- package/src/invoice/index.ts +0 -2
- package/src/invoice/types.ts +0 -11
- package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
- package/src/launch-params/computeLaunchData.ts +0 -81
- package/src/launch-params/computePageReload.ts +0 -13
- package/src/launch-params/getFirstNavigationEntry.ts +0 -10
- package/src/launch-params/index.ts +0 -13
- package/src/launch-params/launchParamsParser.ts +0 -45
- package/src/launch-params/parseLaunchParams.ts +0 -10
- package/src/launch-params/retrieveCurrent.ts +0 -27
- package/src/launch-params/retrieveFromLocation.ts +0 -10
- package/src/launch-params/retrieveFromPerformance.ts +0 -18
- package/src/launch-params/retrieveFromUrl.ts +0 -19
- package/src/launch-params/retrieveLaunchData.ts +0 -30
- package/src/launch-params/serializeLaunchParams.ts +0 -37
- package/src/launch-params/storage.ts +0 -33
- package/src/launch-params/types.ts +0 -62
- package/src/logger/Logger.ts +0 -72
- package/src/logger/__tests__/Logger.ts +0 -107
- package/src/logger/index.ts +0 -1
- package/src/main-button/MainButton.ts +0 -239
- package/src/main-button/__tests__/MainButton.ts +0 -346
- package/src/main-button/index.ts +0 -2
- package/src/main-button/types.ts +0 -26
- package/src/mini-app/MiniApp.ts +0 -348
- package/src/mini-app/__tests__/MiniApp.ts +0 -140
- package/src/mini-app/contactParser.ts +0 -29
- package/src/mini-app/index.ts +0 -2
- package/src/mini-app/types.ts +0 -38
- package/src/misc/__tests__/isRecord.ts +0 -21
- package/src/misc/index.ts +0 -2
- package/src/misc/isRecord.ts +0 -7
- package/src/misc/isTMA.ts +0 -13
- package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
- package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
- package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
- package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
- package/src/navigation/HashNavigator/drop.ts +0 -36
- package/src/navigation/HashNavigator/go.ts +0 -28
- package/src/navigation/HashNavigator/index.ts +0 -2
- package/src/navigation/HashNavigator/types.ts +0 -41
- package/src/navigation/Navigator/Navigator.ts +0 -282
- package/src/navigation/Navigator/index.ts +0 -2
- package/src/navigation/Navigator/types.ts +0 -55
- package/src/navigation/ensurePrefix.ts +0 -9
- package/src/navigation/getHash.ts +0 -17
- package/src/navigation/index.ts +0 -4
- package/src/parsing/ArrayValueParser.ts +0 -79
- package/src/parsing/ParseError.ts +0 -27
- package/src/parsing/ParseSchemaFieldError.ts +0 -21
- package/src/parsing/ValueParser.ts +0 -71
- package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
- package/src/parsing/__tests__/toRecord.ts +0 -10
- package/src/parsing/createValueParserGenerator.ts +0 -16
- package/src/parsing/index.ts +0 -10
- package/src/parsing/parseBySchema.ts +0 -65
- package/src/parsing/parsers/__tests__/array.ts +0 -39
- package/src/parsing/parsers/__tests__/boolean.ts +0 -31
- package/src/parsing/parsers/__tests__/date.ts +0 -25
- package/src/parsing/parsers/__tests__/json.ts +0 -80
- package/src/parsing/parsers/__tests__/number.ts +0 -23
- package/src/parsing/parsers/__tests__/rgb.ts +0 -22
- package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
- package/src/parsing/parsers/__tests__/string.ts +0 -25
- package/src/parsing/parsers/array.ts +0 -9
- package/src/parsing/parsers/boolean.ts +0 -22
- package/src/parsing/parsers/date.ts +0 -11
- package/src/parsing/parsers/index.ts +0 -8
- package/src/parsing/parsers/json.ts +0 -17
- package/src/parsing/parsers/number.ts +0 -21
- package/src/parsing/parsers/rgb.ts +0 -10
- package/src/parsing/parsers/searchParams.ts +0 -24
- package/src/parsing/parsers/string.ts +0 -12
- package/src/parsing/toRecord.ts +0 -27
- package/src/parsing/types.ts +0 -32
- package/src/parsing/unexpectedTypeError.ts +0 -6
- package/src/popup/Popup.ts +0 -91
- package/src/popup/__tests__/Popup.ts +0 -130
- package/src/popup/__tests__/preparePopupParams.ts +0 -85
- package/src/popup/index.ts +0 -2
- package/src/popup/preparePopupParams.ts +0 -59
- package/src/popup/types.ts +0 -69
- package/src/qr-scanner/QRScanner.ts +0 -95
- package/src/qr-scanner/index.ts +0 -2
- package/src/qr-scanner/types.ts +0 -11
- package/src/settings-button/SettingsButton.ts +0 -85
- package/src/settings-button/index.ts +0 -2
- package/src/settings-button/types.ts +0 -15
- package/src/state/State.ts +0 -67
- package/src/state/index.ts +0 -2
- package/src/state/types.ts +0 -31
- package/src/storage.ts +0 -69
- package/src/supports/__tests__/supports.ts +0 -123
- package/src/supports/createSupportsFunc.ts +0 -18
- package/src/supports/createSupportsParamFunc.ts +0 -27
- package/src/supports/index.ts +0 -4
- package/src/supports/supports.ts +0 -84
- package/src/supports/types.ts +0 -1
- package/src/theme-params/ThemeParams.ts +0 -131
- package/src/theme-params/__tests__/keys.ts +0 -19
- package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
- package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
- package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
- package/src/theme-params/index.ts +0 -6
- package/src/theme-params/keys.ts +0 -24
- package/src/theme-params/parseThemeParams.ts +0 -10
- package/src/theme-params/requestThemeParams.ts +0 -13
- package/src/theme-params/serializeThemeParams.ts +0 -20
- package/src/theme-params/themeParamsParser.ts +0 -22
- package/src/theme-params/types.ts +0 -33
- package/src/timeout/TimeoutError.ts +0 -6
- package/src/timeout/__tests__/isTimeoutError.ts +0 -9
- package/src/timeout/__tests__/withTimeout.ts +0 -28
- package/src/timeout/index.ts +0 -4
- package/src/timeout/isTimeoutError.ts +0 -9
- package/src/timeout/sleep.ts +0 -10
- package/src/timeout/withTimeout.ts +0 -24
- package/src/types/index.ts +0 -4
- package/src/types/methods.ts +0 -18
- package/src/types/platform.ts +0 -14
- package/src/types/request-id.ts +0 -10
- package/src/types/utils.ts +0 -50
- package/src/utils/Utils.ts +0 -107
- package/src/utils/index.ts +0 -1
- package/src/version/__tests__/compareVersions.ts +0 -19
- package/src/version/compareVersions.ts +0 -28
- package/src/version/index.ts +0 -2
- package/src/version/types.ts +0 -4
- package/src/viewport/Viewport.ts +0 -171
- package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
- package/src/viewport/__tests__/utils.ts +0 -12
- package/src/viewport/index.ts +0 -4
- package/src/viewport/isStableViewportPlatform.ts +0 -10
- package/src/viewport/requestViewport.ts +0 -23
- package/src/viewport/types.ts +0 -23
- package/src/viewport/utils.ts +0 -7
- /package/dist/dts/{init/css → css}/setCSSVar.d.ts +0 -0
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Viewport } from '~/viewport/index.js';
|
|
2
|
-
|
|
3
|
-
import { setCSSVar } from './setCSSVar.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Accepts Viewport instance and sets CSS variables connected with viewport
|
|
7
|
-
* sizes.
|
|
8
|
-
*
|
|
9
|
-
* Be careful using this function as long as it can impact application
|
|
10
|
-
* performance. Viewport size is changing rather often, this makes CSS
|
|
11
|
-
* variables update, which leads to possible layout redraw.
|
|
12
|
-
*
|
|
13
|
-
* Variables:
|
|
14
|
-
* - `--tg-viewport-height`
|
|
15
|
-
* - `--tg-viewport-width`
|
|
16
|
-
* - `--tg-viewport-stable-height`
|
|
17
|
-
*
|
|
18
|
-
* Variables are being automatically updated in case, corresponding properties
|
|
19
|
-
* updated in passed Viewport instance.
|
|
20
|
-
*
|
|
21
|
-
* @param viewport - Viewport instance.
|
|
22
|
-
*/
|
|
23
|
-
export function bindViewportCSSVars(viewport: Viewport): void {
|
|
24
|
-
const setHeight = () => setCSSVar('--tg-viewport-height', `${viewport.height}px`);
|
|
25
|
-
const setWidth = () => setCSSVar('--tg-viewport-width', `${viewport.width}px`);
|
|
26
|
-
const setStableHeight = () => setCSSVar('--tg-viewport-height', `${viewport.stableHeight}px`);
|
|
27
|
-
|
|
28
|
-
// TODO: Should probably add debounce or throttle.
|
|
29
|
-
viewport.on('change:height', setHeight);
|
|
30
|
-
viewport.on('change:width', setWidth);
|
|
31
|
-
viewport.on('change:stableHeight', setStableHeight);
|
|
32
|
-
|
|
33
|
-
setHeight();
|
|
34
|
-
setWidth();
|
|
35
|
-
setStableHeight();
|
|
36
|
-
}
|
package/src/init/css/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './processCSSVarsOption.js';
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { MiniApp } from '~/mini-app/index.js';
|
|
2
|
-
import type { ThemeParams } from '~/theme-params/index.js';
|
|
3
|
-
import type { Viewport } from '~/viewport/index.js';
|
|
4
|
-
|
|
5
|
-
import { bindMiniAppCSSVars } from './bindMiniAppCSSVars.js';
|
|
6
|
-
import { bindThemeCSSVars } from './bindThemeCSSVars.js';
|
|
7
|
-
import { bindViewportCSSVars } from './bindViewportCSSVars.js';
|
|
8
|
-
import type { InitCSSVarsOption, InitCSSVarsSpecificOption } from '../types.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Converts init cssVars option to more narrow type.
|
|
12
|
-
* @param option - option value.
|
|
13
|
-
*/
|
|
14
|
-
function parseCSSVarsOptions(option: InitCSSVarsOption): InitCSSVarsSpecificOption {
|
|
15
|
-
if (typeof option === 'object') {
|
|
16
|
-
return option;
|
|
17
|
-
}
|
|
18
|
-
return option
|
|
19
|
-
? {
|
|
20
|
-
themeParams: true,
|
|
21
|
-
viewport: true,
|
|
22
|
-
miniApp: true,
|
|
23
|
-
}
|
|
24
|
-
: {};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Process initialization CSS vars option.
|
|
29
|
-
* @param option - option value.
|
|
30
|
-
* @param miniApp - MiniApp instance.
|
|
31
|
-
* @param themeParams - ThemeParams instance.
|
|
32
|
-
* @param viewportOrPromise - Viewport instance or promise resolving it.
|
|
33
|
-
*/
|
|
34
|
-
export function processCSSVars(
|
|
35
|
-
option: InitCSSVarsOption,
|
|
36
|
-
miniApp: MiniApp,
|
|
37
|
-
themeParams: ThemeParams,
|
|
38
|
-
viewportOrPromise: Viewport | Promise<Viewport>,
|
|
39
|
-
): void {
|
|
40
|
-
const cssVarsOptions = parseCSSVarsOptions(option);
|
|
41
|
-
|
|
42
|
-
if (cssVarsOptions.miniApp) {
|
|
43
|
-
bindMiniAppCSSVars(miniApp, themeParams);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (cssVarsOptions.themeParams) {
|
|
47
|
-
bindThemeCSSVars(themeParams);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (cssVarsOptions.viewport) {
|
|
51
|
-
if (viewportOrPromise instanceof Promise) {
|
|
52
|
-
viewportOrPromise.then(bindViewportCSSVars);
|
|
53
|
-
} else {
|
|
54
|
-
bindViewportCSSVars(viewportOrPromise);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/init/index.ts
DELETED
package/src/init/init.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { createPostEvent, isIframe, on } from '~/bridge/index.js';
|
|
2
|
-
import { CloudStorage } from '~/cloud-storage/index.js';
|
|
3
|
-
import { HapticFeedback } from '~/haptic-feedback/index.js';
|
|
4
|
-
import { catchCustomStyles } from '~/init/catchCustomStyles.js';
|
|
5
|
-
import {
|
|
6
|
-
createBackButton,
|
|
7
|
-
createClosingBehavior,
|
|
8
|
-
createMainButton,
|
|
9
|
-
createMiniApp,
|
|
10
|
-
createRequestIdGenerator,
|
|
11
|
-
createSettingsButton,
|
|
12
|
-
createThemeParams,
|
|
13
|
-
createViewport,
|
|
14
|
-
} from '~/init/creators/index.js';
|
|
15
|
-
import { processCSSVars } from '~/init/css/index.js';
|
|
16
|
-
import { InitData } from '~/init-data/index.js';
|
|
17
|
-
import { Invoice } from '~/invoice/index.js';
|
|
18
|
-
import { retrieveLaunchData } from '~/launch-params/index.js';
|
|
19
|
-
import { Popup } from '~/popup/index.js';
|
|
20
|
-
import { QRScanner } from '~/qr-scanner/index.js';
|
|
21
|
-
import { Utils } from '~/utils/index.js';
|
|
22
|
-
|
|
23
|
-
import type { InitOptions, InitResult } from './types.js';
|
|
24
|
-
|
|
25
|
-
type ComputedInitResult<O> = O extends { async: true } | { complete: true }
|
|
26
|
-
? Promise<InitResult>
|
|
27
|
-
: InitResult;
|
|
28
|
-
|
|
29
|
-
export function init(): InitResult;
|
|
30
|
-
export function init<O extends InitOptions>(options: O): ComputedInitResult<O>;
|
|
31
|
-
export function init(options: InitOptions = {}): InitResult | Promise<InitResult> {
|
|
32
|
-
const {
|
|
33
|
-
async = false,
|
|
34
|
-
complete = async,
|
|
35
|
-
cssVars = false,
|
|
36
|
-
acceptCustomStyles = false,
|
|
37
|
-
} = options;
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
// Retrieve launch data.
|
|
41
|
-
const {
|
|
42
|
-
launchParams: {
|
|
43
|
-
initData,
|
|
44
|
-
initDataRaw,
|
|
45
|
-
version,
|
|
46
|
-
platform,
|
|
47
|
-
themeParams,
|
|
48
|
-
botInline = false,
|
|
49
|
-
},
|
|
50
|
-
isPageReload,
|
|
51
|
-
} = retrieveLaunchData();
|
|
52
|
-
|
|
53
|
-
const createRequestId = createRequestIdGenerator();
|
|
54
|
-
const postEvent = createPostEvent(version);
|
|
55
|
-
|
|
56
|
-
// In Telegram web version we should listen to special event sent from the Telegram application
|
|
57
|
-
// to know, when we should reload the Mini App.
|
|
58
|
-
if (isIframe()) {
|
|
59
|
-
if (acceptCustomStyles) {
|
|
60
|
-
catchCustomStyles();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Notify Telegram, iframe is ready. This will result in sending style tag html from native
|
|
64
|
-
// application which is used in catchCustomStyles function. We should call this method also
|
|
65
|
-
// to start receiving "reload_iframe" events from the Telegram application.
|
|
66
|
-
postEvent('iframe_ready', { reload_supported: true });
|
|
67
|
-
on('reload_iframe', () => window.location.reload());
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const result: Omit<InitResult, 'viewport'> = {
|
|
71
|
-
backButton: createBackButton(isPageReload, version, postEvent),
|
|
72
|
-
closingBehavior: createClosingBehavior(isPageReload, postEvent),
|
|
73
|
-
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
|
|
74
|
-
createRequestId,
|
|
75
|
-
hapticFeedback: new HapticFeedback(version, postEvent),
|
|
76
|
-
invoice: new Invoice(version, postEvent),
|
|
77
|
-
mainButton: createMainButton(
|
|
78
|
-
isPageReload,
|
|
79
|
-
themeParams.buttonColor || '#000000',
|
|
80
|
-
themeParams.buttonTextColor || '#ffffff',
|
|
81
|
-
postEvent,
|
|
82
|
-
),
|
|
83
|
-
miniApp: createMiniApp(
|
|
84
|
-
isPageReload,
|
|
85
|
-
themeParams.backgroundColor || '#ffffff',
|
|
86
|
-
version,
|
|
87
|
-
botInline,
|
|
88
|
-
createRequestId,
|
|
89
|
-
postEvent,
|
|
90
|
-
),
|
|
91
|
-
popup: new Popup(version, postEvent),
|
|
92
|
-
postEvent,
|
|
93
|
-
qrScanner: new QRScanner(version, postEvent),
|
|
94
|
-
settingsButton: createSettingsButton(isPageReload, version, postEvent),
|
|
95
|
-
themeParams: createThemeParams(themeParams),
|
|
96
|
-
utils: new Utils(version, createRequestId, postEvent),
|
|
97
|
-
...(initData
|
|
98
|
-
// Init data could be missing in case, application was launched via InlineKeyboardButton.
|
|
99
|
-
? {
|
|
100
|
-
initData: new InitData(initData),
|
|
101
|
-
initDataRaw,
|
|
102
|
-
}
|
|
103
|
-
: {}),
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const viewport = createViewport(isPageReload, platform, postEvent, complete);
|
|
107
|
-
if (viewport instanceof Promise || complete) {
|
|
108
|
-
return Promise.resolve(viewport).then((vp) => {
|
|
109
|
-
processCSSVars(
|
|
110
|
-
cssVars,
|
|
111
|
-
result.miniApp,
|
|
112
|
-
result.themeParams,
|
|
113
|
-
vp,
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
return { ...result, viewport: vp };
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
processCSSVars(
|
|
121
|
-
cssVars,
|
|
122
|
-
result.miniApp,
|
|
123
|
-
result.themeParams,
|
|
124
|
-
viewport,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
return { ...result, viewport };
|
|
128
|
-
} catch (e) {
|
|
129
|
-
if (complete) {
|
|
130
|
-
return Promise.reject(e);
|
|
131
|
-
}
|
|
132
|
-
throw e;
|
|
133
|
-
}
|
|
134
|
-
}
|
package/src/init/types.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type { BackButton } from '~/back-button/index.js';
|
|
2
|
-
import type { PostEvent } from '~/bridge/index.js';
|
|
3
|
-
import type { ClosingBehavior } from '~/closing-behavior/index.js';
|
|
4
|
-
import type { CloudStorage } from '~/cloud-storage/index.js';
|
|
5
|
-
import type { HapticFeedback } from '~/haptic-feedback/index.js';
|
|
6
|
-
import type { InitData } from '~/init-data/index.js';
|
|
7
|
-
import type { Invoice } from '~/invoice/index.js';
|
|
8
|
-
import type { MainButton } from '~/main-button/index.js';
|
|
9
|
-
import type { MiniApp } from '~/mini-app/index.js';
|
|
10
|
-
import type { Popup } from '~/popup/index.js';
|
|
11
|
-
import type { QRScanner } from '~/qr-scanner/index.js';
|
|
12
|
-
import type { SettingsButton } from '~/settings-button/index.js';
|
|
13
|
-
import type { ThemeParams } from '~/theme-params/index.js';
|
|
14
|
-
import type { CreateRequestIdFunc } from '~/types/index.js';
|
|
15
|
-
import type { Utils } from '~/utils/index.js';
|
|
16
|
-
import type { Viewport } from '~/viewport/index.js';
|
|
17
|
-
|
|
18
|
-
export interface InitResult {
|
|
19
|
-
backButton: BackButton;
|
|
20
|
-
closingBehavior: ClosingBehavior;
|
|
21
|
-
cloudStorage: CloudStorage;
|
|
22
|
-
createRequestId: CreateRequestIdFunc;
|
|
23
|
-
hapticFeedback: HapticFeedback;
|
|
24
|
-
initData?: InitData;
|
|
25
|
-
initDataRaw?: string;
|
|
26
|
-
invoice: Invoice;
|
|
27
|
-
mainButton: MainButton;
|
|
28
|
-
miniApp: MiniApp;
|
|
29
|
-
popup: Popup;
|
|
30
|
-
postEvent: PostEvent;
|
|
31
|
-
qrScanner: QRScanner;
|
|
32
|
-
settingsButton: SettingsButton;
|
|
33
|
-
themeParams: ThemeParams;
|
|
34
|
-
utils: Utils;
|
|
35
|
-
viewport: Viewport;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface InitCSSVarsSpecificOption {
|
|
39
|
-
/**
|
|
40
|
-
* Enables theme parameters CSS variables.
|
|
41
|
-
* @see bindThemeCSSVars
|
|
42
|
-
* @default false
|
|
43
|
-
*/
|
|
44
|
-
themeParams?: boolean;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Enables viewport CSS variables.
|
|
48
|
-
* @see bindViewportCSSVars
|
|
49
|
-
* @default false
|
|
50
|
-
*/
|
|
51
|
-
viewport?: boolean;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Enables mini app CSS variables.
|
|
55
|
-
* @see bindMiniAppCSSVars
|
|
56
|
-
* @default false
|
|
57
|
-
*/
|
|
58
|
-
miniApp?: boolean;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export type InitCSSVarsOption = boolean | InitCSSVarsSpecificOption;
|
|
62
|
-
|
|
63
|
-
export interface InitOptions {
|
|
64
|
-
/**
|
|
65
|
-
* @deprecated This option name was considered inappropriate. Use `complete` instead.
|
|
66
|
-
*/
|
|
67
|
-
async?: boolean;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* True if SDK should accept styles sent from the Telegram web application. This option is only
|
|
71
|
-
* used in web versions of Telegram.
|
|
72
|
-
* @default false
|
|
73
|
-
*/
|
|
74
|
-
acceptCustomStyles?: boolean;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Should SDK create global CSS variables related to current Telegram application colors.
|
|
78
|
-
*
|
|
79
|
-
* Possible values:
|
|
80
|
-
* - `false` - no CSS variables will be created.
|
|
81
|
-
* - `true` - all CSS variables will be created.
|
|
82
|
-
* - object - applies specific CSS variables.
|
|
83
|
-
*
|
|
84
|
-
* @default false
|
|
85
|
-
*/
|
|
86
|
-
cssVars?: InitCSSVarsOption;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* True if initialization must be performed completely. This includes retrieving some components
|
|
90
|
-
* state from the Telegram application, and as a result, this makes initialization asynchronous.
|
|
91
|
-
* @default false
|
|
92
|
-
*/
|
|
93
|
-
complete?: boolean;
|
|
94
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Chat,
|
|
3
|
-
ChatType,
|
|
4
|
-
InitDataParsed,
|
|
5
|
-
User,
|
|
6
|
-
} from './types.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Class which is responsible for displaying Mini Apps init data.
|
|
10
|
-
*/
|
|
11
|
-
export class InitData {
|
|
12
|
-
constructor(private readonly initData: InitDataParsed) {
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @see InitDataParsed.authDate
|
|
17
|
-
*/
|
|
18
|
-
get authDate(): Date {
|
|
19
|
-
return this.initData.authDate;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @see InitDataParsed.canSendAfter
|
|
24
|
-
*/
|
|
25
|
-
get canSendAfter(): number | undefined {
|
|
26
|
-
return this.initData.canSendAfter;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Date after which it is allowed to call
|
|
31
|
-
* the [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
|
|
32
|
-
*/
|
|
33
|
-
get canSendAfterDate(): Date | undefined {
|
|
34
|
-
const { canSendAfter } = this;
|
|
35
|
-
|
|
36
|
-
return canSendAfter === undefined
|
|
37
|
-
? undefined
|
|
38
|
-
: new Date(this.authDate.getTime() + canSendAfter * 1000);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @see InitDataParsed.chat
|
|
43
|
-
*/
|
|
44
|
-
get chat(): Chat | undefined {
|
|
45
|
-
return this.initData.chat;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @see InitDataParsed.chatType
|
|
50
|
-
*/
|
|
51
|
-
get chatType(): ChatType | undefined {
|
|
52
|
-
return this.initData.chatType;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @see InitDataParsed.chatInstance
|
|
57
|
-
*/
|
|
58
|
-
get chatInstance(): string | undefined {
|
|
59
|
-
return this.initData.chatInstance;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @see InitDataParsed.hash
|
|
64
|
-
*/
|
|
65
|
-
get hash(): string {
|
|
66
|
-
return this.initData.hash;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @see InitDataParsed.queryId
|
|
71
|
-
*/
|
|
72
|
-
get queryId(): string | undefined {
|
|
73
|
-
return this.initData.queryId;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @see InitDataParsed.receiver
|
|
78
|
-
*/
|
|
79
|
-
get receiver(): User | undefined {
|
|
80
|
-
return this.initData.receiver;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @see InitDataParsed.startParam
|
|
85
|
-
*/
|
|
86
|
-
get startParam(): string | undefined {
|
|
87
|
-
return this.initData.startParam;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @see InitDataParsed.user
|
|
92
|
-
*/
|
|
93
|
-
get user(): User | undefined {
|
|
94
|
-
return this.initData.user;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { InitData } from '../InitData';
|
|
4
|
-
|
|
5
|
-
it('should return fields specified in constructor', () => {
|
|
6
|
-
const authDate = new Date(123);
|
|
7
|
-
const data1 = new InitData({ authDate, hash: 'hash' });
|
|
8
|
-
expect(data1.authDate).toBe(authDate);
|
|
9
|
-
expect(data1.canSendAfter).toBeUndefined();
|
|
10
|
-
expect(data1.chat).toBeUndefined();
|
|
11
|
-
expect(data1.chatType).toBeUndefined();
|
|
12
|
-
expect(data1.chatInstance).toBeUndefined();
|
|
13
|
-
expect(data1.hash).toBe('hash');
|
|
14
|
-
expect(data1.queryId).toBeUndefined();
|
|
15
|
-
expect(data1.receiver).toBeUndefined();
|
|
16
|
-
expect(data1.startParam).toBeUndefined();
|
|
17
|
-
expect(data1.user).toBeUndefined();
|
|
18
|
-
|
|
19
|
-
const canSendAfter = 1;
|
|
20
|
-
const data2 = new InitData({
|
|
21
|
-
authDate,
|
|
22
|
-
canSendAfter,
|
|
23
|
-
chat: {
|
|
24
|
-
id: 999,
|
|
25
|
-
photoUrl: 'photo',
|
|
26
|
-
type: 'group',
|
|
27
|
-
title: 'Title',
|
|
28
|
-
},
|
|
29
|
-
chatType: 'sender',
|
|
30
|
-
chatInstance: 'abc',
|
|
31
|
-
hash: 'joke',
|
|
32
|
-
queryId: 'query id',
|
|
33
|
-
receiver: {
|
|
34
|
-
id: 1000,
|
|
35
|
-
photoUrl: 'receiver photo',
|
|
36
|
-
firstName: 'a',
|
|
37
|
-
lastName: 'b',
|
|
38
|
-
username: 'c',
|
|
39
|
-
isBot: false,
|
|
40
|
-
isPremium: false,
|
|
41
|
-
languageCode: 'en',
|
|
42
|
-
},
|
|
43
|
-
startParam: 'param',
|
|
44
|
-
user: {
|
|
45
|
-
id: 2000,
|
|
46
|
-
photoUrl: 'user photo',
|
|
47
|
-
firstName: 'a',
|
|
48
|
-
lastName: 'b',
|
|
49
|
-
username: 'c',
|
|
50
|
-
languageCode: 'en',
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
expect(data2.authDate).toBe(authDate);
|
|
54
|
-
expect(data2.canSendAfter).toBe(canSendAfter);
|
|
55
|
-
expect(data2.chat).toStrictEqual({
|
|
56
|
-
id: 999,
|
|
57
|
-
photoUrl: 'photo',
|
|
58
|
-
type: 'group',
|
|
59
|
-
title: 'Title',
|
|
60
|
-
});
|
|
61
|
-
expect(data2.chatType).toBe('sender');
|
|
62
|
-
expect(data2.chatInstance).toBe('abc');
|
|
63
|
-
expect(data2.hash).toBe('joke');
|
|
64
|
-
expect(data2.queryId).toBe('query id');
|
|
65
|
-
expect(data2.receiver).toStrictEqual({
|
|
66
|
-
id: 1000,
|
|
67
|
-
photoUrl: 'receiver photo',
|
|
68
|
-
firstName: 'a',
|
|
69
|
-
lastName: 'b',
|
|
70
|
-
username: 'c',
|
|
71
|
-
isBot: false,
|
|
72
|
-
isPremium: false,
|
|
73
|
-
languageCode: 'en',
|
|
74
|
-
});
|
|
75
|
-
expect(data2.startParam).toBe('param');
|
|
76
|
-
expect(data2.user).toStrictEqual({
|
|
77
|
-
id: 2000,
|
|
78
|
-
photoUrl: 'user photo',
|
|
79
|
-
firstName: 'a',
|
|
80
|
-
lastName: 'b',
|
|
81
|
-
username: 'c',
|
|
82
|
-
languageCode: 'en',
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should have canSendAfterData equal to authDate + canSendAfter seconds', () => {
|
|
87
|
-
const authDate = new Date();
|
|
88
|
-
const initData = new InitData({
|
|
89
|
-
authDate,
|
|
90
|
-
hash: 'abc',
|
|
91
|
-
canSendAfter: 32000,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
expect(initData.canSendAfter).toBe(32000);
|
|
95
|
-
expect(initData.canSendAfterDate).toStrictEqual(
|
|
96
|
-
new Date(authDate.getTime() + 32000000),
|
|
97
|
-
);
|
|
98
|
-
});
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { chatParser } from '../chatParser';
|
|
4
|
-
|
|
5
|
-
describe('id', () => {
|
|
6
|
-
it('should throw an error in case, this property is missing', () => {
|
|
7
|
-
expect(
|
|
8
|
-
() => chatParser().parse({
|
|
9
|
-
type: 'group chat',
|
|
10
|
-
title: 'My chat',
|
|
11
|
-
}),
|
|
12
|
-
).toThrow();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('should parse source property as number and pass it to the "id" property', () => {
|
|
16
|
-
expect(
|
|
17
|
-
chatParser().parse({
|
|
18
|
-
id: 882,
|
|
19
|
-
type: 'group chat',
|
|
20
|
-
title: 'My chat',
|
|
21
|
-
}),
|
|
22
|
-
).toMatchObject({
|
|
23
|
-
id: 882,
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe('type', () => {
|
|
29
|
-
it('should throw an error in case, this property is missing', () => {
|
|
30
|
-
expect(
|
|
31
|
-
() => chatParser().parse({
|
|
32
|
-
id: 223,
|
|
33
|
-
title: 'My chat',
|
|
34
|
-
}),
|
|
35
|
-
).toThrow();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should parse source property as number and pass it to the "type" property', () => {
|
|
39
|
-
expect(
|
|
40
|
-
chatParser().parse({
|
|
41
|
-
id: 882,
|
|
42
|
-
type: 'group chat',
|
|
43
|
-
title: 'My chat',
|
|
44
|
-
}),
|
|
45
|
-
).toMatchObject({
|
|
46
|
-
type: 'group chat',
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('title', () => {
|
|
52
|
-
it('should throw an error in case, this property is missing', () => {
|
|
53
|
-
expect(
|
|
54
|
-
() => chatParser().parse({
|
|
55
|
-
id: 223,
|
|
56
|
-
type: 'group chat',
|
|
57
|
-
}),
|
|
58
|
-
).toThrow();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('should parse source property as number and pass it to the "title" property', () => {
|
|
62
|
-
expect(
|
|
63
|
-
chatParser().parse({
|
|
64
|
-
id: 882,
|
|
65
|
-
type: 'group chat',
|
|
66
|
-
title: 'My chat',
|
|
67
|
-
}),
|
|
68
|
-
).toMatchObject({
|
|
69
|
-
title: 'My chat',
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe('photo_url', () => {
|
|
75
|
-
it('should parse source property as number and pass it to the "photoUrl" property', () => {
|
|
76
|
-
expect(
|
|
77
|
-
chatParser().parse({
|
|
78
|
-
id: 882,
|
|
79
|
-
type: 'group chat',
|
|
80
|
-
title: 'My chat',
|
|
81
|
-
photo_url: 'https://image.com',
|
|
82
|
-
}),
|
|
83
|
-
).toMatchObject({
|
|
84
|
-
photoUrl: 'https://image.com',
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
describe('username', () => {
|
|
90
|
-
it('should parse source property as number and pass it to the "username" property', () => {
|
|
91
|
-
expect(
|
|
92
|
-
chatParser().parse({
|
|
93
|
-
id: 882,
|
|
94
|
-
type: 'group chat',
|
|
95
|
-
title: 'My chat',
|
|
96
|
-
username: 'Johny Bravo',
|
|
97
|
-
}),
|
|
98
|
-
).toMatchObject({
|
|
99
|
-
username: 'Johny Bravo',
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|