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+'/'
78 : ''}${encodeURI(config.url)}`);
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 // This event gets triggered on *any* server response
89 r.addEventListener('load', () => {
90 if (r.status >= 400) {
97 // This event gets triggered on network errors
98 r.addEventListener('error', () => {
102 r.upload.addEventListener('error', () => {
111 export interface WebDAVRequestConfig {
113 [key: string]: string;
115 onUploadProgress?: (event: ProgressEvent) => void;
118 interface WebDAVDefaults {
120 headers: { [key: string]: string };
123 interface RequestConfig {
126 headers?: { [key: string]: string };
128 onUploadProgress?: (event: ProgressEvent) => void;