Skip to content

Commit 169b6b5

Browse files
authored
Filter out falsey opts in /relations API hits (#2059)
1 parent 6244d77 commit 169b6b5

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

spec/unit/utils.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ describe("utils", function() {
2626
"foo=bar&baz=beer%40",
2727
);
2828
});
29+
30+
it("should handle boolean and numeric values", function() {
31+
const params = {
32+
string: "foobar",
33+
number: 12345,
34+
boolean: false,
35+
};
36+
expect(utils.encodeParams(params)).toEqual("string=foobar&number=12345&boolean=false");
37+
});
2938
});
3039

3140
describe("encodeUri", function() {

src/@types/requests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export interface IBindThreePidBody {
143143
export interface IRelationsRequestOpts {
144144
from?: string;
145145
to?: string;
146-
limit?: string;
146+
limit?: number;
147147
}
148148

149149
export interface IRelationsResponse {

src/client.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6763,11 +6763,7 @@ export class MatrixClient extends EventEmitter {
67636763
eventType?: EventType | string | null,
67646764
opts: IRelationsRequestOpts = {},
67656765
): Promise<IRelationsResponse> {
6766-
const params = new URLSearchParams();
6767-
for (const [key, val] of Object.entries(opts)) {
6768-
params.set(key, val);
6769-
}
6770-
const queryString = params.toString();
6766+
const queryString = utils.encodeParams(opts as Record<string, string | number>);
67716767

67726768
let templatedUrl = "/rooms/$roomId/relations/$eventId";
67736769
if (relationType !== null) templatedUrl += "/$relationType";

src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ import type NodeCrypto from "crypto";
2727

2828
/**
2929
* Encode a dictionary of query parameters.
30+
* Omits any undefined/null values.
3031
* @param {Object} params A dict of key/values to encode e.g.
3132
* {"foo": "bar", "baz": "taz"}
3233
* @return {string} The encoded string e.g. foo=bar&baz=taz
3334
*/
34-
export function encodeParams(params: Record<string, string>): string {
35-
return new URLSearchParams(params).toString();
35+
export function encodeParams(params: Record<string, string | number | boolean>): string {
36+
const searchParams = new URLSearchParams();
37+
for (const [key, val] of Object.entries(params)) {
38+
if (val !== undefined && val !== null) {
39+
searchParams.set(key, String(val));
40+
}
41+
}
42+
return searchParams.toString();
3643
}
3744

3845
export type QueryDict = Record<string, string | string[]>;

0 commit comments

Comments
 (0)