1.0.0 • Published 7 years ago

@fundcount/ts-rx-rest v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

ts-rx-rest

RxJs Http client for Typescript.

npm version

Provides convenient typed wrappers for http verbs:

  • doGet
  • doPost
  • doPut
  • doDelete

Install

$ npm install --save ts-rx-rest

Usage

import Rest, {errorInterceptor, jsonInterceptor, withCredentialsInterceptors} from 'ts-rx-rest';

const rest = new Rest()
    .wrapRequest(withCredentialsInterceptors) // or .withCredentials()
    .wrap(errorInterceptor) // forwards an XMLHttpRequest object to an error branch of the observable
    .wrap(jsonInterceptor); // converts text representation of the response to json

rest.doGet<Array<User>>('/users').subscribe(users => console.log(users));

Custom interceptors

Request interceptors

const withCredentialsInterceptors = (r: XMLHttpRequest) => {
    r.withCredentials = true;
    return r;
};

Response interceptors

const accessDenied = (observable: Observable<any>) =>
    observable.doOnError(err => {
        if (err.response.status === 403 || err.response.status === 401) {
            // Do some stuff
        }
    });