Skip to content

Commit e8553b2

Browse files
committed
Merge pull request #69 from digitalsadhu/improve_cases
Improve readme and remove case option
2 parents bc6fb9d + 9143806 commit e8553b2

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,37 @@ In your loopback project:
4545
2. Create a `component-config.json` file in your server folder (if you don't already have one)
4646
3. Add the following config to `component-config.json`
4747
```json
48+
{
49+
"loopback-component-jsonapi": {}
50+
}
51+
```
52+
53+
## Advanced usage:
54+
In a fairly limited way, you can configure a how the component behaves.
55+
56+
Example:
57+
```json
4858
{
4959
"loopback-component-jsonapi": {
5060
"restApiRoot": "/api",
51-
"enable": true,
52-
"keyForAttribute": "dash-case",
53-
"keyForType": "dash-case",
54-
"keyForRelation": "dash-case"
61+
"enable": true
5562
}
5663
}
5764
```
65+
### restApiRoot
66+
Url prefix to be used in conjunction with host and resource paths.
67+
eg. http://127.0.0.1:3214/api/people
68+
Default: `/api`
69+
70+
### enable
71+
Whether the component should be enabled or disabled.
72+
Default: true
73+
74+
## Debugging
75+
You can enable debug logging by setting an environment variable:
76+
DEBUG=loopback-component-jsonapi
77+
78+
Example:
79+
```
80+
DEBUG=loopback-component-jsonapi node .
81+
```

lib/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ var debug = require('debug')('loopback-component-jsonapi');
1515
module.exports = function (app, options) {
1616
var defaultOptions = {
1717
restApiRoot: '/api',
18-
enable: true,
19-
keyForAttribute: 'dash-case',
20-
keyForType: 'dash-case',
21-
keyForRelation: 'dash-case'
18+
enable: true
2219
};
2320
options = options || {};
2421
options = _.defaults(options, defaultOptions);

lib/serialize.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ var regexs = [
1818
module.exports = function (app, defaults) {
1919
app.remotes().after('**', function (ctx, next) {
2020
var data,
21-
modelName,
2221
type,
2322
options,
2423
relatedModelName,
24+
modelNamePlural,
2525
relatedModelPlural,
2626
relations,
2727
res;
@@ -48,8 +48,8 @@ module.exports = function (app, defaults) {
4848
}
4949

5050
data = utils.clone(ctx.result);
51-
modelName = utils.modelNameFromContext(ctx);
52-
type = modelName;
51+
modelNamePlural = utils.pluralForModel(utils.getModelFromContext(ctx, app));
52+
type = modelNamePlural;
5353

5454
/**
5555
* HACK: specifically when data is null and GET :model/:id
@@ -89,7 +89,7 @@ module.exports = function (app, defaults) {
8989
if (relatedModelPlural) {
9090
args.push(relatedModelPlural);
9191
} else {
92-
args.push(utils.pluralForModel(app.models[modelName]));
92+
args.push(modelNamePlural);
9393
}
9494

9595
args.push(item.id);

lib/serializer.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var _ = require('lodash');
2-
var inflection = require('inflection');
32
var utils = require('./utils');
43

54
module.exports = function serializer (type, data, relations, options) {
@@ -44,8 +43,6 @@ function parseResource (type, data, relations, options) {
4443
var attributes = {};
4544
var relationships;
4645

47-
type = inflection.pluralize(type);
48-
type = caserize(type, options.keyForRelation);
4946
resource.type = type;
5047
relationships = parseRelations(data, relations, options);
5148

@@ -57,7 +54,6 @@ function parseResource (type, data, relations, options) {
5754
if (property === 'id') {
5855
resource.id = _(value).toString();
5956
} else {
60-
property = caserize(property, options.keyForAttribute);
6157
attributes[property] = value;
6258
}
6359
});
@@ -141,7 +137,6 @@ function parseRelations (data, relations, options) {
141137
var pk = data[pkName];
142138
var fk = data[fkName];
143139

144-
name = caserize(name, options.keyForRelation);
145140
var fromType = utils.pluralForModel(relation.modelFrom);
146141
var toType = utils.pluralForModel(relation.modelTo);
147142

@@ -193,7 +188,7 @@ function makeRelation (type, id, options) {
193188
}
194189

195190
return {
196-
type: caserize(type, options.keyForRelation),
191+
type: type,
197192
id: id
198193
};
199194
}
@@ -238,23 +233,3 @@ function makeLinks (links, item) {
238233

239234
return retLinks;
240235
}
241-
242-
/**
243-
* Handles converting string cases.
244-
* @param {String} value
245-
* @param {String} type
246-
* @return {String}
247-
*/
248-
function caserize (value, type) {
249-
type = type || 'dash-case';
250-
251-
var mapping = {
252-
'dash-case': _.kebabCase,
253-
'underscore_case': inflection.underscore,
254-
'camelCase': _.camelCase,
255-
'Classify': inflection.classify
256-
};
257-
258-
var func = mapping[type];
259-
return func(value);
260-
}

lib/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module.exports = {
2323
* @return {String}
2424
*/
2525
function pluralForModel (model) {
26+
if (model.pluralModelName) {
27+
return model.pluralModelName;
28+
}
29+
2630
if (model.settings && model.settings.http && model.settings.http.path) {
2731
return model.settings.http.path;
2832
}

0 commit comments

Comments
 (0)