@@ -110,23 +110,44 @@ export default class GoogleDriveFileSystem implements FileSystem {
110110 request ( url : string , config ?: RequestInit , nothen ?: boolean ) {
111111 config = config || { } ;
112112 const headers = < Headers > config . headers || new Headers ( ) ;
113- headers . append ( `Authorization` , `Bearer ${ this . accessToken } ` ) ;
113+ headers . set ( `Authorization` , `Bearer ${ this . accessToken } ` ) ;
114114 config . headers = headers ;
115- const ret = fetch ( url , config ) ;
115+ const doFetch = ( ) => fetch ( url , config ) ;
116+ const retryWithFreshToken = async ( ) => {
117+ const token = await AuthVerify ( "googledrive" , true ) ;
118+ this . accessToken = token ;
119+ headers . set ( `Authorization` , `Bearer ${ this . accessToken } ` ) ;
120+ return doFetch ( ) ;
121+ } ;
116122 if ( nothen ) {
117- return < Promise < Response > > ret ;
123+ return doFetch ( ) . then ( async ( resp ) => {
124+ if ( resp . status === 401 ) {
125+ return retryWithFreshToken ( ) ;
126+ }
127+ return resp ;
128+ } ) ;
118129 }
119- return ret
120- . then ( ( data ) => data . json ( ) )
130+ return doFetch ( )
131+ . then ( async ( resp ) => {
132+ if ( resp . status === 401 ) {
133+ resp = await retryWithFreshToken ( ) ;
134+ }
135+ if ( ! resp . ok ) {
136+ throw new Error ( await resp . text ( ) ) ;
137+ }
138+ return resp . json ( ) ;
139+ } )
121140 . then ( async ( data ) => {
122141 if ( data . error ) {
123142 if ( data . error . code === 401 ) {
124143 // Token可能过期,尝试刷新
125- const token = await AuthVerify ( "googledrive" , true ) ;
126- this . accessToken = token ;
127- headers . set ( `Authorization` , `Bearer ${ this . accessToken } ` ) ;
128- return fetch ( url , config )
129- . then ( ( retryData ) => retryData . json ( ) )
144+ return retryWithFreshToken ( )
145+ . then ( async ( retryResp ) => {
146+ if ( ! retryResp . ok ) {
147+ throw new Error ( await retryResp . text ( ) ) ;
148+ }
149+ return retryResp . json ( ) ;
150+ } )
130151 . then ( ( retryData ) => {
131152 if ( retryData . error ) {
132153 throw new Error ( JSON . stringify ( retryData ) ) ;
@@ -145,7 +166,7 @@ export default class GoogleDriveFileSystem implements FileSystem {
145166 // 首先,找到要删除的文件或文件夹
146167 const fileId = await this . getFileId ( fullPath ) ;
147168 if ( ! fileId ) {
148- throw new Error ( `File or directory not found: ${ fullPath } ` ) ;
169+ return ;
149170 }
150171
151172 // 删除文件或文件夹
@@ -156,6 +177,9 @@ export default class GoogleDriveFileSystem implements FileSystem {
156177 } ,
157178 true
158179 ) . then ( async ( resp ) => {
180+ if ( resp . status === 404 ) {
181+ return ;
182+ }
159183 if ( resp . status !== 204 && resp . status !== 200 ) {
160184 throw new Error ( await resp . text ( ) ) ;
161185 }
0 commit comments