File tree Expand file tree Collapse file tree 4 files changed +20
-8
lines changed Expand file tree Collapse file tree 4 files changed +20
-8
lines changed Original file line number Diff line number Diff 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 ( ) {
Original file line number Diff line number Diff line change @@ -143,7 +143,7 @@ export interface IBindThreePidBody {
143143export interface IRelationsRequestOpts {
144144 from ?: string ;
145145 to ?: string ;
146- limit ?: string ;
146+ limit ?: number ;
147147}
148148
149149export interface IRelationsResponse {
Original file line number Diff line number Diff 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" ;
Original file line number Diff line number Diff 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
3845export type QueryDict = Record < string , string | string [ ] > ;
You can’t perform that action at this time.
0 commit comments