Skip to content

Commit

Permalink
fix: fixed issue where empty queries were being appended to url
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Nov 8, 2022
1 parent fbcff78 commit 2c787e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/util/fetch/src/lib/url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ describe('fetchURL()', () => {
expect(result).toBe(url.href + `?${new URLSearchParams(Array.from(expectedParams.entries())).toString()}`);
});

it('should not append an empty query to the url.', () => {
const queryParamsObject = {
hello: null
};
const result = fetchURL({ url, queryParams: queryParamsObject });
expect(result).toBe(url.href);
});

it('should return the url with query params of different types attached as an object', () => {
const queryParamsObject = {
1: 'test',
Expand Down
9 changes: 7 additions & 2 deletions packages/util/fetch/src/lib/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrayOrValue, fixExtraQueryParameters, forEachInIterable, forEachKeyValue, isIterable, mapIterable, Maybe, useIterableOrValue } from '@dereekb/util';
import { ArrayOrValue, fixExtraQueryParameters, forEachInIterable, forEachKeyValue, isEmptyIterable, isIterable, mapIterable, Maybe, useIterableOrValue } from '@dereekb/util';

export type SimpleFetchURLInput = URL | string;

Expand Down Expand Up @@ -27,7 +27,12 @@ export function fetchURL(input: FetchURLInput): string {

if (input.queryParams) {
const searchParams = queryParamsToSearchParams(input.queryParams);
url = fixExtraQueryParameters(baseUrl + `?${searchParams.toString()}`);

if (!isEmptyIterable(searchParams)) {
url = fixExtraQueryParameters(baseUrl + `?${searchParams.toString()}`);
} else {
url = baseUrl;
}
} else {
url = baseUrl;
}
Expand Down

0 comments on commit 2c787e8

Please sign in to comment.