@rxjsx/request
v1.0.2
Published
Simple Reactive HTTP Client
Downloads
1
Readme
RxJSx Request
A simple RxJS-based HTTP client
Install
npm install --save @rxjsx/request rxjs
The library RxJS is a peer dependency. The library is tested with RxJS v6 and v7.
Usage
TypeScript + RxJS
import { request } from '@rxjsx/request';
interface GitHubAPIList {
current_user_url: string;
}
request.get<GitHubAPIList>('https://api.github.com/')
.subscribe(response => {
const list: GitHubAPIList = response.json();
console.log(list.current_user_url);
});
JavaScript + Await
const { request } = require("@rxjsx/request");
const response = await request
.get('https://httpbin.org/get')
.toPromise();
console.log(response.json());
Try it here: https://runkit.com/aerabi/rxjsx-request-await
API
A name request
is exported which is both a function and a namespace. One can call it directly
by passing an object of type RequestOptions
:
const { request } = require("@rxjsx/request");
request({ method: 'GET', path: 'https://httpbin.org/get' })
.subscribe(console.log);
Or one can use the member functions:
const { request } = require("@rxjsx/request");
request.get('https://httpbin.org/get')
.subscribe(console.log);
The full API is as follows:
export declare function request<T, R>(options: RequestOptions<T>): Observable<Response<R>>;
export declare namespace request {
function get<R>(url: string, headers?: Record<string, string>): Observable<Response<R>>;
function post<T, R>(url: string, body?: T, headers?: Record<string, string>): Observable<Response<R>>;
function put<T, R>(url: string, body?: T, headers?: Record<string, string>): Observable<Response<R>>;
function patch<T, R>(url: string, body?: T, headers?: Record<string, string>): Observable<Response<R>>;
function del<R>(url: string, headers?: Record<string, string>): Observable<Response<R>>;
}
Notes
- The functions return a singleton observable, meaning that the observable will contain one element (otherwise would error out).