2020 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2121 *
2222 */
23- import type { DAVResultResponseProps , FileStat , Response , ResponseDataDetailed , WebDAVClient } from 'webdav'
23+ import type { DAVResultResponseProps , FileStat , ResponseDataDetailed , WebDAVClient } from 'webdav'
2424import type { Node } from '../files/node'
2525
2626import { File } from '../files/file'
@@ -29,10 +29,9 @@ import { NodeData } from '../files/nodeData'
2929import { davParsePermissions } from './davPermissions'
3030import { davGetFavoritesReport } from './davProperties'
3131
32- import { getCurrentUser , getRequestToken } from '@nextcloud/auth'
32+ import { getCurrentUser , getRequestToken , onRequestTokenUpdate } from '@nextcloud/auth'
3333import { generateRemoteUrl } from '@nextcloud/router'
34- import { createClient , getPatcher , RequestOptions } from 'webdav'
35- import { request } from 'webdav/dist/node/request.js'
34+ import { createClient , getPatcher } from 'webdav'
3635
3736/**
3837 * Nextcloud DAV result response
@@ -59,28 +58,43 @@ export const davRemoteURL = generateRemoteUrl('dav')
5958 * @param remoteURL The DAV server remote URL
6059 */
6160export const davGetClient = function ( remoteURL = davRemoteURL ) {
62- const client = createClient ( remoteURL , {
63- headers : {
64- requesttoken : getRequestToken ( ) || '' ,
65- } ,
66- } )
61+ const client = createClient ( remoteURL )
62+
63+ /**
64+ * Set headers for DAV requests
65+ * @param token CSRF token
66+ */
67+ function setHeaders ( token : string | null ) {
68+ client . setHeaders ( {
69+ // Add this so the server knows it is an request from the browser
70+ 'X-Requested-With' : 'XMLHttpRequest' ,
71+ // Inject user auth
72+ requesttoken : token ?? '' ,
73+ } )
74+ }
75+
76+ // refresh headers when request token changes
77+ onRequestTokenUpdate ( setHeaders )
78+ setHeaders ( getRequestToken ( ) )
6779
6880 /**
6981 * Allow to override the METHOD to support dav REPORT
7082 *
7183 * @see https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/request.ts
7284 */
7385 const patcher = getPatcher ( )
74- // https://github.com/perry-mitchell/hot-patcher/issues/6
7586 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
7687 // @ts -ignore
77- patcher . patch ( 'request' , ( options : RequestOptions ) : Promise < Response > => {
78- if ( options . headers ?. method ) {
79- options . method = options . headers . method
80- delete options . headers . method
88+ // https://github.com/perry-mitchell/hot-patcher/issues/6
89+ patcher . patch ( 'fetch' , ( url : string , options : RequestInit ) : Promise < Response > => {
90+ const headers = options . headers as Record < string , string >
91+ if ( headers ?. method ) {
92+ options . method = headers . method
93+ delete headers . method
8194 }
82- return request ( options )
95+ return fetch ( url , options )
8396 } )
97+
8498 return client
8599}
86100
0 commit comments