Fluent-style API for building up HTTP requests.

Constructors

Properties

_options: RequestInit = {}
config: BuilderConfig
queryParser: QueryParser = defaultQueryParser

Methods

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

    Parameters

    • body: BodyInit

      the raw body to be set on the request

    Returns RequestBuilder

    the builder object, for chaining

  • 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.

    Parameters

    • options: Partial<RequestInit>

      options which will be passed directly to fetch

    Returns RequestBuilder

    the builder object, for chaining

  • 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

    Parameters

    • params: Record<string, unknown>

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

    Returns RequestBuilder

    the builder object, for chaining

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

    Parameters

    • key: string

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

    • value: string

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

    Returns RequestBuilder

    the builder object, for chaining

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

    Parameters

    • value: any

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

    Returns RequestBuilder

    the builder object, for chaining

  • 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.

    Parameters

    • params: Record<string, unknown>

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

    Returns RequestBuilder

    the builder object, for chaining

    Example

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

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

    Returns RequestOptions

    options for making a fetch request

Generated using TypeDoc