Skip to content

Commit

Permalink
fix: pluralize fields resource name (#216)
Browse files Browse the repository at this point in the history
* deps: update babel-eslint to version 6.1.1 (#204)

https://greenkeeper.io/

* deps: update rollup to version 0.33.2 (#205)

https://greenkeeper.io/

* deps: update babel-eslint to version 6.1.2 (#206)

https://greenkeeper.io/

* deps: update babel-eslint to version 6.1.1 (#204)

https://greenkeeper.io/

* deps: update test-app dependencies (#208)

* deps: update rollup to version 0.34.0 (#210)

https://greenkeeper.io/

* deps: update rollup to version 0.33.2 (#205)

https://greenkeeper.io/

* Fix sparse fieldsets in included resources

* Adapt tests to new fields semantics

* Use the resource name instead of the model name

* rollup version 0.34.1

* babel-eslint: 6.1.2

* pg: 6.0.2
  • Loading branch information
kureuil authored and zacharygolba committed Jul 17, 2016
1 parent 44025b3 commit 6b62779
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/packages/route/middleware/sanitize-params.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { camelize } from 'inflection';
import { camelize, pluralize } from 'inflection';

import pick from '../../../utils/pick';
import entries from '../../../utils/entries';
Expand Down Expand Up @@ -84,7 +84,7 @@ export default function sanitizeParams(req: IncomingMessage): void {
value = [value];
}

if (key === modelName) {
if (key === pluralize(modelName)) {
const { attributes } = this;

if (value.length) {
Expand Down
3 changes: 2 additions & 1 deletion src/packages/route/middleware/set-fields.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import type { IncomingMessage } from 'http';
import { pluralize } from 'inflection';

/**
* @private
Expand All @@ -14,7 +15,7 @@ export default function setFields(req: IncomingMessage): void {
}
} = req;

fields = fields[this.modelName];
fields = fields[pluralize(this.modelName)];
req.params.fields = fields ? fields : this.attributes;
}
}
9 changes: 5 additions & 4 deletions src/packages/route/middleware/set-include.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { singularize } from 'inflection';
import { pluralize } from 'inflection';

import omit from '../../../utils/omit';

Expand Down Expand Up @@ -35,7 +35,7 @@ export default function setInclude(req: IncomingMessage): void {
}
} = req;

fields = omit(fields, modelName);
fields = omit(fields, pluralize(modelName));

Object.assign(req.params, {
include: relationships.reduce((included, value) => {
Expand All @@ -50,11 +50,12 @@ export default function setInclude(req: IncomingMessage): void {
model: {
serializer: {
attributes: relatedAttrs
}
},
modelName: relatedName
}
} = relationship;

let fieldsForRelationship = fields[singularize(value)];
let fieldsForRelationship = fields[pluralize(relatedName)];

if (fieldsForRelationship) {
fieldsForRelationship = fieldsForRelationship.filter(attr => {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ describe('Integration: class Controller', () => {
before(async () => {
[included, excluded] = await Promise.all([
...[
fetch(`${host}/posts?include=author&${encodeURIComponent('fields[post]')}=title&${encodeURIComponent('fields[author]')}=name`),
fetch(`${host}/posts?${encodeURIComponent('fields[post]')}=title`)
fetch(`${host}/posts?include=author&${encodeURIComponent('fields[posts]')}=title&${encodeURIComponent('fields[authors]')}=name`),
fetch(`${host}/posts?${encodeURIComponent('fields[posts]')}=title`)
].map(async (req) => {
const res = await req;

Expand Down Expand Up @@ -327,8 +327,8 @@ describe('Integration: class Controller', () => {
before(async () => {
[included, excluded] = await Promise.all([
...[
fetch(`${host}/posts/1?include=author&${encodeURIComponent('fields[post]')}=title&${encodeURIComponent('fields[author]')}=name`),
fetch(`${host}/posts/1?${encodeURIComponent('fields[post]')}=title`)
fetch(`${host}/posts/1?include=author&${encodeURIComponent('fields[posts]')}=title&${encodeURIComponent('fields[authors]')}=name`),
fetch(`${host}/posts/1?${encodeURIComponent('fields[posts]')}=title`)
].map(async (req) => {
const res = await req;

Expand Down

0 comments on commit 6b62779

Please sign in to comment.