feat: initial commit

This commit is contained in:
2024-07-06 21:21:10 +02:00
commit 96f7c2cfe7
21 changed files with 1511 additions and 0 deletions

40
http/libraries/client.ts Normal file
View File

@@ -0,0 +1,40 @@
import { Request, type RequestMethod } from "./request.ts";
import { type Response } from "./response.ts";
export class Client {
constructor(readonly options: Deno.ConnectOptions | Deno.UnixConnectOptions) {}
/**
* Connection instance to use for a new fetch operation.
*
* Note! A new connection is spawned for every fetch request and is only automatically
* closed when accessing the .stream on the response. Otherwise a manual .close must
* be executed on the response to ensure that the connection is cleaned up.
*/
get connection() {
if ("path" in this.options) {
return Deno.connect(this.options);
}
return Deno.connect(this.options);
}
async fetch(path: string, { method, headers = {}, body }: RequestOptions): Promise<Response> {
const url = new URL(path);
return new Request(await this.connection, {
method,
path: url.pathname + url.search,
headers: {
Host: url.host,
"Content-Type": "application/json",
...headers,
},
body: body ? JSON.stringify(body) : undefined,
}).send();
}
}
type RequestOptions = {
method: RequestMethod;
headers?: RequestInit["headers"];
body?: Record<string, unknown>;
};