mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Merge pull request #5990 from cobbspur/simplify
Simplify fields and includes prior to fetch
This commit is contained in:
commit
f30c0ba484
2 changed files with 58 additions and 11 deletions
|
@ -113,7 +113,7 @@ utils = {
|
||||||
slug: {isSlug: true},
|
slug: {isSlug: true},
|
||||||
page: {matches: /^\d+$/},
|
page: {matches: /^\d+$/},
|
||||||
limit: {matches: /^\d+|all$/},
|
limit: {matches: /^\d+|all$/},
|
||||||
fields: {matches: /^[a-z0-9_,]+$/},
|
fields: {matches: /^[\w, ]+$/},
|
||||||
name: {}
|
name: {}
|
||||||
},
|
},
|
||||||
// these values are sanitised/validated separately
|
// these values are sanitised/validated separately
|
||||||
|
@ -218,20 +218,23 @@ utils = {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
prepareInclude: function prepareInclude(include, allowedIncludes) {
|
trimAndLowerCase: function trimAndLowerCase(params) {
|
||||||
include = include || '';
|
params = params || '';
|
||||||
include = _.intersection(include.split(','), allowedIncludes);
|
if (_.isString(params)) {
|
||||||
|
params = params.split(',');
|
||||||
|
}
|
||||||
|
|
||||||
return include;
|
return _.map(params, function (item) {
|
||||||
|
return item.trim().toLowerCase();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
prepareInclude: function prepareInclude(include, allowedIncludes) {
|
||||||
|
return _.intersection(this.trimAndLowerCase(include), allowedIncludes);
|
||||||
},
|
},
|
||||||
|
|
||||||
prepareFields: function prepareFields(fields) {
|
prepareFields: function prepareFields(fields) {
|
||||||
fields = fields || '';
|
return this.trimAndLowerCase(fields);
|
||||||
if (_.isString(fields)) {
|
|
||||||
fields = fields.split(',');
|
|
||||||
}
|
|
||||||
|
|
||||||
return fields;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -214,6 +214,26 @@ describe('Post API', function () {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can include author and be case insensitive', function (done) {
|
||||||
|
PostAPI.browse({context: {user: 1}, status: 'all', include: 'Author'}).then(function (results) {
|
||||||
|
should.exist(results.posts);
|
||||||
|
should.exist(results.posts[0].author.name);
|
||||||
|
results.posts[0].author.name.should.eql('Joe Bloggs');
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can include author and ignore space in include', function (done) {
|
||||||
|
PostAPI.browse({context: {user: 1}, status: 'all', include: ' author'}).then(function (results) {
|
||||||
|
should.exist(results.posts);
|
||||||
|
should.exist(results.posts[0].author.name);
|
||||||
|
results.posts[0].author.name.should.eql('Joe Bloggs');
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('can fetch all posts for an author', function (done) {
|
it('can fetch all posts for an author', function (done) {
|
||||||
PostAPI.browse({context: {user: 1}, status: 'all', filter: 'author:joe-bloggs', include: 'author'}).then(function (results) {
|
PostAPI.browse({context: {user: 1}, status: 'all', filter: 'author:joe-bloggs', include: 'author'}).then(function (results) {
|
||||||
should.exist(results.posts);
|
should.exist(results.posts);
|
||||||
|
@ -273,6 +293,30 @@ describe('Post API', function () {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('with context.user can fetch multiple fields and be case insensitive', function (done) {
|
||||||
|
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'Slug,Published_At'}).then(function (results) {
|
||||||
|
should.exist(results.posts);
|
||||||
|
|
||||||
|
results.posts[0].published_at.should.exist;
|
||||||
|
results.posts[0].slug.should.exist;
|
||||||
|
should.not.exist(results.posts[0].title);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with context.user can fetch multiple fields ignoring spaces', function (done) {
|
||||||
|
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: ' slug , published_at '}).then(function (results) {
|
||||||
|
should.exist(results.posts);
|
||||||
|
|
||||||
|
results.posts[0].published_at.should.exist;
|
||||||
|
results.posts[0].slug.should.exist;
|
||||||
|
should.not.exist(results.posts[0].title);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('with context.user can fetch a field and not return invalid field', function (done) {
|
it('with context.user can fetch a field and not return invalid field', function (done) {
|
||||||
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'foo,title'}).then(function (results) {
|
PostAPI.browse({context: {user: 1}, status: 'all', limit: 5, fields: 'foo,title'}).then(function (results) {
|
||||||
var objectKeys;
|
var objectKeys;
|
||||||
|
|
Loading…
Add table
Reference in a new issue