@tma.js/sdk 1.0.0 → 1.0.2
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 +18 -16
- 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 +221 -212
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/cloud-storage/CloudStorage.ts +1 -3
- package/src/init/init.ts +89 -82
- package/src/parsing/parsers/date.ts +1 -3
- package/src/parsing/parsers/rgb.ts +1 -3
- package/src/theme-params/themeParamsParser.ts +10 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tma.js/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "TypeScript Source Development Kit for Telegram Mini Apps client application.",
|
|
5
5
|
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
@@ -24,8 +24,6 @@ interface Methods {
|
|
|
24
24
|
saveStorageValue: { key: string; value: string };
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
const stringArray = array().of(string());
|
|
28
|
-
|
|
29
27
|
function objectFromKeys<K extends string, V>(keys: K[], value: V): Record<K, V> {
|
|
30
28
|
return keys.reduce<Record<K, V>>((acc, key) => {
|
|
31
29
|
acc[key] = value;
|
|
@@ -93,7 +91,7 @@ export class CloudStorage {
|
|
|
93
91
|
async getKeys(options?: WiredRequestOptions): Promise<string[]> {
|
|
94
92
|
const result = await this.invokeCustomMethod('getStorageKeys', {}, options);
|
|
95
93
|
|
|
96
|
-
return
|
|
94
|
+
return array().of(string()).parse(result);
|
|
97
95
|
}
|
|
98
96
|
|
|
99
97
|
/**
|
package/src/init/init.ts
CHANGED
|
@@ -30,96 +30,103 @@ export function init<O extends InitOptions>(options: O): ComputedInitResult<O> {
|
|
|
30
30
|
acceptCustomStyles = false,
|
|
31
31
|
} = options;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
try {
|
|
34
|
+
// Retrieve launch data.
|
|
35
|
+
const {
|
|
36
|
+
launchParams: {
|
|
37
|
+
initData,
|
|
38
|
+
initDataRaw,
|
|
39
|
+
version,
|
|
40
|
+
platform,
|
|
41
|
+
themeParams,
|
|
42
|
+
botInline = false,
|
|
43
|
+
},
|
|
44
|
+
isPageReload,
|
|
45
|
+
} = retrieveLaunchData();
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
const createRequestId = createRequestIdGenerator();
|
|
48
|
+
const postEvent = createPostEvent(version);
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// In Telegram web version we should listen to special event sent from the Telegram application
|
|
51
|
+
// to know, when we should reload the Mini App.
|
|
52
|
+
if (isIframe()) {
|
|
53
|
+
if (acceptCustomStyles) {
|
|
54
|
+
catchCustomStyles();
|
|
55
|
+
}
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
// Notify Telegram, iframe is ready. This will result in sending style tag html from native
|
|
58
|
+
// application which is used in catchCustomStyles function. We should call this method also
|
|
59
|
+
// to start receiving "reload_iframe" events from the Telegram application.
|
|
60
|
+
postEvent('iframe_ready', { reload_supported: true });
|
|
61
|
+
on('reload_iframe', () => window.location.reload());
|
|
62
|
+
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
const result: Omit<InitResult, 'viewport'> = {
|
|
65
|
+
backButton: createBackButton(isPageReload, version, postEvent),
|
|
66
|
+
closingBehavior: createClosingBehavior(isPageReload, postEvent),
|
|
67
|
+
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
|
|
68
|
+
createRequestId,
|
|
69
|
+
hapticFeedback: new HapticFeedback(version, postEvent),
|
|
70
|
+
invoice: new Invoice(version, postEvent),
|
|
71
|
+
mainButton: createMainButton(
|
|
72
|
+
isPageReload,
|
|
73
|
+
themeParams.buttonColor || '#000000',
|
|
74
|
+
themeParams.buttonTextColor || '#ffffff',
|
|
75
|
+
postEvent,
|
|
76
|
+
),
|
|
77
|
+
miniApp: createMiniApp(
|
|
78
|
+
isPageReload,
|
|
79
|
+
themeParams.backgroundColor || '#ffffff',
|
|
80
|
+
version,
|
|
81
|
+
botInline,
|
|
82
|
+
postEvent,
|
|
83
|
+
),
|
|
84
|
+
popup: new Popup(version, postEvent),
|
|
74
85
|
postEvent,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
themeParams: createThemeParams(themeParams),
|
|
87
|
-
utils: new Utils(version, createRequestId, postEvent),
|
|
88
|
-
...(initData
|
|
89
|
-
// Init data could be missing in case, application was launched via InlineKeyboardButton.
|
|
90
|
-
? {
|
|
91
|
-
initData: new InitData(initData),
|
|
92
|
-
initDataRaw,
|
|
93
|
-
}
|
|
94
|
-
: {}),
|
|
95
|
-
};
|
|
86
|
+
qrScanner: new QRScanner(version, postEvent),
|
|
87
|
+
themeParams: createThemeParams(themeParams),
|
|
88
|
+
utils: new Utils(version, createRequestId, postEvent),
|
|
89
|
+
...(initData
|
|
90
|
+
// Init data could be missing in case, application was launched via InlineKeyboardButton.
|
|
91
|
+
? {
|
|
92
|
+
initData: new InitData(initData),
|
|
93
|
+
initDataRaw,
|
|
94
|
+
}
|
|
95
|
+
: {}),
|
|
96
|
+
};
|
|
96
97
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
const viewport = async
|
|
99
|
+
? createViewportAsync(isPageReload, platform, postEvent)
|
|
100
|
+
: createViewportSync(isPageReload, platform, postEvent);
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
if (viewport instanceof Promise) {
|
|
103
|
+
return viewport.then((vp) => {
|
|
104
|
+
processCSSVars(
|
|
105
|
+
cssVars,
|
|
106
|
+
result.miniApp,
|
|
107
|
+
result.themeParams,
|
|
108
|
+
vp,
|
|
109
|
+
);
|
|
109
110
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
return {
|
|
112
|
+
...result,
|
|
113
|
+
viewport: vp,
|
|
114
|
+
};
|
|
115
|
+
}) as ComputedInitResult<O>;
|
|
116
|
+
}
|
|
116
117
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
processCSSVars(
|
|
119
|
+
cssVars,
|
|
120
|
+
result.miniApp,
|
|
121
|
+
result.themeParams,
|
|
122
|
+
viewport,
|
|
123
|
+
);
|
|
123
124
|
|
|
124
|
-
|
|
125
|
+
return { ...result, viewport } as ComputedInitResult<O>;
|
|
126
|
+
} catch (e) {
|
|
127
|
+
if (async) {
|
|
128
|
+
return Promise.reject(e) as unknown as ComputedInitResult<O>;
|
|
129
|
+
}
|
|
130
|
+
throw e;
|
|
131
|
+
}
|
|
125
132
|
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { number } from './number.js';
|
|
2
2
|
import { createValueParserGenerator } from '../createValueParserGenerator.js';
|
|
3
3
|
|
|
4
|
-
const num = number();
|
|
5
|
-
|
|
6
4
|
/**
|
|
7
5
|
* Returns parser to parse value as Date.
|
|
8
6
|
*/
|
|
9
7
|
export const date = createValueParserGenerator<Date>((value) => (
|
|
10
8
|
value instanceof Date
|
|
11
9
|
? value
|
|
12
|
-
: new Date(
|
|
10
|
+
: new Date(number().parse(value) * 1000)
|
|
13
11
|
), 'Date');
|
|
@@ -4,9 +4,7 @@ import type { RGB } from '~/colors/index.js';
|
|
|
4
4
|
import { string } from './string.js';
|
|
5
5
|
import { createValueParserGenerator } from '../createValueParserGenerator.js';
|
|
6
6
|
|
|
7
|
-
const str = string();
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* Returns parser to parse value as RGB color.
|
|
11
9
|
*/
|
|
12
|
-
export const rgb = createValueParserGenerator<RGB>((value) => toRGB(
|
|
10
|
+
export const rgb = createValueParserGenerator<RGB>((value) => toRGB(string().parse(value)), 'rgb');
|
|
@@ -7,14 +7,16 @@ import {
|
|
|
7
7
|
import { keyToLocal } from './keys.js';
|
|
8
8
|
import type { ThemeParamsParsed } from './types.js';
|
|
9
9
|
|
|
10
|
-
const rgbOptional = rgb().optional();
|
|
11
|
-
|
|
12
10
|
export const themeParamsParser = createValueParserGenerator<ThemeParamsParsed>(
|
|
13
|
-
(value) =>
|
|
14
|
-
.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
(value) => {
|
|
12
|
+
const rgbOptional = rgb().optional();
|
|
13
|
+
|
|
14
|
+
return Object
|
|
15
|
+
.entries(toRecord(value))
|
|
16
|
+
.reduce<ThemeParamsParsed>((acc, [k, v]) => {
|
|
17
|
+
acc[keyToLocal(k)] = rgbOptional.parse(v);
|
|
18
|
+
return acc;
|
|
19
|
+
}, {});
|
|
20
|
+
},
|
|
19
21
|
'ThemeParams',
|
|
20
22
|
);
|