Skip to content

Commit

Permalink
fix: encode all parameters and not only query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jun 30, 2016
1 parent 4bb2554 commit 19d5131
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
26 changes: 20 additions & 6 deletions modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const parseQueryParams = path => {
.reduce((obj, m) => appendQueryParam(obj, m[0], m[1] ? decodeURIComponent(m[1]) : m[1]), {});
};

const toSerialisable = val => val !== undefined && val !== null && val !== '' ? '=' + encodeURIComponent(val) : '';
const toSerialisable = val => val !== undefined && val !== null && val !== '' ? `=${val}` : '';

const serialise = (key, val) => Array.isArray(val) ? val.map(v => serialise(key, v)).join('&') : key + toSerialisable(val);

Expand Down Expand Up @@ -214,23 +214,37 @@ export default class Path {
}

build(params = {}, opts = {ignoreConstraints: false, ignoreSearch: false}) {
const encodedParams = Object.keys(params).reduce(
(acc, key) => {
// Use encodeURI in case of spats
if (params[key] === undefined) {
acc[key] = undefined;
} else {
acc[key] = Array.isArray(params[key])
? params[key].map(encodeURI)
: encodeURI(params[key]);
}
return acc;
},
{}
);
// Check all params are provided (not search parameters which are optional)
if (this.urlParams.some(p => params[p] === undefined)) throw new Error('Missing parameters');

// Check constraints
if (!opts.ignoreConstraints) {
let constraintsPassed = this.tokens
.filter(t => /^url-parameter/.test(t.type) && !/-splat$/.test(t.type))
.every(t => new RegExp('^' + defaultOrConstrained(t.otherVal[0]) + '$').test(params[t.val]));
.every(t => new RegExp('^' + defaultOrConstrained(t.otherVal[0]) + '$').test(encodedParams[t.val]));

if (!constraintsPassed) throw new Error('Some parameters are of invalid format');
}

let base = this.tokens
.filter(t => /^query-parameter/.test(t.type) === false)
.map(t => {
if (t.type === 'url-parameter-matrix') return `;${t.val}=${params[t.val[0]]}`;
return /^url-parameter/.test(t.type) ? params[t.val[0]] : t.match;
if (t.type === 'url-parameter-matrix') return `;${t.val}=${encodedParams[t.val[0]]}`;
return /^url-parameter/.test(t.type) ? encodedParams[t.val[0]] : t.match;
})
.join('');

Expand All @@ -239,8 +253,8 @@ export default class Path {
const queryParams = this.queryParams.concat(this.queryParamsBr.map(p => p + '[]'));

const searchPart = queryParams
.filter(p => Object.keys(params).indexOf(withoutBrackets(p)) !== -1)
.map(p => serialise(p, params[withoutBrackets(p)]))
.filter(p => Object.keys(encodedParams).indexOf(withoutBrackets(p)) !== -1)
.map(p => serialise(p, encodedParams[withoutBrackets(p)]))
.join('&');

return base + (searchPart ? '?' + searchPart : '');
Expand Down
6 changes: 6 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@ describe('Path', function () {

path.partialMatch('/test/%7B123-456%7D').should.eql({ id: '{123-456}' });
});

it('should encoded values and build paths', function () {
var path = new Path('/test/:id');

path.build({ id: '{123-456}' }).should.equal('/test/%7B123-456%7D');
});
});

0 comments on commit 19d5131

Please sign in to comment.