This API provides a set of methods to interact with a WebDAV server using React and TypeScript. The WebDavService
class handles various WebDAV operations such as fetching files, creating folders, checking if a folder exists, and more.
To use the WebDavService
in your React project, install the required dependencies:
npm install axios xml2js
yard add axios xml2js
Here's how you can create an instance of the WebDavService with login credentials:
import { WebDavService } from './path/to/webdav-service';
const options = {
baseUrl: 'https://your-webdav-server.com',
username: 'your-username',
password: 'your-password',
};
const webDavService = new WebDavService(options);
console.log('Logged in to WebDAV server');
This example demonstrates how to create a new folder, and upload a dummy file to that folder:
async function createAndUploadDummyFile() {
const options = {
baseUrl: 'https://your-webdav-server.com',
username: 'your-username',
password: 'your-password',
};
const webDavService = new WebDavService(options);
const folderPath = 'dummy-folder';
const filePath = `${folderPath}/dummy-file.txt`;
const fileContent = 'This is a dummy file content';
// Create a new folder
await webDavService.createFolder(folderPath);
console.log(`Created folder: ${folderPath}`);
// Upload a dummy file to the new folder
await webDavService.upload(filePath, fileContent);
console.log(`Uploaded file: ${filePath}`);
}
createAndUploadDummyFile();
First, import the WebDavService
class and create an instance with the necessary options:
import { WebDavService } from './path/to/webdav-service';
const options = {
baseUrl: 'https://your-webdav-server.com',
username: 'your-username',
password: 'your-password',
};
const webDavService = new WebDavService(options);
Creates an instance of the WebDavService
class.
constructor(options: WebDavOptions)
options
(WebDavOptions): An object containing the base URL, username, and password for the WebDAV server.
Fetches a file from the WebDAV server.
async get(path: string): Promise<string>
path
(string): The path to the file on the WebDAV server.
Promise<string>
: The content of the file.
const fileContent = await webDavService.get('path/to/file.txt');
console.log(fileContent);
Creates a new folder on the WebDAV server.
async createFolder(folderPath: string): Promise<void>
folderPath
(string): The path where the new folder should be created.
await webDavService.createFolder('path/to/new-folder');
Checks if a folder exists on the WebDAV server.
async folderExists(path: string): Promise<boolean>
path
(string): The path to the folder.
Promise<boolean>
:true
if the folder exists,false
otherwise.
const exists = await webDavService.folderExists('path/to/folder');
console.log(exists); // true or false
Performs a PROPFIND request to get properties of a resource on the WebDAV server.
async propFind(path: string, depth: number = 1): Promise<any>
path
(string): The path to the resource.depth
(number): The depth of the PROPFIND request (default is 1).
Promise<any>
: The parsed XML response.
const properties = await webDavService.propFind('path/to/resource');
console.log(properties);
Uploads a file to the WebDAV server.
async upload(filePath: string, content: string | Blob): Promise<void>
filePath
(string): The path where the file should be uploaded.content
(string | Blob): The content of the file.
const fileContent = 'Hello, world!';
await webDavService.upload('path/to/upload.txt', fileContent);
Gets the content of a directory on the WebDAV server.
async getDirectoryContent(path: string): Promise<any>
path
(string): The path to the directory.
Promise<any>
: The raw XML response.
const directoryContent = await webDavService.getDirectoryContent('path/to/directory');
console.log(directoryContent);
This project is licensed under the MIT License. See the LICENSE file for details.