HTTP Request. This class shouldn't be instanciated directly. Use HTTPRequestFactory createXXXRequest() instead

Constructors

Accessors

  • get meta(): Record<string, any>
  • Get the meta data that was set on the request.

    Returns Record<string, any>

    The meta data of the object.

  • get url(): string
  • Gets the URL of the request.

    Returns string

    the URL of the request

Methods

  • Clears the config builders array and returns the instance. Useful in cases where you want to create a new request that doesn't inherit from API/factory settings that might have headers or other unwanted configuration

    Returns HTTPRequest

    the updated request

  • Executes the fetch request and returns a Promise that resolves with the parsed result.

    Returns Promise<any>

    A Promise that resolves with the result of the request.

  • Sets the accepted MIME types for the request.

    Parameters

    • Rest...mimeTypes: any[]

      An array of MIME types to accept.

    Returns HTTPRequest

    • The updated request instance.
  • Short-hand for setting the accepted MIME types to ['/'] which means the API accepts any MIME type.

    Returns HTTPRequest

    • The current object instance.
  • Sets the credentials policy for the HTTP request.

    Parameters

    • config: RequestCredentials

      The configuration for the credentials.

    Returns HTTPRequest

    • The updated HTTP request instance.
  • Set the request body to a FormData object and allows customizing the form data before sending the request.

    Parameters

    • composerCallBack: ((formData: FormData) => void) = ...

      the callback function that customizes the FormData object

        • (formData): void
        • Parameters

          • formData: FormData

          Returns void

    Returns HTTPRequest

  • Parameters

    • headers: Record<string, HeaderValue>

      name-value pairs to set as headers If value is undefined, the corresponding header will be removed if present

    Returns HTTPRequest

    • The updated request instance.
  • Set the request body as a JSON object or string.

    Parameters

    • json: any

      The JSON object or string to set as the request body.

    Returns HTTPRequest

    • The updated request instance.
  • Sets an ILogger compatible logger for the request. Normally the logger will be set by the factory.

    Parameters

    • logger: ILogger

      The logger to be set.

    Returns HTTPRequest

    • The updated HTTP request instance.
  • Parameters

    • level: LogLevel

      the log level to apply for this request. One of LOG_LEVEL_ERROR, LOG_LEVEL_WARN, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG defined in constants.js Overrides the default log level.

    Returns HTTPRequest

    • The updated request instance.
  • Configures the request with metadata that can be inspected later.

    Parameters

    • param1: string | Record<string, any>

      The key or object containing the key-value pairs to update the meta property.

    • Optionalparam2: any

      The value to associate with the key when param1 is a string.

    Returns HTTPRequest

    • Returns the current object instance for method chaining.
  • Adds a request interceptor to the request configuration. Interceptors are executed in the order they are added.

    • If a request interceptor returns a rejected promise, the request will fail.
    • If a request interceptor returns a resolved promise, the promise's result will be used as the response.
    • If the interceptor returns undefined, the request will continue to the next interceptor, if present, or to the regular request handling
    • the interceptor's second parameter is is a function that can be used to remove the interceptor from further request handling

    Parameters

    Returns void

    • The updated request instance.
  • Sets the response body transformer for the request. The provided function will be called after the request body is parsed. This is especially useful when used in conjuncion with APIs definition to hide some data massaging logic specific to the api.

    Parameters

    Returns HTTPRequest

    • The updated instance of the class.
    factory.withAPIConfig({
    name : 'some-api',
    responseBodyTransformer : (body, request) => {
    //the response.details is a JSON string that we want
    //to parse before the app receives the response
    body.details = JSON.parse(body.details)
    return body;
    }),
    endpoints : {
    'get-stuff' : {
    endpoint : '/get-stuff'
    }
    }
    };

    const response = factory
    .createAPIRequest('some-api', 'get-stuff')
    .execute();
    console.log(response.details.some.deep.data);
  • Sets the uriEncodedBody property of the config object to true. This function is used to indicate that the body of the request should be URL encoded.

    Returns HTTPRequest

    • The updated instance of the class.
  • Adds a URL parameter to the request configuration.

    Parameters

    • name: string

      The name of the URL parameter.

    • value: string

      The value of the URL parameter.

    Returns HTTPRequest

    • The updated request instance.