HTTP client based on observables.
Compatible with both Javascript and Typescript.
import { http } from '@d3byte/rxjs-http';
const configuration = {
baseUrl: 'http://localhost:8080',
};
const httpClient = http(configuration);
httpClient
.post(
'login',
{ username: 'user', password: 'pass' },
{ MyCustomHeader: 'Value' },
)
.subscribe(user => {
// Do something with data
});
All methods you would probably need are available in the package. You can use all of those:
- GET
- POST
- PUT
- PATCH
- DELETE
The package supports configuring clients and keeping multiple instances of clients at the same time.
To configure a client, you have to pass a configuration object to http
function.
This interface represents objects for configuring HTTP Client.
interface ConfigurationInterface {
jwt?: string;
baseUrl?: string;
defaultHeaders?: ObjectInterface;
}
Properties:
- jwt [optional] – property where you can store jwt token for communication with api. Client will automatically insert it into
Authorization
header withBearer
. - baseUrl [optional] – URL to API. For example,
http://localhost:8080
. Pay attention that there should not be a a slash (/
) at the end of the url. If no baseUrl passed, you will have to manually set the entire url in method calls. - defaultHeaders [optional] – an object with pairs of keys and strings. Here you can pass any default headers you want. Client will insert them into requests.
A simple representation of object with dynamic keys and values. It suits for any object you would ever pass.
httpClient
.get(
'user',
{ MyCustomHeader: 'Value' },
)
.subscribe(user => {
// Do something with data
});
httpClient
.post(
'login',
{ username: 'user', password: 'pass' },
{ MyCustomHeader: 'Value' },
)
.subscribe(user => {
// Do something with data
});
httpClient
.put(
'books',
{ title: 'Fav Book' },
{ MyCustomHeader: 'Value' },
)
.subscribe(data => {
// Do something with data
});
httpClient
.patch(
'books',
{ title: 'Fav Book' },
{ MyCustomHeader: 'Value' },
)
.subscribe(data => {
// Do something with data
});
httpClient
.delete(
'books',
{ title: 'Fav Book' },
{ MyCustomHeader: 'Value' },
)
.subscribe(data => {
// Do something with data
});