Skip to content

Commit

Permalink
Remove 'querystring' dependency line#748
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Mar 11, 2024
1 parent f2666af commit 6c2218a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/http-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import axios, {
import { Readable } from "node:stream";
import { HTTPError, ReadError, RequestError } from "./exceptions";
import * as fileType from "file-type";
import * as qs from "node:querystring";

const pkg = require("../package.json");

Expand Down Expand Up @@ -89,7 +88,13 @@ export default class HTTPClient {
}

public async postForm<T>(url: string, body?: any): Promise<T> {
const res = await this.instance.post(url, qs.stringify(body), {
const params = new URLSearchParams();
for (const key in body) {
if (body.hasOwnProperty(key)) {
params.append(key, body[key]);
}
}
const res = await this.instance.post(url, params.toString(), {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});

Expand Down
11 changes: 8 additions & 3 deletions lib/http-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Readable } from "node:stream";
import { HTTPFetchError } from "./exceptions";
import * as qs from "node:querystring";

const pkg = require("../package.json");
export interface FetchRequestConfig {
Expand Down Expand Up @@ -41,7 +40,7 @@ export default class HTTPFetchClient {
public async get<T>(url: string, params?: any): Promise<Response> {
const requestUrl = new URL(url, this.baseURL);
if (params) {
requestUrl.search = qs.stringify(params);
requestUrl.search = new URLSearchParams(params).toString();
}
const response = await fetch(requestUrl, {
headers: this.defaultHeaders,
Expand Down Expand Up @@ -90,13 +89,19 @@ export default class HTTPFetchClient {

public async postForm(url: string, body?: any): Promise<Response> {
const requestUrl = new URL(url, this.baseURL);
const formParams = new URLSearchParams();
for (const key in body) {
if (body.hasOwnProperty(key)) {
formParams.append(key, body[key]);
}
}
const response = await fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
...this.defaultHeaders,
},
body: qs.stringify(body),
body: formParams.toString(),
});
await this.checkResponseStatus(response);
return response;
Expand Down
2 changes: 1 addition & 1 deletion test/http-axios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("http", () => {

const scope = mockPostForm(
"/post/body",
"id=12345&message=hello%2C%20body!",
"id=12345&message=hello%2C+body%21",
);
const res = await httpClient.postForm<any>(`/post/body`, testBody);
equal(scope.isDone(), true);
Expand Down
2 changes: 1 addition & 1 deletion test/http-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("http(fetch)", () => {

const scope = mockPostForm(
"/post/body",
"id=12345&message=hello%2C%20body!",
"id=12345&message=hello%2C+body%21",
);
const res = await client.postForm(`/post/body`, testBody);
equal(scope.isDone(), true);
Expand Down

0 comments on commit 6c2218a

Please sign in to comment.