A factory for creating HTTPRequest instances. It can be configured with defaults, logging options as well as conditional settings using when in a method-chain fashion.

Constructors

Methods

  • Creates a HTTPRequest with configuration based on the given APIConfig's name and endpoint name. It also populates the request's meta with info about the API and endpoint inside request.meta.api merging in any meta defined in the api config's api.meta and endpoint.meta.

    Parameters

    • Rest...args: [string, string] | [string]

    Returns HTTPRequest

    The created request.

    factory.createAPIRequest('my-api', 'my-endpoint')
    .withQueryParam('key', 'value')
    .withHeader('X-PoweredBy', 'Me')
    .execute();
  • Adds a condition for the application of method-chain settings. It can be reset by calling always

    Parameters

    • condition: ((request: HTTPRequest) => boolean)

      A function that takes a HTTPRequest object and returns whether or not to apply the condition.

        • (request): boolean
        • Parameters

          Returns boolean

    Returns HTTPRequestFactory

    • A proxy to the factory instance that allows the conditional configuration
    factory
    .when((request) => request.meta.requiresAuth)
    .withHeader('Authorization', 'some-token')
    .always()
    .withHeader('X-PoweredBy', 'Me')
  • 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 contains commands InterceptorCommands to replace the requests URL or to completely remove the interceptor from configuration

    Parameters

    Returns HTTPRequestFactory

    • The updated request instance.