Skip to content

Commit

Permalink
Merge pull request #13 from VariableVasasMT/feature/issues/9
Browse files Browse the repository at this point in the history
Configure `qs`
  • Loading branch information
Floby authored Sep 20, 2019
2 parents d13323d + ea5d65d commit 3728dbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ query parameter with the [`qs`](https://www.npmjs.com/package/qs) module.
###### `.query(params)`
- shortcut for the previous method with a hash of key/value.

###### `.configQs(config)`

- add config supported by qs.stringify https://www.npmjs.com/package/qs version ^6.5.1


###### `.toString()`, `.valueOf()`, `toJSON()`
- **returns** a *string* of the current state of the `UrlAssembler` instance. Path parameters not yet replaced will appear as `:param_name`.

Expand Down
14 changes: 13 additions & 1 deletion lib/url-assembler-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = function (request) {
}

var query = {};
//For future to keep other configs
this._config = {
qsConfig: {}
};
this._query = addQueryParamTo(query);
this._prefix = '';
this.pathname = '';
Expand Down Expand Up @@ -41,6 +45,7 @@ module.exports = function (request) {
function initWithInstance (self, instance) {
extend(self, selectUrlFields(instance));
self._prefix = instance._prefix;
self._config = instance._config;
self._query(instance.getParsedQuery());
self._requestModule = instance._requestModule;
}
Expand All @@ -57,6 +62,13 @@ module.exports = function (request) {
return chainable;
};

//To config QS
methods.configQs = function(config) {
var chainable = this._chain();
extend(chainable._config.qsConfig, config);
return chainable;
};

methods.segment = function (segment) {
var chainable = this._chain();
chainable.pathname = this.pathname + encodeURI(segment);
Expand Down Expand Up @@ -111,7 +123,7 @@ module.exports = function (request) {
} else {
addOneParameter(key, value)
}
this.search = qs.stringify(query);
this.search = qs.stringify(query, this._config.qsConfig);
}

function addManyParameters (hash) {
Expand Down
17 changes: 17 additions & 0 deletions test/110-instance-with-baseurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ describe('an instance with a baseUrl', function () {

});

describe("when baseUrl has a repeated query param with config", function() {
beforeEach(function() {
myUrl = UrlAssembler("http://domain.com/coucou?hello=world");
});

it("should keep accept qs config options", function() {
expect(
myUrl
.configQs({
arrayFormat: "repeat"
})
.query("a", ["b", "c"])
.toString()
).to.equal("http://domain.com/coucou?hello=world&a=b&a=c");
});
});

describe('when used with special characters', function() {

it('should encode them in the final URL (with template)', function() {
Expand Down

0 comments on commit 3728dbc

Please sign in to comment.