You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
create utility to decode qs into objects
This commit is contained in:
29
src/utils.ts
29
src/utils.ts
@@ -31,15 +31,28 @@ import type NodeCrypto from "crypto";
|
|||||||
* @return {string} The encoded string 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 {
|
export function encodeParams(params: Record<string, string>): string {
|
||||||
let qs = "";
|
return new URLSearchParams(params).toString();
|
||||||
for (const key in params) {
|
}
|
||||||
if (!params.hasOwnProperty(key)) {
|
|
||||||
continue;
|
export type QueryDict = Record<string, string | string[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a query string in `application/x-www-form-urlencoded` format.
|
||||||
|
* @param {string} query A query string to decode e.g.
|
||||||
|
* foo=bar&via=server1&server2
|
||||||
|
* @return {Object} The decoded object, if any keys occurred multiple times
|
||||||
|
* then the value will be an array of strings, else it will be an array.
|
||||||
|
* This behaviour matches Node's qs.parse but is built on URLSearchParams
|
||||||
|
* for native web compatibility
|
||||||
|
*/
|
||||||
|
export function decodeParams(query: string): QueryDict {
|
||||||
|
const o: QueryDict = {};
|
||||||
|
const params = new URLSearchParams(query);
|
||||||
|
for (const key of params.keys()) {
|
||||||
|
const val = params.getAll(key);
|
||||||
|
o[key] = val.length === 1 ? val[0] : val;
|
||||||
}
|
}
|
||||||
qs += "&" + encodeURIComponent(key) + "=" +
|
return o;
|
||||||
encodeURIComponent(params[key]);
|
|
||||||
}
|
|
||||||
return qs.substring(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user