1- import { getStorageValue } from '@telegram-apps/toolkit' ;
1+ import { isLaunchParamsQuery } from '@telegram-apps/transformers' ;
2+ import { getStorageValue , setStorageValue } from '@telegram-apps/toolkit' ;
3+
4+ import { LaunchParamsRetrieveError } from '@/errors.js' ;
5+
6+ const SESSION_STORAGE_KEY = 'launchParams' ;
27
38/**
49 * @param urlString - URL to extract launch parameters from.
@@ -14,25 +19,27 @@ function fromURL(urlString: string): string {
1419}
1520
1621/**
17- * Runs the specified function for each value, where the value is one stored in any known
18- * launch parameters source.
19- * @param fn - function to run. Should return false when the execution must be stopped .
22+ * @returns Launch parameters in a raw format from any known source.
23+ * @throws { LaunchParamsRetrieveError } Unable to retrieve launch parameters. They are probably
24+ * invalid .
2025 */
21- export function forEachLpSource ( fn : ( value : string ) => boolean ) : void {
26+ export function retrieveRawLaunchParams ( ) : string {
2227 for ( const retrieve of [
2328 // Try to retrieve launch parameters from the current location. This method can return
2429 // nothing in case, location was changed, and then the page was reloaded.
2530 ( ) => fromURL ( window . location . href ) ,
2631 // Then, try using the lower level API - window.performance.
2732 ( ) => {
2833 const navigationEntry = performance . getEntriesByType ( 'navigation' ) [ 0 ] as PerformanceNavigationTiming | undefined ;
29- return navigationEntry ? fromURL ( navigationEntry . name ) : undefined ;
34+ return navigationEntry && fromURL ( navigationEntry . name ) ;
3035 } ,
31- ( ) => getStorageValue < string > ( 'launchParams' ) || '' ,
36+ ( ) => getStorageValue < string > ( SESSION_STORAGE_KEY ) ,
3237 ] ) {
3338 const v = retrieve ( ) ;
34- if ( v && ! fn ( v ) ) {
35- return ;
39+ if ( v && isLaunchParamsQuery ( v ) ) {
40+ setStorageValue ( SESSION_STORAGE_KEY , v ) ;
41+ return v ;
3642 }
3743 }
44+ throw new LaunchParamsRetrieveError ( ) ;
3845}
0 commit comments