Commit f39171fc authored by Robert Knight's avatar Robert Knight

Remove remaining uses of query-string with URLSearchParams

Replace remaining use of query-string dependency with the
URLSearchParams browser API. This will allow us to remove the
dependency.
parent a32c53a6
import * as queryString from 'query-string';
import { import {
toBoolean, toBoolean,
toInteger, toInteger,
...@@ -16,7 +15,7 @@ import { ...@@ -16,7 +15,7 @@ import {
*/ */
export default function hostPageConfig(window) { export default function hostPageConfig(window) {
const configStr = window.location.hash.slice(1); const configStr = window.location.hash.slice(1);
const configJSON = queryString.parse(configStr).config; const configJSON = new URLSearchParams(configStr).get('config');
const config = JSON.parse(configJSON || '{}'); const config = JSON.parse(configJSON || '{}');
// Known configuration parameters which we will import from the host page. // Known configuration parameters which we will import from the host page.
......
import * as queryString from 'query-string';
/** /**
* @typedef {'annotation'|'notebook'|'stream'|'sidebar'} RouteName * @typedef {'annotation'|'notebook'|'stream'|'sidebar'} RouteName
* @typedef {Record<string,string>} RouteParams * @typedef {Record<string,string>} RouteParams
...@@ -29,7 +27,13 @@ export class RouterService { ...@@ -29,7 +27,13 @@ export class RouterService {
currentRoute() { currentRoute() {
const path = this._window.location.pathname; const path = this._window.location.pathname;
const pathSegments = path.slice(1).split('/'); const pathSegments = path.slice(1).split('/');
const params = queryString.parse(this._window.location.search); const searchParams = new URLSearchParams(this._window.location.search);
/** @type {Record<string, string>} */
const params = {};
for (let [key, value] of searchParams) {
params[key] = value;
}
// The extension puts client resources under `/client/` to separate them // The extension puts client resources under `/client/` to separate them
// from extension-specific resources. Ignore this part. // from extension-specific resources. Ignore this part.
...@@ -94,9 +98,15 @@ export class RouterService { ...@@ -94,9 +98,15 @@ export class RouterService {
throw new Error(`Cannot generate URL for route "${name}"`); throw new Error(`Cannot generate URL for route "${name}"`);
} }
const query = queryString.stringify(queryParams); let hasParams = false;
if (query.length > 0) { const searchParams = new URLSearchParams();
url += '?' + query; for (let [key, value] of Object.entries(queryParams)) {
hasParams = true;
searchParams.set(key, value);
}
if (hasParams) {
url += '?' + searchParams.toString();
} }
return url; return url;
......
import * as queryString from 'query-string';
import warnOnce from '../../shared/warn-once'; import warnOnce from '../../shared/warn-once';
import { generateHexString } from '../util/random'; import { generateHexString } from '../util/random';
import { Socket } from '../websocket'; import { Socket } from '../websocket';
...@@ -190,9 +188,7 @@ export class StreamerService { ...@@ -190,9 +188,7 @@ export class StreamerService {
// not support setting the `Authorization` header directly as we do for // not support setting the `Authorization` header directly as we do for
// other API requests. // other API requests.
const parsedURL = new URL(this._websocketURL); const parsedURL = new URL(this._websocketURL);
const queryParams = queryString.parse(parsedURL.search); parsedURL.searchParams.set('access_token', token);
queryParams.access_token = token;
parsedURL.search = queryString.stringify(queryParams);
url = parsedURL.toString(); url = parsedURL.toString();
} else { } else {
url = this._websocketURL; url = this._websocketURL;
......
...@@ -211,7 +211,7 @@ describe('StreamerService', () => { ...@@ -211,7 +211,7 @@ describe('StreamerService', () => {
return activeStreamer.connect().then(() => { return activeStreamer.connect().then(() => {
assert.equal( assert.equal(
fakeWebSocket.url, fakeWebSocket.url,
'ws://example.com/ws?access_token=dummy-access-token&foo=bar' 'ws://example.com/ws?foo=bar&access_token=dummy-access-token'
); );
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment