File: src/lib/HTTP.ts

Recommend this page to a friend!
  Classes of Dom Hastings   JS Webdav Client   src/lib/HTTP.ts   Download  
File: src/lib/HTTP.ts
Role: Example script
Content type: text/plain
Description: Example script
Class: JS Webdav Client
Access files of a Webdav server
Author: By
Last change:
Date: 6 months ago
Size: 1,852 bytes
 

Contents

Class file image Download
import RequestFailure from './HTTP/RequestFailure'; type HTTPMethods = | 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE'; type WebDAVMethods = | HTTPMethods | 'COPY' | 'LOCK' | 'MKCOL' | 'MOVE' | 'PROPFIND' | 'PROPPATCH' | 'UNLOCK'; type MethodParams = { [K in WebDAVMethods]?: RequestInit; }; const defaultParams: MethodParams = { PROPFIND: { headers: { Depth: '1', }, }, }; const method = async ( method: string, url: RequestInfo, parameters: RequestInit ): Promise<Response> => { const request = new Request(url, { ...(defaultParams[method] || {}), ...parameters, method, }); const response = await fetch(request); if (!response.ok) { throw new RequestFailure(request, response); } return response; }; export class HTTP { GET(url: string, parameters: RequestInit = {}): Promise<Response> { return method('GET', url, parameters); } HEAD(url: string, parameters: RequestInit = {}): Promise<Response> { return method('HEAD', url, parameters); } PUT(url: string, parameters: RequestInit = {}): Promise<Response> { return method('PUT', url, parameters); } PROPFIND(url: string, parameters: RequestInit = {}): Promise<Response> { return method('PROPFIND', url, parameters); } DELETE(url: string, parameters: RequestInit = {}): Promise<Response> { return method('DELETE', url, parameters); } MKCOL(url: string, parameters: RequestInit = {}): Promise<Response> { return method('MKCOL', url, parameters); } COPY(url: string, parameters: RequestInit = {}): Promise<Response> { return method('COPY', url, parameters); } MOVE(url: string, parameters: RequestInit = {}): Promise<Response> { return method('MOVE', url, parameters); } } export default HTTP;