From 3c629292e323a0b14830fabce2a489e1022505f9 Mon Sep 17 00:00:00 2001 From: AtaS Date: Sun, 20 Oct 2024 19:14:42 +0100 Subject: [PATCH] Use qs in getUrlWithQuery() --- src/utils/__tests__/urlUtils.test.ts | 30 ++++++++++++++-------------- src/utils/urlUtils.ts | 13 +++++++----- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/__tests__/urlUtils.test.ts b/src/utils/__tests__/urlUtils.test.ts index 1275b2e..9e1d148 100644 --- a/src/utils/__tests__/urlUtils.test.ts +++ b/src/utils/__tests__/urlUtils.test.ts @@ -35,14 +35,21 @@ describe('getUrlWithQuery', () => { const url = 'https://example.com'; const query = { foo: 'bar', baz: 'qux' }; const result = getUrlWithQuery(url, query); - expect(result).toBe('https://example.com/?foo=bar&baz=qux'); + expect(result).toBe('https://example.com/?baz=qux&foo=bar'); + }); + + test('should add query parameters to the URL', () => { + const url = '/'; + const query = { foo: 'bar', baz: 'qux' }; + const result = getUrlWithQuery(url, query); + expect(result).toBe('/?baz=qux&foo=bar'); }); test('should overwrite existing query parameters', () => { - const url = 'https://example.com?foo=old'; + const url = ''; const query = { foo: 'new', baz: 'qux' }; const result = getUrlWithQuery(url, query); - expect(result).toBe('https://example.com/?foo=new&baz=qux'); + expect(result).toBe('/?baz=qux&foo=new'); }); test('should handle empty query object', () => { @@ -53,23 +60,16 @@ describe('getUrlWithQuery', () => { }); test('should handle URL with existing query parameters', () => { - const url = 'https://example.com?existing=param'; + const url = '/'; const query = { foo: 'bar' }; const result = getUrlWithQuery(url, query); - expect(result).toBe('https://example.com/?existing=param&foo=bar'); - }); - - test('should handle special characters in query parameters', () => { - const url = 'https://example.com'; - const query = { 'foo bar': 'baz qux' }; - const result = getUrlWithQuery(url, query); - expect(result).toBe('https://example.com/?foo+bar=baz+qux'); + expect(result).toBe('/?foo=bar'); }); - + test('should ignore nulls', () => { - const url = 'https://example.com'; + const url = '/'; const query = { 'foo': null, 'bar': undefined }; const result = getUrlWithQuery(url, query); - expect(result).toBe('https://example.com/'); + expect(result).toBe('/'); }); }); \ No newline at end of file diff --git a/src/utils/urlUtils.ts b/src/utils/urlUtils.ts index d6c0ea3..7a3a0fd 100644 --- a/src/utils/urlUtils.ts +++ b/src/utils/urlUtils.ts @@ -119,9 +119,12 @@ export function isHttpsUrl(url: string) { } export function getUrlWithQuery(url: string, query: { [key: string]: any }) { - const urlObj = new URL(url); - Object.keys(query).filter(k => query[k]).forEach(key => { - urlObj.searchParams.set(key, query[key]); - }); - return urlObj.toString(); + let q = qs.stringify(query, {skipNull: true, skipEmptyString: true}); + q = q ? `?${q}` : ''; + + if (!url.endsWith('/')) { + url += '/'; + } + + return url + q; } \ No newline at end of file