@vbotma/bridge 2.2.4

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 (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -0
  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/isVBMA.d.ts +31 -0
  8. package/dist/dts/env/mockVBotEnv.d.ts +60 -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 -0
  13. package/dist/dts/events/types/events.d.ts +780 -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 +53 -0
  18. package/dist/dts/index.d.ts +26 -0
  19. package/dist/dts/launch-params.d.ts +31 -0
  20. package/dist/dts/methods/captureSameReq.d.ts +10 -0
  21. package/dist/dts/methods/createPostEvent.d.ts +30 -0
  22. package/dist/dts/methods/getReleaseVersion.d.ts +15 -0
  23. package/dist/dts/methods/postEvent.d.ts +35 -0
  24. package/dist/dts/methods/postMessage.d.ts +7 -0
  25. package/dist/dts/methods/supports.d.ts +15 -0
  26. package/dist/dts/methods/types/custom-method.d.ts +59 -0
  27. package/dist/dts/methods/types/haptic-feedback.d.ts +40 -0
  28. package/dist/dts/methods/types/index.d.ts +7 -0
  29. package/dist/dts/methods/types/methods.d.ts +807 -0
  30. package/dist/dts/methods/types/misc.d.ts +27 -0
  31. package/dist/dts/methods/types/notification.d.ts +20 -0
  32. package/dist/dts/methods/types/popup.d.ts +49 -0
  33. package/dist/dts/methods/types/utils.d.ts +9 -0
  34. package/dist/dts/obj-prop-helpers.d.ts +39 -0
  35. package/dist/dts/start-param.d.ts +52 -0
  36. package/dist/dts/utils/compareVersions.d.ts +10 -0
  37. package/dist/dts/utils/invokeCustomMethod.d.ts +34 -0
  38. package/dist/dts/utils/request.d.ts +102 -0
  39. package/dist/dts/utils/request2.d.ts +62 -0
  40. package/dist/index.cjs +4 -0
  41. package/dist/index.cjs.map +1 -0
  42. package/dist/index.iife.js +4 -0
  43. package/dist/index.iife.js.map +1 -0
  44. package/dist/index.js +758 -0
  45. package/dist/index.js.map +1 -0
  46. package/package.json +54 -0
@@ -0,0 +1,3 @@
1
+ export type * from './events.js';
2
+ export type * from './listening.js';
3
+ export type * from './misc.js';
@@ -0,0 +1,5 @@
1
+ import { Handler } from 'mitt';
2
+ import { WildcardHandler } from '../createEmitter.js';
3
+ import { EventName, EventPayload, Events } from './index.js';
4
+ export type EventListener<E extends EventName> = Handler<EventPayload<E>>;
5
+ export type SubscribeListener = WildcardHandler<Events>;
@@ -0,0 +1,16 @@
1
+ export type InvoiceStatus = 'paid' | 'failed' | 'pending' | 'cancelled' | string;
2
+ export type PhoneRequestedStatus = 'sent' | 'cancelled' | string;
3
+ export type EmojiStatusAccessRequestedStatus = 'allowed' | string;
4
+ export type EmojiStatusFailedError = 'SUGGESTED_EMOJI_INVALID' | 'USER_DECLINED' | string;
5
+ export type WriteAccessRequestedStatus = 'allowed' | string;
6
+ export type BiometryType = 'finger' | 'face' | string;
7
+ export type BiometryTokenUpdateStatus = 'updated' | 'removed' | 'failed' | string;
8
+ export type BiometryAuthRequestStatus = 'failed' | 'authorized' | string;
9
+ export type FullScreenErrorStatus = 'ALREADY_FULLSCREEN' | 'UNSUPPORTED' | string;
10
+ export interface SafeAreaInsets {
11
+ top: number;
12
+ bottom: number;
13
+ left: number;
14
+ right: number;
15
+ }
16
+ export type HomeScreenStatus = 'unsupported' | 'unknown' | 'added' | 'missed' | string;
@@ -0,0 +1,53 @@
1
+ import { Logger } from '@vbotma/toolkit';
2
+ /**
3
+ * The current debug mode state.
4
+ *
5
+ * To update the value, use the `setDebug` function.
6
+ * @see setDebug
7
+ */
8
+ export declare const debug: import('@vbotma/signals').Computed<boolean>;
9
+ /**
10
+ * Sets the package debug mode.
11
+ *
12
+ * Enabling debug mode leads to printing additional messages in the console related to the
13
+ * processes inside the package.
14
+ * @param value - enable debug mode.
15
+ */
16
+ export declare function setDebug(value: boolean): void;
17
+ /**
18
+ * The current target origin used by the `postEvent` method.
19
+ *
20
+ * You don't need to override this value until you know what you are doing.
21
+ * To update the value, use the `setTargetOrigin` function.
22
+ * @default 'https://web.telegram.org'
23
+ * @see setTargetOrigin
24
+ */
25
+ export declare const targetOrigin: import('@vbotma/signals').Computed<string>;
26
+ /**
27
+ * Sets a new target origin that is being used when calling the `postEvent` function in Telegram
28
+ * web versions.
29
+ *
30
+ * You don't need to override this value until you know what you are doing.
31
+ * @param origin - allowed target origin value.
32
+ * @see _targetOrigin
33
+ */
34
+ export declare function setTargetOrigin(origin: string): void;
35
+ /**
36
+ * Signal containing a custom implementation of the method to post a message to the parent
37
+ * window. We usually use it to send a message in web versions of Telegram.
38
+ *
39
+ * @default A function behaving like the `window.parent.postMessage` method.
40
+ */
41
+ export declare const postMessageImpl: import('@vbotma/signals').Signal<{
42
+ (message: any, targetOrigin: string, transfer?: Transferable[]): void;
43
+ (message: any, options?: WindowPostMessageOptions): void;
44
+ }>;
45
+ /**
46
+ * The package logger. You can override this value in order to use your own implementation.
47
+ */
48
+ export declare const logger: import('@vbotma/signals').Signal<Logger>;
49
+ /**
50
+ * Resets the package global values. Normally, you don't use this function in your application.
51
+ * We are using it only for test purposes.
52
+ */
53
+ export declare function resetGlobals(): void;
@@ -0,0 +1,26 @@
1
+ export { hasWebviewProxy } from './env/hasWebviewProxy.js';
2
+ export { isIframe } from './env/isIframe.js';
3
+ export { isVBMA, isVBMAFp, type isVBMAError } from './env/isVBMA.js';
4
+ export { mockVBotEnv } from './env/mockVBotEnv.js';
5
+ export { off, offAll, on } from './events/emitter.js';
6
+ export { emitEvent } from './events/emitEvent.js';
7
+ export type { EventListener, SubscribeListener, Events, WriteAccessRequestedStatus, EmojiStatusAccessRequestedStatus, BiometryAuthRequestStatus, BiometryTokenUpdateStatus, BiometryType, EmojiStatusFailedError, EventName, EventPayload, EventWithoutPayload, EventWithPayload, PhoneRequestedStatus, FullScreenErrorStatus, HomeScreenStatus, InvoiceStatus, SafeAreaInsets, } from './events/types/index.js';
8
+ export { captureSameReq } from './methods/captureSameReq.js';
9
+ export { createPostEvent, type CreatePostEventMode, type OnUnsupportedFn, } from './methods/createPostEvent.js';
10
+ export { getReleaseVersion } from './methods/getReleaseVersion.js';
11
+ export { postEvent, postEventFp, type PostEventFn, type PostEventFpFn, type PostEventError, } from './methods/postEvent.js';
12
+ export { postMessage, type PostMessage } from './methods/postMessage.js';
13
+ export { supports } from './methods/supports.js';
14
+ export type { AnyHapticFeedbackParams, AnyInvokeCustomMethodParams, CreateMethodParams, CustomMethodName, ImpactHapticFeedbackParams, ImpactHapticFeedbackStyle, CustomMethodsParams, CustomMethodParams, MethodName, MethodVersionedParams, NotificationHapticFeedbackParams, NotificationHapticFeedbackType, Methods, SelectionHapticFeedbackParams, MethodNameWithOptionalParams, MethodNameWithoutParams, MethodNameWithRequiredParams, MethodNameWithVersionedParams, MethodParams, PopupParams, BackgroundColor, BottomBarColor, HeaderColorKey, PopupButton, OpenLinkBrowser, SecondaryButtonPosition, SwitchInlineQueryChatType, } from './methods/types/index.js';
15
+ export { compareVersions } from './utils/compareVersions.js';
16
+ export { invokeCustomMethod, invokeCustomMethodFp, type InvokeCustomMethodFn, type InvokeCustomMethodOptions, type InvokeCustomMethodFpFn, type InvokeCustomMethodError, type InvokeCustomMethodFpOptions, } from './utils/invokeCustomMethod.js';
17
+ export { request, requestFp, type RequestCaptureEventFn, type RequestCaptureEventsFn, type RequestCaptureFn, type RequestCaptureFnEventsPayload, type RequestFn, type RequestOptions, type RequestResult, type RequestError, type RequestFpOptions, type RequestFpFn, } from './utils/request.js';
18
+ export { request2, request2Fp, type Request2CaptureEventFn, type Request2CaptureEventsFn, type Request2CaptureFn, type Request2CaptureFnEventsPayload, type Request2Fn, type Request2Options, type Request2Result, type Request2Error, type Request2FpOptions, type Request2FpFn, } from './utils/request2.js';
19
+ export { applyPolyfills } from './applyPolyfills.js';
20
+ export { decodeBase64Url, encodeBase64Url, decodeBase64UrlFp, type DecodeBase64UrlError, } from './base64-url.js';
21
+ export { LaunchParamsRetrieveError, InvalidLaunchParamsError, InvokeCustomMethodFailedError, MethodParameterUnsupportedError, MethodUnsupportedError, UnknownEnvError, } from './errors.js';
22
+ export { setDebug, debug, resetGlobals, postMessageImpl, logger, setTargetOrigin, targetOrigin, } from './globals.js';
23
+ export { retrieveLaunchParams, retrieveRawInitData, retrieveRawInitDataFp, retrieveRawLaunchParams, retrieveRawLaunchParamsFp, retrieveLaunchParamsFp, type RetrieveLaunchParamsError, type RetrieveLaunchParamsResult, type RetrieveRawInitDataError, type RetrieveRawLaunchParamsError, } from './launch-params.js';
24
+ export { createStartParam, createStartParamFp, decodeStartParam, decodeStartParamFp, isSafeToCreateStartParam, } from './start-param.js';
25
+ export { createLogger, deepSnakeToCamelObjKeys, type DeepConvertSnakeKeysToCamelCase, type Logger, type LoggerFn, type LoggerForceFn, type LoggerOptions, type LogLevel, } from '@vbotma/toolkit';
26
+ export { CancelledError, TimeoutError, } from 'better-promises';
@@ -0,0 +1,31 @@
1
+ import { LaunchParamsGenType, ParseLaunchParamsQueryError } from '@vbotma/transformers';
2
+ import { either as E, option as O } from 'fp-ts';
3
+ import { LaunchParamsRetrieveError } from './errors.js';
4
+ export type RetrieveRawInitDataError = RetrieveRawLaunchParamsError;
5
+ export type RetrieveRawLaunchParamsError = LaunchParamsRetrieveError;
6
+ export type RetrieveLaunchParamsError = RetrieveRawLaunchParamsError | ParseLaunchParamsQueryError;
7
+ export type RetrieveLaunchParamsResult = LaunchParamsGenType;
8
+ /**
9
+ * @returns Launch parameters from any known source.
10
+ */
11
+ export declare function retrieveLaunchParamsFp(): E.Either<RetrieveLaunchParamsError, RetrieveLaunchParamsResult>;
12
+ /**
13
+ * @see retrieveLaunchParamsFp
14
+ */
15
+ export declare const retrieveLaunchParams: () => RetrieveLaunchParamsResult;
16
+ /**
17
+ * @returns Raw init data from any known source.
18
+ */
19
+ export declare function retrieveRawInitDataFp(): E.Either<RetrieveRawInitDataError, O.Option<string>>;
20
+ /**
21
+ * @see retrieveRawInitDataFp
22
+ */
23
+ export declare function retrieveRawInitData(): string | undefined;
24
+ /**
25
+ * @returns Launch parameters in a raw format from any known source.
26
+ */
27
+ export declare function retrieveRawLaunchParamsFp(): E.Either<RetrieveRawLaunchParamsError, string>;
28
+ /**
29
+ * @see retrieveRawLaunchParamsFp
30
+ */
31
+ export declare const retrieveRawLaunchParams: (() => string) & {};
@@ -0,0 +1,10 @@
1
+ type CaptureSameReqFn = (payload: {
2
+ req_id: string;
3
+ }) => boolean;
4
+ /**
5
+ * Returns a function which can be used in `request` function `capture` property to capture
6
+ * the event with the same request identifier.
7
+ * @param reqId - request identifier.
8
+ */
9
+ export declare function captureSameReq(reqId: string): CaptureSameReqFn;
10
+ export {};
@@ -0,0 +1,30 @@
1
+ import { Version } from '@vbotma/types';
2
+ import { PostEventFn } from './postEvent.js';
3
+ import { MethodName, MethodNameWithVersionedParams, MethodVersionedParams } from './types/index.js';
4
+ export type OnUnsupportedFn = (data: {
5
+ version: Version;
6
+ } & ({
7
+ method: MethodName;
8
+ } | {
9
+ [M in MethodNameWithVersionedParams]: {
10
+ method: M;
11
+ param: MethodVersionedParams<M>;
12
+ };
13
+ }[MethodNameWithVersionedParams])) => void;
14
+ export type CreatePostEventMode = 'strict' | 'non-strict';
15
+ /**
16
+ * Creates a function that checks if the specified method and parameters are supported.
17
+ *
18
+ * If the method or parameters are unsupported, the `onUnsupported` function will be called.
19
+ *
20
+ * If `strict` or `non-strict` value was passed as the second argument, the function
21
+ * will create its own `onUnsupported` function with behavior depending on the value passed.
22
+ *
23
+ * - Passing `strict` will make the function to throw a `MethodParameterUnsupportedError`
24
+ * or a `MethodUnsupportedError` error.
25
+ * - Passing `non-strict` will just warn you about something being unsupported.
26
+ *
27
+ * @param version - VBot Mini Apps version.
28
+ * @param onUnsupportedOrMode - function or strict mode. Default: `strict`
29
+ */
30
+ export declare function createPostEvent(version: Version, onUnsupportedOrMode?: OnUnsupportedFn | CreatePostEventMode): PostEventFn;
@@ -0,0 +1,15 @@
1
+ import { Version } from '@vbotma/types';
2
+ import { MethodName, MethodNameWithVersionedParams, MethodVersionedParams } from './types/index.js';
3
+ /**
4
+ * @returns Version of the specified method parameter release. Returns `null`
5
+ * if passed method or parameter are unknown.
6
+ * @param method - method name
7
+ * @param param - method parameter
8
+ */
9
+ export declare function getReleaseVersion<M extends MethodNameWithVersionedParams>(method: M, param: MethodVersionedParams<M>): Version | null;
10
+ /**
11
+ * @returns Version of the specified method release. Returns `null`
12
+ * if passed method is unknown.
13
+ * @param method - method name.
14
+ */
15
+ export declare function getReleaseVersion(method: MethodName): Version | null;
@@ -0,0 +1,35 @@
1
+ import { either as E } from 'fp-ts';
2
+ import { UnknownEnvError } from '../errors.js';
3
+ import { MethodNameWithOptionalParams, MethodNameWithoutParams, MethodNameWithRequiredParams, MethodParams } from './types/index.js';
4
+ export type PostEventError = UnknownEnvError;
5
+ export type PostEventFn = typeof postEvent;
6
+ export type PostEventFpFn = typeof postEventFp;
7
+ /**
8
+ * @see postEventFp
9
+ */
10
+ export declare function postEvent<Method extends MethodNameWithRequiredParams>(method: Method, params: MethodParams<Method>): void;
11
+ /**
12
+ * @see postEventFp
13
+ */
14
+ export declare function postEvent(method: MethodNameWithoutParams): void;
15
+ /**
16
+ * @see postEventFp
17
+ */
18
+ export declare function postEvent<Method extends MethodNameWithOptionalParams>(method: Method, params?: MethodParams<Method>): void;
19
+ /**
20
+ * Calls Mini Apps methods requiring parameters.
21
+ * @param method - method name.
22
+ * @param params - options along with params.
23
+ */
24
+ export declare function postEventFp<Method extends MethodNameWithRequiredParams>(method: Method, params: MethodParams<Method>): E.Either<PostEventError, void>;
25
+ /**
26
+ * Calls Mini Apps methods accepting no parameters at all.
27
+ * @param method - method name.
28
+ */
29
+ export declare function postEventFp(method: MethodNameWithoutParams): E.Either<PostEventError, void>;
30
+ /**
31
+ * Calls Mini Apps methods accepting optional parameters.
32
+ * @param method - method name.
33
+ * @param params - options along with params.
34
+ */
35
+ export declare function postEventFp<Method extends MethodNameWithOptionalParams>(method: Method, params?: MethodParams<Method>): E.Either<PostEventError, void>;
@@ -0,0 +1,7 @@
1
+ export type PostMessage = typeof window.parent.postMessage;
2
+ /**
3
+ * Posts a message to the parent window. We usually use it to send a message in web versions of
4
+ * Telegram.
5
+ * @param args - `window.parent.postMessage` arguments.
6
+ */
7
+ export declare const postMessage: PostMessage;
@@ -0,0 +1,15 @@
1
+ import { Version } from '@vbotma/types';
2
+ import { MethodName, MethodNameWithVersionedParams, MethodVersionedParams } from './types/index.js';
3
+ /**
4
+ * Returns true in case, passed parameter in specified method is supported.
5
+ * @param method - method name
6
+ * @param param - method parameter
7
+ * @param inVersion - platform version.
8
+ */
9
+ export declare function supports<M extends MethodNameWithVersionedParams>(method: M, param: MethodVersionedParams<M>, inVersion: Version): boolean;
10
+ /**
11
+ * Returns true in case, specified method is supported in a passed version.
12
+ * @param method - method name.
13
+ * @param inVersion - platform version.
14
+ */
15
+ export declare function supports(method: MethodName, inVersion: Version): boolean;
@@ -0,0 +1,59 @@
1
+ interface CreateInvokeCustomMethodParams<M extends string, Params extends object> {
2
+ /**
3
+ * Unique request identifier.
4
+ */
5
+ req_id: string;
6
+ /**
7
+ * Method name.
8
+ */
9
+ method: M;
10
+ /**
11
+ * Method specific parameters.
12
+ */
13
+ params: Params;
14
+ }
15
+ export interface CustomMethodsParams {
16
+ /**
17
+ * Deletes storage values using their keys.
18
+ */
19
+ deleteStorageValues: {
20
+ keys: string | string[];
21
+ };
22
+ /**
23
+ * Returns the current server time.
24
+ */
25
+ getCurrentTime: {};
26
+ /**
27
+ * Gets the current user contact in case, the mini app has access to it.
28
+ */
29
+ getRequestedContact: {};
30
+ /**
31
+ * Gets all registered storage keys.
32
+ */
33
+ getStorageKeys: {};
34
+ /**
35
+ * Gets storage values using their keys.
36
+ */
37
+ getStorageValues: {
38
+ keys: string | string[];
39
+ };
40
+ /**
41
+ * Saves a value using specified key in the storage.
42
+ */
43
+ saveStorageValue: {
44
+ key: string;
45
+ value: string;
46
+ };
47
+ }
48
+ /**
49
+ * Known custom method name.
50
+ */
51
+ export type CustomMethodName = keyof CustomMethodsParams;
52
+ /**
53
+ * Custom method parameters.
54
+ */
55
+ export type CustomMethodParams<M extends CustomMethodName> = CustomMethodsParams[M];
56
+ export type AnyInvokeCustomMethodParams = CreateInvokeCustomMethodParams<string, any> | {
57
+ [M in CustomMethodName]: CreateInvokeCustomMethodParams<M, CustomMethodParams<M>>;
58
+ }[CustomMethodName];
59
+ export {};
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Generic type which creates new types of haptic feedback.
3
+ */
4
+ type CreateHapticFeedbackParams<T extends string, P> = {
5
+ type: T;
6
+ } & P;
7
+ /**
8
+ * Style of impact occurred haptic event.
9
+ * - `light`, indicates a collision between small or lightweight UI objects,
10
+ * - `medium`, indicates a collision between medium-sized or medium-weight UI objects,
11
+ * - `heavy`, indicates a collision between large or heavyweight UI objects,
12
+ * - `rigid`, indicates a collision between hard or inflexible UI objects,
13
+ * - `soft`, indicates a collision between soft or flexible UI objects.
14
+ */
15
+ export type ImpactHapticFeedbackStyle = 'light' | 'medium' | 'heavy' | 'rigid' | 'soft';
16
+ /**
17
+ * Type of notification occurred type event.
18
+ * - `error`, indicates that a task or action has failed,
19
+ * - `success`, indicates that a task or action has completed successfully,
20
+ * - `warning`, indicates that a task or action produced a warning.
21
+ */
22
+ export type NotificationHapticFeedbackType = 'error' | 'success' | 'warning';
23
+ /**
24
+ * `impactOccurred` haptic feedback.
25
+ */
26
+ export type ImpactHapticFeedbackParams = CreateHapticFeedbackParams<'impact', {
27
+ impact_style: ImpactHapticFeedbackStyle;
28
+ }>;
29
+ /**
30
+ * `notificationOccurred` haptic feedback.
31
+ */
32
+ export type NotificationHapticFeedbackParams = CreateHapticFeedbackParams<'notification', {
33
+ notification_type: NotificationHapticFeedbackType;
34
+ }>;
35
+ /**
36
+ * `selectionChanged` haptic feedback.
37
+ */
38
+ export type SelectionHapticFeedbackParams = CreateHapticFeedbackParams<'selection_change', {}>;
39
+ export type AnyHapticFeedbackParams = ImpactHapticFeedbackParams | NotificationHapticFeedbackParams | SelectionHapticFeedbackParams;
40
+ export {};
@@ -0,0 +1,7 @@
1
+ export type * from './custom-method.js';
2
+ export type * from './haptic-feedback.js';
3
+ export type * from './methods.js';
4
+ export type * from './misc.js';
5
+ export type * from './notification.js';
6
+ export type * from './popup.js';
7
+ export type { CreateMethodParams } from './utils.js';