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 = {}) =>
45 headers: { ...config.headers, Destination: this.defaults.baseURL + destination }
48 move = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
52 headers: { ...config.headers, Destination: this.defaults.baseURL + destination }
55 delete = (url: string, config: WebDAVRequestConfig = {}) =>
61 private request = (config: RequestConfig) => {
62 return new Promise<XMLHttpRequest>((resolve, reject) => {
63 const r = this.createRequest();
64 this.defaults.baseURL = this.defaults.baseURL.replace(/\/+$/, '');
66 `${this.defaults.baseURL
67 ? this.defaults.baseURL+'/'
69 const headers = { ...this.defaults.headers, ...config.headers };
72 .forEach(key => r.setRequestHeader(key, headers[key]));
74 if (config.onUploadProgress) {
75 r.upload.addEventListener('progress', config.onUploadProgress);
78 r.addEventListener('load', () => {
79 if (r.status === 404) {
86 r.addEventListener('error', () => {
90 r.upload.addEventListener('error', () => {
99 export interface WebDAVRequestConfig {
101 [key: string]: string;
103 onUploadProgress?: (event: ProgressEvent) => void;
106 interface WebDAVDefaults {
108 headers: { [key: string]: string };
111 interface RequestConfig {
114 headers?: { [key: string]: string };
116 onUploadProgress?: (event: ProgressEvent) => void;