Skip to content

Commit

Permalink
misc: remove querystring in http/Client
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Oct 23, 2023
1 parent 687bd81 commit 634b1fa
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 46 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion react-demo/config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = function override (config, env) {
const loaders = config.resolve
loaders.fallback = {
querystring: require.resolve('querystring-es3'),
crypto: require.resolve("crypto-browserify"),
stream: require.resolve('stream-browserify'),
};
Expand Down
1 change: 0 additions & 1 deletion react-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@types/react-router-dom": "^5.3.3",
"@types/webpack-env": "^1.13.6",
"crypto-browserify": "^3.12.0",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"react-scripts": "^5.0.1",
"react-app-rewired": "^2.2.1",
Expand Down
1 change: 0 additions & 1 deletion redux-demo/config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = function override (config, env) {
const loaders = config.resolve
loaders.fallback = {
querystring: require.resolve('querystring-es3'),
crypto: require.resolve("crypto-browserify"),
stream: require.resolve('stream-browserify'),
url: require.resolve('url'),
Expand Down
1 change: 0 additions & 1 deletion redux-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"@types/redux": "^3.6.0",
"@types/webpack-env": "^1.13.6",
"crypto-browserify": "^3.12.0",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"url": "^0.11.0",
"react-scripts": "^5.0.1",
Expand Down
8 changes: 4 additions & 4 deletions sdk/src/http/Client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {EventEmitter} from 'events';
import * as qs from 'querystring';
import isPlainObject from 'is-plain-object';

import Externals from '../core/Externals';
import {objectToUrlParams} from './utils';

function findHeaderName(name, headers) {
name = name.toLowerCase();
Expand Down Expand Up @@ -126,9 +127,8 @@ export default class Client extends EventEmitter {

// Append Query String
if (init.query) {
init.url = init.url + (init.url.includes('?') ? '&' : '?') + qs.stringify(init.query);
init.url = init.url + (init.url.includes('?') ? '&' : '?') + objectToUrlParams(init.query);
}

// Serialize body
if (isPlainObject(init.body) || !init.body) {
let contentTypeHeaderName = findHeaderName(Client._contentType, init.headers);
Expand All @@ -149,7 +149,7 @@ export default class Client extends EventEmitter {
init.body = JSON.stringify(init.body);
}
} else if (contentType.includes(Client._urlencodedContentType)) {
init.body = qs.stringify(init.body);
init.body = objectToUrlParams(init.body);
}
}

Expand Down
16 changes: 16 additions & 0 deletions sdk/src/http/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function encodeURIComponentWithUndefined(value) {
return typeof value === 'undefined' ? '' : encodeURIComponent(value);
}

export function objectToUrlParams(obj) {
return Object.keys(obj)
.map(key => {
if (Array.isArray(obj[key])) {
return obj[key]
.map(value => encodeURIComponent(key) + '=' + encodeURIComponentWithUndefined(value))
.join('&');
}
return encodeURIComponent(key) + '=' + encodeURIComponentWithUndefined(obj[key]);
})
.join('&');
}
18 changes: 1 addition & 17 deletions sdk/src/platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Cache from '../core/Cache';
import * as Constants from '../core/Constants';
import Externals from '../core/Externals';
import Client, {ApiError} from '../http/Client';
import {objectToUrlParams} from '../http/utils';
import Auth, {AuthOptions} from './Auth';
import Discovery from './Discovery';
import {delay} from './utils';
Expand All @@ -33,23 +34,6 @@ function checkPathHasHttp(path) {
return path.startsWith('http://') || path.startsWith('https://');
}

function encodeURIComponentWithUndefined(value) {
return typeof value === 'undefined' ? '' : encodeURIComponent(value);
}

function objectToUrlParams(obj) {
return Object.keys(obj)
.map(key => {
if (Array.isArray(obj[key])) {
return obj[key]
.map(value => encodeURIComponent(key) + '=' + encodeURIComponentWithUndefined(value))
.join('&');
}
return encodeURIComponent(key) + '=' + encodeURIComponentWithUndefined(obj[key]);
})
.join('&');
}

export default class Platform extends EventEmitter {
public static _cacheId = 'platform';
public static _discoveryCacheId = 'platform-discovery';
Expand Down
1 change: 0 additions & 1 deletion utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"typescript": "^4.8.4",
"process": "^0.11.10",
"path-browserify": "^1.0.1",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"vm-browserify": "1.1.2",
"whatwg-fetch": "^3.0.0"
Expand Down
1 change: 0 additions & 1 deletion utils/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function createConfig({entry, filename, outputPath, libraryName, externals}) {
buffer: require.resolve('buffer'),
events: require.resolve('events'),
path: require.resolve('path-browserify'),
querystring: require.resolve('querystring-es3'),
url: require.resolve('url'),
},
},
Expand Down

0 comments on commit 634b1fa

Please sign in to comment.