@@ -6,7 +6,7 @@ let CLIENT_ID = '';
66
77async function getCredentials ( ) {
88 if ( CLIENT_ID ) return ;
9-
9+
1010 const response = await fetch ( '/api/config' ) ;
1111 if ( ! response . ok ) {
1212 throw new Error ( 'Failed to fetch OneDrive credentials' ) ;
@@ -18,7 +18,6 @@ async function getCredentials() {
1818 }
1919}
2020
21-
2221let msalInstance : PublicClientApplication | null = null ;
2322
2423// Initialize MSAL authentication
@@ -27,24 +26,26 @@ async function initializeMsal() {
2726 if ( ! CLIENT_ID ) {
2827 await getCredentials ( ) ;
2928 }
30-
29+
3130 const msalParams = {
3231 auth : {
3332 authority : 'https://login.microsoftonline.com/consumers' ,
3433 clientId : CLIENT_ID
3534 }
3635 } ;
37-
36+
3837 if ( ! msalInstance ) {
3938 msalInstance = new PublicClientApplication ( msalParams ) ;
4039 if ( msalInstance . initialize ) {
4140 await msalInstance . initialize ( ) ;
4241 }
4342 }
44-
43+
4544 return msalInstance ;
4645 } catch ( error ) {
47- throw new Error ( 'MSAL initialization failed: ' + ( error instanceof Error ? error . message : String ( error ) ) ) ;
46+ throw new Error (
47+ 'MSAL initialization failed: ' + ( error instanceof Error ? error . message : String ( error ) )
48+ ) ;
4849 }
4950}
5051
@@ -57,14 +58,14 @@ async function getToken(): Promise<string> {
5758 if ( ! msalInstance ) {
5859 throw new Error ( 'MSAL not initialized' ) ;
5960 }
60-
61+
6162 const resp = await msalInstance . acquireTokenSilent ( authParams ) ;
6263 accessToken = resp . accessToken ;
6364 } catch ( err ) {
6465 if ( ! msalInstance ) {
6566 throw new Error ( 'MSAL not initialized' ) ;
6667 }
67-
68+
6869 try {
6970 const resp = await msalInstance . loginPopup ( authParams ) ;
7071 msalInstance . setActiveAccount ( resp . account ) ;
@@ -73,14 +74,17 @@ async function getToken(): Promise<string> {
7374 accessToken = resp2 . accessToken ;
7475 }
7576 } catch ( popupError ) {
76- throw new Error ( 'Failed to login: ' + ( popupError instanceof Error ? popupError . message : String ( popupError ) ) ) ;
77+ throw new Error (
78+ 'Failed to login: ' +
79+ ( popupError instanceof Error ? popupError . message : String ( popupError ) )
80+ ) ;
7781 }
7882 }
79-
83+
8084 if ( ! accessToken ) {
8185 throw new Error ( 'Failed to acquire access token' ) ;
8286 }
83-
87+
8488 return accessToken ;
8589}
8690
@@ -106,7 +110,6 @@ const params = {
106110 }
107111} ;
108112
109-
110113// Download file from OneDrive
111114async function downloadOneDriveFile ( fileInfo : any ) : Promise < Blob > {
112115 const accessToken = await getToken ( ) ;
@@ -228,17 +231,17 @@ export async function openOneDrivePicker(): Promise<any | null> {
228231 if ( ! authToken ) {
229232 return reject ( new Error ( 'Failed to acquire access token' ) ) ;
230233 }
231-
234+
232235 pickerWindow = window . open ( '' , 'OneDrivePicker' , 'width=800,height=600' ) ;
233236 if ( ! pickerWindow ) {
234237 return reject ( new Error ( 'Failed to open OneDrive picker window' ) ) ;
235238 }
236-
239+
237240 const queryString = new URLSearchParams ( {
238241 filePicker : JSON . stringify ( params )
239242 } ) ;
240243 const url = `${ baseUrl } ?${ queryString . toString ( ) } ` ;
241-
244+
242245 const form = pickerWindow . document . createElement ( 'form' ) ;
243246 form . setAttribute ( 'action' , url ) ;
244247 form . setAttribute ( 'method' , 'POST' ) ;
@@ -247,10 +250,10 @@ export async function openOneDrivePicker(): Promise<any | null> {
247250 input . setAttribute ( 'name' , 'access_token' ) ;
248251 input . setAttribute ( 'value' , authToken ) ;
249252 form . appendChild ( input ) ;
250-
253+
251254 pickerWindow . document . body . appendChild ( form ) ;
252255 form . submit ( ) ;
253-
256+
254257 window . addEventListener ( 'message' , handleWindowMessage ) ;
255258 } catch ( err ) {
256259 if ( pickerWindow ) {
@@ -267,14 +270,14 @@ export async function openOneDrivePicker(): Promise<any | null> {
267270// Pick and download file from OneDrive
268271export async function pickAndDownloadFile ( ) : Promise < { blob : Blob ; name : string } | null > {
269272 const pickerResult = await openOneDrivePicker ( ) ;
270-
273+
271274 if ( ! pickerResult || ! pickerResult . items || pickerResult . items . length === 0 ) {
272275 return null ;
273276 }
274-
277+
275278 const selectedFile = pickerResult . items [ 0 ] ;
276279 const blob = await downloadOneDriveFile ( selectedFile ) ;
277-
280+
278281 return { blob, name : selectedFile . name } ;
279282}
280283
0 commit comments