@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tma.js/sdk",
3
- "version": "1.0.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 stringArray.parse(result);
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
- // Retrieve launch data.
34
- const {
35
- launchParams: {
36
- initData,
37
- initDataRaw,
38
- version,
39
- platform,
40
- themeParams,
41
- botInline = false,
42
- },
43
- isPageReload,
44
- } = retrieveLaunchData();
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
- const createRequestId = createRequestIdGenerator();
47
- const postEvent = createPostEvent(version);
47
+ const createRequestId = createRequestIdGenerator();
48
+ const postEvent = createPostEvent(version);
48
49
 
49
- // In Telegram web version we should listen to special event sent from the Telegram application
50
- // to know, when we should reload the Mini App.
51
- if (isIframe()) {
52
- if (acceptCustomStyles) {
53
- catchCustomStyles();
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
- // Notify Telegram, iframe is ready. This will result in sending style tag html from native
57
- // application which is used in catchCustomStyles function. We should call this method also
58
- // to start receiving "reload_iframe" events from the Telegram application.
59
- postEvent('iframe_ready', { reload_supported: true });
60
- on('reload_iframe', () => window.location.reload());
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
- const result: Omit<InitResult, 'viewport'> = {
64
- backButton: createBackButton(isPageReload, version, postEvent),
65
- closingBehavior: createClosingBehavior(isPageReload, postEvent),
66
- cloudStorage: new CloudStorage(version, createRequestId, postEvent),
67
- createRequestId,
68
- hapticFeedback: new HapticFeedback(version, postEvent),
69
- invoice: new Invoice(version, postEvent),
70
- mainButton: createMainButton(
71
- isPageReload,
72
- themeParams.buttonColor || '#000000',
73
- themeParams.buttonTextColor || '#ffffff',
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
- miniApp: createMiniApp(
77
- isPageReload,
78
- themeParams.backgroundColor || '#ffffff',
79
- version,
80
- botInline,
81
- postEvent,
82
- ),
83
- popup: new Popup(version, postEvent),
84
- postEvent,
85
- qrScanner: new QRScanner(version, postEvent),
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
- const viewport = async
98
- ? createViewportAsync(isPageReload, platform, postEvent)
99
- : createViewportSync(isPageReload, platform, postEvent);
98
+ const viewport = async
99
+ ? createViewportAsync(isPageReload, platform, postEvent)
100
+ : createViewportSync(isPageReload, platform, postEvent);
100
101
 
101
- if (viewport instanceof Promise) {
102
- return viewport.then((vp) => {
103
- processCSSVars(
104
- cssVars,
105
- result.miniApp,
106
- result.themeParams,
107
- vp,
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
- return {
111
- ...result,
112
- viewport: vp,
113
- };
114
- }) as ComputedInitResult<O>;
115
- }
111
+ return {
112
+ ...result,
113
+ viewport: vp,
114
+ };
115
+ }) as ComputedInitResult<O>;
116
+ }
116
117
 
117
- processCSSVars(
118
- cssVars,
119
- result.miniApp,
120
- result.themeParams,
121
- viewport,
122
- );
118
+ processCSSVars(
119
+ cssVars,
120
+ result.miniApp,
121
+ result.themeParams,
122
+ viewport,
123
+ );
123
124
 
124
- return { ...result, viewport } as ComputedInitResult<O>;
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(num.parse(value) * 1000)
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(str.parse(value)), 'rgb');
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) => Object
14
- .entries(toRecord(value))
15
- .reduce<ThemeParamsParsed>((acc, [k, v]) => {
16
- acc[keyToLocal(k)] = rgbOptional.parse(v);
17
- return acc;
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
  );