@tma.js/sdk-solid 0.1.19 → 1.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 (69) hide show
  1. package/dist/dts/createHook.d.ts +14 -0
  2. package/dist/dts/index.d.ts +3 -6
  3. package/dist/dts/{context.d.ts → provider/SDKContext.d.ts} +1 -1
  4. package/dist/dts/provider/SDKProvider.d.ts +2 -0
  5. package/dist/dts/provider/index.d.ts +6 -0
  6. package/dist/dts/provider/types.d.ts +22 -0
  7. package/dist/dts/provider/useInitResultDynamicValue.d.ts +3 -0
  8. package/dist/dts/provider/useInitResultValue.d.ts +8 -0
  9. package/dist/dts/{hooks.d.ts → provider/useSDKContext.d.ts} +1 -1
  10. package/dist/dts/tools/back-button.d.ts +4 -0
  11. package/dist/dts/tools/closing-behavior.d.ts +4 -0
  12. package/dist/dts/tools/cloud-storage.d.ts +4 -0
  13. package/dist/dts/tools/haptic-feedback.d.ts +4 -0
  14. package/dist/dts/tools/index.d.ts +16 -0
  15. package/dist/dts/tools/init-data-raw.d.ts +4 -0
  16. package/dist/dts/tools/init-data.d.ts +4 -0
  17. package/dist/dts/tools/invoice.d.ts +4 -0
  18. package/dist/dts/tools/launch-params.d.ts +5 -0
  19. package/dist/dts/tools/main-button.d.ts +4 -0
  20. package/dist/dts/tools/mini-app.d.ts +4 -0
  21. package/dist/dts/tools/popup.d.ts +4 -0
  22. package/dist/dts/tools/post-event.d.ts +4 -0
  23. package/dist/dts/tools/qr-scanner.d.ts +4 -0
  24. package/dist/dts/tools/theme-params.d.ts +4 -0
  25. package/dist/dts/tools/utils.d.ts +4 -0
  26. package/dist/dts/tools/viewport.d.ts +4 -0
  27. package/dist/dts/types.d.ts +22 -10
  28. package/dist/index.cjs +1 -1
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.mjs +59 -67
  31. package/dist/index.mjs.map +1 -1
  32. package/package.json +5 -5
  33. package/src/createHook.ts +33 -0
  34. package/src/index.ts +28 -6
  35. package/src/provider/SDKContext.ts +9 -0
  36. package/src/provider/SDKProvider.tsx +21 -0
  37. package/src/provider/index.ts +6 -0
  38. package/src/provider/types.ts +26 -0
  39. package/src/provider/useInitResultDynamicValue.ts +34 -0
  40. package/src/provider/useInitResultValue.ts +23 -0
  41. package/src/provider/useSDKContext.ts +11 -0
  42. package/src/tools/back-button.ts +6 -0
  43. package/src/tools/closing-behavior.ts +6 -0
  44. package/src/tools/cloud-storage.ts +6 -0
  45. package/src/tools/haptic-feedback.ts +6 -0
  46. package/src/tools/index.ts +16 -0
  47. package/src/tools/init-data-raw.ts +6 -0
  48. package/src/tools/init-data.ts +6 -0
  49. package/src/tools/invoice.ts +6 -0
  50. package/src/tools/launch-params.tsx +8 -0
  51. package/src/tools/main-button.ts +6 -0
  52. package/src/tools/mini-app.ts +6 -0
  53. package/src/tools/popup.ts +6 -0
  54. package/src/tools/post-event.ts +6 -0
  55. package/src/tools/qr-scanner.ts +6 -0
  56. package/src/tools/theme-params.ts +6 -0
  57. package/src/tools/utils.ts +6 -0
  58. package/src/tools/viewport.ts +6 -0
  59. package/src/types.ts +29 -13
  60. package/dist/dts/SDKProvider.d.ts +0 -6
  61. package/dist/dts/useDynamicInitResultValue.d.ts +0 -21
  62. package/dist/dts/useInitResultValue.d.ts +0 -8
  63. package/dist/dts/useSDK.d.ts +0 -9
  64. package/src/SDKProvider.tsx +0 -24
  65. package/src/context.ts +0 -5
  66. package/src/hooks.ts +0 -16
  67. package/src/useDynamicInitResultValue.ts +0 -52
  68. package/src/useInitResultValue.ts +0 -14
  69. package/src/useSDK.ts +0 -54
@@ -0,0 +1,33 @@
1
+ import type { Accessor } from 'solid-js';
2
+
3
+ import { useInitResultDynamicValue, useInitResultValue } from './provider/index.js';
4
+ import type {
5
+ DynamicInitResultKey,
6
+ InitResultKey,
7
+ InitResultValue,
8
+ StaticInitResultKey,
9
+ } from './types.js';
10
+
11
+ export type Hook<K extends InitResultKey> = () => Accessor<InitResultValue<K>>;
12
+
13
+ /**
14
+ * Creates hook to retrieve static init result value.
15
+ * @param initResultKey - init result key.
16
+ */
17
+ export function createHook<K extends StaticInitResultKey>(initResultKey: K): Hook<K>;
18
+
19
+ /**
20
+ * Creates hook to retrieve dynamic init result value.
21
+ * @param initResultKey - init result key.
22
+ * @param dynamic - flag to let function know this init result value is dynamic.
23
+ */
24
+ export function createHook<K extends DynamicInitResultKey>(
25
+ initResultKey: K,
26
+ dynamic: true,
27
+ ): Hook<K>;
28
+
29
+ export function createHook(initResultKey: InitResultKey, dynamic?: true): Hook<any> {
30
+ return dynamic
31
+ ? () => useInitResultDynamicValue(initResultKey as DynamicInitResultKey)
32
+ : () => useInitResultValue(initResultKey);
33
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,28 @@
1
- export * from './context.js';
2
- export * from './hooks.js';
3
- export * from './SDKProvider.jsx';
4
- export * from './types.js';
5
- export * from './useSDK.js';
6
- export { classNames, mergeClassNames } from '@tma.js/utils';
1
+ export {
2
+ useThemeParams,
3
+ useInitData,
4
+ useBackButton,
5
+ useClosingBehavior,
6
+ useInitDataRaw,
7
+ useCloudStorage,
8
+ useLaunchParams,
9
+ useMainButton,
10
+ useHapticFeedback,
11
+ useInvoice,
12
+ useMiniApp,
13
+ usePopup,
14
+ usePostEvent,
15
+ useQRScanner,
16
+ useUtils,
17
+ useViewport,
18
+ } from './tools/index.js';
19
+ export {
20
+ useSDKContext,
21
+ SDKProvider,
22
+ type SDKProviderProps,
23
+ type SDKContextType,
24
+ } from './provider/index.js';
25
+ export type {
26
+ InitOptions,
27
+ InitResult,
28
+ } from './types.js';
@@ -0,0 +1,9 @@
1
+ import { createContext } from 'solid-js';
2
+
3
+ import type { SDKContextType } from './types.js';
4
+
5
+ export const SDKContext = createContext<SDKContextType>({
6
+ loading: () => false,
7
+ error: () => undefined,
8
+ initResult: () => undefined,
9
+ }, { name: 'SDKContext' });
@@ -0,0 +1,21 @@
1
+ import { init } from '@tma.js/sdk';
2
+ import { createMemo, createResource } from 'solid-js';
3
+
4
+ import { SDKContext } from './SDKContext.js';
5
+ import type { SDKProviderProps } from './types.js';
6
+
7
+ export function SDKProvider(props: SDKProviderProps) {
8
+ const [data] = createResource(() => props.options, init);
9
+
10
+ const initResult = createMemo(() => {
11
+ return data.state === 'ready' ? data() : undefined;
12
+ });
13
+ const loading = createMemo(() => data.loading);
14
+ const error = createMemo(() => (data.error === undefined ? null : data.error));
15
+
16
+ return (
17
+ <SDKContext.Provider value={{ initResult, loading, error }}>
18
+ {props.children}
19
+ </SDKContext.Provider>
20
+ );
21
+ }
@@ -0,0 +1,6 @@
1
+ export * from './SDKContext.js';
2
+ export * from './SDKProvider.js';
3
+ export * from './types.js';
4
+ export * from './useSDKContext.js';
5
+ export * from './useInitResultDynamicValue.js';
6
+ export * from './useInitResultValue.js';
@@ -0,0 +1,26 @@
1
+ import type { InitOptions, InitResult } from '@tma.js/sdk';
2
+ import type { Accessor, ParentProps } from 'solid-js';
3
+
4
+ export type SDKProviderProps = ParentProps<{
5
+ /**
6
+ * Init function options.
7
+ */
8
+ options?: InitOptions;
9
+ }>;
10
+
11
+ export interface SDKContextType {
12
+ /**
13
+ * Error occurred during initialization.
14
+ */
15
+ error: Accessor<unknown | undefined>;
16
+
17
+ /**
18
+ * Initialization result.
19
+ */
20
+ initResult: Accessor<InitResult | undefined>;
21
+
22
+ /**
23
+ * True if SDK is loading.
24
+ */
25
+ loading: Accessor<boolean>;
26
+ }
@@ -0,0 +1,34 @@
1
+ import {
2
+ type Accessor,
3
+ createEffect,
4
+ createSignal,
5
+ onCleanup,
6
+ } from 'solid-js';
7
+
8
+ import { useInitResultValue } from './useInitResultValue.js';
9
+ import type {
10
+ DynamicInitResultKey,
11
+ InitResultValue,
12
+ Trackable,
13
+ } from '../types.js';
14
+
15
+ export function useInitResultDynamicValue<K extends DynamicInitResultKey>(
16
+ initResultKey: K,
17
+ ): Accessor<InitResultValue<K>> {
18
+ // Extract init result value.
19
+ const initial = useInitResultValue(initResultKey);
20
+
21
+ // Create value which will be returned to the external environment.
22
+ const [signal, setSignal] = createSignal(initial(), { equals: false });
23
+
24
+ // Effect which listens to change event and calls update.
25
+ createEffect(() => {
26
+ onCleanup(
27
+ (initial() as Trackable).on('change', () => {
28
+ setSignal((prev) => prev);
29
+ }),
30
+ );
31
+ });
32
+
33
+ return signal;
34
+ }
@@ -0,0 +1,23 @@
1
+ import { type Accessor, createMemo } from 'solid-js';
2
+
3
+ import { useSDKContext } from './useSDKContext.js';
4
+ import type { InitResultKey, InitResultValue } from '../types.js';
5
+
6
+ /**
7
+ * Returns value by its field name from SDK init result.
8
+ * @param key - init result key.
9
+ * @throws {Error} SDK is not initialized.
10
+ */
11
+ export function useInitResultValue<K extends InitResultKey>(key: K): Accessor<InitResultValue<K>> {
12
+ const { initResult } = useSDKContext();
13
+
14
+ // eslint-disable-next-line solid/reactivity
15
+ return createMemo<InitResultValue<K>>(() => {
16
+ const result = initResult();
17
+
18
+ if (!result) {
19
+ throw new Error(`Unable to get init result key "${key}" as long as SDK is not initialized`);
20
+ }
21
+ return result[key];
22
+ });
23
+ }
@@ -0,0 +1,11 @@
1
+ import { useContext } from 'solid-js';
2
+
3
+ import { SDKContext } from './SDKContext.js';
4
+ import type { SDKContextType } from './types.js';
5
+
6
+ /**
7
+ * Returns current SDK information.
8
+ */
9
+ export function useSDKContext(): SDKContextType {
10
+ return useContext(SDKContext);
11
+ }
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve BackButton component.
5
+ */
6
+ export const useBackButton = createHook('backButton', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve ClosingBehavior component.
5
+ */
6
+ export const useClosingBehavior = createHook('closingBehavior', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve CloudStorage component.
5
+ */
6
+ export const useCloudStorage = createHook('cloudStorage');
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve HapticFeedback component.
5
+ */
6
+ export const useHapticFeedback = createHook('hapticFeedback');
@@ -0,0 +1,16 @@
1
+ export * from './back-button.js';
2
+ export * from './closing-behavior.js';
3
+ export * from './cloud-storage.js';
4
+ export * from './haptic-feedback.js';
5
+ export * from './init-data.js';
6
+ export * from './init-data-raw.js';
7
+ export * from './invoice.js';
8
+ export * from './launch-params.js';
9
+ export * from './main-button.js';
10
+ export * from './mini-app.js';
11
+ export * from './popup.js';
12
+ export * from './post-event.js';
13
+ export * from './qr-scanner.js';
14
+ export * from './theme-params.js';
15
+ export * from './viewport.js';
16
+ export * from './utils.js';
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve init data raw representation.
5
+ */
6
+ export const useInitDataRaw = createHook('initDataRaw');
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve InitData component.
5
+ */
6
+ export const useInitData = createHook('initData');
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve Invoice component.
5
+ */
6
+ export const useInvoice = createHook('invoice', true);
@@ -0,0 +1,8 @@
1
+ import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';
2
+
3
+ /**
4
+ * Hooks to retrieve launch parameters.
5
+ */
6
+ export function useLaunchParams(): LaunchParams {
7
+ return retrieveLaunchData().launchParams;
8
+ }
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve MainButton component.
5
+ */
6
+ export const useMainButton = createHook('mainButton', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve MiniApp component.
5
+ */
6
+ export const useMiniApp = createHook('miniApp', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve Popup component.
5
+ */
6
+ export const usePopup = createHook('popup', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve postEvent function.
5
+ */
6
+ export const usePostEvent = createHook('postEvent');
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve QRScanner component.
5
+ */
6
+ export const useQRScanner = createHook('qrScanner', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve ThemeParams component.
5
+ */
6
+ export const useThemeParams = createHook('themeParams', true);
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve Utils component.
5
+ */
6
+ export const useUtils = createHook('utils');
@@ -0,0 +1,6 @@
1
+ import { createHook } from '../createHook.js';
2
+
3
+ /**
4
+ * Hook to retrieve Viewport component.
5
+ */
6
+ export const useViewport = createHook('viewport', true);
package/src/types.ts CHANGED
@@ -1,13 +1,29 @@
1
- import type { InitResult, InitOptions } from '@tma.js/sdk';
2
- import type { Accessor } from 'solid-js';
3
-
4
- export type SDKInitOptions = InitOptions;
5
- export type SDKInitResult = InitResult;
6
- export type SDKInitResultKey = keyof SDKInitResult;
7
- export type SDKInitResultValue<K extends SDKInitResultKey> = SDKInitResult[K];
8
-
9
- export interface SDKContextType {
10
- initResult: Accessor<SDKInitResult | null>;
11
- loading: Accessor<boolean>;
12
- error: Accessor<unknown | null>;
13
- }
1
+ import type { InitOptions, InitResult } from '@tma.js/sdk';
2
+
3
+ export { InitOptions, InitResult };
4
+
5
+ export interface Trackable {
6
+ on(event: 'change', listener: () => void): () => void;
7
+ }
8
+
9
+ /**
10
+ * SDK init result keys, which describe values with trackable changes.
11
+ */
12
+ export type DynamicInitResultKey = {
13
+ [K in InitResultKey]-?: InitResultValue<K> extends Trackable ? K : never;
14
+ }[InitResultKey];
15
+
16
+ /**
17
+ * SDK init result keys, which describe static values.
18
+ */
19
+ export type StaticInitResultKey = Exclude<InitResultKey, DynamicInitResultKey>;
20
+
21
+ /**
22
+ * SDK initialization result key.
23
+ */
24
+ export type InitResultKey = keyof InitResult;
25
+
26
+ /**
27
+ * Returns SDK init result value type by its key.
28
+ */
29
+ export type InitResultValue<K extends InitResultKey> = InitResult[K];
@@ -1,6 +0,0 @@
1
- import { type ParentProps } from 'solid-js';
2
- import type { SDKInitOptions } from './types.js';
3
- export type SDKProviderProps = ParentProps<{
4
- initOptions?: SDKInitOptions;
5
- }>;
6
- export declare function SDKProvider(props: SDKProviderProps): any;
@@ -1,21 +0,0 @@
1
- import { type Accessor } from 'solid-js';
2
- import type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';
3
- interface Trackable {
4
- on: (event: any, ...args: any[]) => void;
5
- off: (event: any, ...args: any[]) => void;
6
- }
7
- type EventName<T extends Trackable> = T extends {
8
- on(event: infer E, ...args: any[]): any;
9
- } ? E : never;
10
- type DynamicComponentKey = {
11
- [K in SDKInitResultKey]: SDKInitResultValue<K> extends Trackable ? K : never;
12
- }[SDKInitResultKey];
13
- /**
14
- * Extracts value from the SDK init result by specified key and listens to its all specified
15
- * events, making the returned value to update.
16
- * @param initResult - SDK init result.
17
- * @param key - SDK init result key.
18
- * @param events - tracked events list.
19
- */
20
- export declare function useDynamicInitResultValue<K extends DynamicComponentKey>(initResult: Accessor<SDKInitResult>, key: K, events: EventName<SDKInitResultValue<K>>[]): Accessor<SDKInitResultValue<K>>;
21
- export {};
@@ -1,8 +0,0 @@
1
- import { type Accessor } from 'solid-js';
2
- import type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';
3
- /**
4
- * Extracts value from the SDK init result by key.
5
- * @param result - init result accessor.
6
- * @param key - key to extract.
7
- */
8
- export declare function useInitResultValue<K extends SDKInitResultKey>(result: Accessor<SDKInitResult>, key: K): Accessor<SDKInitResultValue<K>>;
@@ -1,9 +0,0 @@
1
- import { type Accessor } from 'solid-js';
2
- import type { SDKInitResultKey, SDKInitResultValue } from './types.js';
3
- export type SDK = {
4
- [K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>;
5
- };
6
- /**
7
- * Returns ready to use SDK components.
8
- */
9
- export declare function useSDK(): SDK;
@@ -1,24 +0,0 @@
1
- import { createMemo, createResource, type ParentProps } from 'solid-js';
2
- import { init } from '@tma.js/sdk';
3
-
4
- import { SDKContext } from './context.js';
5
- import type { SDKInitOptions } from './types.js';
6
-
7
- export type SDKProviderProps = ParentProps<{ initOptions?: SDKInitOptions }>;
8
-
9
- export function SDKProvider(props: SDKProviderProps) {
10
- const initOptions = createMemo(() => props.initOptions || {});
11
- const [data] = createResource(initOptions, init);
12
-
13
- const initResult = createMemo(() => {
14
- return data.state === 'ready' ? data() : null;
15
- });
16
- const loading = createMemo(() => data.loading);
17
- const error = createMemo(() => data.error === undefined ? null : data.error);
18
-
19
- return (
20
- <SDKContext.Provider value={{ initResult, loading, error }}>
21
- {props.children}
22
- </SDKContext.Provider>
23
- );
24
- }
package/src/context.ts DELETED
@@ -1,5 +0,0 @@
1
- import { createContext } from 'solid-js';
2
-
3
- import type { SDKContextType } from './types.js';
4
-
5
- export const SDKContext = createContext<SDKContextType>(undefined, { name: 'SDKContext' });
package/src/hooks.ts DELETED
@@ -1,16 +0,0 @@
1
- import { useContext } from 'solid-js';
2
-
3
- import { SDKContext } from './context.js';
4
- import type { SDKContextType } from './types.js';
5
-
6
- /**
7
- * Uses SDKContext context.
8
- */
9
- export function useSDKContext(): SDKContextType {
10
- const context = useContext(SDKContext);
11
-
12
- if (context === undefined) {
13
- throw new Error('useSDKContext hook was used outside of SDKProvider.');
14
- }
15
- return context;
16
- }
@@ -1,52 +0,0 @@
1
- import { createEffect, createSignal, onCleanup, type Accessor } from 'solid-js';
2
- import { useInitResultValue } from './useInitResultValue.js';
3
- import type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';
4
-
5
- interface Trackable {
6
- on: (event: any, ...args: any[]) => void;
7
- off: (event: any, ...args: any[]) => void;
8
- }
9
-
10
- type EventName<T extends Trackable> = T extends {
11
- on(event: infer E, ...args: any[]): any
12
- } ? E : never;
13
-
14
- type DynamicComponentKey = {
15
- [K in SDKInitResultKey]: SDKInitResultValue<K> extends Trackable ? K : never;
16
- }[SDKInitResultKey];
17
-
18
- /**
19
- * Extracts value from the SDK init result by specified key and listens to its all specified
20
- * events, making the returned value to update.
21
- * @param initResult - SDK init result.
22
- * @param key - SDK init result key.
23
- * @param events - tracked events list.
24
- */
25
- export function useDynamicInitResultValue<K extends DynamicComponentKey>(
26
- initResult: Accessor<SDKInitResult>,
27
- key: K,
28
- events: EventName<SDKInitResultValue<K>>[],
29
- ): Accessor<SDKInitResultValue<K>> {
30
- // Get original value from init result.
31
- const initResultValue = useInitResultValue(initResult, key);
32
-
33
- // Here we store value, which should always update it in case, some of its props
34
- // were changed/
35
- const [value, setValue] = createSignal(initResultValue(), { equals: false });
36
-
37
- createEffect(() => {
38
- const value = initResultValue();
39
- const listener = () => {
40
- // We use prev => prev on purpose. This will make Solid sure, something inside
41
- // dynamic value changed.
42
- setValue(prev => prev);
43
- };
44
-
45
- events.forEach(event => value.on(event, listener));
46
- onCleanup(() => {
47
- events.forEach(event => value.off(event, listener));
48
- });
49
- });
50
-
51
- return value;
52
- }
@@ -1,14 +0,0 @@
1
- import { createMemo, type Accessor } from 'solid-js';
2
- import type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';
3
-
4
- /**
5
- * Extracts value from the SDK init result by key.
6
- * @param result - init result accessor.
7
- * @param key - key to extract.
8
- */
9
- export function useInitResultValue<K extends SDKInitResultKey>(
10
- result: Accessor<SDKInitResult>,
11
- key: K
12
- ): Accessor<SDKInitResultValue<K>> {
13
- return createMemo(() => result()[key]);
14
- }
package/src/useSDK.ts DELETED
@@ -1,54 +0,0 @@
1
- import { createMemo, type Accessor } from 'solid-js';
2
-
3
- import { useSDKContext } from './hooks.js';
4
- import { useInitResultValue } from './useInitResultValue.js';
5
- import { useDynamicInitResultValue } from './useDynamicInitResultValue.js';
6
- import type { SDKInitResultKey, SDKInitResultValue } from './types.js';
7
-
8
- export type SDK = {
9
- [K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>
10
- };
11
-
12
- /**
13
- * Returns ready to use SDK components.
14
- */
15
- export function useSDK(): SDK {
16
- const { initResult } = useSDKContext();
17
-
18
- const sdk = createMemo(() => {
19
- const result = initResult();
20
-
21
- if (result === null) {
22
- throw new Error('Unable to use SDK as it is not ready.');
23
- }
24
- return result;
25
- });
26
-
27
- return {
28
- backButton: useDynamicInitResultValue(sdk, 'backButton', ['isVisibleChanged']),
29
- closingBehavior: useDynamicInitResultValue(sdk, 'closingBehavior', ['isConfirmationNeededChanged']),
30
- cloudStorage: useInitResultValue(sdk, 'cloudStorage'),
31
- haptic: useInitResultValue(sdk, 'haptic'),
32
- initData: useInitResultValue(sdk, 'initData'),
33
- initDataRaw: useInitResultValue(sdk, 'initDataRaw'),
34
- mainButton: useDynamicInitResultValue(sdk, 'mainButton', [
35
- 'backgroundColorChanged',
36
- 'isVisibleChanged',
37
- 'isProgressVisibleChanged',
38
- 'isEnabledChanged',
39
- 'textChanged',
40
- 'textColorChanged',
41
- ]),
42
- popup: useDynamicInitResultValue(sdk, 'popup', ['isOpenedChanged']),
43
- postEvent: useInitResultValue(sdk, 'postEvent'),
44
- qrScanner: useDynamicInitResultValue(sdk, 'qrScanner', ['isOpenedChanged']),
45
- themeParams: useDynamicInitResultValue(sdk, 'themeParams', ['changed']),
46
- viewport: useDynamicInitResultValue(sdk, 'viewport', [
47
- 'heightChanged',
48
- 'isExpandedChanged',
49
- 'stableHeightChanged',
50
- 'widthChanged',
51
- ]),
52
- webApp: useDynamicInitResultValue(sdk, 'webApp', ['backgroundColorChanged', 'headerColorChanged']),
53
- };
54
- }