- Notifications
You must be signed in to change notification settings - Fork50
JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
License
reyesoft/ngx-jsonapi
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
You can test library on this online example 👌http://ngx-jsonapi.reyesoft.com/.
Data is obtained fromJson Api Playground server.
- Cache (on memory): TTL for collections and resources. Before a HTTP request objects are setted with cached data.
- Cache on IndexedDB
- Pagination
- Sorting
- Include param support (also, when you save)
- Equal requests, return a same ResourceObject on memory
- Default values for a new resource (hydrator).
Justinstall,configure and learn withexamples.
First of all, it's advisable to readJsonapi specification. Understanding JsonApi documents structure is recommended.
yarn add ngx-jsonapi@2.0.0-rc.4 --save# or npm if you wish...- Add Jsonapi dependency.
- Configure your url and other paramemeters.
- Inject JsonapiCore somewhere before you extend any class from
Jsonapi.Resource.
/* .. */import{NgxJsonapiModule}from'ngx-jsonapi';@NgModule({imports:[NgxJsonapiModule.forRoot({url:'//jsonapiplayground.reyesoft.com/v2/'})]})exportclassAppModule{}
Library cache anything memory. With Local Store, also store all on IndexDb on browser. Faster apps when we reuse a lot of data.
/* .. */import{NgxJsonapiModule}from'ngx-jsonapi';import{JSONAPI_RIPPER_SERVICE,JSONAPI_STORE_SERVICE}from'./core';import{StoreService}from'ngx-jsonapi/sources/store.service';import{JsonRipper}from'ngx-jsonapi/services/json-ripper';@NgModule({imports:[NgxJsonapiModule.forRoot({url:'//jsonapiplayground.reyesoft.com/v2/'})],providers:[{provide:JSONAPI_RIPPER_SERVICE,useClass:JsonRipperFake},{provide:JSONAPI_STORE_SERVICE,useClass:StoreFakeService}]})exportclassAppModule{}
Like you know, the better way is with examples. Lets go! 🚀
authors.service.ts
import{Injectable}from'@angular/core';import{Autoregister,Service,Resource,DocumentCollection,DocumentResource}from'ngx-jsonapi';import{Book}from'../books/books.service';import{Photo}from'../photos/photos.service';exportclassAuthorextendsResource{publicattributes={name:'default name',date_of_birth:''};publicrelationships={books:newDocumentCollection<Book>(),photo:newDocumentResource<Photo>()};}@Injectable()@Autoregister()exportclassAuthorsServiceextendsService<Author>{publicresource=Author;publictype='authors';}
import{Component}from'@angular/core';import{DocumentCollection}from'ngx-jsonapi';import{AuthorsService,Author}from'./../authors.service';@Component({selector:'demo-authors',templateUrl:'./authors.component.html'})exportclassAuthorsComponent{publicauthors:DocumentCollection<Author>;publicconstructor(privateauthorsService:AuthorsService){authorsService.all({// include: ['books', 'photos'],}).subscribe(authors=>(this.authors=authors));}}
<p*ngFor="let author of authors.data; trackBy: authors.trackBy"> id: {{ author.id }}<br/> name: {{ author.attributes.name }}<br/> birth date: {{ author.attributes.date_of_birth | date }}</p>
Example:name is a authors attribute, and makes a query like/authors?sort=name,job_title
letauthors=authorsService.all({sort:['name','job_title']});
Filter resources withattribute: value values. Filters are used as 'exact match' (only resources with attribute value same as value are returned).value can also be an array, then only objects with sameattribute value as one ofvalues array elements are returned.
authorsService.all({remotefilter:{country:'Argentina'}});
From this point, you only see important code for this library. For a full example, clone and see demo directory.
authorsService.get('some_author_id');
authorsService.get('some_author_id',{include:['books','photos']});
TIP: these parameters work withall() andsave() methods too.
letauthor=this.authorsService.new();author.attributes.name='Pablo Reyes';author.attributes.date_of_birth='2030-12-10';author.save();
letauthor=this.authorsService.new();author.attributes.name='Pablo Reyes';author.attributes.date_of_birth='2030-12-10';// some_book is an another resource like authorletsome_book=booksService.get(1);author.addRelationship(some_book);// some_publisher is a polymorphic resource named company on this caseletsome_publisher=publishersService.get(1);author.addRelationship(some_publisher,'company');// wow, now we need detach a relationshipauthor.removeRelationship('books','book_id');// this library can send include information to server, for atomicityauthor.save({include:['book']});// mmmm, if I need get related resources? For example, books related with author 1letrelatedbooks=booksService.all({beforepath:'authors/1'});// you need get a cached object? you can force ttl on getletauthor$=authorsService.get('some_author_id',{ttl:60}// ttl on seconds (default: 0));
authorsService.get('some_author_id').suscribe(author=>{this.author.attributes.name+='New Name';this.author.save(success=>{console.log('author saved!');});});
authorsService.all({// get page 2 of authors collection, with a limit per page of 50page:{number:2;size:50}});
- number: number of the current page
- size: size of resources per page (it's sended to server by url)
- information returned from server (check if is avaible)total_resources: total of avaible resources on server resources_per_page: total of resources returned per page requested
You can runJsonApi Demo App locally following the next steps:
git clone git@github.com:reyesoft/ngx-jsonapi.gitcd ngx-jsonapiyarnyarn startWe use as backendJson Api Playground.
CheckEnvironment development file 😉.
About
JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.


