observable-api
API client based on Observables (RxJS)
Prerequisites
- Npm: You have to have RxJS installed if using as npm package.
- Bower & CDN: You have to have RxJS library loaded before observable-api if using as bower component or from CDN.
Installing
Using cdn:
Using npm:
$ npm install observable-api
Using bower:
$ bower install observable-api
Example
Creating API
; const adapter = ;const API = ;
Creating API endpoint
const usersEndpoint = API;
Selecting data from endpoint
const users$ = usersEndpointresponse$;
Making requests and using the data
// Request to the endpoint is made on the first subscriptionusers$; // Subsequent subscriptions will use data from the latest requestusers$; // To force new request to the endpointusersEndpoint; // Fetch can receive optional parameters and datausersEndpoint; // returns usersEndpoint instance
jsbin
Combining the data // Using jQuery.ajax for making XHR requestsconst adapter = ObservableAPI;const api = ObservableAPI// Fake data base urlconst baseURL = 'https://jsonplaceholder.typicode.com'; // Creating endpointsconst postsEndpoint = api;const posts$ = postsEndpointresponse$; const usersEndpoint = api;const users$ = usersEndpointresponse$; // Creating custom data selectorsconst usersById$ = users$; const postsWithUsers$ = RxObservable; // Printing the resultpostsWithUsers$;
jsbin
Use latest results with ease // Using jQuery.ajax for making XHR requestsconst adapter = ObservableAPI;const api = ObservableAPI; // creating endpointconst endpointURL = `https://en.wikipedia.org/w/api.php?action=opensearch&search=&limit=3`; // Creating endpointsconst searchEndpoint = api;const searchResuls$ = searchEndpointresponse$;const search = searchEndpoint; const term$ = RxObservable ; term$ ; { const $results = ; $nodes = RxObservable }