Skip to content

Commit 94d4751

Browse files
Nick Frasserdigitalsadhu
authored andcommitted
feat(serialize): Use HTTP path when building model relation links
If a path is defined. Otherwise use the plural name, as before
1 parent 9f4a5be commit 94d4751

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

lib/serialize.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ module.exports = function (app, defaults) {
1818
app.remotes().after('**', function (ctx, next) {
1919
var data,
2020
type,
21+
path,
2122
options,
2223
modelNamePlural,
24+
modelPath,
2325
relatedModel,
2426
relatedModelPlural,
27+
relatedModelPath,
2528
relations,
2629
model,
2730
requestedIncludes
@@ -56,7 +59,9 @@ module.exports = function (app, defaults) {
5659
data = utils.clone(ctx.result)
5760
model = utils.getModelFromContext(ctx, app)
5861
modelNamePlural = utils.pluralForModel(model)
62+
modelPath = utils.httpPathForModel(model)
5963
type = modelNamePlural
64+
path = modelPath
6065
relations = utils.getRelationsFromContext(ctx, app)
6166

6267
/**
@@ -83,11 +88,13 @@ module.exports = function (app, defaults) {
8388
relatedModel = relation.modelTo
8489
}
8590
relatedModelPlural = utils.pluralForModel(relatedModel)
91+
relatedModelPath = utils.httpPathForModel(relatedModel)
8692
primaryKeyField = utils.primaryKeyForModel(relatedModel)
8793

8894
if (relatedModelPlural) {
8995
type = relatedModelPlural
9096
model = relatedModel
97+
path = relatedModelPath
9198
relations = model.relations
9299
}
93100
}
@@ -118,6 +125,7 @@ module.exports = function (app, defaults) {
118125
options = {
119126
app: app,
120127
model: model,
128+
modelPath: path,
121129
method: ctx.method.name,
122130
meta: ctx.meta ? utils.clone(ctx.meta) : null,
123131
primaryKeyField: primaryKeyField,
@@ -126,15 +134,13 @@ module.exports = function (app, defaults) {
126134
dataLinks: {
127135
self: function (item) {
128136
var urlComponents = url.parse(options.host)
129-
var args = [urlComponents.protocol.replace(':', ''), urlComponents.host, options.restApiRoot]
130-
131-
if (relatedModelPlural) {
132-
args.push(relatedModelPlural)
133-
} else {
134-
args.push(modelNamePlural)
135-
}
136-
137-
args.push(item.id)
137+
var args = [
138+
urlComponents.protocol.replace(':', ''),
139+
urlComponents.host,
140+
options.restApiRoot,
141+
path,
142+
item.id
143+
]
138144

139145
return utils.buildModelUrl.apply(this, args)
140146
}

lib/serializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function parseRelations (data, relations, options) {
216216
if (!options.isRelationshipRequest) {
217217
relationships[name] = {
218218
links: {
219-
related: options.host + options.restApiRoot + '/' + options.type + '/' + pk + '/' + name
219+
related: options.host + options.restApiRoot + '/' + options.modelPath + '/' + pk + '/' + name
220220
}
221221
}
222222
}

lib/utils.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
hostFromContext: hostFromContext,
1414
modelNameFromContext: modelNameFromContext,
1515
pluralForModel: pluralForModel,
16+
httpPathForModel: httpPathForModel,
1617
urlFromContext: urlFromContext,
1718
primaryKeyForModel: primaryKeyForModel,
1819
shouldNotApplyJsonApi: shouldNotApplyJsonApi,
@@ -36,10 +37,6 @@ function pluralForModel (model) {
3637
return model.pluralModelName
3738
}
3839

39-
if (model.settings && model.settings.http && model.settings.http.path) {
40-
return model.settings.http.path
41-
}
42-
4340
if (model.settings && model.settings.plural) {
4441
return model.settings.plural
4542
}
@@ -51,6 +48,20 @@ function pluralForModel (model) {
5148
return inflection.pluralize(model.sharedClass.name)
5249
}
5350

51+
/**
52+
* Returns the plural path for a model
53+
* @public
54+
* @memberOf {Utils}
55+
* @param {Object} model
56+
* @return {String}
57+
*/
58+
function httpPathForModel (model) {
59+
if (model.settings && model.settings.http && model.settings.http.path) {
60+
return model.settings.http.path
61+
}
62+
return pluralForModel(model);
63+
}
64+
5465
/**
5566
* Clones an object by stringifying it and parsing it.
5667
* @public

0 commit comments

Comments
 (0)