import axios , { AxiosInstance , AxiosRequestConfig , AxiosResponse , AxiosError , } from 'axios' ; import { Observable , Observer } from 'rxjs' ; import { RemoteException } from '../error' ; export interface RxRemoteProvider { summary POST param url param data returns RemoteException post < T > ( url , data ) : Observable < AxiosResponse < T > > ; summary GET param url returns RemoteException get < T > ( url ) : Observable < AxiosResponse < T > > ; summary PUT param url param data returns RemoteException put < T > ( url , data ) : Observable < AxiosResponse < T > > ; summary DELETE param url returns RemoteException delete < T > ( url ) : Observable < AxiosResponse < T > > ; } export class RxAxiosProviderException extends RemoteException < AxiosError > { } export class BearerAuthorizationRxAxiosProvider < Result = any > implements RxRemoteProvider { private readonly axiosInstance : AxiosInstance ; private token ? : string ; constructor ( config ) { this . axiosInstance = axios . create ( config ) ; } request < T > ( requestConfig ) { return Observable . create ( async ( observer ) => { try { const result = await this . axiosInstance . request ( requestConfig ) ; observer . next ( result ) ; observer . complete ( ) ; } catch ( error ) { observer . error ( new RxAxiosProviderException ( error ) ) ; } } ) ; } post < T > ( url , data ) { return this . request ( { method : 'POST' , data , url , } ) ; } get < T > ( url ) { return this . request ( { method : 'GET' , url , } ) ; } put < T > ( url , data ) { return this . request ( { method : 'PUT' , data , url , } ) ; } delete < T > ( url ) { return this . request ( { method : 'DELETE' , url , } ) ; } }