vp-ngx-jsonapi
This is a JSON API library for Angular 4+. Please use ts-angular-jsonapi for AngularJS.
Online demo
You can test library on this online example 👌 http://vp-ngx-jsonapi.reyesoft.com/.
Data is obtained from Json Api Playground.
Supported features
- Cache (on memory): Before a HTTP request objects are setted with cached data.
- Cache (on memory): TTL for collections and resources
- Cache on localstorage
- Pagination
- Filtering by attributes through a string or a regular expression
- Include param support (also, when you save)
- Two+ equal resource request, only one HTTP call.
- Equal requests, return a same ResourceObject on memory
- Default values for a new resource (hydrator).
- Properties on collections like
$length
,$is_loading
or$source
(empty
|cache
|server
)
Usage
More information on examples section.
Installation
First of all, you need read, read and read Jsonapi specification.
yarn add vp-ngx-jsonapi --save# or npm if you wish...
Dependecies and customization
- Add Jsonapi dependency.
- Configure your url and other paramemeters.
- Inject JsonapiCore somewhere before you extend any class from
Jsonapi.Resource
.
;; @
Examples
Like you know, the better way is with examples. Lets go! 🚀
Defining a resource
authors.service.ts
;;
Get a collection of resources
Controller
;;; @ public authors: ICollection<Author>; public { authorsServiceall // { include: ['books', 'photos'] } ; }
View for this controller
id: {{ author.id }} name: {{ author.attributes.name }} birth date: {{ author.attributes.date_of_birth | date }}
More options? Collection filtering
Filter resources with attribute: 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 same attribute
value as one of values
array elements are returned.
let authors = authorsServiceall localfilter: name: 'xx' // request all data and next filter locally remotefilter: country: 'Argentina' // request data with filter url parameter ;
Get a single resource
From this point, you only see important code for this library. For a full example, clone and see demo directory.
let author = authorsService;
More options? Include resources when you fetch data (or save!)
let author$ = authorsService;
TIP: these parameters work with all()
and save()
methods too.
Add a new resource
let author = thisauthorsService;authorattributesname = 'Pablo Reyes';authorattributesdate_of_birth = '2030-12-10';author;
Need you more control and options?
let author = thisauthorsService;authorattributesname = 'Pablo Reyes';authorattributesdate_of_birth = '2030-12-10'; // some_book is an another resource like authorlet some_book = booksService;author; // some_publisher is a polymorphic resource named company on this caselet some_publisher = publishersService;author; // wow, now we need detach a relationshipauthor; // this library can send include information to server, for atomicityauthor; // mmmm, if I need get related resources? For example, books related with author 1let relatedbooks = booksServiceall beforepath: 'authors/1' ; // you need get a cached object? you can force ttl on getlet author$ = authorsService;
Update a resource
authorsService
Pagination
let authors$ = authorsServiceall // get page 2 of authors collection, with a limit per page of 50 page: number: 2 ; size: 50 ;
Collection page
- 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
Local Demo App
You can run JsonApi Demo App locally following the next steps:
git clone git@github.com:reyesoft/vp-ngx-jsonapi.gitcd vp-ngx-jsonapiyarnyarn start
We use as backend Json Api Playground.
Colaborate
Check Environment development file 😉.