-
Notifications
You must be signed in to change notification settings - Fork 234
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 #640 from Telegram-Mini-Apps/feature/retrieve-raw-lp
Feature/retrieve raw lp
- Loading branch information
Showing
9 changed files
with
81 additions
and
65 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 @@ | ||
--- | ||
"@telegram-apps/sdk": patch | ||
--- | ||
|
||
Remove some errors exported from bridge. |
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 @@ | ||
--- | ||
"@telegram-apps/bridge": minor | ||
--- | ||
|
||
Implement `retrieveRawLaunchParams`. |
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
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,28 +1,9 @@ | ||
import { isLaunchParamsQuery } from '@telegram-apps/transformers'; | ||
|
||
import { forEachLpSource } from '@/launch-params/forEachLpSource.js'; | ||
import { InitDataRetrieveError } from '@/errors.js'; | ||
import { retrieveLaunchParams } from '@/launch-params/retrieveLaunchParams.js'; | ||
import { retrieveRawLaunchParams } from '@/launch-params/retrieveRawLaunchParams.js'; | ||
|
||
/** | ||
* @returns Raw init data from any known source. | ||
* @throws {InitDataRetrieveError} Unable to retrieve init data from any known source. | ||
* @throws {LaunchParamsRetrieveError} Unable to retrieve launch params from any known source. | ||
*/ | ||
export function retrieveRawInitData(): string | undefined { | ||
// Init data depends on the launch parameters. We also want them to be saved. | ||
try { | ||
retrieveLaunchParams(); | ||
} catch { | ||
throw new InitDataRetrieveError(); | ||
} | ||
|
||
let initData: string | undefined; | ||
forEachLpSource(v => { | ||
if (isLaunchParamsQuery(v)) { | ||
initData = new URLSearchParams(v).get('tgWebAppData') || undefined; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return initData; | ||
return new URLSearchParams(retrieveRawLaunchParams()).get('tgWebAppData') || undefined; | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/bridge/src/launch-params/retrieveRawLaunchParams.test.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,46 @@ | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { retrieveRawLaunchParams } from '@/launch-params/retrieveRawLaunchParams.js'; | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe('window.location.href contains launch params', () => { | ||
it('should retrieve launch params from the window.location.href. Throw an error if data is invalid or missing', () => { | ||
vi | ||
.spyOn(window.location, 'href', 'get') | ||
.mockImplementationOnce(() => { | ||
return '/abc?tgWebAppStartParam=location_hash#tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D'; | ||
}); | ||
|
||
expect(retrieveRawLaunchParams()).toBe('tgWebAppStartParam=location_hash&tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D'); | ||
}); | ||
}); | ||
|
||
describe('first navigation entry contains launch params', () => { | ||
it('should retrieve launch params from the window.performance. Throw an error if data is invalid or missing', () => { | ||
vi | ||
.spyOn(performance, 'getEntriesByType') | ||
.mockImplementationOnce(() => [{ | ||
name: '/abc?tgWebAppStartParam=performance#tgWebAppPlatform=macos&tgWebAppVersion=7.3&tgWebAppThemeParams=%7B%7D', | ||
}] as any); | ||
|
||
expect(retrieveRawLaunchParams()).toBe('tgWebAppStartParam=performance&tgWebAppPlatform=macos&tgWebAppVersion=7.3&tgWebAppThemeParams=%7B%7D'); | ||
}); | ||
}); | ||
|
||
describe('session storage contains launch params', () => { | ||
it('should return launch parameters from the session storage tapps/launchParams key. If data is missing or invalid, throw an error', () => { | ||
const spy = vi | ||
.spyOn(sessionStorage, 'getItem') | ||
.mockImplementationOnce(() => ''); | ||
expect(() => retrieveRawLaunchParams()).toThrow(); | ||
|
||
spy.mockClear(); | ||
spy.mockImplementationOnce(() => { | ||
return '"tgWebAppPlatform=android&tgWebAppThemeParams=%7B%22bg_color%22%3A%22%23ffffff%22%7D&tgWebAppVersion=7.5"'; | ||
}); | ||
expect(retrieveRawLaunchParams()).toBe('tgWebAppPlatform=android&tgWebAppThemeParams=%7B%22bg_color%22%3A%22%23ffffff%22%7D&tgWebAppVersion=7.5'); | ||
}); | ||
}); |
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