@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 AsyncDisposable for 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
Package Description
@probitas/client Core utilities and types
@probitas/client-graphql GraphQL client

Installation

deno add jsr:@probitas/client-http

Classes

class

#HttpBadRequestError

class HttpBadRequestError extends HttpError
ExtendsHttpError

HTTP 400 Bad Request error.

NameDescription
name
status
statusText
Constructor
new HttpBadRequestError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus400
  • readonlystatusTextstring
class

#HttpConflictError

class HttpConflictError extends HttpError
ExtendsHttpError

HTTP 409 Conflict error.

NameDescription
name
status
statusText
Constructor
new HttpConflictError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus409
  • readonlystatusTextstring
class

#HttpError

class HttpError extends ClientError

Base HTTP error class.

NameDescription
name
kind
statusHTTP status code
statusTextHTTP status text
responseAssociated HTTP response (if available)
Constructor
new HttpError(message: string, status: number, statusText: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"http"
  • readonlystatusnumber

    HTTP status code

  • readonlystatusTextstring

    HTTP status text

  • readonlyresponse?HttpResponse

    Associated HTTP response (if available)

class

#HttpForbiddenError

class HttpForbiddenError extends HttpError
ExtendsHttpError

HTTP 403 Forbidden error.

NameDescription
name
status
statusText
Constructor
new HttpForbiddenError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus403
  • readonlystatusTextstring
class

#HttpInternalServerError

class HttpInternalServerError extends HttpError
ExtendsHttpError

HTTP 500 Internal Server Error.

NameDescription
name
status
statusText
Constructor
new HttpInternalServerError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus500
  • readonlystatusTextstring
class

#HttpNotFoundError

class HttpNotFoundError extends HttpError
ExtendsHttpError

HTTP 404 Not Found error.

NameDescription
name
status
statusText
Constructor
new HttpNotFoundError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus404
  • readonlystatusTextstring
class

#HttpTooManyRequestsError

class HttpTooManyRequestsError extends HttpError
ExtendsHttpError

HTTP 429 Too Many Requests error.

NameDescription
name
status
statusText
Constructor
new HttpTooManyRequestsError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus429
  • readonlystatusTextstring
class

#HttpUnauthorizedError

class HttpUnauthorizedError extends HttpError
ExtendsHttpError

HTTP 401 Unauthorized error.

NameDescription
name
status
statusText
Constructor
new HttpUnauthorizedError(message: string, options?: HttpErrorOptions)
Properties
  • readonlynamestring
  • readonlystatus401
  • readonlystatusTextstring

Interfaces

interface

#CookieConfig

interface CookieConfig

Cookie handling configuration.

NameDescription
disabledDisable automatic cookie handling.
initialInitial cookies to populate the cookie jar.
Properties
  • readonlydisabled?boolean

    Disable automatic cookie handling. When disabled, cookies are not stored or sent automatically.

  • readonlyinitial?Record<string, string>

    Initial cookies to populate the cookie jar.

interface

#HttpClient

interface HttpClient extends AsyncDisposable

HTTP client interface.

NameDescription
configClient 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
Methods
get(path: string, options?: HttpOptions): Promise<HttpResponse>

Send GET request

Parameters
post(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>

Send POST request

Parameters
put(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>

Send PUT request

Parameters
patch(path: string, body?: BodyInit, options?: HttpOptions): Promise<HttpResponse>

Send PATCH request

Parameters
delete(path: string, options?: HttpOptions): Promise<HttpResponse>

Send DELETE request

Parameters
request(method: string, path: string, options?: HttpOptions & { body?: BodyInit }): Promise<HttpResponse>

Send request with arbitrary method

Parameters
getCookies(): Record<string, string>

Get all cookies in the cookie jar. Returns empty object if cookies are disabled.

setCookie(name: string, value: string): void

Set a cookie in the cookie jar.

Parameters
  • namestring
  • valuestring
clearCookies(): void

Clear all cookies from the cookie jar. No-op if cookies are disabled.

close(): Promise<void>

Close the client and release resources

interface

#HttpClientConfig

interface HttpClientConfig extends CommonOptions

HTTP client configuration.

NameDescription
urlBase URL for all requests.
headersDefault headers for all requests
fetchCustom fetch implementation (for testing/mocking)
redirectDefault redirect handling mode.
throwOnErrorWhether to throw HttpError for non-2xx responses.
cookiesCookie handling configuration.
Properties
  • readonlyurlstring | HttpConnectionConfig

    Base URL for all requests.

    Can be a URL string or a connection configuration object.

  • readonlyheaders?Record<string, string>

    Default headers for all requests

  • readonlyfetch?fetch

    Custom fetch implementation (for testing/mocking)

  • readonlyredirect?RedirectMode

    Default redirect handling mode. Can be overridden per-request via HttpOptions.

  • readonlythrowOnError?boolean

    Whether to throw HttpError for non-2xx responses. Can be overridden per-request via HttpOptions.

  • readonlycookies?CookieConfig

    Cookie handling configuration. By default, the client maintains a cookie jar for automatic cookie management across requests. Set cookies: { disabled: true } to disable.

interface

#HttpConnectionConfig

interface HttpConnectionConfig extends CommonConnectionConfig

HTTP connection configuration.

Extends CommonConnectionConfig with HTTP-specific options.

NameDescription
protocolProtocol to use.
pathBase path prefix for all requests.
Properties
  • readonlyprotocol?"http" | "https"

    Protocol to use.

  • readonlypath?string

    Base path prefix for all requests.

interface

#HttpErrorOptions

interface HttpErrorOptions extends ErrorOptions

Options for HttpError constructor.

NameDescription
responseAssociated HTTP response
Properties
interface

#HttpOptions

interface HttpOptions extends CommonOptions

Options for individual HTTP requests.

NameDescription
queryQuery parameters (arrays for multi-value params)
headersAdditional request headers
redirectRedirect handling mode.
throwOnErrorWhether to throw HttpError for non-2xx responses.
Properties
  • readonlyquery?Record<string, QueryValue | QueryValue[]>

    Query parameters (arrays for multi-value params)

  • readonlyheaders?Record<string, string>

    Additional request headers

  • readonlyredirect?RedirectMode

    Redirect handling mode.

  • readonlythrowOnError?boolean

    Whether to throw HttpError for non-2xx responses. When false, non-2xx responses are returned as HttpResponse.

interface

#HttpResponse

interface HttpResponse

HTTP 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).

NameDescription
typeResult type identifier
okWhether the response was successful (status 200-299)
statusHTTP status code
statusTextHTTP status text
headersResponse headers
urlRequest URL
bodyResponse body as raw bytes (null if no body)
durationResponse time in milliseconds
rawRaw 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
  • readonlytype"http"

    Result type identifier

  • readonlyokboolean

    Whether the response was successful (status 200-299)

  • readonlystatusnumber

    HTTP status code

  • readonlystatusTextstring

    HTTP status text

  • readonlyheadersHeaders

    Response headers

  • readonlyurlstring

    Request URL

  • readonlybodyUint8Array | null

    Response body as raw bytes (null if no body)

  • readonlydurationnumber

    Response time in milliseconds

  • readonlyrawglobalThis.Response

    Raw Web standard Response (for streaming or special cases)

Methods
arrayBuffer(): ArrayBuffer | null

Get body as ArrayBuffer (null if no body)

blob(): Blob | null

Get body as Blob (null if no body)

text(): string | null

Get body as text (null if no body)

data<T = any>(): T | null

Get body as parsed JSON (null if no body)

Functions

function

#createHttpClient

function createHttpClient(config: HttpClientConfig): HttpClient

Create 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
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
function

#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.Response
  • durationnumber

Type Aliases

type

#BodyInit

type BodyInit = string | Uint8Array | Record<string, unknown> | FormData | URLSearchParams

Request body type.

type

#QueryValue

type QueryValue = string | number | boolean

Query parameter value type.

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
Search Documentation