1
+ import { request } from '@/globals.js' ;
2
+ import { wrapSafe } from '@/scopes/wrappers/wrapSafe.js' ;
3
+ import { AccessDeniedError } from '@/errors.js' ;
4
+ import type { RequestOptionsNoCapture } from '@/types.js' ;
5
+
6
+ const METHOD_NAME = 'web_app_request_file_download' ;
7
+
8
+ /**
9
+ * Displays a native popup prompting the user to download a file.
10
+ * @param url - the HTTPS URL of the file to be downloaded.
11
+ * @param file - the suggested name for the downloaded file.
12
+ * @param options - additional request execution options.
13
+ * @since Mini Apps v8.0
14
+ * @throws {FunctionNotAvailableError } The environment is unknown
15
+ * @throws {FunctionNotAvailableError } The SDK is not initialized
16
+ * @throws {FunctionNotAvailableError } The function is not supported
17
+ * @throws {AccessDeniedError } User denied the action
18
+ * @example
19
+ * if (downloadFile.isAvailable()) {
20
+ * await downloadFile('https://telegram.org/js/telegram-web-app.js', 'telegram-sdk.js');
21
+ * }
22
+ */
23
+ export const downloadFile = wrapSafe (
24
+ 'downloadFile' ,
25
+ ( url : string , fileName : string , options ?: RequestOptionsNoCapture ) => {
26
+ return request (
27
+ METHOD_NAME ,
28
+ 'file_download_requested' ,
29
+ { ...options , params : { url, file_name : fileName } } ,
30
+ ) . then ( response => {
31
+ if ( response . status !== 'downloading' ) {
32
+ throw new AccessDeniedError ( 'User denied the action' ) ;
33
+ }
34
+ } ) ;
35
+ } , { isSupported : METHOD_NAME } ,
36
+ ) ;
0 commit comments