Skip to content

Commit 357443b

Browse files
author
Rafael
authored
Replace url node module usage with url-join & URLSearchParams (#17)
1 parent 315fd9f commit 357443b

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
package
55
package-lock.json
66
stipulate-*.tgz
7+
.idea

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.2.0
4+
5+
- Replace `url` node module usage with `url-join` & `URLSearchParams`
6+
37
## 0.1.0
48

59
- Package CommonJS compatible code

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stipulate",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A module extending the Fetch API with some useful default error handling and hooks.",
55
"main": "cjs/stipulate.js",
66
"module": "src/stipulate.js",
@@ -37,7 +37,8 @@
3737
"mocha": "~2.5.3"
3838
},
3939
"dependencies": {
40-
"lodash": "4.x"
40+
"lodash": "4.x",
41+
"url-join": "^4.0.1"
4142
},
4243
"browserify": {
4344
"transform": [

src/resolve-url.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
import _ from 'lodash';
2-
import url from 'url';
2+
import urlJoin from 'url-join';
33

44
const resolveUrl = function(urlString, query) {
5-
const parsedUrl = url.parse(urlString, true);
5+
let parsedUrlQuery = {};
6+
let urlWithoutParams = urlString;
7+
const queryStart = urlString.lastIndexOf('?');
68

7-
const mergedQuery = _.defaults({}, query, parsedUrl.query);
9+
if (queryStart !== -1){
10+
const paramStr = urlString.slice(queryStart);
11+
parsedUrlQuery = Object.fromEntries(
12+
new URLSearchParams(paramStr)
13+
);
14+
urlWithoutParams = urlString.slice(0, queryStart);
15+
}
16+
17+
const mergedQuery = _.defaults({}, query, parsedUrlQuery);
818
const fullQuery = _.pickBy(mergedQuery, (value) => value);
919

10-
delete parsedUrl.search;
11-
parsedUrl.query = fullQuery;
20+
const searchParams = new URLSearchParams(fullQuery);
21+
const searchParamsString = searchParams.toString();
22+
23+
if (searchParamsString){
24+
return urlJoin(urlWithoutParams, `?${searchParamsString}`);
25+
}
1226

13-
return url.format(parsedUrl);
27+
return urlWithoutParams;
1428
};
1529

1630
export default resolveUrl;

0 commit comments

Comments
 (0)