request

Classes

HttpError
NetworkError
ValidResponse

Methods

(async, static) request(url, methodopt, bodyopt, optsopt) → {Promise.<module:request.ValidResponse>}

Make a request to a URL.

Parameters:
Name Type Attributes Default Description
url string

the URL to send a request to.

method String <optional>
"GET"

the HTTP method to use (GET, POST, PUT, DELETE)

body Object | String <optional>

the content of the body of the request. If it's an object, will automatically apply JSON.stringify; otherwise, will send it as a string. Use undefined to specify no message body.

opts * <optional>
{}

fetch options. See fetch() init param: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch

Source:
Throws:
  • If the response contains an HTTP Status Code in the error range (4xx or 5xx), then request() will throw an HttpError. This can be useful to detect, for example, a 401 Unauthorized, a 404 Not Found, a 500 Internal Server Error, or any other HTTP status code.

    This error type can be detected by checking if error instanceof HttpError.

    Type
    module:request.HttpError
  • In the event of a network error, a NetworkError will throw. This can happen if:

    • the user disconnects from their WiFi network
    • the servers are down
    • the request times out

    In the real world, this can happen if a user travels through a tunnel, disconnecting them from their mobile network. It's an important edge case to account for.

    This error type can be detected by checking if error instanceof NetworkError.

    Type
    module:request.NetworkError