File size: 977 Bytes
3f61b9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Minimal typed access to the Telegram WebApp bridge (loaded via telegram-web-app.js).
// Everything is null-guarded so the game still runs in a plain browser (dev mode).

export type TelegramInvoiceStatus = 'paid' | 'cancelled' | 'failed' | 'pending';

type TelegramWebApp = {
  initData: string;
  ready: () => void;
  expand: () => void;
  openInvoice: (url: string, callback?: (status: TelegramInvoiceStatus) => void) => void;
};

export function telegramWebApp(): TelegramWebApp | null {
  return (window as { Telegram?: { WebApp?: TelegramWebApp } }).Telegram?.WebApp ?? null;
}

/** Auth header for every API call; empty outside Telegram (server dev mode allows that). */
export function tgHeaders(): Record<string, string> {
  const initData = telegramWebApp()?.initData ?? '';
  return initData ? { 'X-Telegram-Init-Data': initData } : {};
}

export function bootTelegram(): void {
  const app = telegramWebApp();
  if (!app) return;
  app.ready();
  app.expand();
}