Skip to content

Commit

Permalink
Added EHandler
Browse files Browse the repository at this point in the history
Added Record Browser
Bump version 0.5
  • Loading branch information
dinevillar committed Feb 19, 2018
1 parent 1a47325 commit 09b79bd
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 1,955 deletions.
108 changes: 73 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ module.exports = {
// Register JSON API Types here..
"registry": {
"user": {
"links": {
self: (data) => {
return '/users/' + data.id
"model": 'App/Models/User'
"structure": {
"links": {
self: (data) => {
return '/users/' + data.id
}
},
"topLevelLinks": {
self: '/users'
}
},
"topLevelLinks": {
self: '/users'
}
}
}
Expand All @@ -38,10 +41,6 @@ const providers = [

> Add to your Models
``` javascript
static get jsonApiType(){
return "user"; // Use key in Config.get('jsonApi.registry')
}

static get Serializer() {
return 'JsonApi/Serializer/LucidSerializer'; // Override Lucid/VanillaSerializer
};
Expand Down Expand Up @@ -80,43 +79,53 @@ getUser({request, response}) {
``` javascript
"registry": {
"company": {
id: "id",
links: {
self: (data) => {
return '/companies/' + data.id
}
"model": 'App/Models/Company',
"structure": {
id: "id",
links: {
self: (data) => {
return '/companies/' + data.id
}
}
}
}
"user": {
"links": {
self: (data) => {
return '/users/' + data.id
}
},
"relationships": {
company: {
type: 'company',
links: {
self: '/companies'
}
}
}
"topLevelLinks": {
self: '/users'
"model": 'App/Models/User',
"structure": {
"links": {
self: (data) => {
return '/users/' + data.id
}
},
"relationships": {
company: {
type: 'company',
links: {
self: '/companies'
}
}
}
"topLevelLinks": {
self: '/users'
}
}
}
}
```
> App/Models/Company
``` javascript
static get jsonApiType(){
return "company"; // Use key in Config.get('jsonApi.registry')
}
static get Serializer() {
return 'JsonApi/Serializer/LucidSerializer';
};
```

> App/Models/User
``` javascript
static get Serializer() {
return 'JsonApi/Serializer/LucidSerializer';
};
```

> Somewhere:
``` javascript
getUser({request, response}) {
Expand All @@ -125,10 +134,39 @@ getUser({request, response}) {
response.send(user.toJSON());
};
```
#### library:

#### Record Browser in your Controllers
``` javascript
const Company = use('App/Models/Company')
const JsonApiRB = use('JsonApiRecordBrowser');
const companies = await JsonApiRB
.model(Company)
.request(request.get()) //handle request
.paginateOrFetch();
response.send(companies.toJSON());
```
The record browser supports:
- [Pagination](http://jsonapi.org/format/#fetching-pagination)
- [Sparse Fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets)
- [Inclusion of Related Resources](http://jsonapi.org/format/#fetching-includes)
- [Filtering](http://jsonapi.org/format/#fetching-filtering)
- [Sorting](http://jsonapi.org/format/#fetching-sorting)

#### Exceptions
You can use JsonApi to handle errors and be able to return valid JSON Api error responses.
Create a global ehandler using `adonis make:ehandler` and use JsonApi in `handle()` function.
See `examples/Exception/Handler.js`

``` javascript
async handle(error, options) {
JsonApi.handleError(error, options);
}
```

#### Serializer Library:
> [Serializer functions](https://github.com/danivek/json-api-serializer/blob/master/lib/JSONAPISerializer.js)
``` javascript
const {JsonApiSerializer} = use('JsonApi');
const user = await User.find(1);
JsonApiSerializer.serialize("user", user);
```
```
43 changes: 43 additions & 0 deletions examples/Exceptions/Handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const Logger = use('Logger');
const JsonApi = use('JsonApi');

/**
* This class handles all exceptions thrown during
* the HTTP request lifecycle.
*
* @class ExceptionHandler
*/
class ExceptionHandler {
/**
* Handle exception thrown during the HTTP lifecycle
*
* @method handle
*
* @param {Object} error
* @param {Object} options.request
* @param {Object} options.response
*
* @return {void}
*/
async handle(error, options) {
JsonApi.handleError(error, options);
}

/**
* Report exception for logging or debugging.
*
* @method report
*
* @param {Object} error
* @param {Object} options.request
*
* @return {void}
*/
async report(error, {request}) {
Logger.error(error);
}
}

module.exports = ExceptionHandler;
4 changes: 2 additions & 2 deletions examples/jsonApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = {
"unconvertCase": "camelCase"
},
// Register JSON API Types here..
// For more info: https://github.com/danivek/json-api-serializer
// For more info on structure: https://github.com/danivek/json-api-serializer
"registry": {
"user": {
"user": { //JSON API Type
"model": "App/Models/User", //Lucid Model Namespace
"structure": {
"links": {
Expand Down
Loading

0 comments on commit 09b79bd

Please sign in to comment.