Skip to content

Commit

Permalink
Merge pull request #243 from Telegram-Mini-Apps/feature/deprecate-ret…
Browse files Browse the repository at this point in the history
…rieve-launch-data

Deprecate `retrieveLaunchData` function
  • Loading branch information
heyqbnk authored Mar 1, 2024
2 parents ead2a00 + 86c6cf5 commit 9147846
Show file tree
Hide file tree
Showing 32 changed files with 193 additions and 226 deletions.
7 changes: 7 additions & 0 deletions .changeset/spicy-melons-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@tma.js/sdk-react": patch
"@tma.js/sdk-solid": patch
"@tma.js/sdk": patch
---

Deprecate retrieveLaunchData function
8 changes: 4 additions & 4 deletions apps/docs/packages/tma-js-sdk/launch-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ serializeLaunchParams({
## Retrieving

This package enables the extraction of launch parameters from the current environment using
the `retrieveLaunchData` function which surely extracts launch parameters and determines if current
page was reloaded. This function throws an error if all known sources contain invalid data.
the `retrieveLaunchParams` function which surely extracts launch parameters. This function
throws an error if all known sources contain invalid data.

```typescript
import { retrieveLaunchData } from '@tma.js/sdk';
import { retrieveLaunchParams } from '@tma.js/sdk';

const { launchParams, isPageReload } = retrieveLaunchData();
const launchParams = retrieveLaunchParams();
```
4 changes: 2 additions & 2 deletions apps/docs/packages/tma-js-sdk/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Here is the example how developer could instantiate the stable instance of `Hash

```typescript
import {
retrieveLaunchData,
isPageReload,
HashNavigator,
type HashNavigatorOptions,
} from '@tma.js/sdk';
Expand All @@ -314,7 +314,7 @@ function createNavigator() {

// If page was reloaded, we assume that navigator had to
// previously save its state in the session storage.
if (retrieveLaunchData().isPageReload) {
if (isPageReload()) {
const stateRaw = sessionStorage.getItem('hash-navigator-state');
if (stateRaw) {
try {
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/packages/tma-js-solid-router-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function App() {

```typescript [createNavigator.ts]
import {
retrieveLaunchData,
isPageReload,
HashNavigator,
type HashNavigatorOptions,
} from '@tma.js/sdk';
Expand All @@ -119,7 +119,7 @@ export function createNavigator(): HashNavigator {

// If page was reloaded, we assume that navigator had to previously save
// its state in the session storage.
if (retrieveLaunchData().isPageReload) {
if (isPageReload()) {
const stateRaw = sessionStorage.getItem('hash-navigator-state');
if (stateRaw) {
try {
Expand Down
10 changes: 5 additions & 5 deletions apps/docs/platform/init-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ and init data specifically.

## Retrieving

To extract init data, developer can use `retrieveLaunchData` function
To extract init data, developer can use the `retrieveLaunchParams` function
from [@tma.js/sdk](../packages/tma-js-sdk.md).

```typescript
import { retrieveLaunchData } from '@tma.js/sdk';
import { retrieveLaunchParams } from '@tma.js/sdk';

const { initDataRaw, initData } = retrieveLaunchData().launchParams;
const { initDataRaw, initData } = retrieveLaunchParams();
```

## Authorization and Authentication
Expand All @@ -42,9 +42,9 @@ carried out on the server side.
Here is how a developer could send init data to server:

```typescript
import { retrieveLaunchData } from '@tma.js/sdk';
import { retrieveLaunchParams } from '@tma.js/sdk';

const { initDataRaw } = retrieveLaunchData().launchParams;
const { initDataRaw } = retrieveLaunchParams();

fetch('https://example.com/api', {
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-react/src/tools/launch-params.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';
import { type LaunchParams, retrieveLaunchParams } from '@tma.js/sdk';
import React, { type ComponentType, useMemo } from 'react';

/**
* Hooks to retrieve launch parameters.
*/
export function useLaunchParams(): LaunchParams {
return useMemo(() => retrieveLaunchData().launchParams, []);
return useMemo(retrieveLaunchParams, []);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-solid/src/tools/launch-params.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';
import { type LaunchParams, retrieveLaunchParams } from '@tma.js/sdk';

/**
* Hooks to retrieve launch parameters.
* Hook to retrieve launch parameters.
*/
export function useLaunchParams(): LaunchParams {
return retrieveLaunchData().launchParams;
return retrieveLaunchParams();
}
4 changes: 2 additions & 2 deletions packages/sdk/src/bridge/__tests__/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { type PostEvent, postEvent as globalPostEvent } from '../methods/postEve
import { createWindow } from '../../../test-utils/createWindow';
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent';

vi.mock('../methods/postEvent.js', async () => {
vi.mock('../methods/postEvent', async () => {
const { postEvent: actualPostEvent } = await vi
.importActual('../methods/postEvent.js') as { postEvent: PostEvent };
.importActual('../methods/postEvent') as { postEvent: PostEvent };

return {
postEvent: vi.fn(actualPostEvent),
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/src/bridge/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './hasExternalNotify.js';
export * from './hasWebviewProxy.js';
export * from './isIframe.js';
3 changes: 2 additions & 1 deletion packages/sdk/src/bridge/methods/postEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isIframe } from '~/misc/isIframe.js';

import type {
MiniAppsEmptyMethodName,
MiniAppsMethodName,
Expand All @@ -8,7 +10,6 @@ import { logger, targetOrigin as globalTargetOrigin } from '../../globals.js';
import {
hasExternalNotify,
hasWebviewProxy,
isIframe,
} from '../env/index.js';

interface PostEventOptions {
Expand Down
9 changes: 7 additions & 2 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
} from './back-button/index.js';
export {
createPostEvent,
isIframe,
invokeCustomMethod,
on,
off,
Expand Down Expand Up @@ -90,6 +89,7 @@ export {
launchParamsParser,
parseLaunchParams,
retrieveLaunchData,
retrieveLaunchParams,
serializeLaunchParams,
type LaunchParams,
type LaunchData,
Expand All @@ -110,7 +110,12 @@ export {
type MiniAppEvents,
type MiniAppProps,
} from './mini-app/index.js';
export { isTMA, isRecord } from './misc/index.js';
export {
isTMA,
isRecord,
isIframe,
isPageReload,
} from './misc/index.js';
export {
getHash,
HashNavigator,
Expand Down
36 changes: 17 additions & 19 deletions packages/sdk/src/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPostEvent, isIframe, on } from '~/bridge/index.js';
import { createPostEvent, on } from '~/bridge/index.js';
import { CloudStorage } from '~/cloud-storage/index.js';
import { HapticFeedback } from '~/haptic-feedback/index.js';
import { catchCustomStyles } from '~/init/catchCustomStyles.js';
Expand All @@ -15,7 +15,8 @@ import {
import { processCSSVars } from '~/init/css/index.js';
import { InitData } from '~/init-data/index.js';
import { Invoice } from '~/invoice/index.js';
import { retrieveLaunchData } from '~/launch-params/index.js';
import { retrieveLaunchParams } from '~/launch-params/index.js';
import { isIframe, isPageReload } from '~/misc/index.js';
import { Popup } from '~/popup/index.js';
import { QRScanner } from '~/qr-scanner/index.js';
import { Utils } from '~/utils/index.js';
Expand All @@ -37,18 +38,15 @@ export function init(options: InitOptions = {}): InitResult | Promise<InitResult
} = options;

try {
// Retrieve launch data.
const {
launchParams: {
initData,
initDataRaw,
version,
platform,
themeParams,
botInline = false,
},
isPageReload,
} = retrieveLaunchData();
initData,
initDataRaw,
version,
platform,
themeParams,
botInline = false,
} = retrieveLaunchParams();
const isPageReloaded = isPageReload();

const createRequestId = createRequestIdGenerator();
const postEvent = createPostEvent(version);
Expand All @@ -68,20 +66,20 @@ export function init(options: InitOptions = {}): InitResult | Promise<InitResult
}

const result: Omit<InitResult, 'viewport'> = {
backButton: createBackButton(isPageReload, version, postEvent),
closingBehavior: createClosingBehavior(isPageReload, postEvent),
backButton: createBackButton(isPageReloaded, version, postEvent),
closingBehavior: createClosingBehavior(isPageReloaded, postEvent),
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
createRequestId,
hapticFeedback: new HapticFeedback(version, postEvent),
invoice: new Invoice(version, postEvent),
mainButton: createMainButton(
isPageReload,
isPageReloaded,
themeParams.buttonColor || '#000000',
themeParams.buttonTextColor || '#ffffff',
postEvent,
),
miniApp: createMiniApp(
isPageReload,
isPageReloaded,
themeParams.backgroundColor || '#ffffff',
version,
botInline,
Expand All @@ -91,7 +89,7 @@ export function init(options: InitOptions = {}): InitResult | Promise<InitResult
popup: new Popup(version, postEvent),
postEvent,
qrScanner: new QRScanner(version, postEvent),
settingsButton: createSettingsButton(isPageReload, version, postEvent),
settingsButton: createSettingsButton(isPageReloaded, version, postEvent),
themeParams: createThemeParams(themeParams),
utils: new Utils(version, createRequestId, postEvent),
...(initData
Expand All @@ -103,7 +101,7 @@ export function init(options: InitOptions = {}): InitResult | Promise<InitResult
: {}),
};

const viewport = createViewport(isPageReload, platform, postEvent, complete);
const viewport = createViewport(isPageReloaded, platform, postEvent, complete);
if (viewport instanceof Promise || complete) {
return Promise.resolve(viewport).then((vp) => {
processCSSVars(
Expand Down
81 changes: 0 additions & 81 deletions packages/sdk/src/launch-params/computeLaunchData.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/sdk/src/launch-params/computePageReload.ts

This file was deleted.

5 changes: 1 addition & 4 deletions packages/sdk/src/launch-params/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
export * from './computeLaunchData.js';
export * from './computePageReload.js';
export * from './getFirstNavigationEntry.js';
export * from './launchParamsParser.js';
export * from './parseLaunchParams.js';
export * from './retrieveCurrent.js';
export * from './retrieveFromLocation.js';
export * from './retrieveFromPerformance.js';
export * from './retrieveFromUrl.js';
export * from './retrieveLaunchData.js';
export * from './retrieveLaunchParams.js';
export * from './serializeLaunchParams.js';
export * from './storage.js';
export * from './types.js';
27 changes: 0 additions & 27 deletions packages/sdk/src/launch-params/retrieveCurrent.ts

This file was deleted.

Loading

0 comments on commit 9147846

Please sign in to comment.