fluentrest-ts
Version:
A lightweight, fluent TypeScript API testing library inspired by Java's RestAssured. Built on top of Axios, JSONPath, and Joi for powerful request handling and response validation.
63 lines (62 loc) • 3 kB
TypeScript
import Joi from "joi";
import { AxiosProxyConfig, AxiosRequestConfig, AxiosResponse } from "axios";
import { LogLevel } from "./logger";
import { ResponseValidator } from "../contracts/response-validator-type";
/**
* A wrapper around the HTTP response (or error),
* allowing safe validation and debugging.
*/
export declare class ResponseValidatorImpl implements ResponseValidator {
private response?;
error?: any | undefined;
private config?;
private logLevel;
private logToFile;
private proxyOverride?;
private proxyAgent?;
constructor(response?: AxiosResponse | undefined, error?: any | undefined, config?: AxiosRequestConfig | undefined, logLevel?: LogLevel, logToFile?: boolean, proxyOverride?: AxiosProxyConfig | undefined, proxyAgent?: any | undefined);
/** Returns true if the request failed due to error or missing response. */
wasFailure(): boolean;
/** Returns the raw Axios response object (throws if not available). */
getResponse(): AxiosResponse;
/** Returns the Axios request config used to send the request. */
getRequestConfig(): AxiosRequestConfig;
/** Asserts that the response status matches the expected value. */
thenExpectStatus(status: number): this;
/** Asserts that a JSONPath value in the body matches the expected value. */
thenExpectBody(path: string, expected: any): this;
/** Asserts that the body contains a specified fragment of key-values. */
thenExpectBodyContains(fragment: object): this;
/** Validates the entire body against a Joi schema. */
thenValidateBody(schema: Joi.Schema): this;
/** Asserts that a response header matches the expected value. */
thenExpectHeader(key: string, value: string): this;
thenExtract(path: string): any;
/** Returns the parsed JSON body of the response (throws if unavailable). */
thenJson<T = any>(): T;
/**
* Executes a custom assertion or extraction callback
* and logs failure context if it throws.
*/
catchAndLog(fn?: (err: Error) => void): this;
/** Returns the raw error body (typically from server) if available. */
getErrorBody<T = any>(): T | undefined;
/**
* Runs multiple assertions on the current response context and aggregates any errors.
* This allows for soft-failing multiple expectations without throwing after the first failure.
*
* Each assertion receives the current response object (`this`) and is expected
* to throw an `Error` if it fails. All such errors are caught, aggregated,
* and re-thrown at the end as a combined error with summary output.
*
* @param assertions - Array of functions to run, each receiving the response context.
* @returns `this` for chaining.
*
* @example
* await res.runAssertions([
* r => r.thenExpectStatus(404),
* r => r.thenExpectBody('$.error', 'Not Found'),
* ]).catchAndLog(...);
*/
runAssertions(assertions: ((res: this) => void | Promise<void> | this)[]): this;
}