Skip to content

observable data service based on rxjs using axios for conunicate with server via http (XHR).

License

Notifications You must be signed in to change notification settings

ahadrt/as-service

 
 

Repository files navigation

Build Status

AsService


A parametric, observable and injectable service based on rxjs as a javascript library using axios for connect to http server (XHR) and localforge for cache in indexedDB

Install

npm i @barteh/as-service --save

Usage

Import library

import { AsService, Server } from "@barteh/as-service";

1- Primitive type (number | string | Array) as service

var srv1 = new AsService(5); // number as service
srv1.Observable()
.subscribe(a => console.log("ser1 data via observable is:", a));

srv1.load().then(a => console.log("ser1 data via promis:", a));

2- Pure object as service

var srv2 = new AsService({x: 9}); // object as service

srv2.Observable()
.subscribe(a => console.log("ser2 data via observable is:", a));

srv2.load().then(a => console.log("ser2 data via promis:", a));

3- Function as service (parametric observable)

var srv3 = new AsService(param => param * 3); // function as service
srv3.Observable(2) //parametric observe
.subscribe(a => console.log("ser3 data via observable is:", a));

//passing (Number) 2  as parameter
srv3.load(2).then(a => console.log("ser3 data via promis:", a));

4- Promise as service

var ser4 = new AsService(param => new Promise((res, rej) => res(`im promise with parameter: ${param}`)));

ser4.Observable("myparam")
.subscribe(a => console.log("srv4: ", a));

ser4.load("myparam");

5- XHR as Service

using built in advanced methods name [ Server ] wraps axios for retrive data from http server and localforge for cache data. Following sample uses class [ Server ] as input of AsService. You can use your own xhr library instead of this.

if http://myserver/contacts/getcontact.ctrl http REST service exists.

import {AsService,Server} from "@barteh/as-service"

var controller1 = (x, y) => Server.controller("contacts","getcontact", { name: x, lname: y });

var srv5 = new AsService(controller1);

srv5.Observable("Ahad", "Rafat")
.subscribe(a => console.log("srv5:", a));

Output

> ser1 data via observable is: 5
> ser2 data via observable is: { x: 9 }
> ser3 data via observable is: 6
> srv4:  im promise with parameter: myparam

Test

npm test

Using Both for web and browsers

Build

npm run build

Use in ES5

var { AsService } = require("@barteh/as-service");

var t = new AsService(8);

t.Observable()
.subscribe(a => console.log(a))

License: MIT

About

observable data service based on rxjs using axios for conunicate with server via http (XHR).

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%