Skip to content

Javascript and Typescript HTTP client based on observables

License

Notifications You must be signed in to change notification settings

d3byte/rxjs-http

Repository files navigation

rxjs-http

Build Status

HTTP client based on observables.

Compatible with both Javascript and Typescript.

Table of Content

Example

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
    });

Available methods

All methods you would probably need are available in the package. You can use all of those:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

Configuration

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.

Interfaces

ConfigurationInterface

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 with Bearer.
  • 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.

ObjectInterface

A simple representation of object with dynamic keys and values. It suits for any object you would ever pass.

Examples of all available methods

GET

httpClient
    .get(
        'user', 
        { MyCustomHeader: 'Value' },
    )
    .subscribe(user => {
        // Do something with data
    });

POST

httpClient
    .post(
        'login', 
        { username: 'user', password: 'pass' },
        { MyCustomHeader: 'Value' },
    )
    .subscribe(user => {
        // Do something with data
    });

PUT

httpClient
    .put(
        'books', 
        { title: 'Fav Book' },
        { MyCustomHeader: 'Value' },
    )
    .subscribe(data => {
        // Do something with data
    });

PATCH

httpClient
    .patch(
        'books', 
        { title: 'Fav Book' },
        { MyCustomHeader: 'Value' },
    )
    .subscribe(data => {
        // Do something with data
    });

DELETE

httpClient
    .delete(
        'books', 
        { title: 'Fav Book' },
        { MyCustomHeader: 'Value' },
    )
    .subscribe(data => {
        // Do something with data
    });