CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vkontakte--vk-bridge

Bridge library for VK Mini Apps to communicate with VK clients across iOS, Android, and Web platforms

Pending
Overview
Eval results
Files

core-bridge.mddocs/

Core Bridge Interface

The primary VK Bridge interface providing essential communication methods between VK Mini Apps and VK clients across all supported platforms.

Capabilities

Bridge Instance

The main bridge object providing the complete VK Bridge API.

/**
 * Default export - VK Bridge instance
 * Provides complete API for VK Mini App communication
 */
declare const bridge: VKBridge;

interface VKBridge {
  /** Send event to VK client and receive response */
  send: VKBridgeSend;
  
  /** Deprecated alias for send method */
  sendPromise: VKBridgeSend;
  
  /** Subscribe to events from VK client */
  subscribe: (listener: VKBridgeSubscribeHandler) => void;
  
  /** Unsubscribe from events */
  unsubscribe: (listener: VKBridgeSubscribeHandler) => void;
  
  /** Check method support synchronously (deprecated) */
  supports: <K extends AnyRequestMethodName>(method: K) => boolean;
  
  /** Check method support asynchronously */
  supportsAsync: <K extends AnyRequestMethodName>(method: K) => Promise<boolean>;
  
  /** Check if running in WebView */
  isWebView: () => boolean;
  
  /** Check if running in iframe */
  isIframe: () => boolean;
  
  /** Check if running embedded (WebView or iframe) */
  isEmbedded: () => boolean;
  
  /** Check if running standalone */
  isStandalone: () => boolean;
}

Usage Examples:

import bridge from "@vkontakte/vk-bridge";

// Initialize bridge connection
await bridge.send('VKWebAppInit');

// Check environment
if (bridge.isWebView()) {
  console.log('Running in mobile app WebView');
} else if (bridge.isIframe()) {
  console.log('Running in iframe (web)');
} else {
  console.log('Running standalone');
}

// Check method support before using
const canAccessCamera = await bridge.supportsAsync('VKWebAppOpenCodeReader');
if (canAccessCamera) {
  const result = await bridge.send('VKWebAppOpenCodeReader');
  console.log('QR code:', result.code_data);
}

Send Method

Primary method for sending events to VK client with type-safe responses.

/**
 * Send event to VK client and receive typed response
 * @param method - VK Bridge method name
 * @param props - Method-specific parameters (optional for some methods)
 * @returns Promise with method-specific response data
 */
type VKBridgeSend = <K extends AnyRequestMethodName>(
  method: K,
  props?: RequestProps<K> & RequestIdProp
) => Promise<K extends AnyReceiveMethodName ? ReceiveData<K> : void>;

interface RequestIdProp {
  request_id?: number | string;
}

type RequestProps<M extends AnyRequestMethodName> = RequestPropsMap[M];

Usage Examples:

// Method without parameters
const initResult = await bridge.send('VKWebAppInit');

// Method with required parameters
const userInfo = await bridge.send('VKWebAppGetUserInfo', {
  user_id: 12345
});

// Method with optional parameters
const authToken = await bridge.send('VKWebAppGetAuthToken', {
  app_id: 51665960,
  scope: 'friends,photos'
});

// Method with custom request ID for tracking
const geoData = await bridge.send('VKWebAppGetGeodata', {
  request_id: 'my-geo-request-001'
});

Event Subscription

Subscribe to events from VK client for real-time updates and responses.

/**
 * Subscribe to events from VK client
 * @param listener - Event handler function
 */
subscribe(listener: VKBridgeSubscribeHandler): void;

/**
 * Unsubscribe from events
 * @param listener - Previously subscribed event handler
 */
unsubscribe(listener: VKBridgeSubscribeHandler): void;

type VKBridgeSubscribeHandler = (event: VKBridgeEvent<AnyReceiveMethodName>) => void;

interface VKBridgeEvent<M extends AnyReceiveMethodName> {
  detail: {
    type: M extends AnyRequestMethodName ? ResultResponseEventName<M> : M;
    data: M extends AnyReceiveMethodName ? ReceiveData<M> : never;
  };
}

Usage Examples:

// Subscribe to all events
const eventHandler = (event) => {
  if (!event.detail) return;
  
  const { type, data } = event.detail;
  console.log('Event received:', type, data);
  
  switch (type) {
    case 'VKWebAppInitResult':
      console.log('Bridge initialized successfully');
      break;
    case 'VKWebAppInitFailed':
      console.error('Bridge initialization failed:', data.error_data);
      break;
    case 'VKWebAppUpdateConfig':
      console.log('App config updated:', data);
      break;
    case 'VKWebAppLocationChanged':
      console.log('Location changed:', data.location);
      break;
  }
};

bridge.subscribe(eventHandler);

// Unsubscribe when no longer needed
bridge.unsubscribe(eventHandler);

// Subscribe to specific event types
bridge.subscribe((event) => {
  if (event.detail.type === 'VKWebAppAccelerometerChanged') {
    const { x, y, z } = event.detail.data;
    console.log('Accelerometer:', { x, y, z });
  }
});

Platform Detection

Runtime environment detection for conditional functionality and platform-specific behavior.

/**
 * Check if running in WebView (mobile app)
 * @returns true if running in iOS or Android WebView
 */
isWebView(): boolean;

/**
 * Check if running in iframe (browser)
 * @returns true if running in iframe
 */
isIframe(): boolean;

/**
 * Check if running embedded (WebView or iframe)
 * @returns true if running in WebView or iframe
 */
isEmbedded(): boolean;

/**
 * Check if running standalone (not embedded)
 * @returns true if NOT running in WebView or iframe
 */
isStandalone(): boolean;

Usage Examples:

// Platform-specific functionality
if (bridge.isWebView()) {
  // Full API available in mobile apps
  await bridge.send('VKWebAppTapticImpactOccurred', { style: 'medium' });
  await bridge.send('VKWebAppAccelerometerStart');
} else if (bridge.isIframe()) {
  // Limited API in web iframe
  await bridge.send('VKWebAppShare', { link: 'https://example.com' });
} else if (bridge.isStandalone()) {
  // No bridge functionality
  console.log('Running outside VK environment');
}

// Check embedded vs standalone
if (bridge.isEmbedded()) {
  // Has access to VK Bridge API
  await bridge.send('VKWebAppGetLaunchParams');
} else {
  // Fallback behavior for standalone
  console.warn('VK Bridge not available');
}

Method Support Checking

Check if specific methods are available on the current platform before attempting to use them.

/**
 * Check method support asynchronously (recommended)
 * @param method - VK Bridge method name to check
 * @returns Promise resolving to true if method is supported
 */
supportsAsync<K extends AnyRequestMethodName>(method: K): Promise<boolean>;

/**
 * Check method support synchronously (deprecated)
 * @param method - VK Bridge method name to check
 * @returns true if method is supported
 * @deprecated Use supportsAsync instead
 */
supports<K extends AnyRequestMethodName>(method: K): boolean;

Usage Examples:

// Recommended async approach
const features = {
  camera: await bridge.supportsAsync('VKWebAppOpenCodeReader'),
  haptics: await bridge.supportsAsync('VKWebAppTapticImpactOccurred'),
  geolocation: await bridge.supportsAsync('VKWebAppGetGeodata'),
  payments: await bridge.supportsAsync('VKWebAppOpenPayForm')
};

console.log('Available features:', features);

// Conditional feature usage
if (await bridge.supportsAsync('VKWebAppShowImages')) {
  await bridge.send('VKWebAppShowImages', {
    images: ['https://example.com/photo.jpg']
  });
} else {
  // Fallback: open link
  window.open('https://example.com/photo.jpg', '_blank');
}

// Check multiple methods
const desiredMethods = [
  'VKWebAppStorageSet',
  'VKWebAppStorageGet', 
  'VKWebAppGetUserInfo'
] as const;

const supportedMethods = await Promise.all(
  desiredMethods.map(async method => ({
    method,
    supported: await bridge.supportsAsync(method)
  }))
);

console.log('Method support:', supportedMethods);

Platform Constants

/** Is the runtime environment client-side */
declare const IS_CLIENT_SIDE: boolean;

/** Is the runtime environment an Android WebView */
declare const IS_ANDROID_WEBVIEW: boolean;

/** Is the runtime environment an iOS WebView */
declare const IS_IOS_WEBVIEW: boolean;

/** Is the runtime environment React Native WebView */
declare const IS_REACT_NATIVE_WEBVIEW: boolean;

/** Is the runtime environment a browser */
declare const IS_WEB: boolean;

/** Is the runtime environment m.vk.com */
declare const IS_MVK: boolean;

/** Is the runtime environment desktop vk.com */
declare const IS_DESKTOP_VK: boolean;

/** Event type for current platform */
declare const EVENT_TYPE: string;

/** Array of methods supported on desktop platforms */
declare const DESKTOP_METHODS: Array<AnyRequestMethodName>;

Install with Tessl CLI

npx tessl i tessl/npm-vkontakte--vk-bridge

docs

advertising-monetization.md

application-lifecycle.md

authentication.md

core-bridge.md

device-features.md

geolocation.md

index.md

launch-parameters.md

middleware.md

payments-commerce.md

qr-barcode-scanning.md

social-features.md

storage-data.md

ui-display.md

user-data.md

tile.json