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 r.open(config.method, this.defaults.baseURL + config.url);
65 const headers = { ...this.defaults.headers, ...config.headers };
68 .forEach(key => r.setRequestHeader(key, headers[key]));
70 if (config.onUploadProgress) {
71 r.upload.addEventListener('progress', config.onUploadProgress);
74 r.addEventListener('load', () => {
75 if (r.status === 404) {
82 r.addEventListener('error', () => {
86 r.upload.addEventListener('error', () => {
95 export interface WebDAVRequestConfig {
97 [key: string]: string;
99 onUploadProgress?: (event: ProgressEvent) => void;
102 interface WebDAVDefaults {
104 headers: { [key: string]: string };
107 interface RequestConfig {
110 headers?: { [key: string]: string };
112 onUploadProgress?: (event: ProgressEvent) => void;