@probitas/client-http
HTTP client for Probitas scenario testing framework.
This package provides an HTTP client designed for integration testing of HTTP APIs.
Features
- All HTTP Methods: Support for GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
- Request Building: Headers, query parameters, body (JSON, form, multipart)
- Response Inspection: Status codes, headers, cookies, body parsing
- Duration Tracking: Built-in timing for performance monitoring
- Resource Management: Implements
AsyncDisposablefor proper cleanup
Installation
deno add jsr:@probitas/client-http
Quick Start
import { createHttpClient } from "@probitas/client-http";
const http = createHttpClient({ url: "http://localhost:3000" });
// GET request
const res = await http.get("/users/123");
console.log("Status:", res.status);
// Extract typed data
const user = res.json<User>();
// POST request
const created = await http.post("/users", {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Jane" }),
});
console.log("Created:", created.status);
await http.close();
Using with using Statement
await using http = createHttpClient({ url: "http://localhost:3000" });
const res = await http.get("/health");
console.log("Health:", res.ok);
// Client automatically closed when block exits
Related Packages
| Package | Description |
|---|---|
@probitas/client |
Core utilities and types |
@probitas/client-graphql |
GraphQL client |
Links
Installation
deno add jsr:@probitas/client-httpClasses
#HttpBadRequestError
class HttpBadRequestError extends HttpErrorHttpErrorHTTP 400 Bad Request error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpBadRequestError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status400 - readonly
statusTextstring
#HttpConflictError
class HttpConflictError extends HttpErrorHttpErrorHTTP 409 Conflict error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpConflictError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status409 - readonly
statusTextstring
#HttpError
class HttpError extends ClientErrorClientErrorBase HTTP error class.
| Name | Description |
|---|---|
name | — |
kind | — |
status | HTTP status code |
statusText | HTTP status text |
response | Associated HTTP response (if available) |
Constructor
new HttpError(message: string, status: number, statusText: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
kind"http" - readonly
statusnumberHTTP status code
- readonly
statusTextstringHTTP status text
Associated HTTP response (if available)
#HttpForbiddenError
class HttpForbiddenError extends HttpErrorHttpErrorHTTP 403 Forbidden error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpForbiddenError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status403 - readonly
statusTextstring
#HttpInternalServerError
class HttpInternalServerError extends HttpErrorHttpErrorHTTP 500 Internal Server Error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpInternalServerError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status500 - readonly
statusTextstring
#HttpNotFoundError
class HttpNotFoundError extends HttpErrorHttpErrorHTTP 404 Not Found error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpNotFoundError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status404 - readonly
statusTextstring
#HttpTooManyRequestsError
class HttpTooManyRequestsError extends HttpErrorHttpErrorHTTP 429 Too Many Requests error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpTooManyRequestsError(message: string, options?: HttpErrorOptions)Properties
- readonly
namestring - readonly
status429 - readonly
statusTextstring
#HttpUnauthorizedError
class HttpUnauthorizedError extends HttpErrorHttpErrorHTTP 401 Unauthorized error.
| Name | Description |
|---|---|
name | — |
status | — |
statusText | — |
Constructor
new HttpUnauthorizedError(message: string, options?: HttpErrorOptions)Properties
Interfaces
#CookieConfig
interface CookieConfigCookie handling configuration.
| Name | Description |
|---|---|
disabled | Disable automatic cookie handling. |
initial | Initial cookies to populate the cookie jar. |
Properties
- readonly
disabled?booleanDisable automatic cookie handling. When disabled, cookies are not stored or sent automatically.
- readonly
initial?Record<string, string>Initial cookies to populate the cookie jar.
#HttpClient
interface HttpClient extends AsyncDisposableHTTP client interface.
| Name | Description |
|---|---|
config | Client configuration |
get() | Send GET request |
post() | Send POST request |
put() | Send PUT request |
patch() | Send PATCH request |
delete() | Send DELETE request |
request() | Send request with arbitrary method |
getCookies() | Get all cookies in the cookie jar. |
setCookie() | Set a cookie in the cookie jar. |
clearCookies() | Clear all cookies from the cookie jar. |
close() | Close the client and release resources |
Properties
Client configuration
Methods
get(path: string, options?: HttpOptions): Promise<HttpResponse>Send GET request
Parameters
pathstringoptions?HttpOptions
post(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>Send POST request
Parameters
pathstringbody?BodyInitoptions?HttpOptions
put(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>Send PUT request
Parameters
pathstringbody?BodyInitoptions?HttpOptions
patch(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>Send PATCH request
Parameters
pathstringbody?BodyInitoptions?HttpOptions
delete(path: string, options?: HttpOptions): Promise<HttpResponse>Send DELETE request
Parameters
pathstringoptions?HttpOptions
request(method: string, path: string, options?: HttpOptions & { body?: BodyInit }): Promise<HttpResponse>Send request with arbitrary method
Parameters
methodstringpathstringoptions?HttpOptions & { body?: BodyInit }
getCookies(): Record<string, string>Get all cookies in the cookie jar. Returns empty object if cookies are disabled.
setCookie(name: string, value: string): voidSet a cookie in the cookie jar.
Parameters
namestringvaluestring
clearCookies(): voidClear all cookies from the cookie jar. No-op if cookies are disabled.
close(): Promise<void>Close the client and release resources
#HttpClientConfig
interface HttpClientConfig extends CommonOptionsHTTP client configuration.
| Name | Description |
|---|---|
url | Base URL for all requests. |
headers | Default headers for all requests |
fetch | Custom fetch implementation (for testing/mocking) |
redirect | Default redirect handling mode. |
throwOnError | Whether to throw HttpError for non-2xx responses. |
cookies | Cookie handling configuration. |
Properties
Base URL for all requests.
Can be a URL string or a connection configuration object.
- readonly
headers?Record<string, string>Default headers for all requests
- readonly
fetch?fetchCustom fetch implementation (for testing/mocking)
Default redirect handling mode. Can be overridden per-request via HttpOptions.
- readonly
throwOnError?booleanWhether to throw HttpError for non-2xx responses. Can be overridden per-request via HttpOptions.
#HttpConnectionConfig
interface HttpConnectionConfig extends CommonConnectionConfigHTTP connection configuration.
Extends CommonConnectionConfig with HTTP-specific options.
Properties
- readonly
protocol?"http" | "https"Protocol to use.
- readonly
path?stringBase path prefix for all requests.
#HttpErrorOptions
interface HttpErrorOptions extends ErrorOptionsOptions for HttpError constructor.
| Name | Description |
|---|---|
response | Associated HTTP response |
Properties
Associated HTTP response
#HttpOptions
interface HttpOptions extends CommonOptionsOptions for individual HTTP requests.
| Name | Description |
|---|---|
query | Query parameters (arrays for multi-value params) |
headers | Additional request headers |
redirect | Redirect handling mode. |
throwOnError | Whether to throw HttpError for non-2xx responses. |
Properties
Query parameters (arrays for multi-value params)
- readonly
headers?Record<string, string>Additional request headers
Redirect handling mode.
- readonly
throwOnError?booleanWhether to throw HttpError for non-2xx responses. When false, non-2xx responses are returned as HttpResponse.
#HttpResponse
interface HttpResponseHTTP response with pre-loaded body for synchronous access.
Wraps Web standard Response, allowing body to be read synchronously and multiple times (unlike the streaming-based standard Response).
| Name | Description |
|---|---|
type | Result type identifier |
ok | Whether the response was successful (status 200-299) |
status | HTTP status code |
statusText | HTTP status text |
headers | Response headers |
url | Request URL |
body | Response body as raw bytes (null if no body) |
duration | Response time in milliseconds |
raw | Raw Web standard Response (for streaming or special cases) |
arrayBuffer() | Get body as ArrayBuffer (null if no body) |
blob() | Get body as Blob (null if no body) |
text() | Get body as text (null if no body) |
data() | Get body as parsed JSON (null if no body) |
Properties
- readonly
type"http"Result type identifier
- readonly
okbooleanWhether the response was successful (status 200-299)
- readonly
statusnumberHTTP status code
- readonly
statusTextstringHTTP status text
- readonly
headersHeadersResponse headers
- readonly
urlstringRequest URL
- readonly
bodyUint8Array | nullResponse body as raw bytes (null if no body)
- readonly
durationnumberResponse time in milliseconds
- readonly
rawglobalThis.ResponseRaw Web standard Response (for streaming or special cases)
Methods
arrayBuffer(): ArrayBuffer | nullGet body as ArrayBuffer (null if no body)
blob(): Blob | nullGet body as Blob (null if no body)
text(): string | nullGet body as text (null if no body)
data<T = any>(): T | nullGet body as parsed JSON (null if no body)
Functions
#createHttpClient
function createHttpClient(config: HttpClientConfig): HttpClientCreate a new HTTP client instance.
The client provides methods for making HTTP requests with automatic cookie handling, response body pre-loading, and error handling.
Parameters
configHttpClientConfig- Client configuration including URL and default options
Returns
HttpClient — A new HTTP client instance
Examples
Basic usage with string URL
const http = createHttpClient({ url: "http://localhost:3000" });
const response = await http.get("/users/123");
console.log(response.data());
await http.close();
With connection config object
const http = createHttpClient({
url: { host: "api.example.com", port: 443, protocol: "https" },
});
With default headers
const http = createHttpClient({
url: "http://localhost:3000",
headers: {
"Authorization": "Bearer token123",
"Accept": "application/json",
},
});
Using await using for automatic cleanup
await using http = createHttpClient({ url: "http://localhost:3000" });
const response = await http.get("/health");
// Client automatically closed when scope exits
#createHttpResponse
async function createHttpResponse(raw: globalThis.Response, duration: number): Promise<HttpResponse>Create HttpResponse from raw Response.
Consumes the response body and creates a reusable HttpResponse.
Parameters
rawglobalThis.Responsedurationnumber
Type Aliases
#BodyInit
type BodyInit = string | Uint8Array | Record<string, unknown> | FormData | URLSearchParamsRequest body type.
#RedirectMode
type RedirectMode = "follow" | "manual" | "error"Redirect handling mode.
- "follow": Automatically follow redirects (default)
- "manual": Return redirect response without following
- "error": Throw error on redirect
