Skip to content

Commit a4bd944

Browse files
authored
Add ability to pass arbitrary query params (#101)
```ts Employee.where({ active: true }).qp({ debug: true }) ``` Would hit `/employees?filter[active]=true&debug=true`
1 parent a8748e6 commit a4bd944

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ export class JSORMBase {
752752
return this.scope().per(size)
753753
}
754754

755+
static extraParams<I extends typeof JSORMBase>(this: I, clause: any) {
756+
return this.scope().extraParams(clause)
757+
}
758+
755759
static order<I extends typeof JSORMBase>(
756760
this: I,
757761
clause: SortScope | string

src/scope.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class Scope<T extends typeof JSORMBase = typeof JSORMBase> {
4747
private _extra_fields: FieldScope = {}
4848
private _include: IncludeScopeHash = {}
4949
private _stats: StatsScope = {}
50+
private _extraParams: any = {}
5051

5152
constructor(model: T) {
5253
this.model = model
@@ -112,6 +113,17 @@ export class Scope<T extends typeof JSORMBase = typeof JSORMBase> {
112113
return copy
113114
}
114115

116+
extraParams(clause: any): Scope<T> {
117+
const copy = this.copy()
118+
119+
for (const key in clause) {
120+
if (clause.hasOwnProperty(key)) {
121+
copy._extraParams[key] = clause[key]
122+
}
123+
}
124+
return copy
125+
}
126+
115127
stats(clause: StatsScope): Scope<T> {
116128
const copy = this.copy()
117129

@@ -210,6 +222,10 @@ export class Scope<T extends typeof JSORMBase = typeof JSORMBase> {
210222

211223
this._mergeAssociationQueryParams(qp, this._associations)
212224

225+
Object.keys(this._extraParams).forEach((k) => {
226+
qp[k] = this._extraParams[k]
227+
})
228+
213229
return qp
214230
}
215231

test/unit/scope.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ describe("Scope", () => {
203203
.select({ pets: ["type"] })
204204
.selectExtra({ people: ["net_worth"] })
205205
.stats({ total: "count" })
206+
.extraParams({ foo: "bar" })
206207
.includes({ a: ["b", { c: "d" }] })
207208
const qp = scope.asQueryParams()
208209

@@ -226,6 +227,7 @@ describe("Scope", () => {
226227
stats: {
227228
total: "count"
228229
},
230+
foo: "bar",
229231
include: "a.b,a.c.d"
230232
})
231233
})
@@ -259,6 +261,23 @@ describe("Scope", () => {
259261
expect(scope.toQueryParams()).to.eq(undefined)
260262
})
261263
})
264+
265+
describe("when arbitrary query params added", () => {
266+
it("adds to the param string", () => {
267+
scope = scope.extraParams({ foo: "bar", bar: "baz" })
268+
expect((<string>scope.toQueryParams())).to.eq("foo=bar&bar=baz")
269+
})
270+
271+
it("casts arrays correctly", () => {
272+
scope = scope.extraParams({ foo: "bar,baz" })
273+
expect((<string>scope.toQueryParams())).to.eq("foo=bar,baz")
274+
})
275+
276+
it("casts objects correctly", () => {
277+
scope = scope.extraParams({ foo: { bar: "baz" } })
278+
expect((<string>scope.toQueryParams())).to.eq("foo[bar]=baz")
279+
})
280+
})
262281
})
263282

264283
describe("#copy", () => {

0 commit comments

Comments
 (0)