feat: initial commit
This commit is contained in:
40
http/libraries/client.ts
Normal file
40
http/libraries/client.ts
Normal 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>;
|
||||
};
|
||||
Reference in New Issue
Block a user