Skip to content

Commit bbf44a6

Browse files
committed
Add Scope#stats() method
Allows the user to specify stats that they would like to have returned form the request. Follow up commit will expose this on the CollectionProxy object.
1 parent bfea54d commit bbf44a6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/scope.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class Scope {
1313
_fields: Object = {};
1414
_extra_fields: Object = {};
1515
_include: Object = {};
16+
_stats: Object = {};
1617

1718
constructor(model : typeof Model) {
1819
this.model = model;
@@ -56,6 +57,13 @@ export default class Scope {
5657
return this;
5758
}
5859

60+
stats(clause: Object) : Scope {
61+
for (let key in clause) {
62+
this._stats[key] = clause[key];
63+
}
64+
return this;
65+
}
66+
5967
order(clause: Object | string) : Scope {
6068
if (typeof clause == "object") {
6169
for (let key in clause) {
@@ -107,6 +115,7 @@ export default class Scope {
107115
qp['sort'] = this._sortParam(this._sort);
108116
qp['fields'] = this._fields;
109117
qp['extra_fields'] = this._extra_fields;
118+
qp['stats'] = this._stats;
110119
qp['include'] = new IncludeDirective(this._include).toString();
111120

112121
return qp;

test/unit/scope-test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ describe('Scope', function() {
4949
});
5050
});
5151

52+
describe('#stats()', function() {
53+
it('updates stats request', function() {
54+
scope.stats({ total: 'count' });
55+
scope.stats({ average: 'cost' });
56+
57+
expect(scope._stats).to.eql({
58+
total: 'count',
59+
average: 'cost'
60+
});
61+
});
62+
63+
it('returns the scope', function() {
64+
expect(scope.stats({ total: 'count' })).to.be.instanceof(Scope)
65+
});
66+
});
67+
5268
describe('#order()', function() {
5369
it('updates sort criteria', function() {
5470
scope.order('foo');
@@ -136,6 +152,7 @@ describe('Scope', function() {
136152
.select({ people: ['name', 'age'] })
137153
.select({ pets: ['type'] })
138154
.selectExtra({ people: ['net_worth'] })
155+
.stats({ total: 'count' })
139156
.includes({ a: ['b', { c: 'd' }] })
140157
let qp = scope.asQueryParams();
141158

@@ -156,6 +173,9 @@ describe('Scope', function() {
156173
extra_fields: {
157174
people: ['net_worth']
158175
},
176+
stats: {
177+
total: 'count'
178+
},
159179
include: 'a.b,a.c.d'
160180
});
161181
});
@@ -170,8 +190,9 @@ describe('Scope', function() {
170190
.order('foo')
171191
.order({ bar: 'desc' })
172192
.select({ people: ['name', 'age'] })
193+
.stats({ total: 'count' })
173194
.includes({ a: ['b', { c: 'd' }] })
174-
expect(scope.toQueryParams()).to.eq('page[number]=2&page[size]=10&filter[foo]=bar&sort=foo,-bar&fields[people]=name,age&include=a.b,a.c.d');
195+
expect(scope.toQueryParams()).to.eq('page[number]=2&page[size]=10&filter[foo]=bar&sort=foo,-bar&fields[people]=name,age&stats[total]=count&include=a.b,a.c.d');
175196
});
176197

177198
it('does not include empty objects', function() {

0 commit comments

Comments
 (0)