Skip to content

Commit cb5010b

Browse files
jmcpeakJason McPeak
andauthored
Make updateLocation not script crash (#245)
* issue244 * use try/catch * follow function convention Co-authored-by: Jason McPeak <jmcpeak@payrailz.com>
1 parent 17c8d61 commit cb5010b

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

packages/serialize-query-params/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## serialize-query-params v2.0.2
4+
5+
**Fixes**
6+
- `updateLocation` returns an empty string for `href` when the original `location.href` is not defined. This allows `updateLocation` to be recursively recalled without crashing when creating a `new URL()` (#244)
7+
38
## serialize-query-params v2.0.1
49

510
**Fixes**

packages/serialize-query-params/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serialize-query-params",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A library for simplifying encoding and decoding URL query parameters.",
55
"main": "./dist/index.cjs.js",
66
"types": "./dist/index.d.ts",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { stringify } from 'query-string';
22
import { EncodedQuery } from '..';
33

4-
export function makeMockLocation(query: EncodedQuery): Location {
4+
export function makeMockLocation(query: EncodedQuery, includeHref: boolean = true): Location {
55
const queryStr = stringify(query);
66
const search = queryStr.length ? `?${queryStr}` : '';
77
return {
88
protocol: 'http:',
99
host: 'localhost:3000',
1010
pathname: '/',
1111
search,
12-
href: 'http://localhost:3000/' + search,
12+
href: includeHref ? 'http://localhost:3000/' + search : undefined,
1313
key: `mock-${Date.now()}`,
1414
} as Location & { key: string };
1515
}

packages/serialize-query-params/src/__tests__/updateLocation.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ describe('updateLocation', () => {
7777
);
7878
expect(newLocation2.search).toBe('?bar=[({1,2:6:4,5})]&foo=o%20t%20h');
7979
});
80+
it('call it twice, should not throw exception', () => {
81+
const location = makeMockLocation({
82+
foo: 'one two',
83+
bar: '[({1,2:3:4,5})]',
84+
}, false);
85+
const newLocationAsReturned = updateLocation(
86+
{ foo: 'o t h', bar: '[({1,2:3:6,5})]' },
87+
location,
88+
(params) => stringify(params, { encode: false })
89+
);
90+
const newLocation2 = updateLocation(
91+
{ foo: 'o t h', bar: '[({1,2:6:4,5})]' },
92+
newLocationAsReturned,
93+
(params) => {
94+
const str = stringify(params, { encode: false });
95+
return transformSearchStringJsonSafe(str).replace(/ /g, '%20');
96+
}
97+
);
98+
expect(newLocation2.search).toBe('?bar=[({1,2:6:4,5})]&foo=o%20t%20h');
99+
});
80100
});
81101

82102
describe('updateInLocation', () => {

packages/serialize-query-params/src/updateLocation.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ const JSON_SAFE_CHARS = `{}[],":`
1010
.split('')
1111
.map((d) => [d, encodeURIComponent(d)]);
1212

13+
function getHrefFromLocation(location: Location, search: string): string {
14+
// https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
15+
let href: string = search;
16+
17+
if (location.href) {
18+
// TODO - implement base option if location.href is relative
19+
// see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#syntax
20+
try {
21+
const url = new URL(location.href);
22+
href = `${url.origin}${url.pathname}${search}`;
23+
} catch (e) {
24+
href = '';
25+
}
26+
}
27+
28+
return href;
29+
}
30+
1331
export function transformSearchStringJsonSafe(searchString: string): string {
1432
let str = searchString;
1533
for (let [char, code] of JSON_SAFE_CHARS) {
@@ -30,21 +48,14 @@ export function updateLocation(
3048
let encodedSearchString = objectToSearchStringFn(encodedQuery);
3149

3250
const search = encodedSearchString.length ? `?${encodedSearchString}` : '';
33-
let href: string;
34-
if (location.href) {
35-
const url = new URL(location.href);
36-
href = `${url.origin}${url.pathname}${search}`;
37-
} else {
38-
href = search;
39-
}
4051

4152
const newLocation: Location & {
4253
key: string;
4354
query: EncodedQuery;
4455
} = {
4556
...location,
4657
key: `${Date.now()}`, // needed for some routers (e.g. react-router)
47-
href,
58+
href: getHrefFromLocation(location, search),
4859
search,
4960
query: encodedQuery, // needed for some routers (e.g. found)
5061
};

0 commit comments

Comments
 (0)