@tma.js/sdk 1.4.3 → 1.4.6
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 +2 -2
- package/dist/dts/bridge/events/events.d.ts +16 -16
- package/dist/dts/bridge/methods/methods.d.ts +27 -27
- package/dist/dts/bridge/methods/popup.d.ts +1 -1
- package/dist/dts/{init/css → css}/bindMiniAppCSSVars.d.ts +2 -2
- package/dist/dts/{init/css → css}/bindThemeCSSVars.d.ts +1 -1
- package/dist/dts/{init/css → css}/bindViewportCSSVars.d.ts +1 -1
- package/dist/dts/css/index.d.ts +4 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/init-data/types.d.ts +3 -3
- package/dist/dts/launch-params/types.d.ts +1 -1
- package/dist/dts/theme-params/types.d.ts +1 -1
- package/dist/index.cjs +1 -2
- package/dist/index.iife.js +1 -2
- package/dist/index.mjs +126 -115
- package/package.json +2 -3
- package/dist/index.cjs.map +0 -1
- package/dist/index.iife.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/__tests__/globals.ts +0 -39
- package/src/back-button/BackButton.ts +0 -90
- package/src/back-button/__tests__/BackButton.ts +0 -129
- package/src/back-button/index.ts +0 -2
- package/src/back-button/types.ts +0 -14
- package/src/bridge/__tests__/parseMessage.ts +0 -23
- package/src/bridge/__tests__/request.ts +0 -236
- package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
- package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
- package/src/bridge/env/__tests__/isIframe.ts +0 -30
- package/src/bridge/env/hasExternalNotify.ts +0 -19
- package/src/bridge/env/hasWebviewProxy.ts +0 -19
- package/src/bridge/env/index.ts +0 -3
- package/src/bridge/env/isIframe.ts +0 -11
- package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
- package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
- package/src/bridge/errors/index.ts +0 -2
- package/src/bridge/events/__tests__/createEmitter.ts +0 -143
- package/src/bridge/events/__tests__/off.ts +0 -34
- package/src/bridge/events/__tests__/on.ts +0 -49
- package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
- package/src/bridge/events/__tests__/once.ts +0 -64
- package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
- package/src/bridge/events/__tests__/subscribe.ts +0 -49
- package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
- package/src/bridge/events/createEmitter.ts +0 -108
- package/src/bridge/events/events.ts +0 -170
- package/src/bridge/events/index.ts +0 -9
- package/src/bridge/events/off.ts +0 -14
- package/src/bridge/events/on.ts +0 -19
- package/src/bridge/events/onTelegramEvent.ts +0 -81
- package/src/bridge/events/once.ts +0 -18
- package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
- package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
- package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
- package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
- package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
- package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
- package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
- package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
- package/src/bridge/events/parsers/index.ts +0 -9
- package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
- package/src/bridge/events/parsers/phoneRequested.ts +0 -14
- package/src/bridge/events/parsers/popupClosed.ts +0 -19
- package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
- package/src/bridge/events/parsers/theme-changed.ts +0 -58
- package/src/bridge/events/parsers/viewportChanged.ts +0 -33
- package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
- package/src/bridge/events/singletonEmitter.ts +0 -19
- package/src/bridge/events/subscribe.ts +0 -15
- package/src/bridge/events/unsubscribe.ts +0 -10
- package/src/bridge/index.ts +0 -7
- package/src/bridge/invokeCustomMethod.ts +0 -56
- package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
- package/src/bridge/methods/__tests__/postEvent.ts +0 -137
- package/src/bridge/methods/createPostEvent.ts +0 -40
- package/src/bridge/methods/custom-methods.ts +0 -68
- package/src/bridge/methods/haptic.ts +0 -52
- package/src/bridge/methods/index.ts +0 -6
- package/src/bridge/methods/methods.ts +0 -370
- package/src/bridge/methods/popup.ts +0 -53
- package/src/bridge/methods/postEvent.ts +0 -101
- package/src/bridge/parseMessage.ts +0 -28
- package/src/bridge/request.ts +0 -176
- package/src/classnames/__tests__/classNames.ts +0 -20
- package/src/classnames/__tests__/mergeClassNames.ts +0 -21
- package/src/classnames/classNames.ts +0 -34
- package/src/classnames/index.ts +0 -2
- package/src/classnames/mergeClassNames.ts +0 -60
- package/src/closing-behavior/ClosingBehavior.ts +0 -64
- package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
- package/src/closing-behavior/index.ts +0 -2
- package/src/closing-behavior/types.ts +0 -12
- package/src/cloud-storage/CloudStorage.ts +0 -138
- package/src/cloud-storage/index.ts +0 -1
- package/src/colors/__tests__/isColorDark.ts +0 -12
- package/src/colors/__tests__/isRGB.ts +0 -13
- package/src/colors/__tests__/isRGBShort.ts +0 -13
- package/src/colors/__tests__/toRGB.ts +0 -23
- package/src/colors/index.ts +0 -5
- package/src/colors/isColorDark.ts +0 -22
- package/src/colors/isRGB.ts +0 -9
- package/src/colors/isRGBShort.ts +0 -9
- package/src/colors/toRGB.ts +0 -49
- package/src/colors/types.ts +0 -9
- package/src/event-emitter/EventEmitter.ts +0 -146
- package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
- package/src/event-emitter/index.ts +0 -2
- package/src/event-emitter/types.ts +0 -60
- package/src/globals.ts +0 -38
- package/src/haptic-feedback/HapticFeedback.ts +0 -70
- package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
- package/src/haptic-feedback/index.ts +0 -1
- package/src/index.ts +0 -179
- package/src/init/catchCustomStyles.ts +0 -17
- package/src/init/creators/__tests__/createViewport.ts +0 -96
- package/src/init/creators/createBackButton.ts +0 -25
- package/src/init/creators/createClosingBehavior.ts +0 -24
- package/src/init/creators/createMainButton.ts +0 -51
- package/src/init/creators/createMiniApp.ts +0 -48
- package/src/init/creators/createRequestIdGenerator.ts +0 -13
- package/src/init/creators/createSettingsButton.ts +0 -25
- package/src/init/creators/createThemeParams.ts +0 -11
- package/src/init/creators/createViewport.ts +0 -94
- package/src/init/creators/index.ts +0 -8
- package/src/init/css/bindMiniAppCSSVars.ts +0 -48
- package/src/init/css/bindThemeCSSVars.ts +0 -31
- package/src/init/css/bindViewportCSSVars.ts +0 -36
- package/src/init/css/index.ts +0 -1
- package/src/init/css/processCSSVarsOption.ts +0 -57
- package/src/init/css/setCSSVar.ts +0 -8
- package/src/init/index.ts +0 -2
- package/src/init/init.ts +0 -134
- package/src/init/types.ts +0 -94
- package/src/init-data/InitData.ts +0 -96
- package/src/init-data/__tests__/InitData.ts +0 -98
- package/src/init-data/__tests__/chatParser.ts +0 -102
- package/src/init-data/__tests__/initDataParser.ts +0 -136
- package/src/init-data/__tests__/parseInitData.ts +0 -136
- package/src/init-data/__tests__/userParser.ts +0 -96
- package/src/init-data/chatParser.ts +0 -19
- package/src/init-data/index.ts +0 -6
- package/src/init-data/initDataParser.ts +0 -41
- package/src/init-data/parseInitData.ts +0 -10
- package/src/init-data/types.ts +0 -164
- package/src/init-data/userParser.ts +0 -45
- package/src/invoice/Invoice.ts +0 -123
- package/src/invoice/index.ts +0 -2
- package/src/invoice/types.ts +0 -11
- package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
- package/src/launch-params/computeLaunchData.ts +0 -81
- package/src/launch-params/computePageReload.ts +0 -13
- package/src/launch-params/getFirstNavigationEntry.ts +0 -10
- package/src/launch-params/index.ts +0 -13
- package/src/launch-params/launchParamsParser.ts +0 -45
- package/src/launch-params/parseLaunchParams.ts +0 -10
- package/src/launch-params/retrieveCurrent.ts +0 -27
- package/src/launch-params/retrieveFromLocation.ts +0 -10
- package/src/launch-params/retrieveFromPerformance.ts +0 -18
- package/src/launch-params/retrieveFromUrl.ts +0 -19
- package/src/launch-params/retrieveLaunchData.ts +0 -30
- package/src/launch-params/serializeLaunchParams.ts +0 -37
- package/src/launch-params/storage.ts +0 -33
- package/src/launch-params/types.ts +0 -62
- package/src/logger/Logger.ts +0 -72
- package/src/logger/__tests__/Logger.ts +0 -107
- package/src/logger/index.ts +0 -1
- package/src/main-button/MainButton.ts +0 -239
- package/src/main-button/__tests__/MainButton.ts +0 -346
- package/src/main-button/index.ts +0 -2
- package/src/main-button/types.ts +0 -26
- package/src/mini-app/MiniApp.ts +0 -348
- package/src/mini-app/__tests__/MiniApp.ts +0 -140
- package/src/mini-app/contactParser.ts +0 -29
- package/src/mini-app/index.ts +0 -2
- package/src/mini-app/types.ts +0 -38
- package/src/misc/__tests__/isRecord.ts +0 -21
- package/src/misc/index.ts +0 -2
- package/src/misc/isRecord.ts +0 -7
- package/src/misc/isTMA.ts +0 -13
- package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
- package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
- package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
- package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
- package/src/navigation/HashNavigator/drop.ts +0 -36
- package/src/navigation/HashNavigator/go.ts +0 -28
- package/src/navigation/HashNavigator/index.ts +0 -2
- package/src/navigation/HashNavigator/types.ts +0 -41
- package/src/navigation/Navigator/Navigator.ts +0 -282
- package/src/navigation/Navigator/index.ts +0 -2
- package/src/navigation/Navigator/types.ts +0 -55
- package/src/navigation/ensurePrefix.ts +0 -9
- package/src/navigation/getHash.ts +0 -17
- package/src/navigation/index.ts +0 -4
- package/src/parsing/ArrayValueParser.ts +0 -79
- package/src/parsing/ParseError.ts +0 -27
- package/src/parsing/ParseSchemaFieldError.ts +0 -21
- package/src/parsing/ValueParser.ts +0 -71
- package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
- package/src/parsing/__tests__/toRecord.ts +0 -10
- package/src/parsing/createValueParserGenerator.ts +0 -16
- package/src/parsing/index.ts +0 -10
- package/src/parsing/parseBySchema.ts +0 -65
- package/src/parsing/parsers/__tests__/array.ts +0 -39
- package/src/parsing/parsers/__tests__/boolean.ts +0 -31
- package/src/parsing/parsers/__tests__/date.ts +0 -25
- package/src/parsing/parsers/__tests__/json.ts +0 -80
- package/src/parsing/parsers/__tests__/number.ts +0 -23
- package/src/parsing/parsers/__tests__/rgb.ts +0 -22
- package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
- package/src/parsing/parsers/__tests__/string.ts +0 -25
- package/src/parsing/parsers/array.ts +0 -9
- package/src/parsing/parsers/boolean.ts +0 -22
- package/src/parsing/parsers/date.ts +0 -11
- package/src/parsing/parsers/index.ts +0 -8
- package/src/parsing/parsers/json.ts +0 -17
- package/src/parsing/parsers/number.ts +0 -21
- package/src/parsing/parsers/rgb.ts +0 -10
- package/src/parsing/parsers/searchParams.ts +0 -24
- package/src/parsing/parsers/string.ts +0 -12
- package/src/parsing/toRecord.ts +0 -27
- package/src/parsing/types.ts +0 -32
- package/src/parsing/unexpectedTypeError.ts +0 -6
- package/src/popup/Popup.ts +0 -91
- package/src/popup/__tests__/Popup.ts +0 -130
- package/src/popup/__tests__/preparePopupParams.ts +0 -85
- package/src/popup/index.ts +0 -2
- package/src/popup/preparePopupParams.ts +0 -59
- package/src/popup/types.ts +0 -69
- package/src/qr-scanner/QRScanner.ts +0 -95
- package/src/qr-scanner/index.ts +0 -2
- package/src/qr-scanner/types.ts +0 -11
- package/src/settings-button/SettingsButton.ts +0 -85
- package/src/settings-button/index.ts +0 -2
- package/src/settings-button/types.ts +0 -15
- package/src/state/State.ts +0 -67
- package/src/state/index.ts +0 -2
- package/src/state/types.ts +0 -31
- package/src/storage.ts +0 -69
- package/src/supports/__tests__/supports.ts +0 -123
- package/src/supports/createSupportsFunc.ts +0 -18
- package/src/supports/createSupportsParamFunc.ts +0 -27
- package/src/supports/index.ts +0 -4
- package/src/supports/supports.ts +0 -84
- package/src/supports/types.ts +0 -1
- package/src/theme-params/ThemeParams.ts +0 -131
- package/src/theme-params/__tests__/keys.ts +0 -19
- package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
- package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
- package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
- package/src/theme-params/index.ts +0 -6
- package/src/theme-params/keys.ts +0 -24
- package/src/theme-params/parseThemeParams.ts +0 -10
- package/src/theme-params/requestThemeParams.ts +0 -13
- package/src/theme-params/serializeThemeParams.ts +0 -20
- package/src/theme-params/themeParamsParser.ts +0 -22
- package/src/theme-params/types.ts +0 -33
- package/src/timeout/TimeoutError.ts +0 -6
- package/src/timeout/__tests__/isTimeoutError.ts +0 -9
- package/src/timeout/__tests__/withTimeout.ts +0 -28
- package/src/timeout/index.ts +0 -4
- package/src/timeout/isTimeoutError.ts +0 -9
- package/src/timeout/sleep.ts +0 -10
- package/src/timeout/withTimeout.ts +0 -24
- package/src/types/index.ts +0 -4
- package/src/types/methods.ts +0 -18
- package/src/types/platform.ts +0 -14
- package/src/types/request-id.ts +0 -10
- package/src/types/utils.ts +0 -50
- package/src/utils/Utils.ts +0 -107
- package/src/utils/index.ts +0 -1
- package/src/version/__tests__/compareVersions.ts +0 -19
- package/src/version/compareVersions.ts +0 -28
- package/src/version/index.ts +0 -2
- package/src/version/types.ts +0 -4
- package/src/viewport/Viewport.ts +0 -171
- package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
- package/src/viewport/__tests__/utils.ts +0 -12
- package/src/viewport/index.ts +0 -4
- package/src/viewport/isStableViewportPlatform.ts +0 -10
- package/src/viewport/requestViewport.ts +0 -23
- package/src/viewport/types.ts +0 -23
- package/src/viewport/utils.ts +0 -7
- /package/dist/dts/{init/css → css}/setCSSVar.d.ts +0 -0
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { toSearchParams } from 'test-utils';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { initDataParser } from '../initDataParser';
|
|
5
|
-
|
|
6
|
-
describe('auth_date', () => {
|
|
7
|
-
it('should throw an error in case, this property is missing', () => {
|
|
8
|
-
expect(() => initDataParser().parse(toSearchParams({ hash: 'abcd' }))).toThrow();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should parse source property as Date and pass it to the "authDate" property', () => {
|
|
12
|
-
expect(initDataParser().parse(toSearchParams({ auth_date: 1, hash: 'abcd' }))).toMatchObject({
|
|
13
|
-
authDate: new Date(1000),
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('can_send_after', () => {
|
|
19
|
-
it('should parse source property as Date and pass it to the "canSendAfter" property', () => {
|
|
20
|
-
expect(
|
|
21
|
-
initDataParser().parse(toSearchParams({
|
|
22
|
-
auth_date: 1,
|
|
23
|
-
hash: 'abcd',
|
|
24
|
-
can_send_after: 8882,
|
|
25
|
-
})),
|
|
26
|
-
).toMatchObject({
|
|
27
|
-
canSendAfter: 8882,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe('chat', () => {
|
|
33
|
-
it('should parse source property as Chat and pass it to the "chat" property', () => {
|
|
34
|
-
expect(
|
|
35
|
-
initDataParser().parse(toSearchParams({
|
|
36
|
-
auth_date: 1,
|
|
37
|
-
hash: 'abcd',
|
|
38
|
-
chat: {
|
|
39
|
-
id: 5,
|
|
40
|
-
type: 'group chat',
|
|
41
|
-
title: 'My Chat',
|
|
42
|
-
photo_url: 'https://johny.com',
|
|
43
|
-
username: 'Johny Chat',
|
|
44
|
-
},
|
|
45
|
-
})),
|
|
46
|
-
).toMatchObject({
|
|
47
|
-
chat: {
|
|
48
|
-
id: 5,
|
|
49
|
-
type: 'group chat',
|
|
50
|
-
title: 'My Chat',
|
|
51
|
-
photoUrl: 'https://johny.com',
|
|
52
|
-
username: 'Johny Chat',
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('hash', () => {
|
|
59
|
-
it('should throw an error in case, this property is missing', () => {
|
|
60
|
-
expect(
|
|
61
|
-
() => initDataParser().parse(toSearchParams({
|
|
62
|
-
auth_date: 1,
|
|
63
|
-
})),
|
|
64
|
-
).toThrow();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should parse source property as string and pass it to the "hash" property', () => {
|
|
68
|
-
expect(
|
|
69
|
-
initDataParser().parse(toSearchParams({
|
|
70
|
-
auth_date: 1,
|
|
71
|
-
hash: 'abcd',
|
|
72
|
-
})),
|
|
73
|
-
).toMatchObject({
|
|
74
|
-
hash: 'abcd',
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
[
|
|
80
|
-
['chat_instance', 'chatInstance'],
|
|
81
|
-
['chat_type', 'chatType'],
|
|
82
|
-
['query_id', 'queryId'],
|
|
83
|
-
['start_param', 'startParam'],
|
|
84
|
-
].forEach(([from, to]) => {
|
|
85
|
-
describe(from, () => {
|
|
86
|
-
it(`should parse source property as string and pass it to the "${to}" property`, () => {
|
|
87
|
-
expect(
|
|
88
|
-
initDataParser().parse(toSearchParams({
|
|
89
|
-
auth_date: 1,
|
|
90
|
-
hash: 'abcd',
|
|
91
|
-
[from]: 'my custom property',
|
|
92
|
-
})),
|
|
93
|
-
).toMatchObject({
|
|
94
|
-
[to]: 'my custom property',
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
['user', 'receiver'].forEach((property) => {
|
|
101
|
-
describe(property, () => {
|
|
102
|
-
it('should parse source property as User and pass it to the property with the same name', () => {
|
|
103
|
-
expect(
|
|
104
|
-
initDataParser().parse(toSearchParams({
|
|
105
|
-
auth_date: 1,
|
|
106
|
-
hash: 'abcd',
|
|
107
|
-
[property]: {
|
|
108
|
-
added_to_attachment_menu: true,
|
|
109
|
-
allows_write_to_pm: false,
|
|
110
|
-
first_name: 'Johny',
|
|
111
|
-
id: 333,
|
|
112
|
-
is_bot: false,
|
|
113
|
-
is_premium: true,
|
|
114
|
-
language_code: 'en',
|
|
115
|
-
last_name: 'Bravo',
|
|
116
|
-
photo_url: 'https://johny.com',
|
|
117
|
-
username: 'johnybravo',
|
|
118
|
-
},
|
|
119
|
-
})),
|
|
120
|
-
).toMatchObject({
|
|
121
|
-
[property]: {
|
|
122
|
-
addedToAttachmentMenu: true,
|
|
123
|
-
allowsWriteToPm: false,
|
|
124
|
-
firstName: 'Johny',
|
|
125
|
-
id: 333,
|
|
126
|
-
isBot: false,
|
|
127
|
-
isPremium: true,
|
|
128
|
-
languageCode: 'en',
|
|
129
|
-
lastName: 'Bravo',
|
|
130
|
-
photoUrl: 'https://johny.com',
|
|
131
|
-
username: 'johnybravo',
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { toSearchParams } from 'test-utils';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { parseInitData } from '../parseInitData';
|
|
5
|
-
|
|
6
|
-
describe('auth_date', () => {
|
|
7
|
-
it('should throw an error in case, this property is missing', () => {
|
|
8
|
-
expect(() => parseInitData(toSearchParams({ hash: 'abcd' }))).toThrow();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should parse source property as Date and pass it to the "authDate" property', () => {
|
|
12
|
-
expect(parseInitData(toSearchParams({ auth_date: 1, hash: 'abcd' }))).toMatchObject({
|
|
13
|
-
authDate: new Date(1000),
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('can_send_after', () => {
|
|
19
|
-
it('should parse source property as Date and pass it to the "canSendAfter" property', () => {
|
|
20
|
-
expect(
|
|
21
|
-
parseInitData(toSearchParams({
|
|
22
|
-
auth_date: 1,
|
|
23
|
-
hash: 'abcd',
|
|
24
|
-
can_send_after: 8882,
|
|
25
|
-
})),
|
|
26
|
-
).toMatchObject({
|
|
27
|
-
canSendAfter: 8882,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe('chat', () => {
|
|
33
|
-
it('should parse source property as Chat and pass it to the "chat" property', () => {
|
|
34
|
-
expect(
|
|
35
|
-
parseInitData(toSearchParams({
|
|
36
|
-
auth_date: 1,
|
|
37
|
-
hash: 'abcd',
|
|
38
|
-
chat: {
|
|
39
|
-
id: 5,
|
|
40
|
-
type: 'group chat',
|
|
41
|
-
title: 'My Chat',
|
|
42
|
-
photo_url: 'https://johny.com',
|
|
43
|
-
username: 'Johny Chat',
|
|
44
|
-
},
|
|
45
|
-
})),
|
|
46
|
-
).toMatchObject({
|
|
47
|
-
chat: {
|
|
48
|
-
id: 5,
|
|
49
|
-
type: 'group chat',
|
|
50
|
-
title: 'My Chat',
|
|
51
|
-
photoUrl: 'https://johny.com',
|
|
52
|
-
username: 'Johny Chat',
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('hash', () => {
|
|
59
|
-
it('should throw an error in case, this property is missing', () => {
|
|
60
|
-
expect(
|
|
61
|
-
() => parseInitData(toSearchParams({
|
|
62
|
-
auth_date: 1,
|
|
63
|
-
})),
|
|
64
|
-
).toThrow();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should parse source property as string and pass it to the "hash" property', () => {
|
|
68
|
-
expect(
|
|
69
|
-
parseInitData(toSearchParams({
|
|
70
|
-
auth_date: 1,
|
|
71
|
-
hash: 'abcd',
|
|
72
|
-
})),
|
|
73
|
-
).toMatchObject({
|
|
74
|
-
hash: 'abcd',
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
[
|
|
80
|
-
['chat_instance', 'chatInstance'],
|
|
81
|
-
['chat_type', 'chatType'],
|
|
82
|
-
['query_id', 'queryId'],
|
|
83
|
-
['start_param', 'startParam'],
|
|
84
|
-
].forEach(([from, to]) => {
|
|
85
|
-
describe(from, () => {
|
|
86
|
-
it(`should parse source property as string and pass it to the "${to}" property`, () => {
|
|
87
|
-
expect(
|
|
88
|
-
parseInitData(toSearchParams({
|
|
89
|
-
auth_date: 1,
|
|
90
|
-
hash: 'abcd',
|
|
91
|
-
[from]: 'my custom property',
|
|
92
|
-
})),
|
|
93
|
-
).toMatchObject({
|
|
94
|
-
[to]: 'my custom property',
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
['user', 'receiver'].forEach((property) => {
|
|
101
|
-
describe(property, () => {
|
|
102
|
-
it('should parse source property as User and pass it to the property with the same name', () => {
|
|
103
|
-
expect(
|
|
104
|
-
parseInitData(toSearchParams({
|
|
105
|
-
auth_date: 1,
|
|
106
|
-
hash: 'abcd',
|
|
107
|
-
[property]: {
|
|
108
|
-
added_to_attachment_menu: true,
|
|
109
|
-
allows_write_to_pm: false,
|
|
110
|
-
first_name: 'Johny',
|
|
111
|
-
id: 333,
|
|
112
|
-
is_bot: false,
|
|
113
|
-
is_premium: true,
|
|
114
|
-
language_code: 'en',
|
|
115
|
-
last_name: 'Bravo',
|
|
116
|
-
photo_url: 'https://johny.com',
|
|
117
|
-
username: 'johnybravo',
|
|
118
|
-
},
|
|
119
|
-
})),
|
|
120
|
-
).toMatchObject({
|
|
121
|
-
[property]: {
|
|
122
|
-
addedToAttachmentMenu: true,
|
|
123
|
-
allowsWriteToPm: false,
|
|
124
|
-
firstName: 'Johny',
|
|
125
|
-
id: 333,
|
|
126
|
-
isBot: false,
|
|
127
|
-
isPremium: true,
|
|
128
|
-
languageCode: 'en',
|
|
129
|
-
lastName: 'Bravo',
|
|
130
|
-
photoUrl: 'https://johny.com',
|
|
131
|
-
username: 'johnybravo',
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { userParser } from '../userParser';
|
|
4
|
-
|
|
5
|
-
describe('first_name', () => {
|
|
6
|
-
it('should throw an error in case, this property is missing', () => {
|
|
7
|
-
expect(() => userParser().parse({ id: 123 })).toThrow();
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should parse source property as string and pass it to the "firstName" property', () => {
|
|
11
|
-
expect(
|
|
12
|
-
userParser().parse({ id: 123, first_name: 'Pavel' }),
|
|
13
|
-
).toMatchObject({
|
|
14
|
-
firstName: 'Pavel',
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe('id', () => {
|
|
20
|
-
it('should throw an error in case, this property is missing', () => {
|
|
21
|
-
expect(() => userParser().parse({ first_name: 'Pavel' })).toThrow();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should parse source property as number and pass it to the "id" property', () => {
|
|
25
|
-
expect(
|
|
26
|
-
userParser().parse({
|
|
27
|
-
id: 123,
|
|
28
|
-
first_name: 'Pavel',
|
|
29
|
-
}),
|
|
30
|
-
).toMatchObject({
|
|
31
|
-
id: 123,
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Check optional booleans.
|
|
37
|
-
[
|
|
38
|
-
['added_to_attachment_menu', 'addedToAttachmentMenu'],
|
|
39
|
-
['allows_write_to_pm', 'allowsWriteToPm'],
|
|
40
|
-
['is_bot', 'isBot'],
|
|
41
|
-
['is_premium', 'isPremium'],
|
|
42
|
-
].forEach(([from, to]) => {
|
|
43
|
-
describe(from, () => {
|
|
44
|
-
it(`should parse source property as boolean and pass it to the "${to}" property`, () => {
|
|
45
|
-
expect(
|
|
46
|
-
userParser().parse({
|
|
47
|
-
id: 123,
|
|
48
|
-
first_name: 'Pavel',
|
|
49
|
-
[from]: false,
|
|
50
|
-
}),
|
|
51
|
-
).toMatchObject({
|
|
52
|
-
[to]: false,
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
expect(
|
|
56
|
-
() => userParser().parse({
|
|
57
|
-
id: 123,
|
|
58
|
-
first_name: 'Pavel',
|
|
59
|
-
[from]: 'non-boolean',
|
|
60
|
-
}),
|
|
61
|
-
).toThrow();
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Check optional strings.
|
|
67
|
-
[
|
|
68
|
-
['language_code', 'languageCode'],
|
|
69
|
-
['last_name', 'lastName'],
|
|
70
|
-
['photo_url', 'photoUrl'],
|
|
71
|
-
['username', 'username'],
|
|
72
|
-
].forEach(([from, to]) => {
|
|
73
|
-
describe(from, () => {
|
|
74
|
-
it(`should parse source property as string and pass it to the "${to}" property`, () => {
|
|
75
|
-
expect(
|
|
76
|
-
userParser().parse({
|
|
77
|
-
id: 123,
|
|
78
|
-
first_name: 'Pavel',
|
|
79
|
-
[from]: 'my custom property',
|
|
80
|
-
}),
|
|
81
|
-
).toMatchObject({
|
|
82
|
-
[to]: 'my custom property',
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
expect(
|
|
86
|
-
() => userParser().parse({
|
|
87
|
-
id: 123,
|
|
88
|
-
first_name: 'Pavel',
|
|
89
|
-
[from]: {
|
|
90
|
-
key: 'cant parse it',
|
|
91
|
-
},
|
|
92
|
-
}),
|
|
93
|
-
).toThrow();
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { json, number, string } from '~/parsing/index.js';
|
|
2
|
-
|
|
3
|
-
import type { Chat } from './types.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns parser used to parse chat data.
|
|
7
|
-
*/
|
|
8
|
-
export function chatParser() {
|
|
9
|
-
return json<Chat>({
|
|
10
|
-
id: number(),
|
|
11
|
-
type: string(),
|
|
12
|
-
title: string(),
|
|
13
|
-
photoUrl: {
|
|
14
|
-
type: string().optional(),
|
|
15
|
-
from: 'photo_url',
|
|
16
|
-
},
|
|
17
|
-
username: string().optional(),
|
|
18
|
-
}, 'Chat');
|
|
19
|
-
}
|
package/src/init-data/index.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { date, number, searchParams, string } from '~/parsing/index.js';
|
|
2
|
-
|
|
3
|
-
import { chatParser } from './chatParser.js';
|
|
4
|
-
import type { InitDataParsed } from './types.js';
|
|
5
|
-
import { userParser } from './userParser.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Returns parser used to parse init data, presented as search params.
|
|
9
|
-
*/
|
|
10
|
-
export function initDataParser() {
|
|
11
|
-
return searchParams<InitDataParsed>({
|
|
12
|
-
authDate: {
|
|
13
|
-
type: date(),
|
|
14
|
-
from: 'auth_date',
|
|
15
|
-
},
|
|
16
|
-
canSendAfter: {
|
|
17
|
-
type: number().optional(),
|
|
18
|
-
from: 'can_send_after',
|
|
19
|
-
},
|
|
20
|
-
chat: chatParser().optional(),
|
|
21
|
-
chatInstance: {
|
|
22
|
-
type: string().optional(),
|
|
23
|
-
from: 'chat_instance',
|
|
24
|
-
},
|
|
25
|
-
chatType: {
|
|
26
|
-
type: string().optional(),
|
|
27
|
-
from: 'chat_type',
|
|
28
|
-
},
|
|
29
|
-
hash: string(),
|
|
30
|
-
queryId: {
|
|
31
|
-
type: string().optional(),
|
|
32
|
-
from: 'query_id',
|
|
33
|
-
},
|
|
34
|
-
receiver: userParser().optional(),
|
|
35
|
-
startParam: {
|
|
36
|
-
type: string().optional(),
|
|
37
|
-
from: 'start_param',
|
|
38
|
-
},
|
|
39
|
-
user: userParser().optional(),
|
|
40
|
-
}, 'InitData');
|
|
41
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { initDataParser } from './initDataParser.js';
|
|
2
|
-
import type { InitDataParsed } from './types.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parses incoming value as init data.
|
|
6
|
-
* @param value - value to parse.
|
|
7
|
-
*/
|
|
8
|
-
export function parseInitData(value: unknown): InitDataParsed {
|
|
9
|
-
return initDataParser().parse(value);
|
|
10
|
-
}
|
package/src/init-data/types.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Known type of chat.
|
|
3
|
-
*/
|
|
4
|
-
export type ChatType =
|
|
5
|
-
| 'sender'
|
|
6
|
-
| 'private'
|
|
7
|
-
| 'group'
|
|
8
|
-
| 'supergroup'
|
|
9
|
-
| 'channel'
|
|
10
|
-
| string;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Describes Telegram Mini Apps [User](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#user) type.
|
|
14
|
-
*/
|
|
15
|
-
export interface User {
|
|
16
|
-
/**
|
|
17
|
-
* True, if this user added the bot to the attachment menu.
|
|
18
|
-
*/
|
|
19
|
-
addedToAttachmentMenu?: boolean;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* True, if this user allowed the bot to message them.
|
|
23
|
-
*/
|
|
24
|
-
allowsWriteToPm?: boolean;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* First name of the user or bot.
|
|
28
|
-
*/
|
|
29
|
-
firstName: string;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* A unique identifier for the user or bot.
|
|
33
|
-
*/
|
|
34
|
-
id: number;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* True, if this user is a bot. Returned in the `receiver` field only.
|
|
38
|
-
* @see InitDataParsed.receiver
|
|
39
|
-
*/
|
|
40
|
-
isBot?: boolean;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* True, if this user is a Telegram Premium user.
|
|
44
|
-
*/
|
|
45
|
-
isPremium?: boolean;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Last name of the user or bot.
|
|
49
|
-
*/
|
|
50
|
-
lastName?: string;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language.
|
|
54
|
-
* Returns in user field only.
|
|
55
|
-
*/
|
|
56
|
-
languageCode?: string;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* URL of the user’s profile photo. The photo can be in .jpeg or .svg
|
|
60
|
-
* formats. Only returned for Mini Apps launched from the attachment menu.
|
|
61
|
-
*/
|
|
62
|
-
photoUrl?: string;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Username of the user or bot.
|
|
66
|
-
*/
|
|
67
|
-
username?: string;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Describes Telegram Mini Apps [Chat](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#chat) type.
|
|
72
|
-
*/
|
|
73
|
-
export interface Chat {
|
|
74
|
-
/**
|
|
75
|
-
* Unique identifier for this chat.
|
|
76
|
-
*/
|
|
77
|
-
id: number;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
|
|
81
|
-
* Only returned for Mini Apps launched from the attachment menu.
|
|
82
|
-
*/
|
|
83
|
-
photoUrl?: string;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Type of chat.
|
|
87
|
-
*/
|
|
88
|
-
type: 'group' | 'supergroup' | 'channel' | string;
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Title of the chat.
|
|
92
|
-
*/
|
|
93
|
-
title: string;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Username of the chat.
|
|
97
|
-
*/
|
|
98
|
-
username?: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Describes Telegram Mini Apps [InitData](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#parameters-list)
|
|
103
|
-
* type.
|
|
104
|
-
*/
|
|
105
|
-
export interface InitDataParsed {
|
|
106
|
-
/**
|
|
107
|
-
* Init data generation date.
|
|
108
|
-
*/
|
|
109
|
-
authDate: Date;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* The number of seconds after which a message can be sent via the method [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery).
|
|
113
|
-
*/
|
|
114
|
-
canSendAfter?: number;
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* An object containing data about the chat where the bot was launched via
|
|
118
|
-
* the attachment menu. Returned for supergroups, channels and group
|
|
119
|
-
* chats – only for Mini Apps launched via the attachment menu.
|
|
120
|
-
*/
|
|
121
|
-
chat?: Chat;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* The type of chat from which Mini App was opened.
|
|
125
|
-
*/
|
|
126
|
-
chatType?: ChatType;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* A global identifier indicating the chat from which Mini App was opened. Returned only for
|
|
130
|
-
* applications opened by direct link.
|
|
131
|
-
*/
|
|
132
|
-
chatInstance?: string;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* A hash of all passed parameters, which the bot server can use to
|
|
136
|
-
* check their [validity](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
|
|
137
|
-
*/
|
|
138
|
-
hash: string;
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* A unique identifier for the Mini App session, required for sending
|
|
142
|
-
* messages via the [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
|
|
143
|
-
*/
|
|
144
|
-
queryId?: string;
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* An object containing data about the chat partner of the current user in
|
|
148
|
-
* the chat where the bot was launched via the attachment menu.
|
|
149
|
-
* Returned only for private chats and only for Mini Apps launched
|
|
150
|
-
* via the attachment menu.
|
|
151
|
-
*/
|
|
152
|
-
receiver?: User;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* The value of the `startattach` parameter, passed via link. Only returned for
|
|
156
|
-
* Mini Apps when launched from the attachment menu via link.
|
|
157
|
-
*/
|
|
158
|
-
startParam?: string;
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* An object containing data about the current user.
|
|
162
|
-
*/
|
|
163
|
-
user?: User;
|
|
164
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { boolean, json, number, string } from '~/parsing/index.js';
|
|
2
|
-
|
|
3
|
-
import type { User } from './types.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns parser used to parse user data.
|
|
7
|
-
*/
|
|
8
|
-
export function userParser() {
|
|
9
|
-
return json<User>({
|
|
10
|
-
addedToAttachmentMenu: {
|
|
11
|
-
type: boolean().optional(),
|
|
12
|
-
from: 'added_to_attachment_menu',
|
|
13
|
-
},
|
|
14
|
-
allowsWriteToPm: {
|
|
15
|
-
type: boolean().optional(),
|
|
16
|
-
from: 'allows_write_to_pm',
|
|
17
|
-
},
|
|
18
|
-
firstName: {
|
|
19
|
-
type: string(),
|
|
20
|
-
from: 'first_name',
|
|
21
|
-
},
|
|
22
|
-
id: number(),
|
|
23
|
-
isBot: {
|
|
24
|
-
type: boolean().optional(),
|
|
25
|
-
from: 'is_bot',
|
|
26
|
-
},
|
|
27
|
-
isPremium: {
|
|
28
|
-
type: boolean().optional(),
|
|
29
|
-
from: 'is_premium',
|
|
30
|
-
},
|
|
31
|
-
languageCode: {
|
|
32
|
-
type: string().optional(),
|
|
33
|
-
from: 'language_code',
|
|
34
|
-
},
|
|
35
|
-
lastName: {
|
|
36
|
-
type: string().optional(),
|
|
37
|
-
from: 'last_name',
|
|
38
|
-
},
|
|
39
|
-
photoUrl: {
|
|
40
|
-
type: string().optional(),
|
|
41
|
-
from: 'photo_url',
|
|
42
|
-
},
|
|
43
|
-
username: string().optional(),
|
|
44
|
-
}, 'User');
|
|
45
|
-
}
|