@tma.js/sdk 1.4.4 → 1.4.7
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/env/index.d.ts +0 -1
- 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/index.d.ts +3 -3
- package/dist/dts/init-data/types.d.ts +3 -3
- package/dist/dts/launch-params/index.d.ts +1 -4
- package/dist/dts/launch-params/retrieveFromLocation.d.ts +2 -2
- package/dist/dts/launch-params/retrieveFromPerformance.d.ts +3 -4
- package/dist/dts/launch-params/retrieveFromUrl.d.ts +3 -2
- package/dist/dts/launch-params/retrieveLaunchData.d.ts +1 -0
- package/dist/dts/launch-params/retrieveLaunchParams.d.ts +6 -0
- package/dist/dts/launch-params/storage.d.ts +3 -5
- package/dist/dts/launch-params/types.d.ts +1 -1
- package/dist/dts/{launch-params → misc}/getFirstNavigationEntry.d.ts +1 -1
- package/dist/dts/misc/index.d.ts +3 -0
- package/dist/dts/{bridge/env → misc}/isIframe.d.ts +1 -1
- package/dist/dts/misc/isPageReload.d.ts +5 -0
- 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 +841 -866
- package/package.json +2 -3
- package/dist/dts/launch-params/computeLaunchData.d.ts +0 -6
- package/dist/dts/launch-params/computePageReload.d.ts +0 -6
- package/dist/dts/launch-params/retrieveCurrent.d.ts +0 -5
- 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/css/__tests__/bindMiniAppCSSVars.ts +0 -175
- package/src/css/__tests__/bindThemeCSSVars.ts +0 -52
- package/src/css/__tests__/bindViewportCSSVars.ts +0 -55
- package/src/css/__tests__/setCSSVar.ts +0 -14
- package/src/css/bindMiniAppCSSVars.ts +0 -51
- package/src/css/bindThemeCSSVars.ts +0 -31
- package/src/css/bindViewportCSSVars.ts +0 -36
- package/src/css/index.ts +0 -4
- package/src/css/setCSSVar.ts +0 -8
- 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 -185
- 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/index.ts +0 -1
- package/src/init/css/processCSSVarsOption.ts +0 -55
- 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
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { boolean, json, number } from '~/parsing/index.js';
|
|
2
|
-
|
|
3
|
-
export interface ViewportChangedPayload {
|
|
4
|
-
/**
|
|
5
|
-
* The viewport height.
|
|
6
|
-
*/
|
|
7
|
-
height: number;
|
|
8
|
-
/**
|
|
9
|
-
* The viewport width.
|
|
10
|
-
*/
|
|
11
|
-
width: number;
|
|
12
|
-
/**
|
|
13
|
-
* Is the viewport currently expanded.
|
|
14
|
-
*/
|
|
15
|
-
is_expanded: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Is the viewport current state stable and not going to change in the next moment.
|
|
18
|
-
*/
|
|
19
|
-
is_state_stable: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function viewportChanged() {
|
|
23
|
-
return json<ViewportChangedPayload>({
|
|
24
|
-
height: number(),
|
|
25
|
-
width: (value) => (
|
|
26
|
-
value === null || value === undefined
|
|
27
|
-
? window.innerWidth
|
|
28
|
-
: number().parse(value)
|
|
29
|
-
),
|
|
30
|
-
is_state_stable: boolean(),
|
|
31
|
-
is_expanded: boolean(),
|
|
32
|
-
});
|
|
33
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { json, string } from '~/parsing/index.js';
|
|
2
|
-
|
|
3
|
-
export type WriteAccessRequestedStatus = 'allowed' | string;
|
|
4
|
-
|
|
5
|
-
export interface WriteAccessRequestedPayload {
|
|
6
|
-
/**
|
|
7
|
-
* Request status.
|
|
8
|
-
*/
|
|
9
|
-
status: WriteAccessRequestedStatus;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function writeAccessRequested() {
|
|
13
|
-
return json<WriteAccessRequestedPayload>({ status: string() });
|
|
14
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createEmitter } from '~/bridge/events/createEmitter.js';
|
|
2
|
-
import type { MiniAppsEventEmitter } from '~/bridge/index.js';
|
|
3
|
-
|
|
4
|
-
const CACHED_EMITTER = 'telegram-mini-apps-cached-emitter';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Returns singleton instance of bridge EventEmitter. Also, defines
|
|
8
|
-
* Telegram event handlers.
|
|
9
|
-
*/
|
|
10
|
-
export function singletonEmitter(): MiniAppsEventEmitter {
|
|
11
|
-
const wnd: any = window;
|
|
12
|
-
const cachedEmitter = wnd[CACHED_EMITTER];
|
|
13
|
-
|
|
14
|
-
if (cachedEmitter === undefined) {
|
|
15
|
-
wnd[CACHED_EMITTER] = createEmitter();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return wnd[CACHED_EMITTER];
|
|
19
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { MiniAppsGlobalEventListener } from './events.js';
|
|
2
|
-
import { singletonEmitter } from './singletonEmitter.js';
|
|
3
|
-
import { unsubscribe } from './unsubscribe.js';
|
|
4
|
-
|
|
5
|
-
type StopListening = () => void;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Subscribes to all events sent from the native Telegram application.
|
|
9
|
-
* Returns function used to remove added event listener.
|
|
10
|
-
* @param listener - event listener.
|
|
11
|
-
*/
|
|
12
|
-
export function subscribe(listener: MiniAppsGlobalEventListener): StopListening {
|
|
13
|
-
singletonEmitter().subscribe(listener);
|
|
14
|
-
return () => unsubscribe(listener);
|
|
15
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { MiniAppsGlobalEventListener } from './events.js';
|
|
2
|
-
import { singletonEmitter } from './singletonEmitter.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Removes global event listener.
|
|
6
|
-
* @param listener - event listener.
|
|
7
|
-
*/
|
|
8
|
-
export function unsubscribe(listener: MiniAppsGlobalEventListener): void {
|
|
9
|
-
singletonEmitter().unsubscribe(listener);
|
|
10
|
-
}
|
package/src/bridge/index.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { ExecuteWithOptions } from '~/types/index.js';
|
|
2
|
-
|
|
3
|
-
import type { CustomMethodName, CustomMethodParams } from './methods/index.js';
|
|
4
|
-
import { request } from './request.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Invokes known custom method. Returns method execution result.
|
|
8
|
-
* @param method - method name.
|
|
9
|
-
* @param params - method parameters.
|
|
10
|
-
* @param requestId - request identifier.
|
|
11
|
-
* @param options - additional options.
|
|
12
|
-
*/
|
|
13
|
-
export async function invokeCustomMethod<M extends CustomMethodName>(
|
|
14
|
-
method: M,
|
|
15
|
-
params: CustomMethodParams<M>,
|
|
16
|
-
requestId: string,
|
|
17
|
-
options?: ExecuteWithOptions,
|
|
18
|
-
): Promise<unknown>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Invokes unknown custom method. Returns method execution result.
|
|
22
|
-
* @param method - method name.
|
|
23
|
-
* @param params - method parameters.
|
|
24
|
-
* @param requestId - request identifier.
|
|
25
|
-
* @param options - additional options.
|
|
26
|
-
*/
|
|
27
|
-
export function invokeCustomMethod(
|
|
28
|
-
method: string,
|
|
29
|
-
params: object,
|
|
30
|
-
requestId: string,
|
|
31
|
-
options?: ExecuteWithOptions,
|
|
32
|
-
): Promise<unknown>;
|
|
33
|
-
|
|
34
|
-
export async function invokeCustomMethod(
|
|
35
|
-
method: string,
|
|
36
|
-
params: object,
|
|
37
|
-
requestId: string,
|
|
38
|
-
options: ExecuteWithOptions = {},
|
|
39
|
-
): Promise<unknown> {
|
|
40
|
-
const { result, error } = await request(
|
|
41
|
-
'web_app_invoke_custom_method',
|
|
42
|
-
{
|
|
43
|
-
method,
|
|
44
|
-
params,
|
|
45
|
-
req_id: requestId,
|
|
46
|
-
},
|
|
47
|
-
'custom_method_invoked',
|
|
48
|
-
options,
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
if (error) {
|
|
52
|
-
throw new Error(error);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { expect, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { createPostEvent } from '../../index';
|
|
4
|
-
import * as postEventModule from '../postEvent';
|
|
5
|
-
|
|
6
|
-
vi.mock('~/bridge/methods/postEvent.js', () => ({
|
|
7
|
-
postEvent: vi.fn(),
|
|
8
|
-
}));
|
|
9
|
-
|
|
10
|
-
it('should throw error if passed method is unsupported in specified version', () => {
|
|
11
|
-
const postEvent = createPostEvent('6.0');
|
|
12
|
-
expect(() => postEvent('web_app_request_write_access'))
|
|
13
|
-
.toThrow('Method "web_app_request_write_access" is unsupported in the Mini Apps version 6.0.');
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should throw error if passed method parameter is unsupported in specified version', () => {
|
|
17
|
-
const postEvent = createPostEvent('6.3');
|
|
18
|
-
expect(() => postEvent('web_app_open_link', {
|
|
19
|
-
url: '',
|
|
20
|
-
try_instant_view: true,
|
|
21
|
-
}))
|
|
22
|
-
.toThrow('Parameter "try_instant_view" in method "web_app_open_link" is unsupported in the Mini Apps version 6.3.');
|
|
23
|
-
|
|
24
|
-
expect(() => postEvent('web_app_set_header_color', {
|
|
25
|
-
color: '#aaaaaa',
|
|
26
|
-
}))
|
|
27
|
-
.toThrow('Parameter "color" in method "web_app_set_header_color" is unsupported in the Mini Apps version 6.3.');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should call global postEvent function', () => {
|
|
31
|
-
const postEvent = createPostEvent('6.3');
|
|
32
|
-
const spy = vi.spyOn(postEventModule, 'postEvent');
|
|
33
|
-
|
|
34
|
-
postEvent('web_app_request_viewport');
|
|
35
|
-
expect(spy).toHaveBeenCalledOnce();
|
|
36
|
-
expect(spy).toHaveBeenCalledWith('web_app_request_viewport', undefined);
|
|
37
|
-
});
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
afterEach,
|
|
3
|
-
beforeAll,
|
|
4
|
-
expect,
|
|
5
|
-
it,
|
|
6
|
-
type SpyInstance,
|
|
7
|
-
vi,
|
|
8
|
-
} from 'vitest';
|
|
9
|
-
|
|
10
|
-
import { setTargetOrigin } from '../../../globals';
|
|
11
|
-
import { postEvent } from '../../index';
|
|
12
|
-
|
|
13
|
-
let windowSpy: SpyInstance<[], Window & typeof globalThis>;
|
|
14
|
-
|
|
15
|
-
beforeAll(() => {
|
|
16
|
-
windowSpy = vi.spyOn(window, 'window', 'get');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
windowSpy.mockReset();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should call "window.parent.postMessage" with object with properties {eventType: string, eventData: any} converted to string in case, current environment is iframe', () => {
|
|
24
|
-
const postMessageSpy = vi.fn();
|
|
25
|
-
windowSpy.mockImplementation(() => ({
|
|
26
|
-
self: 1000,
|
|
27
|
-
top: 900,
|
|
28
|
-
parent: { postMessage: postMessageSpy },
|
|
29
|
-
}) as any);
|
|
30
|
-
|
|
31
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(0);
|
|
32
|
-
postEvent('web_app_close');
|
|
33
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(1);
|
|
34
|
-
expect(postMessageSpy).toHaveBeenCalledWith(JSON.stringify({
|
|
35
|
-
eventType: 'web_app_close',
|
|
36
|
-
eventData: undefined,
|
|
37
|
-
}), 'https://web.telegram.org');
|
|
38
|
-
|
|
39
|
-
postMessageSpy.mockClear();
|
|
40
|
-
|
|
41
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(0);
|
|
42
|
-
postEvent('web_app_set_header_color', { color_key: 'bg_color' });
|
|
43
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(1);
|
|
44
|
-
expect(postMessageSpy).toHaveBeenCalledWith(JSON.stringify({
|
|
45
|
-
eventType: 'web_app_set_header_color',
|
|
46
|
-
eventData: { color_key: 'bg_color' },
|
|
47
|
-
}), 'https://web.telegram.org');
|
|
48
|
-
|
|
49
|
-
postMessageSpy.mockClear();
|
|
50
|
-
|
|
51
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(0);
|
|
52
|
-
postEvent('web_app_close', { targetOrigin: 'abc' });
|
|
53
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(1);
|
|
54
|
-
expect(postMessageSpy).toHaveBeenCalledWith(JSON.stringify({
|
|
55
|
-
eventType: 'web_app_close',
|
|
56
|
-
eventData: undefined,
|
|
57
|
-
}), 'abc');
|
|
58
|
-
|
|
59
|
-
postMessageSpy.mockClear();
|
|
60
|
-
|
|
61
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(0);
|
|
62
|
-
postEvent(
|
|
63
|
-
'web_app_set_header_color',
|
|
64
|
-
{ color_key: 'bg_color' },
|
|
65
|
-
{ targetOrigin: 'abc' },
|
|
66
|
-
);
|
|
67
|
-
expect(postMessageSpy).toHaveBeenCalledTimes(1);
|
|
68
|
-
expect(postMessageSpy).toHaveBeenCalledWith(JSON.stringify({
|
|
69
|
-
eventType: 'web_app_set_header_color',
|
|
70
|
-
eventData: { color_key: 'bg_color' },
|
|
71
|
-
}), 'abc');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should call "window.TelegramWebviewProxy.postEvent" in case this path exists. Function accepts event name (string) as the first argument and event data (object converted to string) as the second one.', () => {
|
|
75
|
-
const spy = vi.fn();
|
|
76
|
-
windowSpy.mockImplementation(() => ({
|
|
77
|
-
TelegramWebviewProxy: { postEvent: spy },
|
|
78
|
-
}) as any);
|
|
79
|
-
|
|
80
|
-
// Without parameters.
|
|
81
|
-
expect(spy).toHaveBeenCalledTimes(0);
|
|
82
|
-
postEvent('web_app_close');
|
|
83
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
84
|
-
expect(spy).toHaveBeenCalledWith('web_app_close', undefined);
|
|
85
|
-
|
|
86
|
-
spy.mockClear();
|
|
87
|
-
|
|
88
|
-
// With parameters.
|
|
89
|
-
expect(spy).toHaveBeenCalledTimes(0);
|
|
90
|
-
postEvent('web_app_set_header_color', { color_key: 'bg_color' });
|
|
91
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
92
|
-
expect(spy).toHaveBeenCalledWith('web_app_set_header_color', '{"color_key":"bg_color"}');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should call "window.external.notify" in case it exists. Passed value is object, converted to string. This object should contain fields "eventType" and "eventData".', () => {
|
|
96
|
-
const spy = vi.fn();
|
|
97
|
-
windowSpy.mockImplementation(() => ({
|
|
98
|
-
external: { notify: spy },
|
|
99
|
-
}) as any);
|
|
100
|
-
|
|
101
|
-
// Without parameters.
|
|
102
|
-
expect(spy).toHaveBeenCalledTimes(0);
|
|
103
|
-
postEvent('web_app_close');
|
|
104
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
105
|
-
expect(spy).toHaveBeenCalledWith('{"eventType":"web_app_close"}');
|
|
106
|
-
|
|
107
|
-
spy.mockClear();
|
|
108
|
-
|
|
109
|
-
// With parameters.
|
|
110
|
-
expect(spy).toHaveBeenCalledTimes(0);
|
|
111
|
-
postEvent('web_app_set_header_color', { color_key: 'bg_color' });
|
|
112
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
113
|
-
expect(spy).toHaveBeenCalledWith('{"eventType":"web_app_set_header_color","eventData":{"color_key":"bg_color"}}');
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should throw an error in case, current environment is unknown', () => {
|
|
117
|
-
windowSpy.mockImplementation(() => ({}) as any);
|
|
118
|
-
expect(() => postEvent('web_app_close'))
|
|
119
|
-
.toThrow('Unable to determine current environment and possible way to send event');
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should use globally set target origin', () => {
|
|
123
|
-
const postMessageSpy = vi.fn();
|
|
124
|
-
windowSpy.mockImplementation(() => ({
|
|
125
|
-
self: 1000,
|
|
126
|
-
top: 900,
|
|
127
|
-
parent: { postMessage: postMessageSpy },
|
|
128
|
-
}) as any);
|
|
129
|
-
|
|
130
|
-
setTargetOrigin('here we go!');
|
|
131
|
-
postEvent('iframe_ready');
|
|
132
|
-
|
|
133
|
-
expect(postMessageSpy).toHaveBeenCalledWith(
|
|
134
|
-
JSON.stringify({ eventType: 'iframe_ready' }),
|
|
135
|
-
'here we go!',
|
|
136
|
-
);
|
|
137
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { isRecord } from '~/misc/index.js';
|
|
2
|
-
import { supports } from '~/supports/index.js';
|
|
3
|
-
import type { Version } from '~/version/index.js';
|
|
4
|
-
|
|
5
|
-
import { type PostEvent, postEvent } from './postEvent.js';
|
|
6
|
-
import { MethodUnsupportedError, ParameterUnsupportedError } from '../errors/index.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates function which checks if specified method and parameters are supported. In case,
|
|
10
|
-
* method or parameters are unsupported, an error will be thrown.
|
|
11
|
-
* @param version - Telegram Mini Apps version.
|
|
12
|
-
* @throws {MethodUnsupportedError} Method is unsupported.
|
|
13
|
-
* @throws {ParameterUnsupportedError} Method parameter is unsupported.
|
|
14
|
-
*/
|
|
15
|
-
export function createPostEvent(version: Version): PostEvent {
|
|
16
|
-
return (method: any, params: any) => {
|
|
17
|
-
// Firstly, check if method itself is supported.
|
|
18
|
-
if (!supports(method, version)) {
|
|
19
|
-
throw new MethodUnsupportedError(method, version);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Method could use parameters, which are supported only in specific versions of Telegram
|
|
23
|
-
// Mini Apps.
|
|
24
|
-
if (isRecord(params)) {
|
|
25
|
-
let validateParam: string | undefined;
|
|
26
|
-
|
|
27
|
-
if (method === 'web_app_open_link' && 'try_instant_view' in params) {
|
|
28
|
-
validateParam = 'try_instant_view';
|
|
29
|
-
} else if (method === 'web_app_set_header_color' && 'color' in params) {
|
|
30
|
-
validateParam = 'color';
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (validateParam && !supports(method, validateParam, version)) {
|
|
34
|
-
throw new ParameterUnsupportedError(method, validateParam, version);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return postEvent(method, params);
|
|
39
|
-
};
|
|
40
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import type { RequestId } from '~/types/index.js';
|
|
2
|
-
|
|
3
|
-
interface CreateInvokeCustomMethodParams<M extends string, Params extends object> {
|
|
4
|
-
/**
|
|
5
|
-
* Unique request identifier.
|
|
6
|
-
*/
|
|
7
|
-
req_id: RequestId;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Method name.
|
|
11
|
-
*/
|
|
12
|
-
method: M;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Method specific parameters.
|
|
16
|
-
*/
|
|
17
|
-
params: Params;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface CustomMethodsParams {
|
|
21
|
-
/**
|
|
22
|
-
* Deletes storage values by their keys.
|
|
23
|
-
*/
|
|
24
|
-
deleteStorageValues: {
|
|
25
|
-
keys: string | string[]
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Gets current user contact in case, Mini has access to it.
|
|
30
|
-
*/
|
|
31
|
-
getRequestedContact: {};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Gets all registered storage keys.
|
|
35
|
-
*/
|
|
36
|
-
getStorageKeys: {};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Gets storage values by their keys.
|
|
40
|
-
*/
|
|
41
|
-
getStorageValues: {
|
|
42
|
-
keys: string | string[]
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Saves value by specified key in the storage.
|
|
47
|
-
*/
|
|
48
|
-
saveStorageValue: {
|
|
49
|
-
key: string;
|
|
50
|
-
value: string;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Known custom method name.
|
|
56
|
-
*/
|
|
57
|
-
export type CustomMethodName = keyof CustomMethodsParams;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Custom method parameters.
|
|
61
|
-
*/
|
|
62
|
-
export type CustomMethodParams<M extends CustomMethodName> = CustomMethodsParams[M];
|
|
63
|
-
|
|
64
|
-
export type AnyInvokeCustomMethodParams =
|
|
65
|
-
| CreateInvokeCustomMethodParams<string, any>
|
|
66
|
-
| {
|
|
67
|
-
[M in CustomMethodName]: CreateInvokeCustomMethodParams<M, CustomMethodParams<M>>
|
|
68
|
-
}[CustomMethodName];
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic type which creates new types of haptic feedback.
|
|
3
|
-
*/
|
|
4
|
-
type CreateHapticFeedbackParams<T extends string, P> = { type: T } & P;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Style of impact occurred haptic event.
|
|
8
|
-
* - `light`, indicates a collision between small or lightweight UI objects,
|
|
9
|
-
* - `medium`, indicates a collision between medium-sized or medium-weight UI objects,
|
|
10
|
-
* - `heavy`, indicates a collision between large or heavyweight UI objects,
|
|
11
|
-
* - `rigid`, indicates a collision between hard or inflexible UI objects,
|
|
12
|
-
* - `soft`, indicates a collision between soft or flexible UI objects.
|
|
13
|
-
*/
|
|
14
|
-
export type ImpactHapticFeedbackStyle =
|
|
15
|
-
| 'light'
|
|
16
|
-
| 'medium'
|
|
17
|
-
| 'heavy'
|
|
18
|
-
| 'rigid'
|
|
19
|
-
| 'soft';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Type of notification occurred type event.
|
|
23
|
-
* - `error`, indicates that a task or action has failed,
|
|
24
|
-
* - `success`, indicates that a task or action has completed successfully,
|
|
25
|
-
* - `warning`, indicates that a task or action produced a warning.
|
|
26
|
-
*/
|
|
27
|
-
export type NotificationHapticFeedbackType = 'error' | 'success' | 'warning';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* `impactOccurred` haptic feedback.
|
|
31
|
-
*/
|
|
32
|
-
export type ImpactHapticFeedbackParams = CreateHapticFeedbackParams<'impact', {
|
|
33
|
-
impact_style: ImpactHapticFeedbackStyle;
|
|
34
|
-
}>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* `notificationOccurred` haptic feedback.
|
|
38
|
-
*/
|
|
39
|
-
export type NotificationHapticFeedbackParams = CreateHapticFeedbackParams<'notification', {
|
|
40
|
-
notification_type: NotificationHapticFeedbackType;
|
|
41
|
-
}>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* `selectionChanged` haptic feedback.
|
|
45
|
-
*/
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
47
|
-
export type SelectionHapticFeedbackParams = CreateHapticFeedbackParams<'selection_change', {}>;
|
|
48
|
-
|
|
49
|
-
export type AnyHapticFeedbackParams =
|
|
50
|
-
| ImpactHapticFeedbackParams
|
|
51
|
-
| NotificationHapticFeedbackParams
|
|
52
|
-
| SelectionHapticFeedbackParams;
|