Files
testcontainers/http/libraries/client.ts
2025-04-17 17:23:05 +00:00

47 lines
1.5 KiB
TypeScript

import { existsSync } from "@std/fs";
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(): Promise<Deno.UnixConn> | Promise<Deno.TcpConn> {
if ("path" in this.options) {
const hasDockerSock = existsSync(this.options.path);
if (hasDockerSock === false) {
throw new Error(`Failed to resolve '${this.options.path}' unix connection path.`);
}
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>;
};