Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WebDav method #82

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}