@tma.js/sdk 2.0.2 → 2.2.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 (34) hide show
  1. package/dist/dts/bridge/events/types.d.ts +12 -6
  2. package/dist/dts/components/BackButton/initBackButton.d.ts +1 -1
  3. package/dist/dts/components/BiometryManager/initBiometryManager.d.ts +1 -1
  4. package/dist/dts/components/ClosingBehavior/initClosingBehavior.d.ts +1 -1
  5. package/dist/dts/components/CloudStorage/initCloudStorage.d.ts +1 -1
  6. package/dist/dts/components/HapticFeedback/initHapticFeedback.d.ts +1 -1
  7. package/dist/dts/components/InitData/initInitData.d.ts +1 -1
  8. package/dist/dts/components/Invoice/initInvoice.d.ts +1 -1
  9. package/dist/dts/components/MainButton/MainButton.d.ts +28 -28
  10. package/dist/dts/components/MainButton/initMainButton.d.ts +1 -1
  11. package/dist/dts/components/MainButton/types.d.ts +1 -1
  12. package/dist/dts/components/MiniApp/initMiniApp.d.ts +1 -1
  13. package/dist/dts/components/Popup/initPopup.d.ts +1 -1
  14. package/dist/dts/components/QRScanner/initQRScanner.d.ts +1 -1
  15. package/dist/dts/components/SettingsButton/initSettingsButton.d.ts +1 -1
  16. package/dist/dts/components/ThemeParams/initThemeParams.d.ts +1 -1
  17. package/dist/dts/components/Utils/initUtils.d.ts +1 -1
  18. package/dist/dts/components/Viewport/initViewport.d.ts +1 -2
  19. package/dist/dts/index.d.ts +1 -1
  20. package/dist/dts/misc/createCleanup.d.ts +11 -0
  21. package/dist/dts/misc/createComponentInitFn/createComponentInitFn.d.ts +7 -6
  22. package/dist/dts/misc/createComponentInitFn/types.d.ts +28 -20
  23. package/dist/dts/navigation/BrowserNavigator/BrowserNavigator.d.ts +1 -1
  24. package/dist/dts/navigation/BrowserNavigator/types.d.ts +5 -4
  25. package/dist/index.cjs +1 -1
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.iife.js +1 -1
  28. package/dist/index.iife.js.map +1 -1
  29. package/dist/index.js +671 -654
  30. package/dist/index.js.map +1 -1
  31. package/dist/index.low-level.iife.js +1 -1
  32. package/dist/index.low-level.iife.js.map +1 -1
  33. package/package.json +1 -1
  34. package/dist/dts/components/Viewport/initViewportFromRequest.d.ts +0 -10
@@ -1,6 +1,7 @@
1
1
  import { RGB } from '../../colors/types.js';
2
+ import { EventListener } from '../../events/event-emitter/types.js';
3
+ import { RemoveEventListenerFn } from '../../events/types.js';
2
4
  import { EventEmitter } from '../../events/event-emitter/EventEmitter.js';
3
- import { EventListener, SubscribeListener } from '../../events/event-emitter/types.js';
4
5
  import { RequestId } from '../../request-id/types.js';
5
6
 
6
7
  export type InvoiceStatus = 'paid' | 'failed' | 'pending' | 'cancelled' | string;
@@ -286,11 +287,16 @@ export type MiniAppsEventPayload<E extends MiniAppsEventName> = MiniAppsEvents[E
286
287
  * Returns event listener for the specified Mini Apps event.
287
288
  */
288
289
  export type MiniAppsEventListener<E extends MiniAppsEventName> = EventListener<MiniAppsEvents[E]>;
290
+ export interface MiniAppsEventEmitter extends Pick<EventEmitter<MiniAppsEvents>, 'on' | 'off' | 'count'> {
291
+ subscribe(listener: MiniAppsSubscribeListener): RemoveEventListenerFn;
292
+ unsubscribe(listener: MiniAppsSubscribeListener): void;
293
+ }
289
294
  /**
290
295
  * Mini Apps event listener used in `subscribe` and `unsubscribe` functions.
291
296
  */
292
- export type MiniAppsSubscribeListener = SubscribeListener<MiniAppsEvents>;
293
- /**
294
- * Mini Apps event emitter.
295
- */
296
- export type MiniAppsEventEmitter = EventEmitter<MiniAppsEvents>;
297
+ export type MiniAppsSubscribeListener = (payload: {
298
+ [E in MiniAppsEventName]: {
299
+ name: E;
300
+ payload: MiniAppsEventPayload<E>;
301
+ };
302
+ }[MiniAppsEventName]) => void;
@@ -4,4 +4,4 @@ import { BackButton } from './BackButton.js';
4
4
  * @returns A new initialized instance of the `BackButton` class.
5
5
  * @see BackButton
6
6
  */
7
- export declare const initBackButton: import('../../index.js').InitComponentFn<"version", BackButton, import('./types.js').BackButtonState>;
7
+ export declare const initBackButton: import('../../index.js').InitDynamicComponentFn<BackButton>;
@@ -4,4 +4,4 @@ import { BiometryManager } from './BiometryManager.js';
4
4
  * @returns A promise with a new initialized instance of the `BiometryManager` class.
5
5
  * @see BiometryManager
6
6
  */
7
- export declare const initBiometryManager: import('../../index.js').InitComponentFn<"version", Promise<BiometryManager>, import('./types.js').BiometryManagerState>;
7
+ export declare const initBiometryManager: import('../../index.js').InitDynamicComponentFn<Promise<BiometryManager>>;
@@ -4,4 +4,4 @@ import { ClosingBehavior } from './ClosingBehavior.js';
4
4
  * @returns A new initialized instance of the `ClosingBehavior` class.
5
5
  * @see ClosingBehavior
6
6
  */
7
- export declare const initClosingBehavior: import('../../index.js').InitComponentFn<never, ClosingBehavior, import('./types.js').ClosingBehaviorState>;
7
+ export declare const initClosingBehavior: import('../../index.js').InitDynamicComponentFn<ClosingBehavior>;
@@ -4,4 +4,4 @@ import { CloudStorage } from './CloudStorage.js';
4
4
  * @returns A new initialized instance of the `CloudStorage` class.
5
5
  * @see CloudStorage
6
6
  */
7
- export declare const initCloudStorage: import('../../index.js').InitComponentFn<"version", CloudStorage, never>;
7
+ export declare const initCloudStorage: import('../../index.js').InitStaticComponentFn<CloudStorage>;
@@ -4,4 +4,4 @@ import { HapticFeedback } from './HapticFeedback.js';
4
4
  * @returns A new initialized instance of the `HapticFeedback` class.
5
5
  * @see HapticFeedback
6
6
  */
7
- export declare const initHapticFeedback: import('../../index.js').InitComponentFn<"version", HapticFeedback, never>;
7
+ export declare const initHapticFeedback: import('../../index.js').InitStaticComponentFn<HapticFeedback>;
@@ -4,4 +4,4 @@ import { InitData } from './InitData.js';
4
4
  * @returns A new initialized instance of the `InitData` class or undefined.
5
5
  * @see InitData
6
6
  */
7
- export declare const initInitData: import('../../index.js').InitComponentFn<"initData", InitData | undefined, never>;
7
+ export declare const initInitData: import('../../index.js').InitStaticComponentFn<InitData | undefined>;
@@ -4,4 +4,4 @@ import { Invoice } from './Invoice.js';
4
4
  * @returns A new initialized instance of the `Invoice` class.
5
5
  * @see Invoice
6
6
  */
7
- export declare const initInvoice: import('../../index.js').InitComponentFn<"version", Invoice, never>;
7
+ export declare const initInvoice: import('../../index.js').InitStaticComponentFn<Invoice>;
@@ -11,37 +11,14 @@ type Emitter = EventEmitter<MainButtonEvents>;
11
11
  export declare class MainButton extends WithStateUtils<MainButtonState> {
12
12
  private readonly postEvent;
13
13
  constructor({ postEvent, ...rest }: MainButtonProps);
14
- /**
15
- * Sends current local state to the Telegram application.
16
- */
17
- private commit;
18
- private set isEnabled(value);
19
- /**
20
- * True if the MainButton is enabled.
21
- */
22
- get isEnabled(): boolean;
23
- private set isLoaderVisible(value);
24
- /**
25
- * True if the MainButton loader is visible.
26
- */
27
- get isLoaderVisible(): boolean;
28
- private set isVisible(value);
29
- /**
30
- * True if the MainButton is visible.
31
- */
32
- get isVisible(): boolean;
33
14
  /**
34
15
  * The MainButton background color.
35
16
  */
36
- get backgroundColor(): RGB;
17
+ get bgColor(): RGB;
37
18
  /**
38
- * The MainButton text.
39
- */
40
- get text(): string;
41
- /**
42
- * The MainButton text color.
19
+ * Sends current local state to the Telegram application.
43
20
  */
44
- get textColor(): RGB;
21
+ private commit;
45
22
  /**
46
23
  * Disables the MainButton.
47
24
  * @see Does not work on Android: https://github.com/Telegram-Mini-Apps/issues/issues/1
@@ -59,6 +36,21 @@ export declare class MainButton extends WithStateUtils<MainButtonState> {
59
36
  * Hides the MainButton loading indicator.
60
37
  */
61
38
  hideLoader(): this;
39
+ private set isEnabled(value);
40
+ /**
41
+ * True if the MainButton is enabled.
42
+ */
43
+ get isEnabled(): boolean;
44
+ private set isLoaderVisible(value);
45
+ /**
46
+ * True if the MainButton loader is visible.
47
+ */
48
+ get isLoaderVisible(): boolean;
49
+ private set isVisible(value);
50
+ /**
51
+ * True if the MainButton is visible.
52
+ */
53
+ get isVisible(): boolean;
62
54
  /**
63
55
  * Adds a new event listener.
64
56
  * @param event - event to listen.
@@ -94,13 +86,21 @@ export declare class MainButton extends WithStateUtils<MainButtonState> {
94
86
  setTextColor(textColor: RGB): this;
95
87
  /**
96
88
  * Updates current Main Button color.
97
- * @param backgroundColor - color to set.
89
+ * @param bgColor - color to set.
98
90
  */
99
- setBackgroundColor(backgroundColor: RGB): this;
91
+ setBgColor(bgColor: RGB): this;
100
92
  /**
101
93
  * Allows setting multiple Main Button parameters.
102
94
  * @param params - Main Button parameters.
103
95
  */
104
96
  setParams(params: Partial<MainButtonParams>): this;
97
+ /**
98
+ * The MainButton text.
99
+ */
100
+ get text(): string;
101
+ /**
102
+ * The MainButton text color.
103
+ */
104
+ get textColor(): RGB;
105
105
  }
106
106
  export {};
@@ -4,4 +4,4 @@ import { MainButton } from './MainButton.js';
4
4
  * @returns A new initialized instance of the `MainButton` class.
5
5
  * @see MainButton
6
6
  */
7
- export declare const initMainButton: import('../../index.js').InitComponentFn<"themeParams", MainButton, import('./types.js').MainButtonState>;
7
+ export declare const initMainButton: import('../../index.js').InitDynamicComponentFn<MainButton>;
@@ -3,7 +3,7 @@ import { StateEvents } from '../../classes/State/types.js';
3
3
  import { RGB } from '../../colors/types.js';
4
4
 
5
5
  export interface MainButtonParams {
6
- backgroundColor: RGB;
6
+ bgColor: RGB;
7
7
  isEnabled: boolean;
8
8
  isLoaderVisible: boolean;
9
9
  isVisible: boolean;
@@ -4,4 +4,4 @@ import { MiniApp } from './MiniApp.js';
4
4
  * @returns A new initialized instance of the `MiniApp` class.
5
5
  * @see MiniApp
6
6
  */
7
- export declare const initMiniApp: import('../../index.js').InitComponentFn<"botInline" | "themeParams" | "version", MiniApp, import('./types.js').MiniAppState>;
7
+ export declare const initMiniApp: import('../../index.js').InitDynamicComponentFn<MiniApp>;
@@ -4,4 +4,4 @@ import { Popup } from './Popup.js';
4
4
  * @returns A new initialized instance of the `Popup` class.
5
5
  * @see Popup
6
6
  */
7
- export declare const initPopup: import('../../index.js').InitComponentFn<"version", Popup, never>;
7
+ export declare const initPopup: import('../../index.js').InitStaticComponentFn<Popup>;
@@ -4,4 +4,4 @@ import { QRScanner } from './QRScanner.js';
4
4
  * @returns A new initialized instance of the `QRScanner` class.
5
5
  * @see QRScanner
6
6
  */
7
- export declare const initQRScanner: import('../../index.js').InitComponentFn<"version", QRScanner, never>;
7
+ export declare const initQRScanner: import('../../index.js').InitStaticComponentFn<QRScanner>;
@@ -4,4 +4,4 @@ import { SettingsButton } from './SettingsButton.js';
4
4
  * @returns A new initialized instance of the `SettingsButton` class.
5
5
  * @see SettingsButton
6
6
  */
7
- export declare const initSettingsButton: import('../../index.js').InitComponentFn<"version", SettingsButton, import('./types.js').SettingsButtonState>;
7
+ export declare const initSettingsButton: import('../../index.js').InitDynamicComponentFn<SettingsButton>;
@@ -4,4 +4,4 @@ import { ThemeParams } from './ThemeParams.js';
4
4
  * @returns A new initialized instance of the `ThemeParams` class.
5
5
  * @see ThemeParams
6
6
  */
7
- export declare const initThemeParams: import('../../index.js').InitComponentFn<"themeParams", ThemeParams, import('./types.js').ThemeParamsParsed>;
7
+ export declare const initThemeParams: import('../../index.js').InitDynamicComponentFn<ThemeParams>;
@@ -4,4 +4,4 @@ import { Utils } from './Utils.js';
4
4
  * @returns A new initialized instance of the `Utils` class.
5
5
  * @see Utils
6
6
  */
7
- export declare const initUtils: import('../../index.js').InitComponentFn<"version", Utils, never>;
7
+ export declare const initUtils: import('../../index.js').InitStaticComponentFn<Utils>;
@@ -1,8 +1,7 @@
1
1
  import { Viewport } from './Viewport.js';
2
- import { ViewportState } from './types.js';
3
2
 
4
3
  /**
5
4
  * @returns A promise with a new initialized instance of the `Viewport` class.
6
5
  * @see Viewport
7
6
  */
8
- export declare const initViewport: import('../../misc/createComponentInitFn/types.js').InitComponentFn<"platform", Promise<Viewport>, ViewportState>;
7
+ export declare const initViewport: import('../../index.js').InitDynamicComponentFn<Promise<Viewport>>;
@@ -118,7 +118,7 @@ export * from './launch-params/types.js';
118
118
  /**
119
119
  * Misc.
120
120
  */
121
- export type { FactoryOptions, FactoryStatic, Factory, FactoryDynamic, InitComponentFn, WithOnChange, } from './misc/createComponentInitFn/types.js';
121
+ export type { FactoryOptions, FactoryStatic, Factory, FactoryDynamic, InitComponentFn, InitStaticComponentFn, InitDynamicComponentFn, WithOnChange, } from './misc/createComponentInitFn/types.js';
122
122
  /**
123
123
  * Navigation.
124
124
  */
@@ -0,0 +1,11 @@
1
+ import { CleanupFn } from '../types/index.js';
2
+
3
+ /**
4
+ * Returns a tuple, containing function to add cleanup, call cleanup, and flag showing whether
5
+ * cleanup was called. Cleanup will not be performed in case, it was done before.
6
+ */
7
+ export declare function createCleanup(...fns: CleanupFn[]): [
8
+ add: (fn: CleanupFn) => void,
9
+ call: () => void,
10
+ cleanedUp: boolean
11
+ ];
@@ -1,15 +1,16 @@
1
- import { LaunchParamName } from '../../launch-params/types.js';
2
1
  import { StorageKey, StorageValue } from '../../storage/storage.js';
3
- import { FactoryDynamic, FactoryStatic, InitComponentFn, WithOnChange } from './types.js';
2
+ import { FactoryDynamic, FactoryStatic, InitStaticComponentFn, InitDynamicComponentFn, WithOnChange } from './types.js';
4
3
 
5
4
  /**
6
- * Creates new init function based only on common options.
5
+ * Creates a new init function based on factory, creating a component, not synchronizing its
6
+ * state with the session storage.
7
7
  * @param factory - function creating new component instance.
8
8
  */
9
- export declare function createComponentInitFn<R, LP extends LaunchParamName = never>(factory: FactoryStatic<LP, R>): InitComponentFn<LP, R, never>;
9
+ export declare function createComponentInitFn<R>(factory: FactoryStatic<R>): InitStaticComponentFn<R>;
10
10
  /**
11
- * Creates new init function based on common options and storage data.
11
+ * Creates a new init function based on factory, creating a component, synchronizing its
12
+ * state with the session storage.
12
13
  * @param factory - function creating new component instance.
13
14
  * @param storageKey - storage key to restore component from.
14
15
  */
15
- export declare function createComponentInitFn<SK extends StorageKey, R extends WithOnChange<StorageValue<SK>> | Promise<WithOnChange<StorageValue<SK>>>, LP extends LaunchParamName = never>(storageKey: SK, factory: FactoryDynamic<LP, R, SK>): InitComponentFn<LP, R, StorageValue<SK>>;
16
+ export declare function createComponentInitFn<SK extends StorageKey, R extends WithOnChange<StorageValue<SK>> | Promise<WithOnChange<StorageValue<SK>>>>(storageKey: SK, factory: FactoryDynamic<R, SK>): InitDynamicComponentFn<R>;
@@ -1,46 +1,54 @@
1
1
  import { PostEvent } from '../../bridge/methods/postEvent.js';
2
- import { LaunchParamName, PickLaunchParams } from '../../launch-params/types.js';
2
+ import { LaunchParams } from '../../launch-params/types.js';
3
3
  import { CreateRequestIdFn } from '../../request-id/types.js';
4
4
  import { StorageKey, StorageValue } from '../../storage/storage.js';
5
- import { If, IsNever } from '../../types/index.js';
5
+ import { CleanupFn, If, IsNever } from '../../types/index.js';
6
+ import { RemoveEventListenerFn } from '../../events/types.js';
6
7
 
7
8
  /**
8
9
  * Object, which supports tracking its state changes.
9
10
  */
10
11
  export interface WithOnChange<State> {
11
- on(event: 'change', listener: (state: State) => void): void;
12
+ on(event: 'change', listener: (state: State) => void): RemoveEventListenerFn;
12
13
  }
13
- type WithState<State> = If<IsNever<State>, {}, {
14
- state?: State;
15
- }>;
16
14
  /**
17
15
  * Options passed to a component factory.
18
16
  */
19
- export type FactoryOptions<LP extends LaunchParamName, State> = PickLaunchParams<LP> & WithState<State> & {
17
+ export type FactoryOptions<State, SideEffects extends boolean> = LaunchParams & {
20
18
  postEvent: PostEvent;
21
19
  createRequestId: CreateRequestIdFn;
22
- };
23
- export interface Factory<LP extends LaunchParamName, R, State> {
20
+ } & If<IsNever<State>, {}, {
21
+ state?: State;
22
+ }> & If<SideEffects, {
23
+ addCleanup(fn: CleanupFn): void;
24
+ }, {}>;
25
+ export interface Factory<Result, SideEffects extends boolean, State> {
24
26
  /**
25
27
  * Creates a new component instance.
26
28
  * @param options - factory options.
27
29
  */
28
- (options: FactoryOptions<LP, State>): R;
30
+ (options: FactoryOptions<State, SideEffects>): Result;
29
31
  }
30
- export interface FactoryStatic<LP extends LaunchParamName, R> extends Factory<LP, R, never> {
32
+ export interface FactoryStatic<Result> extends Factory<Result, false, never> {
31
33
  }
32
- export interface FactoryDynamic<LP extends LaunchParamName, R, SK extends StorageKey> extends Factory<LP, R, StorageValue<SK>> {
34
+ export interface FactoryDynamic<Result, SK extends StorageKey> extends Factory<Result, true, StorageValue<SK>> {
33
35
  }
34
- export interface InitComponentFn<LP extends LaunchParamName, Result, State> {
36
+ export interface InitComponentFn<Result, SideEffects extends boolean> {
35
37
  /**
36
- * Initializes new component instance.
37
- * @param options - initialization options.
38
+ * Initializes a new static component instance.
38
39
  */
39
- (options?: {
40
+ (): If<SideEffects, [
41
+ /**
42
+ * Execution result.
43
+ */
44
+ result: Result,
40
45
  /**
41
- * Options, applicable only to SSR mode.
46
+ * Cleanup function.
42
47
  */
43
- ssr?: Partial<PickLaunchParams<LP>> & WithState<State>;
44
- }): Result;
48
+ cleanup: CleanupFn
49
+ ], Result>;
50
+ }
51
+ export interface InitStaticComponentFn<Result> extends InitComponentFn<Result, false> {
52
+ }
53
+ export interface InitDynamicComponentFn<Result> extends InitComponentFn<Result, true> {
45
54
  }
46
- export {};
@@ -5,7 +5,7 @@ type Emitter<State> = EventEmitter<BrowserNavigatorEvents<State>>;
5
5
  export declare class BrowserNavigator<State = {}> {
6
6
  private readonly navigator;
7
7
  private readonly ee;
8
- readonly hashMode?: BrowserNavigatorHashMode;
8
+ readonly hashMode: BrowserNavigatorHashMode | null;
9
9
  readonly base: string;
10
10
  constructor(
11
11
  /**
@@ -13,9 +13,10 @@ export interface BrowserNavigatorConOptions {
13
13
  */
14
14
  base?: string;
15
15
  /**
16
- * Hash navigation mode. Omit, if non-hash mode is required.
16
+ * Hash navigation mode. Pass null to enable MPA (Multi Page Application) navigation.
17
+ * @default 'classic'
17
18
  */
18
- hashMode?: BrowserNavigatorHashMode;
19
+ hashMode?: BrowserNavigatorHashMode | null;
19
20
  /**
20
21
  * Function to call Mini Apps methods.
21
22
  * @default Global postEvent function.
@@ -32,12 +33,12 @@ export interface BrowserNavigatorHistoryItem<State> extends URLLike {
32
33
  export type BrowserNavigatorAnyHistoryItem<State> = string | Partial<BrowserNavigatorHistoryItem<State>>;
33
34
  /**
34
35
  * Hash navigation mode.
35
- * @example 'default'
36
+ * @example 'classic'
36
37
  * '#pathname?search'
37
38
  * @example 'slash'
38
39
  * '#/pathname?search'
39
40
  */
40
- export type BrowserNavigatorHashMode = 'default' | 'slash';
41
+ export type BrowserNavigatorHashMode = 'classic' | 'slash';
41
42
  /**
42
43
  * Events supported by `BrowserNavigator`.
43
44
  */