Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
coverage/
node_modules/
npm-debug.log
temp/
temp/
package-lock.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ path.build({ id: '123' }) // => '/users/123'
Test if the provided path matches the defined path template. Options available are:
- `'caseSensitive'`: whether matching should be case sensitive or not (default to `false`)
- `'strictTrailingSlash'`: whether or not it should strictly match trailing slashes (default to `false`)
- `'optionalQueryParams'`: whether or not it should match query params (default to `false`)
- `'queryParams'`: [options for query parameters](https://github.com/troch/search-params#options)


Expand Down
25 changes: 16 additions & 9 deletions dist/cjs/path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */

var __assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};

var defaultOrConstrained = function (match) {
Expand Down Expand Up @@ -167,7 +170,7 @@ var Path = /** @class */ (function () {
};
Path.prototype.test = function (path, opts) {
var _this = this;
var options = __assign({ strictTrailingSlash: false, queryParams: {} }, opts);
var options = __assign({ strictTrailingSlash: false, optionalQueryParams: false, queryParams: {} }, opts);
// trailingSlash: falsy => non optional, truthy => optional
var source = optTrailingSlash(this.source, options.strictTrailingSlash);
// Check if exact match
Expand All @@ -178,10 +181,14 @@ var Path = /** @class */ (function () {
}
// Extract query params
var queryParams = searchParams.parse(path, options.queryParams);
var unexpectedQueryParams = Object.keys(queryParams).filter(function (p) { return !_this.isQueryParam(p); });
var unexpectedQueryParams = options.optionalQueryParams
? []
: Object.keys(queryParams).filter(function (p) { return !_this.isQueryParam(p); });
if (unexpectedQueryParams.length === 0) {
// Extend url match
Object.keys(queryParams).forEach(function (p) { return (match[p] = queryParams[p]); });
Object.keys(queryParams).forEach(function (p) {
match[p] = queryParams[p];
});
return match;
}
return null;
Expand Down
25 changes: 16 additions & 9 deletions dist/es/path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */

var __assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};

var defaultOrConstrained = function (match) {
Expand Down Expand Up @@ -163,7 +166,7 @@ var Path = /** @class */ (function () {
};
Path.prototype.test = function (path, opts) {
var _this = this;
var options = __assign({ strictTrailingSlash: false, queryParams: {} }, opts);
var options = __assign({ strictTrailingSlash: false, optionalQueryParams: false, queryParams: {} }, opts);
// trailingSlash: falsy => non optional, truthy => optional
var source = optTrailingSlash(this.source, options.strictTrailingSlash);
// Check if exact match
Expand All @@ -174,10 +177,14 @@ var Path = /** @class */ (function () {
}
// Extract query params
var queryParams = parse(path, options.queryParams);
var unexpectedQueryParams = Object.keys(queryParams).filter(function (p) { return !_this.isQueryParam(p); });
var unexpectedQueryParams = options.optionalQueryParams
? []
: Object.keys(queryParams).filter(function (p) { return !_this.isQueryParam(p); });
if (unexpectedQueryParams.length === 0) {
// Extend url match
Object.keys(queryParams).forEach(function (p) { return (match[p] = queryParams[p]); });
Object.keys(queryParams).forEach(function (p) {
match[p] = queryParams[p];
});
return match;
}
return null;
Expand Down
20 changes: 13 additions & 7 deletions modules/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface IPartialTestOptions {
export interface ITestOptions {
caseSensitive?: boolean
strictTrailingSlash?: boolean
optionalQueryParams?: boolean
queryParams?: IOptions
}

Expand Down Expand Up @@ -116,7 +117,12 @@ export class Path {
}

public test(path: string, opts?: ITestOptions): TestMatch {
const options = { strictTrailingSlash: false, queryParams: {}, ...opts }
const options = {
strictTrailingSlash: false,
optionalQueryParams: false,
queryParams: {},
...opts
}
// trailingSlash: falsy => non optional, truthy => optional
const source = optTrailingSlash(
this.source,
Expand All @@ -134,14 +140,14 @@ export class Path {
}
// Extract query params
const queryParams = parseQueryParams(path, options.queryParams)
const unexpectedQueryParams = Object.keys(queryParams).filter(
p => !this.isQueryParam(p)
)

const unexpectedQueryParams = options.optionalQueryParams
? []
: Object.keys(queryParams).filter(p => !this.isQueryParam(p))
if (unexpectedQueryParams.length === 0) {
// Extend url match
Object.keys(queryParams).forEach(p => (match[p] = queryParams[p]))

Object.keys(queryParams).forEach(p => {
match[p] = queryParams[p]
})
return match
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"sideEffects": false,
"typings": "./typings/Path.d.ts",
"scripts": {
"test": "mocha -r ts-node/register 'test/main.js'",
"test": "mocha -r ts-node/register \"./test/main.js\"",
"lint": "tslint modules/*.ts",
"build": "npm run clean && rollup -c rollup.config.js",
"clean": "rimraf dist && rimraf temp",
Expand Down
22 changes: 20 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('Path', function() {
.test('/users?offset=1&limit=15')
.should.eql({ offset: '1', limit: '15' })
path.test('/users?limit=15').should.eql({ limit: '15' })
path.test('/users?limit=15').should.eql({ limit: '15' })
path
.partialTest('/users?offset=true&limits=1', {
queryParams: { booleanFormat: 'string' }
Expand Down Expand Up @@ -137,7 +136,7 @@ describe('Path', function() {
path
.partialTest('/users/profile/123-456')
.should.eql({ id: '123', id2: '456' })
// Un,successful match
// Unsuccessful match
should.not.exist(path.test('/users/details/123-456'))
should.not.exist(path.test('/users/profile/123-456?id3=789&id4=000'))

Expand Down Expand Up @@ -296,4 +295,23 @@ describe('Path', function() {
param: '1+2=3@*'
})
})

it('should match path and query params when query params are optional', function() {
const path = new Path('/users/profile/:id?name&surname')
// Successful match
path
.test('/users/profile/123', { optionalQueryParams: true })
.should.eql({ id: '123' })
path
.test('/users/profile/123?name=John&test=match', {
optionalQueryParams: true
})
.should.eql({ id: '123', name: 'John', test: 'match' })

// Unsuccessful match
should.not.exist(path.test('/users/profile/123'))
should.not.exist(
path.test('/users/profile/123', { optionalQueryParams: false })
)
})
})
1 change: 1 addition & 0 deletions typings/Path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IPartialTestOptions {
export interface ITestOptions {
caseSensitive?: boolean;
strictTrailingSlash?: boolean;
optionalQueryParams?: boolean;
queryParams?: IOptions;
}
export interface IBuildOptions {
Expand Down