Skip to content

Commit

Permalink
refactor: share http://localhost string
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Sep 5, 2024
1 parent 997afbb commit ad22663
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type QueryValue =
| undefined;
export type QueryObject = Record<string, QueryValue | QueryValue[]>;

const DEFAULT_BASE_URL = "http://localhost";

/**
* Removes the leading slash from the given path if it has one.
*/
Expand Down Expand Up @@ -80,7 +82,7 @@ export function withQuery(input: string, query: QueryObject): string {
let searchParams: URLSearchParams;

if (input.includes("?")) {
url = new URL(input, "http://localhost");
url = new URL(input, DEFAULT_BASE_URL);
searchParams = new URLSearchParams(url.search);

Check warning on line 86 in src/path.ts

View check run for this annotation

Codecov / codecov/patch

src/path.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
} else {
searchParams = new URLSearchParams();
Expand Down Expand Up @@ -109,7 +111,7 @@ export function withQuery(input: string, query: QueryObject): string {
if (url) {
url.search = queryString;
let urlWithQuery = url.toString();
if (urlWithQuery.startsWith("http://localhost")) {
if (urlWithQuery.startsWith(DEFAULT_BASE_URL)) {
urlWithQuery = urlWithQuery.slice(16);
}
return urlWithQuery;
Expand Down

0 comments on commit ad22663

Please sign in to comment.