sd-angular-jsonapi v1.0.0
Skyline Dynamics - JSON API - Angular 5 Implementation
THIS DOCUMENTATION IS DEPRECATED *** WAIT FOR NEW DOCUMENTATION PLEASE
Installation
Install the library with the following command:
npm install --save sd-angular-jsonapi
or
yarn add sd-angular-jsonapi
Add the Module to your app.module.ts
file:
@NgModule({
imports: [
...
SDJsonApiModule.forRoot({baseUrl: 'http://yourdomain.com'})
],
})
###Configuration
The following configuration options are available when importing the module:
Variable | Required | Type | Default Value | Description |
---|---|---|---|---|
baseUrl | Yes | string | empty | The root URL of your API |
version | No | string | empty | The version identified of your API. If ommitted, no version will be used. Can be any string like 'v1' or '1' |
###Resources
####Creating a resource
To create a resource, create a class that extends from JSONAPIResource
and decorate it with the @JSONAPI()
decorator
@JSONAPI()
export class Author extends JSONAPIResource{
}
optionally you can define attributes on the class that should be returned by the API. Note that these attributes are only to enable type-hinting in compatible IDE's. The attributes will not actually be used to define the data that will be returned from the API.
####Instantiating a resource
Instantiate a resource by calling the new
constructor on it:
const author = new Author();
That's it!
####Fetch all resources
To fetch all entries of a certain resource, you can call the .all()
method, or optionally the .get()
method without parameters.
So
author.all();
is the same as
author.get();
Calling the .all()
or .get()
method on a resource returns an Observable of type Collection
that will contain the results of the API call
// TODO error handling is not implemented yet, but if an API call fails, it should return a properly typed ErrorResult
Example for a full call:
// Define the Resource
@JSONAPI()
export class Author extends JSONAPIResource{
}
//Instantiate the resource
const authors = new Author();
//Get all authors
authors.all().subscribe(
(successResult: Collection) => {
console.log('Loaded the following authors from the API: ' + successResult.all());
},
(errorResult) => {
console.log('Whoops, something went wrong!');
});
####Fetch a single resource
To fetch a single resource, simply call the .get()
method on the resource and provide the ID of the resource you'd like to fetch:
// Define the Resource
@JSONAPI()
export class Author extends JSONAPIResource{
}
//Instantiate the resource
const authors = new Author();
//Get author with the ID 23
authors.get(23).subscribe(
(successResult: Collection) => {
console.log('Loaded the following author from the API: ' + successResult.first());
},
(errorResult) => {
console.log('Whoops, something went wrong!');
});
Note that, even though we're requesting a single resource, at this time the library will still return a full collection (which only contains the author we requested).
// TODO implement .find(<id>)
method, that only returns the resource, not a collection
####Fetch all resources (or a single one) including related data
// TODO
####Filter a request
You can filter a resource by using the .filter()
and .filters()
methods on the resource.
Note that filters need to be applied BEFORE sending the request, so BEFORE you call .all()
, .get()
or .find()
// Define the Resource
@JSONAPI()
export class Author extends JSONAPIResource{
}
//Instantiate the resource
const authors = new Author();
//Get all authors that are still alive
authors.filter('alive',true).all().subscribe(
(successResult: Collection) => {
console.log('Loaded the following authors from the API: ' + successResult.all());
},
(errorResult) => {
console.log('Whoops, something went wrong!');
});
####Paginate a request // TODO
####Filter returned fields // TODO
###Collections // TODO
###Error Handling // TODO
7 years ago