@tma.js/sdk 0.12.9 → 0.13.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/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';
@@ -1,7 +1,6 @@
1
1
  export * from './createBackButton.js';
2
2
  export * from './createClosingBehavior.js';
3
3
  export * from './createMainButton.js';
4
- export * from './createPostEvent.js';
5
4
  export * from './createRequestIdGenerator.js';
6
5
  export * from './createThemeParams.js';
7
6
  export * from './createViewport.js';
package/src/init/init.ts CHANGED
@@ -3,14 +3,11 @@ import {
3
3
  setDebug,
4
4
  setTargetOrigin,
5
5
  on,
6
+ createPostEvent,
7
+ postEvent as bridgePostEvent,
6
8
  } from '@tma.js/bridge';
7
9
  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';
10
+ import { parse, retrieveLaunchData } from '@tma.js/launch-params';
14
11
 
15
12
  import {
16
13
  CloudStorage,
@@ -26,49 +23,17 @@ import {
26
23
  parseCSSVarsOptions,
27
24
  } from './css.js';
28
25
  import {
29
- createPostEvent,
30
26
  createThemeParams,
31
27
  createBackButton,
32
28
  createMainButton,
33
29
  createViewport,
34
- createWebApp, createRequestIdGenerator, createClosingBehavior,
30
+ createWebApp,
31
+ createRequestIdGenerator,
32
+ createClosingBehavior,
35
33
  } from './creators/index.js';
36
- import { retrieveLaunchParams } from '../launch-params.js';
37
34
 
38
35
  import type { InitOptions, InitResult } from './types.js';
39
36
 
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
37
  /**
73
38
  * Represents actual init function.
74
39
  * @param options - init options.
@@ -80,8 +45,8 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
80
45
  acceptScrollbarStyle = true,
81
46
  acceptCustomStyles = acceptScrollbarStyle,
82
47
  targetOrigin,
48
+ launchParams: launchParamsOption,
83
49
  debug = false,
84
- launchParams: optionsLaunchParams,
85
50
  } = options;
86
51
 
87
52
  // Set global settings.
@@ -93,19 +58,12 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
93
58
  setTargetOrigin(targetOrigin);
94
59
  }
95
60
 
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);
61
+ // Retrieve launch data.
62
+ const { launchParams, isPageReload } = retrieveLaunchData({
63
+ currentLaunchParams: typeof launchParamsOption === 'string' || launchParamsOption instanceof URLSearchParams
64
+ ? parse(launchParamsOption)
65
+ : launchParamsOption,
66
+ });
109
67
 
110
68
  const {
111
69
  initData,
@@ -121,7 +79,9 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
121
79
  } = lpThemeParams;
122
80
 
123
81
  const createRequestId = createRequestIdGenerator();
124
- const postEvent = createPostEvent(checkCompat, version);
82
+ const postEvent = checkCompat
83
+ ? createPostEvent(version)
84
+ : bridgePostEvent;
125
85
  const themeParams = createThemeParams(lpThemeParams);
126
86
  const webApp = createWebApp(
127
87
  isPageReload,
package/src/supports.ts CHANGED
@@ -2,15 +2,15 @@ import type { Version } from '@tma.js/utils';
2
2
  import {
3
3
  supports,
4
4
  type MethodName,
5
- type HasCheckSupportMethodName,
6
- type HasCheckSupportMethodParam,
5
+ type MethodVersionedParams,
6
+ type MethodWithVersionedParams,
7
7
  } from '@tma.js/bridge';
8
8
 
9
9
  export type SupportsFunc<M extends string> = (method: M) => boolean;
10
10
 
11
11
  type HasCheckSupportMethodTuple = {
12
- [M in HasCheckSupportMethodName]: [M, HasCheckSupportMethodParam<M>]
13
- }[HasCheckSupportMethodName];
12
+ [M in MethodWithVersionedParams]: [M, MethodVersionedParams<M>]
13
+ }[MethodWithVersionedParams];
14
14
 
15
15
  /**
16
16
  * Returns function, which accepts predefined method name and checks if it is supported
@@ -1,7 +0,0 @@
1
- import { type PostEvent } from '@tma.js/bridge';
2
- /**
3
- * Creates postEvent function.
4
- * @param checkCompat - should compatibility check be enabled.
5
- * @param version - platform version.
6
- */
7
- export declare function createPostEvent(checkCompat: boolean, version: string): PostEvent;
@@ -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,36 +0,0 @@
1
- import {
2
- supports,
3
- postEvent as defaultPostEvent,
4
- detectSupportParams,
5
- type PostEvent,
6
- } from '@tma.js/bridge';
7
- import { isRecord } from '@tma.js/utils';
8
-
9
- import { MethodNotSupportedError, ParameterUnsupportedError } from '../../errors/index.js';
10
-
11
- /**
12
- * Creates postEvent function.
13
- * @param checkCompat - should compatibility check be enabled.
14
- * @param version - platform version.
15
- */
16
- export function createPostEvent(checkCompat: boolean, version: string): PostEvent {
17
- return checkCompat
18
- ? (method: any, params: any) => {
19
- // Firstly, check if method itself is supported.
20
- if (!supports(method, version)) {
21
- throw new MethodNotSupportedError(method, version);
22
- }
23
-
24
- // Method could use parameters, which are supported only in specific versions of TWA.
25
- if (isRecord(params)) {
26
- detectSupportParams(method, params).forEach((param) => {
27
- if (!supports(method as any, param, version)) {
28
- throw new ParameterUnsupportedError(method, param, version);
29
- }
30
- });
31
- }
32
-
33
- return defaultPostEvent(method, params);
34
- }
35
- : defaultPostEvent;
36
- }
@@ -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
- }