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, path: string, files: File[], config: WebDAVRequestConfig = {}) => {
32 const fd = new FormData();
33 fd.append('path', path);
34 files.forEach((f, idx) => {
35 fd.append(`file-${idx}`, f);
45 copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
49 headers: { ...config.headers, Destination: this.defaults.baseURL + destination }
52 move = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
56 headers: { ...config.headers, Destination: this.defaults.baseURL + destination }
59 delete = (url: string, config: WebDAVRequestConfig = {}) =>
65 private request = (config: RequestConfig) => {
66 return new Promise<XMLHttpRequest>((resolve, reject) => {
67 const r = this.createRequest();
68 r.open(config.method, this.defaults.baseURL + config.url);
70 const headers = { ...this.defaults.headers, ...config.headers };
73 .forEach(key => r.setRequestHeader(key, headers[key]));
75 if (config.onUploadProgress) {
76 r.upload.addEventListener('progress', config.onUploadProgress);
79 r.addEventListener('load', () => {
80 if (r.status === 404) {
87 r.addEventListener('error', () => {
91 r.upload.addEventListener('error', () => {
100 export interface WebDAVRequestConfig {
102 [key: string]: string;
104 onUploadProgress?: (event: ProgressEvent) => void;
107 interface WebDAVDefaults {
109 headers: { [key: string]: string };
112 interface RequestConfig {
115 headers?: { [key: string]: string };
117 onUploadProgress?: (event: ProgressEvent) => void;