Skip to content

Commit

Permalink
feat: add WebDav method
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Jul 7, 2023
1 parent bea7de7 commit 16147c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(fn: () => Promise<T>) => Promise<T>;

export interface ReqOptions {
Expand Down Expand Up @@ -42,7 +43,11 @@ export interface RequestResponse<T> {
* const { statusCode, data } = await request("GET", "https://ws-dev.myunisoft.fr/ws_monitoring");
* console.log(statusCode, data); // 200 "true"
*/
export async function request<T>(method: HttpMethod, uri: string | URL, options: ReqOptions = {}): Promise<RequestResponse<T>> {
export async function request<T>(
method: HttpMethod | WebDavMethod,
uri: string | URL,
options: ReqOptions = {}
): Promise<RequestResponse<T>> {
const { maxRedirections = 0 } = options;

const computedURI = computeURI(uri);
Expand All @@ -59,7 +64,7 @@ export async function request<T>(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));
Expand Down
22 changes: 17 additions & 5 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReqOptions, "limit">;

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);
Expand All @@ -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<undici.Dispatcher.StreamData>;

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);

Expand All @@ -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
);
}

0 comments on commit 16147c2

Please sign in to comment.