1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { customEncodeURI, encodeHash } from "./url";
9 defaults: WebDAVDefaults = {
14 constructor(config?: Partial<WebDAVDefaults>, private createRequest = () => new XMLHttpRequest()) {
16 this.defaults = { ...this.defaults, ...config };
20 propfind = (url: string, config: WebDAVRequestConfig = {}) =>
26 put = (url: string, data?: any, config: WebDAVRequestConfig = {}) =>
33 upload = (url: string, files: File[], config: WebDAVRequestConfig = {}) => {
35 files.map(file => this.request({
43 copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
49 Destination: this.defaults.baseURL
50 ? this.defaults.baseURL.replace(/\/+$/, '') + '/' + destination.replace(/^\/+/, '')
55 move = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
61 Destination: this.defaults.baseURL
62 ? this.defaults.baseURL.replace(/\/+$/, '') + '/' + destination.replace(/^\/+/, '')
67 delete = (url: string, config: WebDAVRequestConfig = {}) =>
73 private request = (config: RequestConfig) => {
74 return new Promise<XMLHttpRequest>((resolve, reject) => {
75 const r = this.createRequest();
76 this.defaults.baseURL = this.defaults.baseURL.replace(/\/+$/, '');
78 `${this.defaults.baseURL
79 ? this.defaults.baseURL+'/'
80 : ''}${customEncodeURI(config.url)}`);
82 if (config.headers && config.headers.Destination) {
83 config.headers.Destination = encodeHash(config.headers.Destination);
86 const headers = { ...this.defaults.headers, ...config.headers };
89 .forEach(key => r.setRequestHeader(key, headers[key]));
91 if (config.onUploadProgress) {
92 r.upload.addEventListener('progress', config.onUploadProgress);
95 // This event gets triggered on *any* server response
96 r.addEventListener('load', () => {
97 if (r.status >= 400) {
104 // This event gets triggered on network errors
105 r.addEventListener('error', () => {
109 r.upload.addEventListener('error', () => {
118 export interface WebDAVRequestConfig {
120 [key: string]: string;
122 onUploadProgress?: (event: ProgressEvent) => void;
125 interface WebDAVDefaults {
127 headers: { [key: string]: string };
130 interface RequestConfig {
133 headers?: { [key: string]: string };
135 onUploadProgress?: (event: ProgressEvent) => void;