项目作者: OpenMarshal

项目描述 :
WebDAV Client on npm
高级语言: TypeScript
项目地址: git://github.com/OpenMarshal/npm-WebDAV-Client.git
创建时间: 2017-07-14T06:16:15Z
项目社区:https://github.com/OpenMarshal/npm-WebDAV-Client

开源协议:The Unlicense

下载


webdav-client npm Version Build Status

This is a WebDAV client.

It is not meant to be used on browsers yet.

Install

  1. npm install webdav-client

Usage

  1. import * as webdavClient from 'webdav-client'
  2. // or
  3. import { Connection } from 'webdav-client'
  4. // or
  5. const webdavClient = require('webdav-client');
  6. // Create the client object
  7. const connection = new webdavClient.Connection('http://my-webdav-server:1900');
  8. connection.get('/path/of/my/file.txt', (e, content) => {
  9. if(e)
  10. throw e;
  11. console.log(content);
  12. })

Methods

  1. class Connection
  2. {
  3. constructor(url : string)
  4. constructor(options : ConnectionOptions)
  5. request(options : RequestOptions, callback : ResponseCallback) // Custom request
  6. stream(options : RequestOptions) : Stream // Custom streaming request
  7. // Might be needed before using the streaming form of the methods (put and get)
  8. prepareForStreaming(path : string, callback : (error : Error) => void) : void
  9. prepareForStreaming(callback : (error : Error) => void) : void
  10. readdir(path : string, callback : (error : Error, files ?: string[]) => void) : void
  11. readdir(path : string, options : ConnectionReaddirOptions, callback : (error : Error, files : string[] | ConnectionReaddirComplexResult[]) => void) : void
  12. exists(path : string, callback : (error : Error, exists : boolean) => void) : void
  13. mkdir(path : string, callback : (error : Error) => void) : void
  14. delete(path : string, callback : (error : Error) => void) : void
  15. get(path : string, callback : (error : Error, body : ContentType) => void) : void
  16. get(path : string, callback : (error : Error, body : ContentType) => void) : Stream
  17. put(path : string, content : ContentType, callback : (error : Error) => void) : void
  18. put(path : string) : Stream
  19. move(pathSource : string, pathDestination : string, override : boolean, callback : (error : Error) => void) : void
  20. move(pathSource : string, pathDestination : string, callback : (error : Error) => void) : void
  21. copy(pathSource : string, pathDestination : string, override : boolean, callback : (error : Error) => void) : void
  22. copy(pathSource : string, pathDestination : string, callback : (error : Error) => void) : void
  23. lock(path : string, callback : (error ?: Error, lockUID ?: Lock) => void) : void
  24. refreshLock(path : string, lock : string | Lock, callback : (error : Error) => void) : void
  25. unlock(path : string, lock : string | Lock, callback : (error : Error) => void) : void
  26. setProperties(path : string, properties : Properties, callback : (error : Error) => void) : void
  27. removeProperties(path : string, properties : string[], callback : (error : Error) => void) : void
  28. getProperties(path : string, callback : (error : Error, properties : Properties) => void) : void
  29. getProperties(path : string, options : ConnectionReaddirOptions, callback : (error : Error, properties : Properties) => void) : void
  30. }

The Connection options :

  1. interface ConnectionOptions
  2. {
  3. url : string
  4. authenticator ?: Authenticator
  5. username ?: string
  6. password ?: string
  7. }

The ConnectionReaddirOptions interface :

  1. interface ConnectionReaddirOptions
  2. {
  3. // true = get a ConnectionReaddirComplexResult Array as callback result
  4. // false (default) = get a String Array as callback result
  5. properties ?: boolean
  6. // An array of properties which will be sent with the PROPFIND request
  7. extraProperties: ConnectionReaddirProperty[]
  8. }

The ConnectionReaddirOptions interface :

  1. interface ConnectionReaddirProperty
  2. {
  3. namespace: string
  4. namespaceShort: string
  5. element: string
  6. // Default value. If undefined and the XML response doesn't have this element, it will not be returned
  7. default?: any
  8. // true = It will be cast to number | string | boolean
  9. // false (default) = it is returned as string
  10. nativeType?: boolean
  11. }

The ConnectionReaddirComplexResult interface :

  1. interface ConnectionReaddirComplexResult
  2. {
  3. creationDate : Date
  4. lastModified : Date
  5. isDirectory : boolean
  6. isFile : boolean
  7. type : 'directory' | 'file'
  8. size : number
  9. href : string
  10. name : string
  11. extraProperties: {
  12. [name : string] : string | number | boolean
  13. }
  14. }

Streaming

If you want to perform a get or put in streaming mode, you can call the methods without a callback (and without the content argument for the put).

  1. const stream = connection.get('/my/file.txt');
  2. stream.on('data', (chunk) => {
  3. console.log(chunk.toString());
  4. })
  5. stream.on('end', () => {
  6. console.log('Done.');
  7. });
  1. const stream = connection.put('/my/file.txt');
  2. stream.on('finish', () => {
  3. console.log('Done.');
  4. });
  5. otherStream.pipe(stream);

Custom requests

To do custom requests, you can use the request(...) and stream(...) methods.

They take a RequestOptions argument.

  1. interface RequestOptions
  2. {
  3. url : string
  4. method : string
  5. headers ?: {
  6. [name : string] : string
  7. }
  8. body ?: ContentType
  9. }

Web browser compatibility

This library can be used in a web browser.

You can produce the web browser library from your from with, for instance, browserify or you can use the “browserified” file itsef (located at lib/browserified.js).

Here is the usage of the browserified.js file :

  1. <html>
  2. <head>
  3. <!-- Load the library -->
  4. <script src="node_modules/webdav-client/lib/browserified.js"></script>
  5. <!-- Usage of the library -->
  6. <script>
  7. const connection = new webdavClient.Connection('http://my-webdav-server:1900');
  8. connection.get('/path/of/my/file.txt', (e, content) => {
  9. if(e)
  10. throw e;
  11. console.log(content);
  12. });
  13. </script>
  14. </head>
  15. <body></body>
  16. </html>

Keep in mind that the library uses the request package, which might not be the most optimized package for web browsers.