@tma.js/bridge 1.4.0 → 2.0.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.
Files changed (92) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +44 -13
  3. package/dist/dts/applyPolyfills.d.ts +5 -0
  4. package/dist/dts/base64-url.d.ts +24 -0
  5. package/dist/dts/env/hasWebviewProxy.d.ts +10 -0
  6. package/dist/dts/env/isIframe.d.ts +5 -0
  7. package/dist/dts/env/isTMA.d.ts +31 -0
  8. package/dist/dts/env/mockTelegramEnv.d.ts +57 -0
  9. package/dist/dts/errors.d.ts +27 -0
  10. package/dist/dts/events/createEmitter.d.ts +58 -0
  11. package/dist/dts/events/emitEvent.d.ts +33 -0
  12. package/dist/dts/events/emitter.d.ts +2 -11
  13. package/dist/dts/events/types/events.d.ts +721 -0
  14. package/dist/dts/events/types/index.d.ts +3 -0
  15. package/dist/dts/events/types/listening.d.ts +5 -0
  16. package/dist/dts/events/types/misc.d.ts +16 -0
  17. package/dist/dts/globals.d.ts +45 -14
  18. package/dist/dts/index.d.ts +24 -7
  19. package/dist/dts/launch-params.d.ts +32 -0
  20. package/dist/dts/methods/captureSameReq.d.ts +10 -0
  21. package/dist/dts/methods/createPostEvent.d.ts +27 -7
  22. package/dist/dts/methods/postEvent.d.ts +31 -25
  23. package/dist/dts/methods/postMessage.d.ts +7 -0
  24. package/dist/dts/methods/supports.d.ts +15 -0
  25. package/dist/dts/methods/types/custom-method.d.ts +59 -0
  26. package/dist/dts/methods/types/index.d.ts +6 -0
  27. package/dist/dts/methods/types/methods.d.ts +756 -0
  28. package/dist/dts/methods/types/misc.d.ts +27 -0
  29. package/dist/dts/methods/{popup.d.ts → types/popup.d.ts} +1 -1
  30. package/dist/dts/methods/types/utils.d.ts +9 -0
  31. package/dist/dts/obj-prop-helpers.d.ts +39 -0
  32. package/dist/dts/start-param.d.ts +53 -0
  33. package/dist/dts/utils/compareVersions.d.ts +10 -0
  34. package/dist/dts/utils/invokeCustomMethod.d.ts +34 -0
  35. package/dist/dts/utils/request.d.ts +90 -0
  36. package/dist/index.cjs +3 -1
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.iife.js +3 -1
  39. package/dist/index.iife.js.map +1 -1
  40. package/dist/index.js +977 -0
  41. package/dist/index.js.map +1 -0
  42. package/package.json +26 -26
  43. package/dist/dts/env.d.ts +0 -29
  44. package/dist/dts/errors/MethodUnsupportedError.d.ts +0 -8
  45. package/dist/dts/errors/ParameterUnsupportedError.d.ts +0 -8
  46. package/dist/dts/errors/index.d.ts +0 -2
  47. package/dist/dts/events/events.d.ts +0 -130
  48. package/dist/dts/events/index.d.ts +0 -7
  49. package/dist/dts/events/off.d.ts +0 -7
  50. package/dist/dts/events/on.d.ts +0 -10
  51. package/dist/dts/events/onTelegramEvent.d.ts +0 -7
  52. package/dist/dts/events/once.d.ts +0 -9
  53. package/dist/dts/events/parsing.d.ts +0 -38
  54. package/dist/dts/events/payloads.d.ts +0 -93
  55. package/dist/dts/events/subscribe.d.ts +0 -9
  56. package/dist/dts/events/unsubscribe.d.ts +0 -6
  57. package/dist/dts/methods/index.d.ts +0 -6
  58. package/dist/dts/methods/invoke-custom-method.d.ts +0 -24
  59. package/dist/dts/methods/methods.d.ts +0 -330
  60. package/dist/dts/request.d.ts +0 -66
  61. package/dist/dts/shared.d.ts +0 -5
  62. package/dist/dts/supports.d.ts +0 -15
  63. package/dist/index.mjs +0 -297
  64. package/dist/index.mjs.map +0 -1
  65. package/src/env.ts +0 -49
  66. package/src/errors/MethodUnsupportedError.ts +0 -13
  67. package/src/errors/ParameterUnsupportedError.ts +0 -13
  68. package/src/errors/index.ts +0 -2
  69. package/src/events/emitter.ts +0 -126
  70. package/src/events/events.ts +0 -169
  71. package/src/events/index.ts +0 -7
  72. package/src/events/off.ts +0 -12
  73. package/src/events/on.ts +0 -17
  74. package/src/events/onTelegramEvent.ts +0 -83
  75. package/src/events/once.ts +0 -16
  76. package/src/events/parsing.ts +0 -110
  77. package/src/events/payloads.ts +0 -125
  78. package/src/events/subscribe.ts +0 -16
  79. package/src/events/unsubscribe.ts +0 -11
  80. package/src/globals.ts +0 -38
  81. package/src/index.ts +0 -7
  82. package/src/methods/createPostEvent.ts +0 -39
  83. package/src/methods/haptic.ts +0 -52
  84. package/src/methods/index.ts +0 -6
  85. package/src/methods/invoke-custom-method.ts +0 -25
  86. package/src/methods/methods.ts +0 -373
  87. package/src/methods/popup.ts +0 -53
  88. package/src/methods/postEvent.ts +0 -101
  89. package/src/request.ts +0 -168
  90. package/src/shared.ts +0 -5
  91. package/src/supports.ts +0 -82
  92. /package/dist/dts/methods/{haptic.d.ts → types/haptic-feedback.d.ts} +0 -0
package/src/request.ts DELETED
@@ -1,168 +0,0 @@
1
- import { withTimeout, isRecord } from '@tma.js/utils';
2
- import type { And, If, IsNever } from '@tma.js/util-types';
3
-
4
- import { postEvent as defaultPostEvent, type PostEvent } from './methods/postEvent.js';
5
- import { on, type EventName, type EventParams, type EventHasParams } from './events/index.js';
6
-
7
- import type {
8
- EmptyMethodName,
9
- MethodAcceptParams,
10
- MethodName,
11
- MethodParams,
12
- NonEmptyMethodName,
13
- } from './methods/methods.js';
14
-
15
- /**
16
- * Names of methods, which require passing "req_id" parameter.
17
- */
18
- type MethodWithRequestId = {
19
- [M in MethodName]: If<
20
- And<MethodAcceptParams<M>, MethodParams<M> extends { req_id: string } ? true : false>,
21
- M,
22
- never
23
- >;
24
- }[MethodName];
25
-
26
- /**
27
- * Names of events, which contain "req_id" parameter.
28
- */
29
- type EventWithRequestId = {
30
- [E in EventName]: If<
31
- And<EventHasParams<E>, EventParams<E> extends { req_id: string } ? true : false>,
32
- E,
33
- never
34
- >;
35
- }[EventName];
36
-
37
- export interface RequestOptions {
38
- /**
39
- * Bridge postEvent method.
40
- * @default Global postEvent method.
41
- */
42
- postEvent?: PostEvent;
43
-
44
- /**
45
- * Execution timeout.
46
- */
47
- timeout?: number;
48
- }
49
-
50
- export interface RequestOptionsAdvanced<EventPayload> extends RequestOptions {
51
- /**
52
- * Should return true in case, this event should be captured. If not specified,
53
- * request is not skipping captured events.
54
- */
55
- capture?: If<IsNever<EventPayload>, () => boolean, (payload: EventPayload) => boolean>;
56
- }
57
-
58
- /**
59
- * Calls specified TWA method and captures one of the specified events. Returns promise
60
- * which will be resolved in case, event with specified in method request identifier
61
- * was captured.
62
- * @param method - method to execute.
63
- * @param params - method parameters.
64
- * @param event - event or events to listen.
65
- * @param options - additional execution options.
66
- */
67
- export function request<M extends MethodWithRequestId, E extends EventWithRequestId>(
68
- method: M,
69
- params: MethodParams<M>,
70
- event: E | E[],
71
- options?: RequestOptions,
72
- ): Promise<EventParams<E>>;
73
-
74
- /**
75
- * Calls specified TWA method and captures one of the specified events. Returns promise
76
- * which will be resolved in case, specified event was captured.
77
- * @param method - method to execute.
78
- * @param event - event or events to listen.
79
- * @param options - additional execution options.
80
- */
81
- export function request<M extends EmptyMethodName, E extends EventName>(
82
- method: M,
83
- event: E | E[],
84
- options?: RequestOptionsAdvanced<EventParams<E>>,
85
- ): Promise<EventParams<E>>;
86
-
87
- /**
88
- * Calls specified TWA method and captures one of the specified events. Returns promise
89
- * which will be resolved in case, specified event was captured.
90
- * @param method - method to execute
91
- * @param params - method parameters.
92
- * @param event - event or events to listen
93
- * @param options - additional execution options.
94
- */
95
- export function request<M extends NonEmptyMethodName, E extends EventName>(
96
- method: M,
97
- params: MethodParams<M>,
98
- event: E | E[],
99
- options?: RequestOptionsAdvanced<EventParams<E>>,
100
- ): Promise<EventParams<E>>;
101
-
102
- export function request(
103
- method: MethodName,
104
- eventOrParams: EventName | EventName[] | EventParams<any>,
105
- eventOrOptions?: EventName | EventName[] | RequestOptions | RequestOptionsAdvanced<any>,
106
- options?: RequestOptions | RequestOptionsAdvanced<any>,
107
- ): Promise<any> {
108
- let executionOptions: RequestOptions | RequestOptionsAdvanced<any> | undefined;
109
- let methodParams: EventParams<any> | undefined;
110
- let events: EventName[];
111
- let requestId: string | undefined;
112
-
113
- if (typeof eventOrParams === 'string' || Array.isArray(eventOrParams)) {
114
- // Override: [method, event, options?]
115
- events = Array.isArray(eventOrParams) ? eventOrParams : [eventOrParams] as EventName[];
116
- executionOptions = eventOrOptions as (RequestOptionsAdvanced<any> | undefined);
117
- } else {
118
- // Override: [method, params, event, options?]
119
- methodParams = eventOrParams as EventParams<any>;
120
- events = Array.isArray(eventOrOptions) ? eventOrOptions : [eventOrOptions] as EventName[];
121
- executionOptions = options;
122
- }
123
-
124
- // In case, method parameters were passed, and they contained request identifier, we should store
125
- // it and wait for the event with this identifier to occur.
126
- if (isRecord(methodParams) && typeof methodParams.req_id === 'string') {
127
- requestId = methodParams.req_id;
128
- }
129
-
130
- const { postEvent = defaultPostEvent, timeout } = executionOptions || {};
131
- const capture = executionOptions && 'capture' in executionOptions
132
- ? executionOptions.capture
133
- : null;
134
-
135
- const promise = new Promise((res, rej) => {
136
- // Iterate over each event and create event listener.
137
- const stoppers = events.map((ev) => on(ev, (data?) => {
138
- // If request identifier was specified, we are waiting for event with the same value
139
- // to occur.
140
- if (typeof requestId === 'string' && (!isRecord(data) || data.req_id !== requestId)) {
141
- return;
142
- }
143
-
144
- if (typeof capture === 'function' && !capture(data)) {
145
- return;
146
- }
147
-
148
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
149
- stopListening();
150
- res(data);
151
- }));
152
-
153
- // Function which removes all event listeners.
154
- const stopListening = () => stoppers.forEach((stop) => stop());
155
-
156
- try {
157
- // We are wrapping this call in try catch, because it can throw errors in case,
158
- // compatibility check was enabled. We want an error to be captured by promise, not by
159
- // another one external try catch.
160
- postEvent(method as any, methodParams);
161
- } catch (e) {
162
- stopListening();
163
- rej(e);
164
- }
165
- });
166
-
167
- return typeof timeout === 'number' ? withTimeout(promise, timeout) : promise;
168
- }
package/src/shared.ts DELETED
@@ -1,5 +0,0 @@
1
- /**
2
- * Request identifier which should be generated locally. Native Telegram application
3
- * uses it to generate a response to called method.
4
- */
5
- export type RequestId = string;
package/src/supports.ts DELETED
@@ -1,82 +0,0 @@
1
- import { compareVersions, type Version } from '@tma.js/utils';
2
-
3
- import type {
4
- MethodVersionedParams,
5
- MethodWithVersionedParams,
6
- MethodName,
7
- } from './methods/index.js';
8
-
9
- /**
10
- * Returns true if "a" version is less than or equal to "b" version.
11
- * @param a
12
- * @param b
13
- */
14
- function versionLessOrEqual(a: Version, b: Version): boolean {
15
- return compareVersions(a, b) <= 0;
16
- }
17
-
18
- /**
19
- * Returns true in case, passed parameter in specified method is supported.
20
- * @param method - method name
21
- * @param param - method parameter
22
- * @param inVersion - platform version.
23
- */
24
- export function supports<M extends MethodWithVersionedParams>(
25
- method: M,
26
- param: MethodVersionedParams<M>,
27
- inVersion: Version,
28
- ): boolean;
29
-
30
- /**
31
- * Returns true in case, specified method is supported in passed version.
32
- * @param method - method name.
33
- * @param inVersion - platform version.
34
- */
35
- export function supports(method: MethodName, inVersion: Version): boolean;
36
-
37
- export function supports(
38
- method: MethodName,
39
- paramOrVersion: Version | string,
40
- inVersion?: string,
41
- ): boolean {
42
- // Method name, parameter, target version.
43
- if (typeof inVersion === 'string') {
44
- if (method === 'web_app_open_link') {
45
- if (paramOrVersion === 'try_instant_view') {
46
- return versionLessOrEqual('6.4', inVersion);
47
- }
48
- }
49
-
50
- if (method === 'web_app_set_header_color') {
51
- if (paramOrVersion === 'color') {
52
- return versionLessOrEqual('6.9', inVersion);
53
- }
54
- }
55
- }
56
-
57
- switch (method) {
58
- case 'web_app_open_tg_link':
59
- case 'web_app_open_invoice':
60
- case 'web_app_setup_back_button':
61
- case 'web_app_set_background_color':
62
- case 'web_app_set_header_color':
63
- case 'web_app_trigger_haptic_feedback':
64
- return versionLessOrEqual('6.1', paramOrVersion);
65
- case 'web_app_open_popup':
66
- return versionLessOrEqual('6.2', paramOrVersion);
67
- case 'web_app_close_scan_qr_popup':
68
- case 'web_app_open_scan_qr_popup':
69
- case 'web_app_read_text_from_clipboard':
70
- return versionLessOrEqual('6.4', paramOrVersion);
71
- case 'web_app_switch_inline_query':
72
- return versionLessOrEqual('6.7', paramOrVersion);
73
- case 'web_app_invoke_custom_method':
74
- case 'web_app_request_write_access':
75
- case 'web_app_request_phone':
76
- return versionLessOrEqual('6.9', paramOrVersion);
77
- case 'web_app_setup_settings_button':
78
- return versionLessOrEqual('6.10', paramOrVersion);
79
- default:
80
- return true;
81
- }
82
- }