Skip to content

Commit dc569ae

Browse files
author
Lee Richmond
committed
Add support for extra_fields
1 parent f3bba11 commit dc569ae

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export default class Model {
7373
return this.scope().select(clause);
7474
}
7575

76+
static selectExtra(clause: Object) : Scope {
77+
return this.scope().selectExtra(clause);
78+
}
79+
7680
static includes(clause: string | Object | Array<any>) : Scope {
7781
return this.scope().includes(clause);
7882
}

src/scope.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class Scope {
1111
_filter: Object = {};
1212
_sort: Object = {};
1313
_fields: Object = {};
14+
_extra_fields: Object = {};
1415
_include: Object = {};
1516

1617
constructor(model : typeof Model) {
@@ -75,6 +76,14 @@ export default class Scope {
7576
return this;
7677
}
7778

79+
selectExtra(clause: Object) {
80+
for (let key in clause) {
81+
this._extra_fields[key] = clause[key];
82+
}
83+
84+
return this;
85+
}
86+
7887
includes(clause: Object | string | Array<any>) : Scope {
7988
let directive = new IncludeDirective(clause);
8089
let directiveObject = directive.toObject();
@@ -89,11 +98,12 @@ export default class Scope {
8998
asQueryParams() : Object {
9099
let qp = {};
91100

92-
qp['page'] = this._pagination;
93-
qp['filter'] = this._filter;
94-
qp['sort'] = this._sortParam(this._sort);
95-
qp['fields'] = this._fields;
96-
qp['include'] = new IncludeDirective(this._include).toString();
101+
qp['page'] = this._pagination;
102+
qp['filter'] = this._filter;
103+
qp['sort'] = this._sortParam(this._sort);
104+
qp['fields'] = this._fields;
105+
qp['extra_fields'] = this._extra_fields;
106+
qp['include'] = new IncludeDirective(this._include).toString();
97107

98108
return qp;
99109
}

test/integration/finders-test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ describe('Model finders', function() {
166166
});
167167
});
168168

169+
describe('#select_extra', function() {
170+
before(function () {
171+
fetchMock.get('http://example.com/api/v1/people?extra_fields[people]=net_worth,best_friend', {
172+
data: [
173+
{ id: '2', type: 'people' }
174+
]
175+
});
176+
});
177+
178+
it('queries correctly', function() {
179+
return expect(Person.selectExtra({ people: ['net_worth', 'best_friend'] }).all()).to.eventually
180+
.all.be.instanceof(Person)
181+
.all.have.property('id', '2')
182+
});
183+
});
184+
169185
describe('#includes', function() {
170186
before(function () {
171187
fetchMock.get('http://example.com/api/v1/people?include=a.b,a.c.d', {

test/unit/scope-test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ describe('Scope', function() {
129129
.order({ bar: 'desc' })
130130
.select({ people: ['name', 'age'] })
131131
.select({ pets: ['type'] })
132+
.selectExtra({ people: ['net_worth'] })
132133
.includes({ a: ['b', { c: 'd' }] })
133134
let qp = scope.asQueryParams();
134135

@@ -146,6 +147,9 @@ describe('Scope', function() {
146147
people: ['name', 'age'],
147148
pets: ['type']
148149
},
150+
extra_fields: {
151+
people: ['net_worth']
152+
},
149153
include: 'a.b,a.c.d'
150154
});
151155
});
@@ -164,6 +168,11 @@ describe('Scope', function() {
164168
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');
165169
});
166170

171+
it('does not include empty objects', function() {
172+
scope.page(2);
173+
expect(scope.toQueryParams().match(/field/) === null).to.eq(true);
174+
});
175+
167176
describe('when no scoping criteria present', function() {
168177
it('returns undefined', function() {
169178
expect(scope.toQueryParams()).to.eq(undefined);

0 commit comments

Comments
 (0)