@tma.js/sdk 0.12.9 → 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/src/env.ts CHANGED
@@ -1,17 +1,22 @@
1
- import { retrieveLaunchParams } from './launch-params.js';
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 isTWA(): boolean {
6
+ export function isTMA(): boolean {
11
7
  try {
12
- retrieveLaunchParams();
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
@@ -2,6 +2,5 @@ export * from './components/index.js';
2
2
  export * from './errors/index.js';
3
3
  export * from './init/index.js';
4
4
  export * from './env.js';
5
- export * from './launch-params.js';
6
5
  export * from './types.js';
7
6
  export * from './url.js';
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 type { LaunchParams } from '@tma.js/launch-params';
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
- // Get Mini App launch params and save them to session storage, so they will be accessible from
97
- // anywhere.
98
- const launchParamsFromStorage = retrieveFromStorage();
99
- const launchParams = optionsLaunchParams instanceof URLSearchParams || typeof optionsLaunchParams === 'string'
100
- ? parseLaunchParams(optionsLaunchParams)
101
- : retrieveLaunchParams();
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;
@@ -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
- }