Skip to content

Commit 60e203c

Browse files
committed
remove node dependencies
1 parent 2ff71d3 commit 60e203c

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

package-lock.json

+84-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"jest": "^26.6.3",
7171
"next": "^13.5.1",
7272
"prettier": "^2.2.1",
73+
"query-string": "^9.0.0",
7374
"react": "^18.2.0",
7475
"react-dom": "^18.2.0",
7576
"react-test-renderer": "^18.2.0",

src/MemoryRouter.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { NextRouter, RouterEvent } from "next/router";
22
import mitt, { MittEmitter } from "./lib/mitt";
3-
import { parseUrl, stringifyQueryString } from "./urls";
4-
import { parse as parseQueryString } from "querystring";
3+
import { parseUrl, objectifyQueryString, stringifyQueryString } from "./urls";
54

65
export type Url = string | UrlObject;
76
export type UrlObject = {
@@ -212,7 +211,7 @@ export class MemoryRouter extends BaseRouter {
212211
*/
213212
function parseUrlToCompleteUrl(url: Url, currentPathname: string): UrlObjectComplete {
214213
const parsedUrl = typeof url === "object" ? url : parseUrl(url);
215-
const query = parsedUrl.search ? parseQueryString(parsedUrl.search) : parsedUrl.query ?? {};
214+
const query = parsedUrl.search ? objectifyQueryString(parsedUrl.search) : parsedUrl.query ?? {};
216215

217216
return {
218217
pathname: normalizeTrailingSlash(parsedUrl.pathname ?? currentPathname),

src/urls.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NextRouter } from "next/router";
22
import type { UrlObject } from "./MemoryRouter";
3+
import queryString from "querystring";
34

45
export function parseUrl(url: string): UrlObject {
56
const base = "https://base.com"; // base can be anything
@@ -17,12 +18,8 @@ export function parseUrl(url: string): UrlObject {
1718
};
1819
}
1920
export function stringifyQueryString(query: NextRouter["query"]): string {
20-
const params = new URLSearchParams();
21-
Object.keys(query).forEach((key) => {
22-
const values = query[key];
23-
for (const value of Array.isArray(values) ? values : [values]) {
24-
params.append(key, value!);
25-
}
26-
});
27-
return params.toString();
21+
return queryString.stringify(query);
22+
}
23+
export function objectifyQueryString(query: string): NextRouter["query"] {
24+
return queryString.parse(query);
2825
}

0 commit comments

Comments
 (0)