Jsonapi client library developed for AngularJS based on typescript.
- TS Definitions for strong typing and autocomplete (See example image)
- Get resource and collection of resources
- Include support (also, when you save)
- Caché
- Pagination
First of all, you need read, read and read Jsonapi specification.
bower install ts-angular-jsonapi --saveOr throw npm
npm install ts-angular-jsonapi --save- Add reference path to Typescript Definitions (dts).
- Add Jsonapi dependency.
- Configure your url and other paramemeters.
- Inject JsonapiCore somewhere before you extend any class from
Jsonapi.Resource.
/// <reference path="../../bower_components/ts-angular-jsonapi/dist/ts-angular-jsonapi.d.ts"/>
var app = angular.module('yourAppName', ['rsJsonapiConfig']);
app.config(['rsJsonapiConfig', (rsJsonapiConfig) => {
angular.extend(rsJsonapiConfig, {
url: 'http://localhost:8080/v1/'
});
}]);
var MyController = function(JsonapiCore) {
// ...
}
MyController.$inject = ['JsonapiCore'];Like you know, the better way is with examples. Based on endpoints example library.
authors.service.ts
class AuthorsService extends Jsonapi.Resource {
type = 'authors';
public schema: Jsonapi.ISchema = {
attributes: {
name: { presence: true, length: { maximum: 96 } },
date_of_birth: {},
date_of_death: {},
created_at: {},
updated_at: {}
},
relationships: {
books: {},
photos: {}
}
};
}
angular.module('demoApp').service('AuthorsService', AuthorsService);class AuthorsController {
public authors: any = null;
/** @ngInject */
constructor(AuthorsService) {
this.authors = AuthorsService.all();
}
}<p ng-repeat="author in vm.authors">
id: {{ author.id }} <br />
name: {{ author.attributes.name }} <br />
birth date: {{ author.attributes.date_of_birth | date }}
</p>From this point, you only see important code for this library. For a full example, clone and see demo directory.
let author = AuthorsService.get('some_author_id');let author = AuthorsService.get(
'some_author_id',
{ include: ['books', 'photos'] },
success => {
console.log('Author loaded.', success);
},
error => {
console.log('Author don`t loaded. Error.', error);
}
);TIP: these parameters work with all() and save() methods too.
let author = this.AuthorsService.new();
author.attributes.name = 'Pablo Reyes';
author.attributes.date_of_birth = '2030-12-10';
author.save();let author = this.AuthorsService.new();
author.attributes.name = 'Pablo Reyes';
author.attributes.date_of_birth = '2030-12-10';
// book is an another resource like author
author.addRelationship(book);
// editorial is a polymorphic resource named company on this case
author.addRelationship(editorial, 'company');
// this library can send include information to server, for atomicity
author.save( { include: ['book'] });let author = AuthorsService.get('some_author_id');
this.author.attributes.name += 'New Name';
this.author.save(success => {
console.log('author saved!');
});- Pagination
- Include anothers resources -
For demo purposes you can run local server and test this library:
git clone git@github.com:endpoints/endpoints-example.git
cd endpoints-example
npm install
npm startMore information in https://github.com/endpoints/endpoints-example.
Now, you have jsonapi endpoints like http://localhost:8080/v1/authors.
git clone git@github.com:reyesoft/ts-angular-jsonapi.git
cd ts-angular-jsonapi
npm install
gulp serveFirst you need run the demo. Next, when you made new features on your fork, please run
gulp distAnd commit! Don't forget your pull request :)