-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from Telegram-Mini-Apps/feature/launch-params…
…-from-url Feature/launch params from url
- Loading branch information
Showing
9 changed files
with
84 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tma.js/sdk": minor | ||
--- | ||
|
||
Implement retrieveFromUrl launch params function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/sdk/src/launch-params/__tests__/retrieveFromUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { expect, it } from 'vitest'; | ||
import { retrieveFromUrl } from '../retrieveFromUrl'; | ||
|
||
it('should return launch parameters based on hash only in case, URL does not contain the query part', () => { | ||
expect(retrieveFromUrl('#tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D')).toEqual({ | ||
platform: 'tdesktop', | ||
version: '7.0', | ||
themeParams: {}, | ||
}); | ||
}); | ||
|
||
it('should return launch parameters based on query params and hash in case, URL contains both parts', () => { | ||
expect(retrieveFromUrl('?tgWebAppStartParam=900#tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D')).toEqual({ | ||
platform: 'tdesktop', | ||
version: '7.0', | ||
themeParams: {}, | ||
startParam: '900', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { parseLaunchParams } from './parseLaunchParams.js'; | ||
import { retrieveFromUrl } from './retrieveFromUrl.js'; | ||
import type { LaunchParams } from './types.js'; | ||
|
||
/** | ||
* Attempts to extract launch parameters from the current window location hash. | ||
* @throws {Error} window.location.hash contains invalid data. | ||
*/ | ||
export function retrieveFromLocation(): LaunchParams { | ||
return parseLaunchParams(window.location.hash.slice(1)); | ||
return retrieveFromUrl(window.location.href); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { parseLaunchParams } from '~/launch-params/parseLaunchParams.js'; | ||
import type { LaunchParams } from '~/launch-params/types.js'; | ||
|
||
/** | ||
* Retrieves launch parameters from the specified URL. | ||
* @param url - URL to extract launch parameters from. | ||
*/ | ||
export function retrieveFromUrl(url: string): LaunchParams { | ||
const queryParams = url.includes('?') | ||
// If URL includes "?", we expect it to possibly contain "#". The reason is TMA allows passing | ||
// start parameter via tgWebAppStartParam **query** parameter. Replacing "#" with "&" we | ||
// expect merging query parameters with hash parameters and creating complete launch | ||
// parameters information. | ||
? url.replace('#', '&').slice(url.indexOf('?') + 1) | ||
// Otherwise, we expect specifying launch parameters only in the hash part. | ||
: url.slice(url.indexOf('#') + 1); | ||
|
||
return parseLaunchParams(queryParams); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters