From 16147c2c1bdadf56055c9e92c73cc1f4e4b2c0f1 Mon Sep 17 00:00:00 2001 From: fraxken Date: Sun, 25 Jun 2023 13:16:45 +0200 Subject: [PATCH] feat: add WebDav method --- src/request.ts | 11 ++++++++--- src/stream.ts | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/request.ts b/src/request.ts index 8f5261a..0568cba 100644 --- a/src/request.ts +++ b/src/request.ts @@ -10,7 +10,8 @@ import status from "statuses"; import * as Utils from "./utils"; import { computeURI } from "./agents"; -export type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH"; +export type WebDavMethod = "MKCOL" | "COPY" | "MOVE" | "LOCK" | "UNLOCK" | "PROPFIND" | "PROPPATCH"; +export type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" ; export type InlineCallbackAction = (fn: () => Promise) => Promise; export interface ReqOptions { @@ -42,7 +43,11 @@ export interface RequestResponse { * const { statusCode, data } = await request("GET", "https://ws-dev.myunisoft.fr/ws_monitoring"); * console.log(statusCode, data); // 200 "true" */ -export async function request(method: HttpMethod, uri: string | URL, options: ReqOptions = {}): Promise> { +export async function request( + method: HttpMethod | WebDavMethod, + uri: string | URL, + options: ReqOptions = {} +): Promise> { const { maxRedirections = 0 } = options; const computedURI = computeURI(uri); @@ -59,7 +64,7 @@ export async function request(method: HttpMethod, uri: string | URL, options: const headers = Utils.createHeaders({ headers: options.headers, authorization: options.authorization }); const body = Utils.createBody(options.body, headers); - const requestOptions = { method, headers, body, dispatcher, maxRedirections }; + const requestOptions = { method: method as HttpMethod, headers, body, dispatcher, maxRedirections }; const requestResponse = limit === null ? await undici.request(computedURI.url, requestOptions) : await limit(() => undici.request(computedURI.url, requestOptions)); diff --git a/src/stream.ts b/src/stream.ts index a80f79a..6568cd3 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -5,13 +5,17 @@ import { Duplex, Writable } from "stream"; import * as undici from "undici"; // Import Internal Dependencies -import { ReqOptions, HttpMethod } from "./request"; +import { ReqOptions, HttpMethod, WebDavMethod } from "./request"; import { computeURI } from "./agents"; import * as Utils from "./utils"; export type StreamOptions = Omit; -export function pipeline(method: HttpMethod, uri: string | URL, options: StreamOptions = {}): Duplex { +export function pipeline( + method: HttpMethod | WebDavMethod, + uri: string | URL, + options: StreamOptions = {} +): Duplex { const { maxRedirections = 0 } = options; const computedURI = computeURI(uri); @@ -27,13 +31,17 @@ export function pipeline(method: HttpMethod, uri: string | URL, options: StreamO const body = Utils.createBody(options.body, headers); return undici.pipeline(computedURI.url, { - method, headers, body, dispatcher, maxRedirections + method: method as HttpMethod, headers, body, dispatcher, maxRedirections }, ({ body }) => body); } export type WritableStreamCallback = (writable: Writable) => Promise; -export function stream(method: HttpMethod, uri: string | URL, options: StreamOptions = {}): WritableStreamCallback { +export function stream( + method: HttpMethod | WebDavMethod, + uri: string | URL, + options: StreamOptions = {} +): WritableStreamCallback { const { maxRedirections = 0 } = options; const computedURI = computeURI(uri); @@ -42,5 +50,9 @@ export function stream(method: HttpMethod, uri: string | URL, options: StreamOpt const body = Utils.createBody(options.body, headers); return (writable: Writable) => undici - .stream(computedURI.url, { method, headers, body, dispatcher, maxRedirections }, () => writable); + .stream( + computedURI.url, + { method: method as HttpMethod, headers, body, dispatcher, maxRedirections }, + () => writable + ); }