Skip to content

Commit 4a93ea7

Browse files
committed
Added commonMeta in config for global meta props.
Refactored serializer for commonMeta addition. Provided exception services in JsonApi service. Added example EHandler
1 parent 6b62b37 commit 4a93ea7

File tree

5 files changed

+121
-80
lines changed

5 files changed

+121
-80
lines changed

examples/Exceptions/Handler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
const Logger = use('Logger');
44
const JsonApi = use('JsonApi');
5+
const BaseExceptionHandler = use('BaseExceptionHandler');
6+
const {JsonApiException} = require('@dinevillar/src/Exceptions');
57

68
/**
79
* This class handles all exceptions thrown during
810
* the HTTP request lifecycle.
911
*
1012
* @class ExceptionHandler
1113
*/
12-
class ExceptionHandler {
14+
class ExceptionHandler extends BaseExceptionHandler {
1315
/**
1416
* Handle exception thrown during the HTTP lifecycle
1517
*
@@ -22,7 +24,9 @@ class ExceptionHandler {
2224
* @return {void}
2325
*/
2426
async handle(error, options) {
25-
JsonApi.handleError(error, options);
27+
if (error.name instanceof JsonApiException) {
28+
JsonApi.handleError(error, options);
29+
}
2630
}
2731

2832
/**

examples/jsonApi.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ module.exports = {
66
"convertCase": "snake_case",
77
"unconvertCase": "camelCase"
88
},
9+
"commonMeta": {
10+
"copyright": "Copyright " + (new Date()).getFullYear() + " Example Corporation",
11+
"documentation": "http://example.com/docs",
12+
"authors": [
13+
"John Doe - [email protected]"
14+
]
15+
},
916
// Register JSON API Types here..
1017
// For more info on structure: https://github.com/danivek/json-api-serializer
1118
"registry": {

src/Exceptions/specification-exceptions.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ class UnknownResourceObjectType extends JsonApiException {
5656
}
5757
}
5858

59+
class ResourceObjectDoesNotExist extends JsonApiException {
60+
static invoke(type, id, message) {
61+
if (!message) {
62+
message = `Resource object: ${type} - ${id} does not exist.`
63+
}
64+
return new this(message, 404, 'E_JSON_API_RESOURCE_OBJECT_DOES_NOT_EXIST');
65+
}
66+
}
5967

6068
module.exports = {
6169
NotAcceptable,
6270
UnsupportedMediaType,
6371
UnprocessableResourceObject,
64-
UnknownResourceObjectType
72+
UnknownResourceObjectType,
73+
ResourceObjectDoesNotExist
6574
};

src/Serializer/LucidSerializer.js

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,86 +9,89 @@ const JsonApi = use('JsonApi');
99
const Logger = use('Logger');
1010

1111
class LucidSerializer extends VanillaSerializer {
12-
toJSON() {
13-
if (this.isOne) {
14-
const jsonApiType = JsonApi.getTypeOfModel(this.rows.constructor.name);
15-
if (jsonApiType !== undefined) {
16-
try {
17-
return JsonApi.JsonApiSerializer.serialize(
18-
jsonApiType,
19-
this._getRowJSON(this.rows)
20-
);
21-
} catch (error) {
22-
Logger.warning(error);
23-
}
24-
} else {
25-
throw TypeNotDefinedException.invoke(this.rows.constructor.name);
26-
}
27-
} else {
28-
const jsonApiType = JsonApi.getTypeOfModel(this.rows[0].constructor.name);
29-
const data = this.rows.map(this._getRowJSON.bind(this));
30-
if (jsonApiType !== undefined) {
31-
try {
32-
if (this.pages) {
33-
const jsonApiPaged = JsonApi.JsonApiSerializer.serialize(jsonApiType, data);
34-
if (_.has(jsonApiPaged, 'links.self')) {
35-
this._setPageLinks(jsonApiPaged.links, jsonApiPaged.links.self);
12+
toJSON() {
13+
let returnJson = null;
14+
if (this.isOne) {
15+
const jsonApiType = JsonApi.getTypeOfModel(this.rows.constructor.name);
16+
if (jsonApiType !== undefined) {
17+
try {
18+
returnJson = JsonApi.JsonApiSerializer.serialize(
19+
jsonApiType,
20+
this._getRowJSON(this.rows)
21+
);
22+
} catch (error) {
23+
Logger.warning(error);
24+
}
25+
} else {
26+
throw TypeNotDefinedException.invoke(this.rows.constructor.name);
27+
}
28+
} else {
29+
const jsonApiType = JsonApi.getTypeOfModel(this.rows[0].constructor.name);
30+
const data = this.rows.map(this._getRowJSON.bind(this));
31+
if (jsonApiType !== undefined) {
32+
try {
33+
if (this.pages) {
34+
returnJson = JsonApi.JsonApiSerializer.serialize(jsonApiType, data);
35+
if (_.has(returnJson, 'links.self')) {
36+
this._setPageLinks(returnJson.links, returnJson.links.self);
37+
}
38+
returnJson.meta = _.merge({}, returnJson.meta, {
39+
"page": {
40+
'number': parseInt(this.pages.page),
41+
'size': parseInt(this.pages.perPage),
42+
'pages': parseInt(this.pages.lastPage)
43+
},
44+
'total': parseInt(this.pages.total)
45+
});
46+
} else {
47+
returnJson = JsonApi.JsonApiSerializer.serialize(jsonApiType, data);
48+
returnJson.meta = _.merge({}, returnJson.meta, {'total': this.rows.length});
49+
}
50+
} catch (error) {
51+
Logger.warning(error);
52+
}
53+
} else {
54+
throw TypeNotDefinedException.invoke(this.rows[0].constructor.name);
3655
}
37-
jsonApiPaged.meta = _.merge({}, {
38-
"page": {
39-
'number': parseInt(this.pages.page),
40-
'size': parseInt(this.pages.perPage),
41-
'pages': parseInt(this.pages.lastPage)
42-
},
43-
'total': parseInt(this.pages.total)
44-
});
45-
return jsonApiPaged;
46-
} else {
47-
const jsonApiList = JsonApi.JsonApiSerializer.serialize(jsonApiType, data);
48-
jsonApiList.meta = _.merge({}, {'total': this.rows.length});
49-
return jsonApiList;
50-
}
51-
} catch (error) {
52-
Logger.warning(error);
5356
}
54-
} else {
55-
throw TypeNotDefinedException.invoke(this.rows[0].constructor.name);
56-
}
57+
if (returnJson) {
58+
returnJson = JsonApi.setCommon(returnJson)
59+
}
60+
return returnJson;
5761
}
58-
}
5962

60-
_setPageLinks(links, refLink) {
61-
links.first = refLink + '?' + qs.stringify({
62-
page: {
63-
number: 1,
64-
size: this.pages.perPage
65-
}
66-
});
67-
links.prev = refLink + '?' + qs.stringify({
68-
page: {
69-
number: this.pages.page > 1 ? (this.pages.page - 1) : 1,
70-
size: this.pages.perPage
71-
}
72-
});
73-
links.next = refLink + '?' + qs.stringify({
74-
page: {
75-
number: this.pages.page >= this.pages.lastPage ? this.pages.lastPage : (parseInt(this.pages.page) + 1),
76-
size: this.pages.perPage
77-
}
78-
});
79-
links.last = refLink + '?' + qs.stringify({
80-
page: {
81-
number: this.pages.lastPage,
82-
size: this.pages.perPage
83-
}
84-
});
85-
links.self = refLink + '?' + qs.stringify({
86-
page: {
87-
number: this.pages.page,
88-
size: this.pages.perPage
89-
}
90-
});
91-
}
63+
_setPageLinks(links, refLink) {
64+
links.first = refLink + '?' + qs.stringify({
65+
page: {
66+
number: 1,
67+
size: this.pages.perPage
68+
}
69+
});
70+
links.prev = refLink + '?' + qs.stringify({
71+
page: {
72+
number: this.pages.page > 1 ? (this.pages.page - 1) : 1,
73+
size: this.pages.perPage
74+
}
75+
});
76+
links.next = refLink + '?' + qs.stringify({
77+
page: {
78+
number: this.pages.page >= this.pages.lastPage ? this.pages.lastPage : (parseInt(this.pages.page) + 1),
79+
size: this.pages.perPage
80+
}
81+
});
82+
links.last = refLink + '?' + qs.stringify({
83+
page: {
84+
number: this.pages.lastPage,
85+
size: this.pages.perPage
86+
}
87+
});
88+
links.self = refLink + '?' + qs.stringify({
89+
page: {
90+
number: this.pages.page,
91+
size: this.pages.perPage
92+
}
93+
});
94+
}
9295
}
9396

9497
module.exports = LucidSerializer;

src/Service/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
const _ = require('lodash');
44
const process = require('process');
55
const Serializer = require('json-api-serializer');
6-
const {JsonApiException} = require('../Exceptions');
6+
const {JsonApiException, JsonApiSpecificationException} = require('../Exceptions');
77
const Logger = use('Logger');
88

99
class JsonApi {
1010
constructor(Config) {
1111
this.config = Config.get('jsonApi');
1212
this.JsonApiSerializer = new Serializer(this.config.globalOptions);
1313
this.includeStackTrace = process.env.NODE_ENV !== 'production';
14+
this.JSE = JsonApiSpecificationException;
15+
this.JsonApiException = JsonApiException;
1416
this.jsonApiErrors = [];
1517
}
1618

19+
setCommon(jsonApi) {
20+
if (this.config.commonMeta) {
21+
jsonApi.meta = _.merge({}, jsonApi.meta, this.config.commonMeta);
22+
}
23+
return jsonApi;
24+
}
25+
26+
empty() {
27+
return this.setCommon({
28+
jsonapi: {
29+
version: '1.0'
30+
},
31+
data: null
32+
});
33+
}
34+
1735
getRegistry() {
1836
return this.config.registry;
1937
}

0 commit comments

Comments
 (0)