1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 defaults: WebDAVDefaults = {
12 constructor(config?: Partial<WebDAVDefaults>, private createRequest = () => new XMLHttpRequest()) {
14 this.defaults = { ...this.defaults, ...config };
18 propfind = (url: string, config: WebDAVRequestConfig = {}) =>
24 put = (url: string, data?: any, config: WebDAVRequestConfig = {}) =>
31 upload = (url: string, files: File[], config: WebDAVRequestConfig = {}) => {
33 files.map(file => this.request({
41 copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
47 Destination: this.defaults.baseURL
48 ? this.defaults.baseURL.replace(/\/+$/, '') + '/' + destination.replace(/^\/+/, '')
53 move = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
59 Destination: this.defaults.baseURL
60 ? this.defaults.baseURL.replace(/\/+$/, '') + '/' + destination.replace(/^\/+/, '')
65 delete = (url: string, config: WebDAVRequestConfig = {}) =>
71 private request = (config: RequestConfig) => {
72 return new Promise<XMLHttpRequest>((resolve, reject) => {
73 const r = this.createRequest();
74 this.defaults.baseURL = this.defaults.baseURL.replace(/\/+$/, '');
76 `${this.defaults.baseURL
77 ? this.defaults.baseURL+'/'
79 const headers = { ...this.defaults.headers, ...config.headers };
82 .forEach(key => r.setRequestHeader(key, headers[key]));
84 if (config.onUploadProgress) {
85 r.upload.addEventListener('progress', config.onUploadProgress);
88 r.addEventListener('load', () => {
89 if (r.status === 404) {
96 r.addEventListener('error', () => {
100 r.upload.addEventListener('error', () => {
109 export interface WebDAVRequestConfig {
111 [key: string]: string;
113 onUploadProgress?: (event: ProgressEvent) => void;
116 interface WebDAVDefaults {
118 headers: { [key: string]: string };
121 interface RequestConfig {
124 headers?: { [key: string]: string };
126 onUploadProgress?: (event: ProgressEvent) => void;