From 57d5cfb480cbb52fc545a49f01ddd536af2336aa Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 25 Nov 2023 22:55:11 +1300 Subject: [PATCH] Fix path() doing nothing without a mxid --- matrix/api.js | 8 +++++--- matrix/api.test.js | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/matrix/api.js b/matrix/api.js index d6fd28b6..bbcb8644 100644 --- a/matrix/api.js +++ b/matrix/api.js @@ -19,15 +19,17 @@ const makeTxnId = sync.require("./txnid") * @returns {string} the new endpoint */ function path(p, mxid, otherParams = {}) { - if (!mxid) return p const u = new URL(p, "http://localhost") - u.searchParams.set("user_id", mxid) + if (mxid) u.searchParams.set("user_id", mxid) for (const entry of Object.entries(otherParams)) { if (entry[1] != undefined) { u.searchParams.set(entry[0], entry[1]) } } - return u.pathname + "?" + u.searchParams.toString() + let result = u.pathname + const str = u.searchParams.toString() + if (str) result += "?" + str + return result } /** diff --git a/matrix/api.test.js b/matrix/api.test.js index 6c74e50e..82565ebe 100644 --- a/matrix/api.test.js +++ b/matrix/api.test.js @@ -20,3 +20,7 @@ test("api path: existing query parameters with mxid", t => { test("api path: real world mxid", t => { t.equal(path("/hello/world", "@cookie_monster:cadence.moe"), "/hello/world?user_id=%40cookie_monster%3Acadence.moe") }) + +test("api path: extras number works", t => { + t.equal(path(`/client/v3/rooms/!example/timestamp_to_event`, null, {ts: 1687324651120}), "/client/v3/rooms/!example/timestamp_to_event?ts=1687324651120") +})