Getting Started
Coolio's HTTP Package
@coolio/http
is a simple, yet extendable HTTP client for web apps. It features:
body parsing and serialization, with case-conversion support
raw data (ArrayBuffer)
JSON
URL-encoded
plain text
multipart (FormData)
extensibility via interceptors, with built-ins such as:
logger
error handler
redirection interceptor
configurable requestHandler layer, which let's you choose the underlaying mechanism used for HTTP communication
Quick start
Installation
Install @coolio/http
package using npm
or yarn
.
Creating a client
You may create multiple HttpClient
instances, as your application connects to various APIs and each API probably has slightly different conventions. You can initialize a HttpClient and configure it as below:
In the example above, we create HttpClient
instance which will route all paths to api.example.com/v1
address:
if you request
/users
, the request will go toapi.example.com/v1/users
if you request
https://google.com
, the request will go togoogle.com
, as you specified full URL andbaseUrl
is not applied in such case.
Then, our httpClient
uses fetchRequestHandler
to deal with HTTP requests, in order to use Fetch API as request handler. Each requestHandler
in coolio acts as a bridge between natively available mechanisms (such as XmlHttpRequest, Fetch API or http module available in Node.js) and RawHttpResponse. Such Raw response has to be further processed by bodyParser
. BodyParser and BodySerializer automatically handle most common body types, such as JSON, URL-encoded, plain text, multipart and raw (for binary uploads). As a result we get a nicely processed HttpResponse, which lets us get response body, headers and status code of a response.
You can find all options directly in HttpClientConfig API docs.
Requests
httpClient
allows you to perform requests using all basic http methods. See HttpRequestOptions to check what can be passed there.
Repositories
You may use the repository pattern to separate API requests from other code. You can use previously defined HttpClient
as below:
After doing the following, you can simply call UserRepository.getProfile()
to receive the Promise returning User
object.
Another way of dealing with repositories is to use class-based approach:
Advanced Use Cases
If you happen to use JSON API, you may benefit from using a wrapper library that handles most of it's specs:
See @coolio/json-api docs for details.
Last updated