All Projects β†’ reyesoft β†’ Ngx Jsonapi

reyesoft / Ngx Jsonapi

Licence: mit
JSON API client library for Angular 5+ πŸ‘Œ :: Production Ready πŸš€

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Jsonapi

Nebular
πŸ’₯ Customizable Angular UI Library based on Eva Design System 🌚✨Dark Mode
Stars: ✭ 7,368 (+8996.3%)
Mutual labels:  angular2, angular4
Drip Ionic3
γ€Œζ°΄ζ»΄ζ‰“ε‘γ€App Open Source Code Base On Ionic V3 Framework
Stars: ✭ 74 (-8.64%)
Mutual labels:  angular2, angular4
Ngvas
An Angular2/Angular4 library for HTML Canvas.
Stars: ✭ 31 (-61.73%)
Mutual labels:  angular2, angular4
Ng2 Dnd
Angular 2 Drag-and-Drop without dependencies
Stars: ✭ 861 (+962.96%)
Mutual labels:  angular2, angular4
Ngx Lazy Load Images
Image lazy load library for Angular 2+
Stars: ✭ 77 (-4.94%)
Mutual labels:  angular2, angular4
Bootstrap
Open Source JS plugins
Stars: ✭ 13 (-83.95%)
Mutual labels:  angular2, angular4
Angular2 Social Login
Simple client side social authentication for Angular2 application.
Stars: ✭ 34 (-58.02%)
Mutual labels:  angular2, angular4
Angular Prest
pREST component for Angular
Stars: ✭ 16 (-80.25%)
Mutual labels:  angular2, angular4
Ngautocomplete
Light-weight autocomplete component for Angular.
Stars: ✭ 52 (-35.8%)
Mutual labels:  angular2, angular4
Awesome Angular
πŸ“„ A curated list of awesome Angular resources
Stars: ✭ 8,160 (+9974.07%)
Mutual labels:  angular2, angular4
Angular2 Carousel
An lightweight , touchable and responsive library to create a carousel for angular 2 / 4 / 5
Stars: ✭ 26 (-67.9%)
Mutual labels:  angular2, angular4
Angular Redux Ngrx Examples
Sample projects with Angular (4.x) + Angular CLI + ngrx (Redux) + Firebase
Stars: ✭ 73 (-9.88%)
Mutual labels:  angular2, angular4
Ng Sticky
Angular 4 sticky, have header or any component sticky easy to use.
Stars: ✭ 25 (-69.14%)
Mutual labels:  angular2, angular4
Egeo
EGEO is the open-source UI library used to build Stratio's UI. It includes UI Components, Utilities, Services and much more to build user interfaces quickly and with ease. The library is distributed in AoT mode.
Stars: ✭ 69 (-14.81%)
Mutual labels:  angular2, angular4
Angular Select2
select2 in angular(>=2.0-release).
Stars: ✭ 16 (-80.25%)
Mutual labels:  angular2, angular4
Angulartics2
Vendor-agnostic analytics for Angular2 applications.
Stars: ✭ 963 (+1088.89%)
Mutual labels:  angular2, angular4
Ngx Permissions
Permission and roles based access control for your angular(angular 2,4,5,6,7,9+) applications(AOT, lazy modules compatible
Stars: ✭ 749 (+824.69%)
Mutual labels:  angular2, angular4
Material Dashboard Angular2
Material Dashboard Angular
Stars: ✭ 814 (+904.94%)
Mutual labels:  angular2, angular4
Angular Tree Component
A simple yet powerful tree component for Angular (>=2)
Stars: ✭ 1,031 (+1172.84%)
Mutual labels:  angular2, angular4
Angular2
Angular 2 Seed
Stars: ✭ 75 (-7.41%)
Mutual labels:  angular2, angular4

ngx-jsonapi

angular jsonapi

CircleCI Codacy Badge npm version Coverage Status

This is a JSON API library for Angular 6+. Please use [ts-angular-jsonapi](https://github.com/reyesoft/ts-angular-jsonapi) for AngularJS.

Online demo

You can test library on this online example πŸ‘Œ http://ngx-jsonapi.reyesoft.com/.

demo app

Data is obtained from Json Api Playground server.

Supported features

  • 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).

Migration

Usage

Just install, configure and learn with examples.

First of all, it's advisable to read Jsonapi specification. Understanding JsonApi documents structure is recommended.

Installation

yarn add [email protected] --save
# or npm if you wish...

Dependecies and customization

  1. Add Jsonapi dependency.
  2. Configure your url and other paramemeters.
  3. 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/'
        })
    ]
})
export class AppModule {}

Enable Local Cache

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
        }
    ]
})
export class AppModule {}

Examples

Like you know, the better way is with examples. Lets go! πŸš€

Defining a resource

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';

export class Author extends Resource {
    public attributes = {
        name: 'default name',
        date_of_birth: ''
    };

    public relationships = {
        books: new DocumentCollection<Book>(),
        photo: new DocumentResource<Photo>()
    };
}

@Injectable()
@Autoregister()
export class AuthorsService extends Service<Author> {
    public resource = Author;
    public type = 'authors';
}

Get a collection of resources

Controller

import { Component } from '@angular/core';
import { DocumentCollection } from 'ngx-jsonapi';
import { AuthorsService, Author } from './../authors.service';

@Component({
    selector: 'demo-authors',
    templateUrl: './authors.component.html'
})
export class AuthorsComponent {
    public authors: DocumentCollection<Author>;

    public constructor(private authorsService: AuthorsService) {
        authorsService
            .all({
                // include: ['books', 'photos'],
            })
            .subscribe(authors => (this.authors = authors));
    }
}

View for this controller

<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>

Collection sort

Example: name is a authors attribute, and makes a query like /authors?sort=name,job_title

let authors = authorsService.all({
    sort: ['name', 'job_title']
});

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.

authorsService.all({
    remotefilter: { country: 'Argentina' }
});

Get a single resource

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');

More options? Include resources when you fetch data (or save!)

authorsService.get('some_author_id', { include: ['books', 'photos'] });

TIP: these parameters work with all() and save() methods too.

Add a new resource

let author = this.authorsService.new();
author.attributes.name = 'Pablo Reyes';
author.attributes.date_of_birth = '2030-12-10';
author.save();

Need you more control and options?

let author = this.authorsService.new();
author.attributes.name = 'Pablo Reyes';
author.attributes.date_of_birth = '2030-12-10';

// some_book is an another resource like author
let some_book = booksService.get(1);
author.addRelationship(some_book);

// some_publisher is a polymorphic resource named company on this case
let some_publisher = publishersService.get(1);
author.addRelationship(some_publisher, 'company');

// wow, now we need detach a relationship
author.removeRelationship('books', 'book_id');

// this library can send include information to server, for atomicity
author.save({ include: ['book'] });

// mmmm, if I need get related resources? For example, books related with author 1
let relatedbooks = booksService.all({ beforepath: 'authors/1' });

// you need get a cached object? you can force ttl on get
let author$ = authorsService.get(
    'some_author_id',
    { ttl: 60 } // ttl on seconds (default: 0)
);

Update a resource

authorsService.get('some_author_id').suscribe(author => {
    this.author.attributes.name += 'New Name';
    this.author.save(success => {
        console.log('author saved!');
    });
});

Pagination

authorsService.all({
  // 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 [email protected]:reyesoft/ngx-jsonapi.git
cd ngx-jsonapi
yarn
yarn start

We use as backend Json Api Playground.

Colaborate

Check Environment development file πŸ˜‰.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].