Fix path() doing nothing without a mxid

This commit is contained in:
Cadence Ember 2023-11-25 22:55:11 +13:00
parent b9ec28a5ad
commit 57d5cfb480
2 changed files with 9 additions and 3 deletions

View File

@ -19,15 +19,17 @@ const makeTxnId = sync.require("./txnid")
* @returns {string} the new endpoint * @returns {string} the new endpoint
*/ */
function path(p, mxid, otherParams = {}) { function path(p, mxid, otherParams = {}) {
if (!mxid) return p
const u = new URL(p, "http://localhost") 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)) { for (const entry of Object.entries(otherParams)) {
if (entry[1] != undefined) { if (entry[1] != undefined) {
u.searchParams.set(entry[0], entry[1]) 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
} }
/** /**

View File

@ -20,3 +20,7 @@ test("api path: existing query parameters with mxid", t => {
test("api path: real world 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") 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")
})