Fluent-style API for building up HTTP requests.

Hierarchy

  • RequestBuilder

Constructors

Properties

_options: RequestInit = {}
config: BuilderConfig

Methods

  • Set the raw body on the request. This could be as simple as a string, or as complex as a stream.

    Returns

    the builder object, for chaining

    Parameters

    • body: BodyInit

      the raw body to be set on the request

    Returns RequestBuilder

  • Override fetch options directly. This is useful if you want to provide highly customized logic to the underlying request, such as providing an AbortController.

    Note that this merges your options directly with the builder's underlying options, so if, for example, you previously set a header, but now override headers, your previous changes will not be reflected in the request. For this reason, it's strongly recommended to use this method only for options that are not provided elsewhere on the builder.

    Returns

    the builder object, for chaining

    Parameters

    • options: Partial<RequestInit>

      options which will be passed directly to fetch

    Returns RequestBuilder

  • Encodes an object as form data and sets it as the body of the request, along with setting the Content-Type header to multipart/form-data

    Returns

    the builder object, for chaining

    Parameters

    • params: Record<string, unknown>

      a JSON-like object to be encoded as form data on the request

    Returns RequestBuilder

  • Sets a key/value pair as a header on the request.

    Returns

    the builder object, for chaining

    Parameters

    • key: string

      the header key, e.g. 'Content-Type'

    • value: string

      the header value, e.g. 'text/plain'

    Returns RequestBuilder

  • Encodes a value as JSON and sets it as the body of the request, along with setting the Content-Type header to application/json

    Returns

    the builder object, for chaining

    Parameters

    • value: any

      a JSON-stringifiable value to be encoded as JSON on the request

    Returns RequestBuilder

  • Encodes and sets query parameters on the URL. Uses the query parser that the RequestBuilder was intiialized with. To customize how query parameters are parsed, you can pass in a function into the microtest runner configuration.

    Example

    const runner = microtest('http://localhost:3000', {
    queryParser: (paramsObject) => 'custom parser logic'
    })

    Returns

    the builder object, for chaining

    Parameters

    • params: Record<string, unknown>

      an object representing the query params to be stringified and set on the request

    Returns RequestBuilder

  • Private

    Serializes all the current options into an object which can be used to make a request using the fetch API.

    Returns

    options for making a fetch request

    Returns RequestOptions

Generated using TypeDoc