Skip to content

Commit

Permalink
chore: use Node.js's built-in 'querystring' module
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Jul 21, 2024
1 parent 459f034 commit a6f7758
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"extend": "^3.0.2",
"gaxios": "^6.0.3",
"google-auth-library": "^9.7.0",
"qs": "^6.7.0",
"url-template": "^2.0.8",
"uuid": "^9.0.0"
},
Expand All @@ -51,7 +50,6 @@
"@types/ncp": "^2.0.1",
"@types/nock": "^11.0.0",
"@types/proxyquire": "^1.3.28",
"@types/qs": "^6.5.3",
"@types/sinon": "^17.0.0",
"@types/tmp": "0.2.6",
"@types/url-template": "^2.0.28",
Expand Down
6 changes: 4 additions & 2 deletions src/apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import {GaxiosPromise, Headers} from 'gaxios';
import {DefaultTransporter, OAuth2Client} from 'google-auth-library';
import * as qs from 'qs';
import * as querystring from 'querystring';
import * as stream from 'stream';
import * as urlTemplate from 'url-template';
import * as uuid from 'uuid';
Expand Down Expand Up @@ -186,7 +186,9 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {
// This serializer also encodes spaces in the querystring as `%20`,
// whereas the default serializer in gaxios encodes to a `+`.
options.paramsSerializer = params => {
return qs.stringify(params, {arrayFormat: 'repeat'});
return querystring.stringify(params, undefined, undefined, {
encodeURIComponent,
});
};

// delete path params from the params object so they do not end up in query
Expand Down
15 changes: 10 additions & 5 deletions src/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import * as http2 from 'http2';
import * as zlib from 'zlib';
import {URL} from 'url';
import * as qs from 'qs';
import * as querystring from 'querystring';
import * as extend from 'extend';
import {Stream, Readable} from 'stream';
import * as util from 'util';
Expand Down Expand Up @@ -69,12 +69,17 @@ export async function request<T>(
clearTimeout(sessionData.timeoutHandle);
}

// Assemble the querystring based on config.params. We're using the
// `qs` module to make life a little easier.
// Assemble the querystring based on config.params.
let pathWithQs = url.pathname;
if (config.params && Object.keys(config.params).length > 0) {
const serializer = config.paramsSerializer || qs.stringify;
const q = serializer(opts.params);
let q: string;
if (config.paramsSerializer) {
q = config.paramsSerializer(opts.params);
} else {
q = querystring.stringify(opts.params, undefined, undefined, {
encodeURIComponent,
});
}
pathWithQs += `?${q}`;
}

Expand Down
24 changes: 24 additions & 0 deletions test/test.http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ describe('http2', () => {
await reqPromise;
});

it('should encode querystring parameters with a custom paramsSerializer', async () => {
let stream!: EventEmitter;
connectStub = () => {
const client = new FakeClient();
client.request = (headers: coreHttp2.OutgoingHttpHeaders) => {
assert.strictEqual(headers[HTTP2_HEADER_PATH], '/tasks?hello=world');
stream = new EventEmitter();
return stream;
};
return client;
};
const reqPromise = http2.request({
url,
params: {
hello: 'world',
},
paramsSerializer: () => 'x',
});
stream.emit('response', {[HTTP2_HEADER_STATUS]: 200});
stream.emit('data', Buffer.from('x'));
stream.emit('end');
await reqPromise;
});

it('should reject the promise on stream errors', async () => {
const resPromise = http2.request({url});
requestStream.emit('response', {
Expand Down

0 comments on commit a6f7758

Please sign in to comment.