@tma.js/sdk 0.11.6 → 0.12.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/CloudStorage/CloudStorage.d.ts +2 -2
- package/dist/dts/components/ThemeParams/ThemeParams.d.ts +4 -3
- package/dist/dts/components/ThemeParams/types.d.ts +2 -2
- package/dist/dts/components/WebApp/WebApp.d.ts +5 -4
- package/dist/dts/index.d.ts +2 -3
- package/dist/dts/init/creators/createBackButton.d.ts +2 -1
- package/dist/dts/init/creators/createClosingBehavior.d.ts +2 -1
- package/dist/dts/init/creators/createMainButton.d.ts +2 -1
- package/dist/dts/init/creators/createThemeParams.d.ts +7 -0
- package/dist/dts/init/creators/createViewport.d.ts +3 -2
- package/dist/dts/init/creators/createWebApp.d.ts +4 -2
- package/dist/dts/init/creators/index.d.ts +1 -1
- package/dist/dts/init/types.d.ts +5 -5
- package/dist/dts/launch-params.d.ts +3 -17
- package/dist/dts/state/State.d.ts +1 -1
- package/dist/dts/storage.d.ts +0 -1
- package/dist/dts/types.d.ts +0 -4
- package/dist/index.js +290 -304
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +1 -1
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +7 -4
- package/src/components/MainButton/MainButton.ts +1 -1
- package/src/components/ThemeParams/ThemeParams.ts +3 -3
- package/src/components/ThemeParams/types.ts +5 -5
- package/src/components/WebApp/WebApp.ts +2 -1
- package/src/index.ts +2 -3
- package/src/init/creators/createBackButton.ts +7 -2
- package/src/init/creators/createClosingBehavior.ts +6 -2
- package/src/init/creators/createMainButton.ts +3 -1
- package/src/init/creators/{createSyncedThemeParams.ts → createThemeParams.ts} +3 -3
- package/src/init/creators/createViewport.ts +8 -4
- package/src/init/creators/createWebApp.ts +5 -2
- package/src/init/creators/index.ts +1 -1
- package/src/init/init.ts +67 -20
- package/src/init/types.ts +5 -5
- package/src/launch-params.ts +14 -55
- package/src/storage.ts +1 -2
- package/src/types.ts +0 -15
- package/dist/dts/init/creators/createSyncedThemeParams.d.ts +0 -7
- package/dist/dts/theme-params.d.ts +0 -18
- package/src/theme-params.ts +0 -34
|
@@ -10,12 +10,13 @@ import {
|
|
|
10
10
|
type InvoiceStatus,
|
|
11
11
|
type PostEvent,
|
|
12
12
|
} from '@tma.js/bridge';
|
|
13
|
+
import type { Platform } from '@tma.js/launch-params';
|
|
13
14
|
|
|
14
15
|
import { formatURL } from '../../url.js';
|
|
15
16
|
import { State } from '../../state/index.js';
|
|
16
17
|
import { createSupportsFunc, createSupportsParamFunc, type SupportsFunc } from '../../supports.js';
|
|
17
18
|
|
|
18
|
-
import type { ColorScheme
|
|
19
|
+
import type { ColorScheme } from '../../types.js';
|
|
19
20
|
import type { WebAppEvents, WebAppHeaderColor, WebAppState } from './types.js';
|
|
20
21
|
|
|
21
22
|
/**
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export * from './components/index.js';
|
|
2
|
-
export
|
|
3
|
-
export
|
|
2
|
+
export * from './errors/index.js';
|
|
3
|
+
export * from './init/index.js';
|
|
4
4
|
export * from './env.js';
|
|
5
5
|
export * from './launch-params.js';
|
|
6
|
-
export * from './theme-params.js';
|
|
7
6
|
export * from './types.js';
|
|
8
7
|
export * from './url.js';
|
|
@@ -7,11 +7,16 @@ import { getStorageValue, saveStorageValue } from '../../storage.js';
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates BackButton instance using last locally saved data also saving each state in
|
|
9
9
|
* the storage.
|
|
10
|
+
* @param isPageReload - was current page reloaded.
|
|
10
11
|
* @param version - platform version.
|
|
11
12
|
* @param postEvent - Bridge postEvent function
|
|
12
13
|
*/
|
|
13
|
-
export function createBackButton(
|
|
14
|
-
|
|
14
|
+
export function createBackButton(
|
|
15
|
+
isPageReload: boolean,
|
|
16
|
+
version: string,
|
|
17
|
+
postEvent: PostEvent,
|
|
18
|
+
): BackButton {
|
|
19
|
+
const { isVisible = false } = isPageReload ? getStorageValue('back-button') || {} : {};
|
|
15
20
|
const component = new BackButton(isVisible, version, postEvent);
|
|
16
21
|
|
|
17
22
|
component.on('isVisibleChanged', () => {
|
|
@@ -7,10 +7,14 @@ import { getStorageValue, saveStorageValue } from '../../storage.js';
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates ClosingBehaviour instance using last locally saved data also saving each state in
|
|
9
9
|
* the storage.
|
|
10
|
+
* @param isPageReload - was current page reloaded.
|
|
10
11
|
* @param postEvent - Bridge postEvent function
|
|
11
12
|
*/
|
|
12
|
-
export function createClosingBehavior(
|
|
13
|
-
|
|
13
|
+
export function createClosingBehavior(
|
|
14
|
+
isPageReload: boolean,
|
|
15
|
+
postEvent: PostEvent,
|
|
16
|
+
): ClosingBehaviour {
|
|
17
|
+
const { isConfirmationNeeded = false } = isPageReload ? getStorageValue('closing-behavior') || {} : {};
|
|
14
18
|
|
|
15
19
|
const component = new ClosingBehaviour(isConfirmationNeeded, postEvent);
|
|
16
20
|
|
|
@@ -8,11 +8,13 @@ import { getStorageValue, saveStorageValue } from '../../storage.js';
|
|
|
8
8
|
/**
|
|
9
9
|
* Creates MainButton instance using last locally saved data also saving each state in
|
|
10
10
|
* the storage.
|
|
11
|
+
* @param isPageReload - was current page reloaded.
|
|
11
12
|
* @param backgroundColor - background color.
|
|
12
13
|
* @param textColor - text color.
|
|
13
14
|
* @param postEvent - Bridge postEvent function
|
|
14
15
|
*/
|
|
15
16
|
export function createMainButton(
|
|
17
|
+
isPageReload: boolean,
|
|
16
18
|
backgroundColor: RGB,
|
|
17
19
|
textColor: RGB,
|
|
18
20
|
postEvent: PostEvent,
|
|
@@ -24,7 +26,7 @@ export function createMainButton(
|
|
|
24
26
|
isProgressVisible = false,
|
|
25
27
|
textColor: stateTextColor = textColor,
|
|
26
28
|
text = '',
|
|
27
|
-
} = getStorageValue('main-button') || {};
|
|
29
|
+
} = isPageReload ? getStorageValue('main-button') || {} : {};
|
|
28
30
|
|
|
29
31
|
const component = new MainButton(
|
|
30
32
|
stateBackgroundColor,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { ThemeParams } from '
|
|
1
|
+
import type { ThemeParams as ThemeParamsType } from '@tma.js/theme-params';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { ThemeParams } from '../../components/index.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates synced instance of ThemeParams.
|
|
7
7
|
* @param params - theme parameters.
|
|
8
8
|
*/
|
|
9
|
-
export function
|
|
9
|
+
export function createThemeParams(params: ThemeParamsType): ThemeParams {
|
|
10
10
|
const themeParams = new ThemeParams(params);
|
|
11
11
|
ThemeParams.sync(themeParams);
|
|
12
12
|
|
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
import type { PostEvent } from '@tma.js/bridge';
|
|
2
|
+
import type { Platform } from '@tma.js/launch-params';
|
|
2
3
|
|
|
3
4
|
import { Viewport } from '../../components/index.js';
|
|
4
5
|
|
|
5
6
|
import { getStorageValue, saveStorageValue } from '../../storage.js';
|
|
6
7
|
|
|
7
|
-
import type { Platform } from '../../types.js';
|
|
8
|
-
|
|
9
8
|
/**
|
|
10
9
|
* Creates Viewport instance using last locally saved data also saving each state in
|
|
11
10
|
* the storage.
|
|
11
|
+
* @param isPageReload - was current page reloaded.
|
|
12
12
|
* @param platform - Telegram Web Apps platform name.
|
|
13
13
|
* @param postEvent - Bridge postEvent function
|
|
14
14
|
*/
|
|
15
|
-
export async function createViewport(
|
|
15
|
+
export async function createViewport(
|
|
16
|
+
isPageReload: boolean,
|
|
17
|
+
platform: Platform,
|
|
18
|
+
postEvent: PostEvent,
|
|
19
|
+
): Promise<Viewport> {
|
|
16
20
|
const {
|
|
17
21
|
height = window.innerHeight,
|
|
18
22
|
stableHeight = window.innerHeight,
|
|
19
23
|
width = window.innerWidth,
|
|
20
24
|
isExpanded = false,
|
|
21
|
-
} = getStorageValue('viewport') || {};
|
|
25
|
+
} = isPageReload ? getStorageValue('viewport') || {} : {};
|
|
22
26
|
|
|
23
27
|
const createSynced = () => {
|
|
24
28
|
const viewport = new Viewport(height, width, stableHeight, isExpanded, postEvent);
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import type { RGB } from '@tma.js/colors';
|
|
2
2
|
import type { PostEvent } from '@tma.js/bridge';
|
|
3
|
+
import type { Platform } from '@tma.js/launch-params';
|
|
3
4
|
|
|
4
5
|
import { WebApp } from '../../components/index.js';
|
|
5
6
|
|
|
6
7
|
import { getStorageValue, saveStorageValue } from '../../storage.js';
|
|
7
8
|
|
|
8
|
-
import type { CreateRequestIdFunc
|
|
9
|
+
import type { CreateRequestIdFunc } from '../../types.js';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Creates WebApp instance using last locally saved data also saving each state in
|
|
12
13
|
* the storage.
|
|
14
|
+
* @param isPageReload - was current page reloaded.
|
|
13
15
|
* @param backgroundColor - web app background color.
|
|
14
16
|
* @param version - platform version.
|
|
15
17
|
* @param platform - Telegram Web Apps platform name.
|
|
@@ -17,6 +19,7 @@ import type { CreateRequestIdFunc, Platform } from '../../types.js';
|
|
|
17
19
|
* @param postEvent - Bridge postEvent function
|
|
18
20
|
*/
|
|
19
21
|
export function createWebApp(
|
|
22
|
+
isPageReload: boolean,
|
|
20
23
|
backgroundColor: RGB,
|
|
21
24
|
version: string,
|
|
22
25
|
platform: Platform,
|
|
@@ -26,7 +29,7 @@ export function createWebApp(
|
|
|
26
29
|
const {
|
|
27
30
|
backgroundColor: stateBackgroundColor = backgroundColor,
|
|
28
31
|
headerColor = 'bg_color',
|
|
29
|
-
} = getStorageValue('web-app') || {};
|
|
32
|
+
} = isPageReload ? getStorageValue('web-app') || {} : {};
|
|
30
33
|
|
|
31
34
|
const component = new WebApp(
|
|
32
35
|
headerColor,
|
|
@@ -3,6 +3,6 @@ export * from './createClosingBehavior.js';
|
|
|
3
3
|
export * from './createMainButton.js';
|
|
4
4
|
export * from './createPostEvent.js';
|
|
5
5
|
export * from './createRequestIdGenerator.js';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './createThemeParams.js';
|
|
7
7
|
export * from './createViewport.js';
|
|
8
8
|
export * from './createWebApp.js';
|
package/src/init/init.ts
CHANGED
|
@@ -5,6 +5,12 @@ 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
14
|
|
|
9
15
|
import {
|
|
10
16
|
CloudStorage,
|
|
@@ -13,7 +19,6 @@ import {
|
|
|
13
19
|
Popup,
|
|
14
20
|
QRScanner,
|
|
15
21
|
} from '../components/index.js';
|
|
16
|
-
import { parseLaunchParams, retrieveLaunchParams, type LaunchParams } from '../launch-params.js';
|
|
17
22
|
import {
|
|
18
23
|
bindThemeCSSVariables,
|
|
19
24
|
bindViewportCSSVariables,
|
|
@@ -22,15 +27,48 @@ import {
|
|
|
22
27
|
} from './css.js';
|
|
23
28
|
import {
|
|
24
29
|
createPostEvent,
|
|
25
|
-
|
|
30
|
+
createThemeParams,
|
|
26
31
|
createBackButton,
|
|
27
32
|
createMainButton,
|
|
28
33
|
createViewport,
|
|
29
34
|
createWebApp, createRequestIdGenerator, createClosingBehavior,
|
|
30
35
|
} from './creators/index.js';
|
|
36
|
+
import { retrieveLaunchParams } from '../launch-params.js';
|
|
31
37
|
|
|
32
38
|
import type { InitOptions, InitResult } from './types.js';
|
|
33
39
|
|
|
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
|
+
|
|
34
72
|
/**
|
|
35
73
|
* Represents actual init function.
|
|
36
74
|
* @param options - init options.
|
|
@@ -43,29 +81,31 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
43
81
|
acceptCustomStyles = acceptScrollbarStyle,
|
|
44
82
|
targetOrigin,
|
|
45
83
|
debug = false,
|
|
46
|
-
launchParams:
|
|
84
|
+
launchParams: optionsLaunchParams = retrieveLaunchParams(),
|
|
47
85
|
} = options;
|
|
48
86
|
|
|
49
|
-
// Set global
|
|
87
|
+
// Set global settings.
|
|
50
88
|
if (debug) {
|
|
51
89
|
setDebug(debug);
|
|
52
90
|
}
|
|
53
91
|
|
|
54
|
-
// Set global target origin.
|
|
55
92
|
if (typeof targetOrigin === 'string') {
|
|
56
93
|
setTargetOrigin(targetOrigin);
|
|
57
94
|
}
|
|
58
95
|
|
|
59
|
-
// Get Web App launch params
|
|
60
|
-
|
|
96
|
+
// Get Web 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
|
+
: optionsLaunchParams;
|
|
61
102
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
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);
|
|
69
109
|
|
|
70
110
|
const {
|
|
71
111
|
initData,
|
|
@@ -82,8 +122,15 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
82
122
|
|
|
83
123
|
const createRequestId = createRequestIdGenerator();
|
|
84
124
|
const postEvent = createPostEvent(checkCompat, version);
|
|
85
|
-
const themeParams =
|
|
86
|
-
const webApp = createWebApp(
|
|
125
|
+
const themeParams = createThemeParams(lpThemeParams);
|
|
126
|
+
const webApp = createWebApp(
|
|
127
|
+
isPageReload,
|
|
128
|
+
backgroundColor,
|
|
129
|
+
version,
|
|
130
|
+
platform,
|
|
131
|
+
createRequestId,
|
|
132
|
+
postEvent,
|
|
133
|
+
);
|
|
87
134
|
|
|
88
135
|
const {
|
|
89
136
|
themeParams: createThemeParamsCSSVars,
|
|
@@ -99,7 +146,7 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
99
146
|
bindThemeCSSVariables(themeParams);
|
|
100
147
|
}
|
|
101
148
|
|
|
102
|
-
const viewport = await createViewport(platform, postEvent);
|
|
149
|
+
const viewport = await createViewport(isPageReload, platform, postEvent);
|
|
103
150
|
|
|
104
151
|
// Apply viewport CSS variables.
|
|
105
152
|
if (createViewportCSSVars) {
|
|
@@ -128,11 +175,11 @@ async function actualInit(options: InitOptions = {}): Promise<InitResult> {
|
|
|
128
175
|
}
|
|
129
176
|
|
|
130
177
|
const result: InitResult = {
|
|
131
|
-
backButton: createBackButton(version, postEvent),
|
|
132
|
-
closingBehavior: createClosingBehavior(postEvent),
|
|
178
|
+
backButton: createBackButton(isPageReload, version, postEvent),
|
|
179
|
+
closingBehavior: createClosingBehavior(isPageReload, postEvent),
|
|
133
180
|
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
|
|
134
181
|
haptic: new HapticFeedback(version, postEvent),
|
|
135
|
-
mainButton: createMainButton(buttonColor, buttonTextColor, postEvent),
|
|
182
|
+
mainButton: createMainButton(isPageReload, buttonColor, buttonTextColor, postEvent),
|
|
136
183
|
popup: new Popup(version, postEvent),
|
|
137
184
|
postEvent,
|
|
138
185
|
qrScanner: new QRScanner(version, postEvent),
|
package/src/init/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PostEvent } from '@tma.js/bridge';
|
|
2
|
+
import type { LaunchParams } from '@tma.js/launch-params';
|
|
2
3
|
|
|
3
4
|
import type {
|
|
4
5
|
BackButton,
|
|
@@ -13,9 +14,8 @@ import type {
|
|
|
13
14
|
Viewport,
|
|
14
15
|
WebApp,
|
|
15
16
|
} from '../components/index.js';
|
|
16
|
-
import type { LaunchParams } from '../launch-params.js';
|
|
17
17
|
|
|
18
|
-
export
|
|
18
|
+
export interface InitResult {
|
|
19
19
|
backButton: BackButton;
|
|
20
20
|
closingBehavior: ClosingBehaviour;
|
|
21
21
|
cloudStorage: CloudStorage;
|
|
@@ -29,9 +29,9 @@ export type InitResult = {
|
|
|
29
29
|
themeParams: ThemeParams;
|
|
30
30
|
viewport: Viewport;
|
|
31
31
|
webApp: WebApp;
|
|
32
|
-
}
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
export
|
|
34
|
+
export interface InitCSSVarsSpecificOption {
|
|
35
35
|
/**
|
|
36
36
|
* Enables theme parameters CSS variables:
|
|
37
37
|
* - `--tg-theme-bg-color`
|
|
@@ -63,7 +63,7 @@ export type InitCSSVarsSpecificOption = {
|
|
|
63
63
|
* @see bindWebAppVariables
|
|
64
64
|
*/
|
|
65
65
|
webApp?: true;
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
67
|
|
|
68
68
|
export type InitCSSVarsOption = boolean | InitCSSVarsSpecificOption;
|
|
69
69
|
|
package/src/launch-params.ts
CHANGED
|
@@ -1,70 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { parseThemeParams, type ThemeParamsType } from './theme-params.js';
|
|
5
|
-
import { saveStorageValue, getStorageValue } from './storage.js';
|
|
6
|
-
|
|
7
|
-
import type { Platform } from './types.js';
|
|
8
|
-
|
|
9
|
-
export interface LaunchParams {
|
|
10
|
-
version: string;
|
|
11
|
-
initData?: InitData;
|
|
12
|
-
initDataRaw?: string;
|
|
13
|
-
platform: Platform;
|
|
14
|
-
themeParams: ThemeParamsType;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const launchParamsParser = searchParams<LaunchParams>({
|
|
18
|
-
version: { type: string(), from: 'tgWebAppVersion' },
|
|
19
|
-
initData: { type: initData.optional(), from: 'tgWebAppData' },
|
|
20
|
-
initDataRaw: { type: string().optional(), from: 'tgWebAppData' },
|
|
21
|
-
platform: { type: string(), from: 'tgWebAppPlatform' },
|
|
22
|
-
themeParams: { type: parseThemeParams, from: 'tgWebAppThemeParams' },
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Parses query parameters as launch parameters.
|
|
27
|
-
* @param query - query parameters presented as string or URLSearchParams
|
|
28
|
-
* instance.
|
|
29
|
-
*/
|
|
30
|
-
export function parseLaunchParams(query: string | URLSearchParams): LaunchParams {
|
|
31
|
-
return launchParamsParser.parse(query);
|
|
32
|
-
}
|
|
1
|
+
import { parse, retrieveFromStorage } from '@tma.js/launch-params';
|
|
2
|
+
import type { LaunchParams } from '@tma.js/launch-params';
|
|
33
3
|
|
|
34
4
|
/**
|
|
35
|
-
*
|
|
5
|
+
* Attempts to extract launch params from window.location.hash. In case, window.location.hash
|
|
6
|
+
* lacks of valid data, function attempts to extract launch params from the sessionStorage.
|
|
36
7
|
*/
|
|
37
8
|
export function retrieveLaunchParams(): LaunchParams {
|
|
38
|
-
|
|
9
|
+
let error: unknown | undefined;
|
|
39
10
|
|
|
40
|
-
// Try to extract
|
|
11
|
+
// Try to extract Mini App data from hash. This block of code covers usual flow, when
|
|
41
12
|
// application was firstly opened by the user and its hash always contains required parameters.
|
|
42
13
|
try {
|
|
43
|
-
|
|
44
|
-
const launchParamsParsed = parseLaunchParams(launchParamsRaw);
|
|
45
|
-
|
|
46
|
-
// Previous line of code worked fine. Then, we can save taken launch params raw value.
|
|
47
|
-
saveStorageValue('launch-params', launchParamsRaw);
|
|
48
|
-
|
|
49
|
-
return launchParamsParsed;
|
|
14
|
+
return parse(window.location.hash.slice(1));
|
|
50
15
|
} catch (e) {
|
|
51
|
-
|
|
16
|
+
error = e;
|
|
52
17
|
}
|
|
53
18
|
|
|
54
|
-
//
|
|
19
|
+
// Mini Apps allows reloading current page. In this case, window.location.reload() will be
|
|
55
20
|
// called which means, that init will be called again. As the result, current window
|
|
56
|
-
// location will lose
|
|
21
|
+
// location will lose Mini App data. To solve this problem, we are extracting launch
|
|
57
22
|
// params saved previously.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return parseLaunchParams(launchParamsRaw);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
errors.push('Launch params are missing in local storage');
|
|
65
|
-
} catch (e) {
|
|
66
|
-
errors.push(e instanceof Error ? e.message : 'unknown error');
|
|
23
|
+
const fromStorage = retrieveFromStorage();
|
|
24
|
+
if (fromStorage) {
|
|
25
|
+
return fromStorage;
|
|
67
26
|
}
|
|
68
27
|
|
|
69
|
-
throw new Error(
|
|
28
|
+
throw new Error('Unable to extract launch params', { cause: error });
|
|
70
29
|
}
|
package/src/storage.ts
CHANGED
|
@@ -29,7 +29,6 @@ interface StorageParams {
|
|
|
29
29
|
backgroundColor: RGB;
|
|
30
30
|
headerColor: HeaderColorKey | RGB;
|
|
31
31
|
};
|
|
32
|
-
'launch-params': string;
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
@@ -42,7 +41,7 @@ type StorageKey = keyof StorageParams;
|
|
|
42
41
|
* @param key - session storage key.
|
|
43
42
|
*/
|
|
44
43
|
function formatKey(key: StorageKey): string {
|
|
45
|
-
return `telegram-
|
|
44
|
+
return `telegram-mini-apps-${key}`;
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
/**
|
package/src/types.ts
CHANGED
|
@@ -7,21 +7,6 @@ export type PostEvent = BridgePostEvent;
|
|
|
7
7
|
*/
|
|
8
8
|
export type ColorScheme = 'dark' | 'light';
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Native Telegram application identifier.
|
|
12
|
-
*/
|
|
13
|
-
export type Platform =
|
|
14
|
-
| 'android'
|
|
15
|
-
| 'android_x'
|
|
16
|
-
| 'ios'
|
|
17
|
-
| 'macos'
|
|
18
|
-
| 'tdesktop'
|
|
19
|
-
| 'web'
|
|
20
|
-
| 'weba'
|
|
21
|
-
| 'unigram'
|
|
22
|
-
| 'unknown'
|
|
23
|
-
| string;
|
|
24
|
-
|
|
25
10
|
/**
|
|
26
11
|
* Function which generates unique request identifiers.
|
|
27
12
|
*/
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ThemeParams } from '../../components/index.js';
|
|
2
|
-
import type { ThemeParamsType } from '../../theme-params.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates synced instance of ThemeParams.
|
|
5
|
-
* @param params - theme parameters.
|
|
6
|
-
*/
|
|
7
|
-
export declare function createSyncedThemeParams(params: ThemeParamsType): ThemeParams;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { RGB } from '@tma.js/colors';
|
|
2
|
-
/**
|
|
3
|
-
* Application theme parameters. Defines palette used by the Telegram application.
|
|
4
|
-
*/
|
|
5
|
-
export interface ThemeParamsType {
|
|
6
|
-
backgroundColor?: RGB;
|
|
7
|
-
buttonColor?: RGB;
|
|
8
|
-
buttonTextColor?: RGB;
|
|
9
|
-
hintColor?: RGB;
|
|
10
|
-
linkColor?: RGB;
|
|
11
|
-
secondaryBackgroundColor?: RGB;
|
|
12
|
-
textColor?: RGB;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Parses incoming value as theme parameters.
|
|
16
|
-
* @param value - value to parse.
|
|
17
|
-
*/
|
|
18
|
-
export declare function parseThemeParams(value: unknown): ThemeParamsType;
|
package/src/theme-params.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { json, rgb } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
import type { RGB } from '@tma.js/colors';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Application theme parameters. Defines palette used by the Telegram application.
|
|
7
|
-
*/
|
|
8
|
-
export interface ThemeParamsType {
|
|
9
|
-
backgroundColor?: RGB;
|
|
10
|
-
buttonColor?: RGB;
|
|
11
|
-
buttonTextColor?: RGB;
|
|
12
|
-
hintColor?: RGB;
|
|
13
|
-
linkColor?: RGB;
|
|
14
|
-
secondaryBackgroundColor?: RGB;
|
|
15
|
-
textColor?: RGB;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const themeParamsParser = json<ThemeParamsType>({
|
|
19
|
-
backgroundColor: { type: rgb().optional(), from: 'bg_color' },
|
|
20
|
-
buttonColor: { type: rgb().optional(), from: 'button_color' },
|
|
21
|
-
buttonTextColor: { type: rgb().optional(), from: 'button_text_color' },
|
|
22
|
-
hintColor: { type: rgb().optional(), from: 'hint_color' },
|
|
23
|
-
linkColor: { type: rgb().optional(), from: 'link_color' },
|
|
24
|
-
textColor: { type: rgb().optional(), from: 'text_color' },
|
|
25
|
-
secondaryBackgroundColor: { type: rgb().optional(), from: 'secondary_bg_color' },
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Parses incoming value as theme parameters.
|
|
30
|
-
* @param value - value to parse.
|
|
31
|
-
*/
|
|
32
|
-
export function parseThemeParams(value: unknown): ThemeParamsType {
|
|
33
|
-
return themeParamsParser.parse(value);
|
|
34
|
-
}
|