You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-07 23:02:56 +03:00
Filter out falsey opts in /relations API hits (#2059)
This commit is contained in:
committed by
GitHub
parent
6244d77d44
commit
169b6b5572
@@ -26,6 +26,15 @@ describe("utils", function() {
|
||||
"foo=bar&baz=beer%40",
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle boolean and numeric values", function() {
|
||||
const params = {
|
||||
string: "foobar",
|
||||
number: 12345,
|
||||
boolean: false,
|
||||
};
|
||||
expect(utils.encodeParams(params)).toEqual("string=foobar&number=12345&boolean=false");
|
||||
});
|
||||
});
|
||||
|
||||
describe("encodeUri", function() {
|
||||
|
@@ -143,7 +143,7 @@ export interface IBindThreePidBody {
|
||||
export interface IRelationsRequestOpts {
|
||||
from?: string;
|
||||
to?: string;
|
||||
limit?: string;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface IRelationsResponse {
|
||||
|
@@ -6763,11 +6763,7 @@ export class MatrixClient extends EventEmitter {
|
||||
eventType?: EventType | string | null,
|
||||
opts: IRelationsRequestOpts = {},
|
||||
): Promise<IRelationsResponse> {
|
||||
const params = new URLSearchParams();
|
||||
for (const [key, val] of Object.entries(opts)) {
|
||||
params.set(key, val);
|
||||
}
|
||||
const queryString = params.toString();
|
||||
const queryString = utils.encodeParams(opts as Record<string, string | number>);
|
||||
|
||||
let templatedUrl = "/rooms/$roomId/relations/$eventId";
|
||||
if (relationType !== null) templatedUrl += "/$relationType";
|
||||
|
11
src/utils.ts
11
src/utils.ts
@@ -27,12 +27,19 @@ import type NodeCrypto from "crypto";
|
||||
|
||||
/**
|
||||
* Encode a dictionary of query parameters.
|
||||
* Omits any undefined/null values.
|
||||
* @param {Object} params A dict of key/values to encode e.g.
|
||||
* {"foo": "bar", "baz": "taz"}
|
||||
* @return {string} The encoded string e.g. foo=bar&baz=taz
|
||||
*/
|
||||
export function encodeParams(params: Record<string, string>): string {
|
||||
return new URLSearchParams(params).toString();
|
||||
export function encodeParams(params: Record<string, string | number | boolean>): string {
|
||||
const searchParams = new URLSearchParams();
|
||||
for (const [key, val] of Object.entries(params)) {
|
||||
if (val !== undefined && val !== null) {
|
||||
searchParams.set(key, String(val));
|
||||
}
|
||||
}
|
||||
return searchParams.toString();
|
||||
}
|
||||
|
||||
export type QueryDict = Record<string, string | string[]>;
|
||||
|
Reference in New Issue
Block a user