@tma.js/sdk 0.12.8 → 0.13.0
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/dts/components/InitData/InitData.d.ts +1 -1
- package/dist/dts/components/ThemeParams/ThemeParams.d.ts +2 -2
- package/dist/dts/env.d.ts +6 -4
- package/dist/dts/index.d.ts +0 -1
- 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 +253 -257
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
- package/src/components/InitData/InitData.ts +1 -1
- package/src/components/ThemeParams/ThemeParams.ts +16 -4
- package/src/env.ts +12 -7
- package/src/index.ts +0 -1
- package/src/init/init.ts +8 -53
- package/dist/dts/launch-params.d.ts +0 -6
- package/src/launch-params.ts +0 -28
|
@@ -11,23 +11,35 @@ import type { ThemeParamsEvents, ThemeParamsState } from './types.js';
|
|
|
11
11
|
|
|
12
12
|
function prepareThemeParams(value: ThemeParamsType): ThemeParamsState {
|
|
13
13
|
const {
|
|
14
|
+
accentTextColor = null,
|
|
14
15
|
backgroundColor = null,
|
|
15
|
-
buttonTextColor = null,
|
|
16
16
|
buttonColor = null,
|
|
17
|
+
buttonTextColor = null,
|
|
18
|
+
destructiveTextColor = null,
|
|
19
|
+
headerBackgroundColor = null,
|
|
17
20
|
hintColor = null,
|
|
18
21
|
linkColor = null,
|
|
19
|
-
textColor = null,
|
|
20
22
|
secondaryBackgroundColor = null,
|
|
23
|
+
sectionBackgroundColor = null,
|
|
24
|
+
sectionHeaderTextColor = null,
|
|
25
|
+
subtitleTextColor = null,
|
|
26
|
+
textColor = null,
|
|
21
27
|
} = value;
|
|
22
28
|
|
|
23
29
|
return {
|
|
30
|
+
accentTextColor,
|
|
24
31
|
backgroundColor,
|
|
25
|
-
buttonTextColor,
|
|
26
32
|
buttonColor,
|
|
33
|
+
buttonTextColor,
|
|
34
|
+
destructiveTextColor,
|
|
35
|
+
headerBackgroundColor,
|
|
27
36
|
hintColor,
|
|
28
37
|
linkColor,
|
|
29
|
-
textColor,
|
|
30
38
|
secondaryBackgroundColor,
|
|
39
|
+
sectionBackgroundColor,
|
|
40
|
+
sectionHeaderTextColor,
|
|
41
|
+
subtitleTextColor,
|
|
42
|
+
textColor,
|
|
31
43
|
};
|
|
32
44
|
}
|
|
33
45
|
|
package/src/env.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { retrieveLaunchData } from '@tma.js/launch-params';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns true in case, current environment is Telegram Mini Apps.
|
|
5
|
-
*
|
|
6
|
-
* `isTWA` utilizes such function as `retrieveLaunchParams`, which attempts to retrieve
|
|
7
|
-
* launch parameters from the current environment.
|
|
8
|
-
* @see retrieveLaunchParams
|
|
9
5
|
*/
|
|
10
|
-
export function
|
|
6
|
+
export function isTMA(): boolean {
|
|
11
7
|
try {
|
|
12
|
-
|
|
8
|
+
retrieveLaunchData();
|
|
13
9
|
return true;
|
|
14
10
|
} catch (e) {
|
|
15
11
|
return false;
|
|
16
12
|
}
|
|
17
13
|
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns true in case, current environment is Telegram Mini Apps.
|
|
17
|
+
* @see computeLaunchData
|
|
18
|
+
* @deprecated Use `isTMA`
|
|
19
|
+
*/
|
|
20
|
+
export function isTWA(): boolean {
|
|
21
|
+
return isTMA();
|
|
22
|
+
}
|
package/src/index.ts
CHANGED
package/src/init/init.ts
CHANGED
|
@@ -5,12 +5,7 @@ import {
|
|
|
5
5
|
on,
|
|
6
6
|
} from '@tma.js/bridge';
|
|
7
7
|
import { withTimeout } from '@tma.js/utils';
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
parse as parseLaunchParams,
|
|
11
|
-
saveToStorage as saveLaunchParamsToStorage,
|
|
12
|
-
retrieveFromStorage,
|
|
13
|
-
} from '@tma.js/launch-params';
|
|
8
|
+
import { parse, retrieveLaunchData } from '@tma.js/launch-params';
|
|
14
9
|
|
|
15
10
|
import {
|
|
16
11
|
CloudStorage,
|
|
@@ -33,42 +28,9 @@ import {
|
|
|
33
28
|
createViewport,
|
|
34
29
|
createWebApp, createRequestIdGenerator, createClosingBehavior,
|
|
35
30
|
} from './creators/index.js';
|
|
36
|
-
import { retrieveLaunchParams } from '../launch-params.js';
|
|
37
31
|
|
|
38
32
|
import type { InitOptions, InitResult } from './types.js';
|
|
39
33
|
|
|
40
|
-
/**
|
|
41
|
-
* Returns true in case, current session was created due to native location reload.
|
|
42
|
-
*/
|
|
43
|
-
function isNativePageReload(): boolean {
|
|
44
|
-
return (
|
|
45
|
-
window
|
|
46
|
-
.performance
|
|
47
|
-
.getEntriesByType('navigation') as PerformanceNavigationTiming[]
|
|
48
|
-
).some((entry) => entry.type === 'reload');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Returns true if current page was reloaded.
|
|
53
|
-
* @param launchParamsFromStorage - launch parameters from sessionStorage.
|
|
54
|
-
* @param currentLaunchParams - actual launch parameters.
|
|
55
|
-
*/
|
|
56
|
-
function computePageReload(
|
|
57
|
-
launchParamsFromStorage: LaunchParams | null,
|
|
58
|
-
currentLaunchParams: LaunchParams,
|
|
59
|
-
): boolean {
|
|
60
|
-
// To check if page was reloaded, we should check if previous init data hash equals to the
|
|
61
|
-
// current one. Nevertheless, there are some cases, when init data is missing. For example,
|
|
62
|
-
// when app was launched via KeyboardButton. In this case we try to use the native way of
|
|
63
|
-
// checking if current page was reloaded (which could still return incorrect result).
|
|
64
|
-
// Issue: https://github.com/Telegram-Mini-Apps/issues/issues/12
|
|
65
|
-
if (!launchParamsFromStorage) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return launchParamsFromStorage.initData?.hash === currentLaunchParams.initData?.hash;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
34
|
/**
|
|
73
35
|
* Represents actual init function.
|
|
74
36
|
* @param options - init options.
|
|
@@ -80,8 +42,8 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
80
42
|
acceptScrollbarStyle = true,
|
|
81
43
|
acceptCustomStyles = acceptScrollbarStyle,
|
|
82
44
|
targetOrigin,
|
|
45
|
+
launchParams: launchParamsOption,
|
|
83
46
|
debug = false,
|
|
84
|
-
launchParams: optionsLaunchParams,
|
|
85
47
|
} = options;
|
|
86
48
|
|
|
87
49
|
// Set global settings.
|
|
@@ -93,19 +55,12 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
93
55
|
setTargetOrigin(targetOrigin);
|
|
94
56
|
}
|
|
95
57
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
saveLaunchParamsToStorage(launchParams);
|
|
104
|
-
|
|
105
|
-
// Compute if page was reloaded. We will need it to decide if SDK components should be restored
|
|
106
|
-
// or created from scratch.
|
|
107
|
-
const isPageReload = isNativePageReload()
|
|
108
|
-
|| computePageReload(launchParamsFromStorage, launchParams);
|
|
58
|
+
// Retrieve launch data.
|
|
59
|
+
const { launchParams, isPageReload } = retrieveLaunchData({
|
|
60
|
+
currentLaunchParams: typeof launchParamsOption === 'string' || launchParamsOption instanceof URLSearchParams
|
|
61
|
+
? parse(launchParamsOption)
|
|
62
|
+
: launchParamsOption,
|
|
63
|
+
});
|
|
109
64
|
|
|
110
65
|
const {
|
|
111
66
|
initData,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type LaunchParams } from '@tma.js/launch-params';
|
|
2
|
-
/**
|
|
3
|
-
* Attempts to extract launch params from window.location.hash. In case, window.location.hash
|
|
4
|
-
* lacks of valid data, function attempts to extract launch params from the sessionStorage.
|
|
5
|
-
*/
|
|
6
|
-
export declare function retrieveLaunchParams(): LaunchParams;
|
package/src/launch-params.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { parse, retrieveFromStorage, type LaunchParams } from '@tma.js/launch-params';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Attempts to extract launch params from window.location.hash. In case, window.location.hash
|
|
5
|
-
* lacks of valid data, function attempts to extract launch params from the sessionStorage.
|
|
6
|
-
*/
|
|
7
|
-
export function retrieveLaunchParams(): LaunchParams {
|
|
8
|
-
let error: unknown | undefined;
|
|
9
|
-
|
|
10
|
-
// Try to extract Mini App data from hash. This block of code covers usual flow, when
|
|
11
|
-
// application was firstly opened by the user and its hash always contains required parameters.
|
|
12
|
-
try {
|
|
13
|
-
return parse(window.location.hash.slice(1));
|
|
14
|
-
} catch (e) {
|
|
15
|
-
error = e;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Mini Apps allows reloading current page. In this case, window.location.reload() will be
|
|
19
|
-
// called which means, that init will be called again. As the result, current window
|
|
20
|
-
// location will lose Mini App data. To solve this problem, we are extracting launch
|
|
21
|
-
// params saved previously.
|
|
22
|
-
const fromStorage = retrieveFromStorage();
|
|
23
|
-
if (fromStorage) {
|
|
24
|
-
return fromStorage;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
throw new Error('Unable to extract launch params', { cause: error });
|
|
28
|
-
}
|