Skip to content

Commit

Permalink
Adding 'fields' param for browse requests
Browse files Browse the repository at this point in the history
refs TryGhost#5601, TryGhost#5463, TryGhost#5343

- adds rudimentary support for a 'fields' parameter on browse requests
  • Loading branch information
ErisDS committed Aug 28, 2015
1 parent f705798 commit e9a9681
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
16 changes: 15 additions & 1 deletion core/server/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ utils = {
// ### Manual Default Options
// These must be provided by the endpoint
// browseDefaultOptions - valid for all browse api endpoints
browseDefaultOptions: ['page', 'limit'],
browseDefaultOptions: ['page', 'limit', 'fields'],
// idDefaultOptions - valid whenever an id is valid
idDefaultOptions: ['id'],

Expand Down Expand Up @@ -112,6 +112,7 @@ utils = {
uuid: {isUUID: true},
page: {matches: /^\d+$/},
limit: {matches: /^\d+|all$/},
fields: {matches: /^[a-z0-9_,]+$/},
name: {}
},
// these values are sanitised/validated separately
Expand Down Expand Up @@ -223,6 +224,15 @@ utils = {
return include;
},

prepareFields: function prepareFields(fields) {
fields = fields || '';
if (_.isString(fields)) {
fields = fields.split(',');
}

return fields;
},

/**
* ## Convert Options
* @param {Array} allowedIncludes
Expand All @@ -238,6 +248,10 @@ utils = {
if (options.include) {
options.include = utils.prepareInclude(options.include, allowedIncludes);
}
if (options.fields) {
options.columns = utils.prepareFields(options.fields);
delete options.fields;
}
return options;
};
},
Expand Down
15 changes: 11 additions & 4 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,18 @@ Post = ghostBookshelf.Model.extend({
},

toJSON: function toJSON(options) {
options = options || {};

var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);

attrs.author = attrs.author || attrs.author_id;
attrs.url = config.urlPathForPost(attrs, permalinkSetting);
delete attrs.author_id;
if (!options.columns || (options.columns && options.columns.indexOf('author') > -1)) {
attrs.author = attrs.author || attrs.author_id;
delete attrs.author_id;
}

if (!options.columns || (options.columns && options.columns.indexOf('url') > -1)) {
attrs.url = config.urlPathForPost(attrs, permalinkSetting);
}

return attrs;
}
Expand Down Expand Up @@ -346,7 +353,7 @@ Post = ghostBookshelf.Model.extend({
validOptions = {
findAll: ['withRelated'],
findOne: ['importing', 'withRelated'],
findPage: ['page', 'limit', 'status', 'staticPages', 'featured'],
findPage: ['page', 'limit', 'columns', 'status', 'staticPages', 'featured'],
add: ['importing']
};

Expand Down
4 changes: 3 additions & 1 deletion core/server/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Tag = ghostBookshelf.Model.extend({
},

toJSON: function toJSON(options) {
options = options || {};

var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);

attrs.parent = attrs.parent || attrs.parent_id;
Expand Down Expand Up @@ -81,7 +83,7 @@ Tag = ghostBookshelf.Model.extend({
// whitelists for the `options` hash argument on methods, by method name.
// these are the only options that can be passed to Bookshelf / Knex.
validOptions = {
findPage: ['page', 'limit']
findPage: ['page', 'limit', 'columns']
};

if (validOptions[methodName]) {
Expand Down
4 changes: 3 additions & 1 deletion core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ User = ghostBookshelf.Model.extend({
},

toJSON: function toJSON(options) {
options = options || {};

var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
// remove password hash for security reasons
delete attrs.password;
Expand Down Expand Up @@ -229,7 +231,7 @@ User = ghostBookshelf.Model.extend({
findAll: ['withRelated'],
setup: ['id'],
edit: ['withRelated', 'id'],
findPage: ['page', 'limit', 'status']
findPage: ['page', 'limit', 'columns', 'status']
};

if (validOptions[methodName]) {
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/api_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('API Utils', function () {
describe('Default Options', function () {
it('should provide a set of default options', function () {
apiUtils.globalDefaultOptions.should.eql(['context', 'include']);
apiUtils.browseDefaultOptions.should.eql(['page', 'limit']);
apiUtils.browseDefaultOptions.should.eql(['page', 'limit', 'fields']);
apiUtils.dataDefaultOptions.should.eql(['data']);
apiUtils.idDefaultOptions.should.eql(['id']);
});
Expand Down

0 comments on commit e9a9681

Please sign in to comment.