0.2.0 • Published 2 years ago
rx-restful-client v0.2.0
rx-restful-client
Define the Api Http Client Adapter Class (axios-http-client.ts)
AxiosHttpClient
import {from, Observable} from "rxjs";
export class AxiosHttpClient implements GenericHttpClientAdapter<AxiosRequestConfig, AxiosResponse> {
private readonly axios: Axios;
constructor() {
this.axios = new Axios();
}
delete<T>(path: string, options?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
return from(this.axios.delete<T>(path, options));
}
get<T>(path: string, options?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
return from(this.axios.get<T>(path, options));
}
patch<T>(path: string, body: any, options?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
return from(this.axios.patch<T>(path, options));
}
post<T>(path: string, body: any, options?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
return from(this.axios.post<T>(path, body, options));
}
put<T>(path: string, body: any, options?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
return from(this.axios.put<T>(path, body, options));
}
}
Define the Basic Api Class (axios-api.ts)
AxiosApi
export class AxiosApi extends GenericRestApi<AxiosRequestConfig, AxiosResponse, AxiosHttpClient> {
readonly condos = new CondosResource(this);
readonly parkings = new ParkingsResource(this);
readonly apartments = new ApartmentsResource(this);
constructor() {
super(new AxiosHttpClient(), 'https://axios-api.com');
}
}
Define the Basic Class Types (axios-types.ts)
export class AxiosResource extends GenericRestResource<AxiosRequestConfig, AxiosResponse, AxiosApi> {
constructor(api: AxiosApi, path: string) {
super(api, path);
}
}
export class AxiosRestfulCollection
extends GenericRestfulCollection<AxiosRequestConfig, AxiosResponse, AxiosApi> {
constructor(api: AxiosApi, path: string) {
super(api, path);
}
}
export class AxiosRestfulResource<T extends AxiosRestfulCollection>
extends GenericRestfulResource<AxiosRequestConfig, AxiosResponse, AxiosApi, T> {
constructor(api: AxiosApi, path: string, supplier: (path: string) => T) {
super(api, path, supplier);
}
}
Define the RestResource Classes
CondosResource, CondosCollection
export class CondosResource extends AxiosRestfulResource<CondosCollection> {
constructor(api: AxiosApi, path: string = '') {
super(api, `${path}/condos`, path => new CondosCollection(api, path));
}
}
class CondosCollection extends AxiosRestfulCollection {
readonly parkings = new ParkingsResource(this.api, this.path);
readonly apartments = new ApartmentsResource(this.api, this.path);
constructor(api: AxiosApi, path: string) {
super(api, path);
}
}
ParkingsResource
export class ParkingsResource extends AxiosResource {
constructor(api: AxiosApi, path: string = '') {
super(api, `${path}/parkings-areas`);
}
availables(): Observable<AxiosResponse<number[]>> {
return this.doGet('/available');
}
}
ApartmentsResource, ApartmentsCollections
export class ApartmentsResource extends AxiosRestfulResource<ApartmentsCollection> {
constructor(api: AxiosApi, path: string = '') {
super(api, `${path}/apartments`, path => new ApartmentsCollection(api, path));
}
}
class ApartmentsCollection extends AxiosRestfulCollection {
constructor(api: AxiosApi, path: string) {
super(api, path);
}
reserve(): Observable<AxiosResponse> {
return this.doPut<any>('/reserve', {});
}
cancelReserve(): Observable<AxiosResponse> {
return this.doGet<any>('/cancel-reserve');
}
}
Call the API path you want from your application
const api = new AxiosApi();
api.condos.id(1).apartments.id(2).reserve().subscribe((next: any) => {
console.log('api.condos.id(1).apartments.id(2).reserve()', next);
},
);
The above example will generate an HTTP call with the path bellow:
https://axios-api.com/condos/1/apartments/2/reserve