Skip to content

resource type bug fix - added api/v1 from config #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var _ = require('lodash')
var RelUtils = require('./utilities/relationship-utils')
var utils = require('./utils')


var apiRoot = 'api/v1/';
function defaultBeforeSerialize (options, cb) {
cb(null, options)
}
Expand Down Expand Up @@ -33,6 +34,10 @@ function defaultSerialize (options, cb) {
}

resultData.data = result

// Overriding data.links as the links were wrong
resultData.data.links = makeLinks(options.topLevelLinks);

if (options.meta) resultData.meta = options.meta

/**
Expand Down Expand Up @@ -60,10 +65,16 @@ function defaultAfterSerialize (options, cb) {
}

module.exports = function serializer (type, data, relations, options, cb) {

// #SAM
// as we always have model type based on api versioning lets say api/v1 concatenating this
type = apiRoot + type ;

options = _.clone(options)
options.attributes = options.attributes || {}

options.isRelationshipRequest = false
options.modelPath = apiRoot + options.modelPath;

if (options.topLevelLinks.self.match(/\/relationships\//)) {
options.topLevelLinks.related = options.topLevelLinks.self.replace(
Expand Down Expand Up @@ -238,15 +249,16 @@ function parseRelations (data, relations, options) {
} else {
toType = utils.pluralForModel(relation.modelTo)
}


toType = apiRoot + toType
// Relationship `links` should always be defined unless this is a
// relationship request
if (!options.isRelationshipRequest) {

relationships[name] = {
links: {
related: options.host +
options.restApiRoot +
'/' +
'/' +
options.modelPath +
'/' +
pk +
Expand Down Expand Up @@ -411,7 +423,7 @@ function handleIncludes (resp, includes, relations, options) {
resource.relationships[include].data = included.map(relData => {
return {
id: String(relData[propertyKey]),
type: plural
type: apiRoot + plural
}
})
} else {
Expand All @@ -427,7 +439,7 @@ function handleIncludes (resp, includes, relations, options) {

resource.relationships[include].data = {
id: String(resource.attributes[include][propertyKey]),
type: plural
type: apiRoot + plural
}
embeds.push(compoundIncludes)
}
Expand Down Expand Up @@ -482,6 +494,7 @@ function createCompoundIncludes (
includedRelations,
options
) {
type = apiRoot + type;
var compoundInclude = makeRelation(type, String(relationship[key]))

if (options && !_.isEmpty(includedRelations)) {
Expand Down